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