Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Add Envelope types #4527

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions packages/types/src/envelope.ts
Original file line number Diff line number Diff line change
@@ -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<ItemHeader extends { type: string }, Payload = unknown> = [
CommonEnvelopeItemHeaders & ItemHeader,
Payload,
];

type UnknownEnvelopeItem = BaseEnvelopeItem<{ type: '__unknown__' }>;

type BaseEnvelope<
EnvelopeHeaders extends Record<string, unknown>,
EnvelopeItem extends BaseEnvelopeItem<{ type: string }>,
> = {
headers: CommonEnvelopeHeaders & EnvelopeHeaders;
items: Array<EnvelopeItem | UnknownEnvelopeItem>;
};

export type EventEnvelopeItem = BaseEnvelopeItem<{ type: 'event' | 'transaction' }, Event>;
Copy link

@HofmannZ HofmannZ Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line causes a type error on node version 14.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh interesting. Could you open a GitHub issue please with your tsconfig, package.json and some details about your setup?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to do that tomorrow, we now locked the @sentry/types to 6.17.7 in our package.json and that resolves the issue.

...
"@sentry/types": "6.17.7"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think it's filed with #4583. Woooops, thats on me. Will fix with #4584


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<Record<string, unknown>, ClientReportEnvelopeItem>;

export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope;
9 changes: 9 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down