diff --git a/packages/types/src/envelope.ts b/packages/types/src/envelope.ts new file mode 100644 index 000000000000..0c981cb1557a --- /dev/null +++ b/packages/types/src/envelope.ts @@ -0,0 +1,69 @@ +import { SentryRequestType } from './request'; +import { SdkInfo } from './sdkinfo'; +import { Session, SessionAggregates } from './session'; +import { Outcome } from './transport'; +import { User } from './user'; + +// Based on: https://develop.sentry.dev/sdk/envelopes/ + +type CommonEnvelopeHeaders = { + dsn?: string; + sdk?: SdkInfo; +}; + +type CommonEnvelopeItemHeaders = { + length?: number; +}; + +/** + * 1st Item: Item headers + * 2nd Item: Item payload + */ +type BaseEnvelopeItem = [ + CommonEnvelopeItemHeaders & ItemHeader, + Payload, +]; + +type UnknownEnvelopeItem = BaseEnvelopeItem<{ type: '__unknown__' }>; + +type BaseEnvelope< + EnvelopeHeaders extends Record, + EnvelopeItem extends BaseEnvelopeItem<{ type: string }>, +> = { + headers: CommonEnvelopeHeaders & EnvelopeHeaders; + items: Array; +}; + +export type EventEnvelopeItem = BaseEnvelopeItem<{ type: 'event' | 'transaction' }, Event>; + +type AttachmentEnvelopeItem = BaseEnvelopeItem<{ type: 'attachment'; filename: 'string' }>; + +type UserFeedbackEnvelopeItem = BaseEnvelopeItem< + { type: 'user_report' }, + { + event_id: string; + email: User['email']; + name: string; + comments: string; + } +>; + +export type EventEnvelope = BaseEnvelope< + { event_id: string; sent_at: string }, + EventEnvelopeItem | AttachmentEnvelopeItem | UserFeedbackEnvelopeItem +>; + +export type SessionEnvelopeItem = + | BaseEnvelopeItem<{ type: 'session' }, Session> + | BaseEnvelopeItem<{ type: 'sessions' }, SessionAggregates>; + +export type SessionEnvelope = BaseEnvelope<{ sent_at: string }, SessionEnvelopeItem>; + +export type ClientReportEnvelopeItem = BaseEnvelopeItem< + { type: 'client_report' }, + { timestamp: number; discarded_events: { reason: Outcome; category: SentryRequestType; quantity: number } } +>; + +export type ClientReportEnvelope = BaseEnvelope, ClientReportEnvelopeItem>; + +export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index ecb62c2937fa..818f50759fa0 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -3,6 +3,15 @@ export { Client } from './client'; export { Context, Contexts } from './context'; export { DsnComponents, DsnLike, DsnProtocol } from './dsn'; export { DebugImage, DebugImageType, DebugMeta } from './debugMeta'; +export { + ClientReportEnvelope, + ClientReportEnvelopeItem, + Envelope, + EventEnvelope, + EventEnvelopeItem, + SessionEnvelope, + SessionEnvelopeItem, +} from './envelope'; export { ExtendedError } from './error'; export { Event, EventHint } from './event'; export { EventStatus } from './eventstatus';