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