Skip to content

Commit 2a150ee

Browse files
authored
feat(types): Add Envelope types (#4527)
To prep for the introduction of formal envelopes API, add types representing envelopes and their items. These are based on the documentation from the develop docs at the time of this patch. Envelope items are stored as a tuple of length 2 (item headers and payload) to reduce bundle size. This is as array access is smaller than having to access through object properties, and they will stay constant. Each Envelope is generic over a `BaseEnvelope` type, which allows for items and headers to be configured, but sets a `CommonEnvelopeHeaders` alongside a default `UnknownEnvelopeItem` (as per the spec). Each Envelope item is generic over a `BaseEnvelopeItem` type, which establishs `CommonEnvelopeItemHeaders` over an arbitrary item payload. Envelope items are defined for events, attachments, user feedback, sessions, and client reports. Envelopes are defined for events, sessions, and client reports. This is as attachments and user feedback (for now) must be in the envelope alongside the event. As User Feedback and Attachment envelopes are currently not supported in the SDK, they are not exported, but will be once v7 is introduced. The next step from the patch is to add the public API around creating and mutating Envelopes of different natures, as well as mapping from an Envelope -> `SentryRequest`. See: https://develop.sentry.dev/sdk/envelopes/
1 parent 1bb3ff6 commit 2a150ee

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/types/src/envelope.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { SentryRequestType } from './request';
2+
import { SdkInfo } from './sdkinfo';
3+
import { Session, SessionAggregates } from './session';
4+
import { Outcome } from './transport';
5+
import { User } from './user';
6+
7+
// Based on: https://develop.sentry.dev/sdk/envelopes/
8+
9+
type CommonEnvelopeHeaders = {
10+
dsn?: string;
11+
sdk?: SdkInfo;
12+
};
13+
14+
type CommonEnvelopeItemHeaders = {
15+
length?: number;
16+
};
17+
18+
/**
19+
* 1st Item: Item headers
20+
* 2nd Item: Item payload
21+
*/
22+
type BaseEnvelopeItem<ItemHeader extends { type: string }, Payload = unknown> = [
23+
CommonEnvelopeItemHeaders & ItemHeader,
24+
Payload,
25+
];
26+
27+
type UnknownEnvelopeItem = BaseEnvelopeItem<{ type: '__unknown__' }>;
28+
29+
type BaseEnvelope<
30+
EnvelopeHeaders extends Record<string, unknown>,
31+
EnvelopeItem extends BaseEnvelopeItem<{ type: string }>,
32+
> = {
33+
headers: CommonEnvelopeHeaders & EnvelopeHeaders;
34+
items: Array<EnvelopeItem | UnknownEnvelopeItem>;
35+
};
36+
37+
export type EventEnvelopeItem = BaseEnvelopeItem<{ type: 'event' | 'transaction' }, Event>;
38+
39+
type AttachmentEnvelopeItem = BaseEnvelopeItem<{ type: 'attachment'; filename: 'string' }>;
40+
41+
type UserFeedbackEnvelopeItem = BaseEnvelopeItem<
42+
{ type: 'user_report' },
43+
{
44+
event_id: string;
45+
email: User['email'];
46+
name: string;
47+
comments: string;
48+
}
49+
>;
50+
51+
export type EventEnvelope = BaseEnvelope<
52+
{ event_id: string; sent_at: string },
53+
EventEnvelopeItem | AttachmentEnvelopeItem | UserFeedbackEnvelopeItem
54+
>;
55+
56+
export type SessionEnvelopeItem =
57+
| BaseEnvelopeItem<{ type: 'session' }, Session>
58+
| BaseEnvelopeItem<{ type: 'sessions' }, SessionAggregates>;
59+
60+
export type SessionEnvelope = BaseEnvelope<{ sent_at: string }, SessionEnvelopeItem>;
61+
62+
export type ClientReportEnvelopeItem = BaseEnvelopeItem<
63+
{ type: 'client_report' },
64+
{ timestamp: number; discarded_events: { reason: Outcome; category: SentryRequestType; quantity: number } }
65+
>;
66+
67+
export type ClientReportEnvelope = BaseEnvelope<Record<string, unknown>, ClientReportEnvelopeItem>;
68+
69+
export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope;

packages/types/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ export { Client } from './client';
33
export { Context, Contexts } from './context';
44
export { DsnComponents, DsnLike, DsnProtocol } from './dsn';
55
export { DebugImage, DebugImageType, DebugMeta } from './debugMeta';
6+
export {
7+
ClientReportEnvelope,
8+
ClientReportEnvelopeItem,
9+
Envelope,
10+
EventEnvelope,
11+
EventEnvelopeItem,
12+
SessionEnvelope,
13+
SessionEnvelopeItem,
14+
} from './envelope';
615
export { ExtendedError } from './error';
716
export { Event, EventHint } from './event';
817
export { EventStatus } from './eventstatus';

0 commit comments

Comments
 (0)