|
7 | 7 | SentryRequestType, |
8 | 8 | Session, |
9 | 9 | SessionAggregates, |
| 10 | + SessionEnvelope, |
| 11 | + SessionItem, |
10 | 12 | } from '@sentry/types'; |
11 | 13 | import { createEnvelope, dsnToString, normalize, serializeEnvelope } from '@sentry/utils'; |
12 | 14 |
|
@@ -40,19 +42,20 @@ function enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event { |
40 | 42 | /** Creates a SentryRequest from a Session. */ |
41 | 43 | export function sessionToSentryRequest(session: Session | SessionAggregates, api: APIDetails): SentryRequest { |
42 | 44 | const sdkInfo = getSdkMetadataForEnvelopeHeader(api); |
43 | | - const envelopeHeaders = JSON.stringify({ |
| 45 | + const envelopeHeaders = { |
44 | 46 | sent_at: new Date().toISOString(), |
45 | 47 | ...(sdkInfo && { sdk: sdkInfo }), |
46 | 48 | ...(!!api.tunnel && { dsn: dsnToString(api.dsn) }), |
47 | | - }); |
48 | | - // I know this is hacky but we don't want to add `session` to request type since it's never rate limited |
49 | | - const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session'; |
50 | | - const itemHeaders = JSON.stringify({ |
51 | | - type, |
52 | | - }); |
| 49 | + }; |
| 50 | + |
| 51 | + // I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited |
| 52 | + const type = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session'; |
53 | 53 |
|
| 54 | + // TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType` |
| 55 | + const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem; |
| 56 | + const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]); |
54 | 57 | return { |
55 | | - body: `${envelopeHeaders}\n${itemHeaders}\n${JSON.stringify(session)}`, |
| 58 | + body: serializeEnvelope(envelope), |
56 | 59 | type, |
57 | 60 | url: getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel), |
58 | 61 | }; |
|
0 commit comments