Skip to content

Commit 5f6335d

Browse files
authored
ref(utils): Introduce getEnvelopeType helper (#4751)
Simplify new transport send by grabbing the envelope category from the envelope instead of passing it in explicitly.
1 parent 6d63e58 commit 5f6335d

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

packages/core/src/transports/base.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Envelope, EventStatus } from '@sentry/types';
22
import {
33
disabledUntil,
44
eventStatusFromHttpCode,
5+
getEnvelopeType,
56
isRateLimited,
67
makePromiseBuffer,
78
PromiseBuffer,
@@ -68,6 +69,7 @@ export interface BrowserTransportOptions extends BaseTransportOptions {
6869

6970
// TODO: Move into Node transport
7071
export interface NodeTransportOptions extends BaseTransportOptions {
72+
headers?: Record<string, string>;
7173
// Set a HTTP proxy that should be used for outbound requests.
7274
httpProxy?: string;
7375
// Set a HTTPS proxy that should be used for outbound requests.
@@ -81,7 +83,7 @@ export interface NewTransport {
8183
// TODO(v7): Remove this as we will no longer have split between
8284
// old and new transports.
8385
$: boolean;
84-
send(request: Envelope, category: TransportCategory): PromiseLike<TransportResponse>;
86+
send(request: Envelope): PromiseLike<TransportResponse>;
8587
flush(timeout?: number): PromiseLike<boolean>;
8688
}
8789

@@ -104,7 +106,9 @@ export function createTransport(
104106

105107
const flush = (timeout?: number): PromiseLike<boolean> => buffer.drain(timeout);
106108

107-
function send(envelope: Envelope, category: TransportCategory): PromiseLike<TransportResponse> {
109+
function send(envelope: Envelope): PromiseLike<TransportResponse> {
110+
const envCategory = getEnvelopeType(envelope);
111+
const category = envCategory === 'event' ? 'error' : (envCategory as TransportCategory);
108112
const request: TransportRequest = {
109113
category,
110114
body: serializeEnvelope(envelope),

packages/core/test/lib/transports/base.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('createTransport', () => {
4747
expect(req.body).toEqual(serializeEnvelope(ERROR_ENVELOPE));
4848
return resolvedSyncPromise({ statusCode: 200, reason: 'OK' });
4949
});
50-
const res = await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
50+
const res = await transport.send(ERROR_ENVELOPE);
5151
expect(res.status).toBe('success');
5252
expect(res.reason).toBe('OK');
5353
});
@@ -59,7 +59,7 @@ describe('createTransport', () => {
5959
return resolvedSyncPromise({ statusCode: 400, reason: 'Bad Request' });
6060
});
6161
try {
62-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
62+
await transport.send(ERROR_ENVELOPE);
6363
} catch (res) {
6464
expect(res.status).toBe('invalid');
6565
expect(res.reason).toBe('Bad Request');
@@ -73,7 +73,7 @@ describe('createTransport', () => {
7373
return resolvedSyncPromise({ statusCode: 500 });
7474
});
7575
try {
76-
await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
76+
await transport.send(TRANSACTION_ENVELOPE);
7777
} catch (res) {
7878
expect(res.status).toBe('failed');
7979
expect(res.reason).toBe('Unknown transport error');
@@ -135,7 +135,7 @@ describe('createTransport', () => {
135135
});
136136

137137
try {
138-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
138+
await transport.send(ERROR_ENVELOPE);
139139
} catch (res) {
140140
expect(res.status).toBe('rate_limit');
141141
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
@@ -144,13 +144,13 @@ describe('createTransport', () => {
144144
setTransportResponse({ statusCode: 200 });
145145

146146
try {
147-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
147+
await transport.send(ERROR_ENVELOPE);
148148
} catch (res) {
149149
expect(res.status).toBe('rate_limit');
150150
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
151151
}
152152

153-
const res = await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
153+
const res = await transport.send(ERROR_ENVELOPE);
154154
expect(res.status).toBe('success');
155155
});
156156

@@ -181,7 +181,7 @@ describe('createTransport', () => {
181181
});
182182

183183
try {
184-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
184+
await transport.send(ERROR_ENVELOPE);
185185
} catch (res) {
186186
expect(res.status).toBe('rate_limit');
187187
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
@@ -190,20 +190,20 @@ describe('createTransport', () => {
190190
setTransportResponse({ statusCode: 200 });
191191

192192
try {
193-
await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
193+
await transport.send(TRANSACTION_ENVELOPE);
194194
} catch (res) {
195195
expect(res.status).toBe('rate_limit');
196196
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
197197
}
198198

199199
try {
200-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
200+
await transport.send(ERROR_ENVELOPE);
201201
} catch (res) {
202202
expect(res.status).toBe('rate_limit');
203203
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
204204
}
205205

206-
const res = await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
206+
const res = await transport.send(TRANSACTION_ENVELOPE);
207207
expect(res.status).toBe('success');
208208
});
209209

@@ -234,21 +234,21 @@ describe('createTransport', () => {
234234
});
235235

236236
try {
237-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
237+
await transport.send(ERROR_ENVELOPE);
238238
} catch (res) {
239239
expect(res.status).toBe('rate_limit');
240240
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
241241
}
242242

243243
try {
244-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
244+
await transport.send(ERROR_ENVELOPE);
245245
} catch (res) {
246246
expect(res.status).toBe('rate_limit');
247247
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
248248
}
249249

250250
try {
251-
await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
251+
await transport.send(TRANSACTION_ENVELOPE);
252252
} catch (res) {
253253
expect(res.status).toBe('rate_limit');
254254
expect(res.reason).toBe(
@@ -258,10 +258,10 @@ describe('createTransport', () => {
258258

259259
setTransportResponse({ statusCode: 200 });
260260

261-
const eventRes = await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
261+
const eventRes = await transport.send(ERROR_ENVELOPE);
262262
expect(eventRes.status).toBe('success');
263263

264-
const transactionRes = await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
264+
const transactionRes = await transport.send(TRANSACTION_ENVELOPE);
265265
expect(transactionRes.status).toBe('success');
266266
});
267267

@@ -296,21 +296,21 @@ describe('createTransport', () => {
296296
});
297297

298298
try {
299-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
299+
await transport.send(ERROR_ENVELOPE);
300300
} catch (res) {
301301
expect(res.status).toBe('rate_limit');
302302
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
303303
}
304304

305305
try {
306-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
306+
await transport.send(ERROR_ENVELOPE);
307307
} catch (res) {
308308
expect(res.status).toBe('rate_limit');
309309
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
310310
}
311311

312312
try {
313-
await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
313+
await transport.send(TRANSACTION_ENVELOPE);
314314
} catch (res) {
315315
expect(res.status).toBe('rate_limit');
316316
expect(res.reason).toBe(
@@ -320,10 +320,10 @@ describe('createTransport', () => {
320320

321321
setTransportResponse({ statusCode: 200 });
322322

323-
const eventRes = await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
323+
const eventRes = await transport.send(ERROR_ENVELOPE);
324324
expect(eventRes.status).toBe('success');
325325

326-
const transactionRes = await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
326+
const transactionRes = await transport.send(TRANSACTION_ENVELOPE);
327327
expect(transactionRes.status).toBe('success');
328328
});
329329

@@ -352,14 +352,14 @@ describe('createTransport', () => {
352352
});
353353

354354
try {
355-
await transport.send(ERROR_ENVELOPE, ERROR_TRANSPORT_CATEGORY);
355+
await transport.send(ERROR_ENVELOPE);
356356
} catch (res) {
357357
expect(res.status).toBe('rate_limit');
358358
expect(res.reason).toBe(`Too many error requests, backing off until: ${new Date(afterLimit).toISOString()}`);
359359
}
360360

361361
try {
362-
await transport.send(TRANSACTION_ENVELOPE, TRANSACTION_TRANSPORT_CATEGORY);
362+
await transport.send(TRANSACTION_ENVELOPE);
363363
} catch (res) {
364364
expect(res.status).toBe('rate_limit');
365365
expect(res.reason).toBe(

packages/types/src/envelope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type BaseEnvelope<EH extends BaseEnvelopeHeaders, I extends BaseEnvelopeItem<Bas
2828

2929
type EventItemHeaders = {
3030
type: 'event' | 'transaction';
31-
sample_rates: [{ id?: TransactionSamplingMethod; rate?: number }];
31+
sample_rates?: [{ id?: TransactionSamplingMethod; rate?: number }];
3232
};
3333
type AttachmentItemHeaders = { type: 'attachment'; filename: string };
3434
type UserFeedbackItemHeaders = { type: 'user_report' };

packages/utils/src/envelope.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export function addItemToEnvelope<E extends Envelope>(envelope: E, newItem: E[1]
2121
return [headers, [...items, newItem]] as E;
2222
}
2323

24+
/**
25+
* Get the type of the envelope. Grabs the type from the first envelope item.
26+
*/
27+
export function getEnvelopeType<E extends Envelope>(envelope: E): string {
28+
const [, [[firstItemHeader]]] = envelope;
29+
return firstItemHeader.type;
30+
}
31+
2432
/**
2533
* Serializes an envelope into a string.
2634
*/

packages/utils/test/envelope.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEnvelope } from '@sentry/types';
22

3-
import { addItemToEnvelope, createEnvelope, serializeEnvelope } from '../src/envelope';
3+
import { addItemToEnvelope, createEnvelope, getEnvelopeType, serializeEnvelope } from '../src/envelope';
44
import { parseEnvelope } from './testutils';
55

66
describe('envelope', () => {
@@ -44,4 +44,14 @@ describe('envelope', () => {
4444
expect(parsedNewEnvelope[2]).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' });
4545
});
4646
});
47+
48+
describe('getEnvelopeType', () => {
49+
it('returns the type of the envelope', () => {
50+
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
51+
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
52+
[{ type: 'attachment', filename: 'me.txt' }, '123456'],
53+
]);
54+
expect(getEnvelopeType(env)).toEqual('event');
55+
});
56+
});
4757
});

0 commit comments

Comments
 (0)