Skip to content

Commit 52af0b1

Browse files
authored
ref(browser): Unify BrowserTransportOptions (#5058)
Enforce browser transport types by unifying fetch and xhr types into a single `BrowserTransportOptions` interface. This is used as a generic for `ClientOptions<BrowserTransportOptions>`. This cleans up the transport typing, and enables the transportOptions to be typed correctly in init.
1 parent 1342c42 commit 52af0b1

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

packages/browser/src/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { eventFromException, eventFromMessage } from './eventbuilder';
1313
import { IS_DEBUG_BUILD } from './flags';
1414
import { Breadcrumbs } from './integrations';
1515
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
16+
import { BrowserTransportOptions } from './transports/types';
1617
import { sendReport } from './transports/utils';
1718

1819
const globalObject = getGlobalObject<Window>();
@@ -37,13 +38,13 @@ export interface BaseBrowserOptions {
3738
* Configuration options for the Sentry Browser SDK.
3839
* @see @sentry/types Options for more information.
3940
*/
40-
export interface BrowserOptions extends Options, BaseBrowserOptions {}
41+
export interface BrowserOptions extends Options<BrowserTransportOptions>, BaseBrowserOptions {}
4142

4243
/**
4344
* Configuration options for the Sentry Browser SDK Client class
4445
* @see BrowserClient for more information.
4546
*/
46-
export interface BrowserClientOptions extends ClientOptions, BaseBrowserOptions {}
47+
export interface BrowserClientOptions extends ClientOptions<BrowserTransportOptions>, BaseBrowserOptions {}
4748

4849
/**
4950
* The Sentry Browser SDK Client.

packages/browser/src/transports/fetch.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import { createTransport } from '@sentry/core';
2-
import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
2+
import { Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
33

4+
import { BrowserTransportOptions } from './types';
45
import { FetchImpl, getNativeFetchImplementation } from './utils';
56

6-
export interface FetchTransportOptions extends BaseTransportOptions {
7-
requestOptions?: RequestInit;
8-
}
9-
107
/**
118
* Creates a Transport that uses the Fetch API to send events to Sentry.
129
*/
1310
export function makeFetchTransport(
14-
options: FetchTransportOptions,
11+
options: BrowserTransportOptions,
1512
nativeFetch: FetchImpl = getNativeFetchImplementation(),
1613
): Transport {
1714
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
1815
const requestOptions: RequestInit = {
1916
body: request.body,
2017
method: 'POST',
2118
referrerPolicy: 'origin',
22-
...options.requestOptions,
19+
headers: options.headers,
20+
...options.fetchOptions,
2321
};
2422

2523
return nativeFetch(options.url, requestOptions).then(response => ({
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BaseTransportOptions } from '@sentry/types';
2+
3+
export interface BrowserTransportOptions extends BaseTransportOptions {
4+
/** Fetch API init parameters. Used by the FetchTransport */
5+
fetchOptions?: RequestInit;
6+
/** Custom headers for the transport. Used by the XHRTransport and FetchTransport */
7+
headers?: { [key: string]: string };
8+
}

packages/browser/src/transports/xhr.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { createTransport } from '@sentry/core';
2-
import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
2+
import { Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
33
import { SyncPromise } from '@sentry/utils';
44

5+
import { BrowserTransportOptions } from './types';
6+
57
/**
68
* The DONE ready state for XmlHttpRequest
79
*
@@ -12,14 +14,10 @@ import { SyncPromise } from '@sentry/utils';
1214
*/
1315
const XHR_READYSTATE_DONE = 4;
1416

15-
export interface XHRTransportOptions extends BaseTransportOptions {
16-
headers?: { [key: string]: string };
17-
}
18-
1917
/**
2018
* Creates a Transport that uses the XMLHttpRequest API to send events to Sentry.
2119
*/
22-
export function makeXHRTransport(options: XHRTransportOptions): Transport {
20+
export function makeXHRTransport(options: BrowserTransportOptions): Transport {
2321
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
2422
return new SyncPromise((resolve, reject) => {
2523
const xhr = new XMLHttpRequest();

packages/browser/test/unit/transports/fetch.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { EventEnvelope, EventItem } from '@sentry/types';
22
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
33

4-
import { FetchTransportOptions, makeFetchTransport } from '../../../src/transports/fetch';
4+
import { makeFetchTransport } from '../../../src/transports/fetch';
5+
import { BrowserTransportOptions } from '../../../src/transports/types';
56
import { FetchImpl } from '../../../src/transports/utils';
67

7-
const DEFAULT_FETCH_TRANSPORT_OPTIONS: FetchTransportOptions = {
8+
const DEFAULT_FETCH_TRANSPORT_OPTIONS: BrowserTransportOptions = {
89
url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7',
910
recordDroppedEvent: () => undefined,
1011
};
@@ -83,7 +84,7 @@ describe('NewFetchTransport', () => {
8384
};
8485

8586
const transport = makeFetchTransport(
86-
{ ...DEFAULT_FETCH_TRANSPORT_OPTIONS, requestOptions: REQUEST_OPTIONS },
87+
{ ...DEFAULT_FETCH_TRANSPORT_OPTIONS, fetchOptions: REQUEST_OPTIONS },
8788
mockFetch,
8889
);
8990

packages/browser/test/unit/transports/xhr.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { EventEnvelope, EventItem } from '@sentry/types';
22
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
33

4-
import { makeXHRTransport, XHRTransportOptions } from '../../../src/transports/xhr';
4+
import { BrowserTransportOptions } from '../../../src/transports/types';
5+
import { makeXHRTransport } from '../../../src/transports/xhr';
56

6-
const DEFAULT_XHR_TRANSPORT_OPTIONS: XHRTransportOptions = {
7+
const DEFAULT_XHR_TRANSPORT_OPTIONS: BrowserTransportOptions = {
78
url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7',
89
recordDroppedEvent: () => undefined,
910
};
@@ -82,7 +83,7 @@ describe('NewXHRTransport', () => {
8283
keepalive: 'true',
8384
referrer: 'http://example.org',
8485
};
85-
const options: XHRTransportOptions = {
86+
const options: BrowserTransportOptions = {
8687
...DEFAULT_XHR_TRANSPORT_OPTIONS,
8788
headers,
8889
};

0 commit comments

Comments
 (0)