From b990820a5ff777de8e5cbf324b5a05c279670f73 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 9 May 2022 15:10:50 -0400 Subject: [PATCH 1/2] ref(browser): Unify BrowserTransportOptions Enforce browser transport types by unifying fetch and xhr types into a single `BrowserTransportOptions` interface. This is used as a generic for `ClientOptions`. This cleans up the transport typing, and enables the transportOptions to be typed correctly in init. --- packages/browser/src/client.ts | 5 +++-- packages/browser/src/transports/fetch.ts | 12 +++++------- packages/browser/src/transports/types.ts | 8 ++++++++ packages/browser/src/transports/xhr.ts | 10 ++++------ packages/browser/test/unit/transports/fetch.test.ts | 7 ++++--- packages/browser/test/unit/transports/xhr.test.ts | 7 ++++--- 6 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 packages/browser/src/transports/types.ts diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 261ceba0a88e..2eeefc18c7ed 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -13,6 +13,7 @@ import { eventFromException, eventFromMessage } from './eventbuilder'; import { IS_DEBUG_BUILD } from './flags'; import { Breadcrumbs } from './integrations'; import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs'; +import { BrowserTransportOptions } from './transports/types'; import { sendReport } from './transports/utils'; const globalObject = getGlobalObject(); @@ -37,13 +38,13 @@ export interface BaseBrowserOptions { * Configuration options for the Sentry Browser SDK. * @see @sentry/types Options for more information. */ -export interface BrowserOptions extends Options, BaseBrowserOptions {} +export interface BrowserOptions extends Options, BaseBrowserOptions {} /** * Configuration options for the Sentry Browser SDK Client class * @see BrowserClient for more information. */ -export interface BrowserClientOptions extends ClientOptions, BaseBrowserOptions {} +export interface BrowserClientOptions extends ClientOptions, BaseBrowserOptions {} /** * The Sentry Browser SDK Client. diff --git a/packages/browser/src/transports/fetch.ts b/packages/browser/src/transports/fetch.ts index d6f44090abb9..d4e57e59862e 100644 --- a/packages/browser/src/transports/fetch.ts +++ b/packages/browser/src/transports/fetch.ts @@ -1,17 +1,14 @@ import { createTransport } from '@sentry/core'; -import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; +import { Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; +import { BrowserTransportOptions } from './types'; import { FetchImpl, getNativeFetchImplementation } from './utils'; -export interface FetchTransportOptions extends BaseTransportOptions { - requestOptions?: RequestInit; -} - /** * Creates a Transport that uses the Fetch API to send events to Sentry. */ export function makeFetchTransport( - options: FetchTransportOptions, + options: BrowserTransportOptions, nativeFetch: FetchImpl = getNativeFetchImplementation(), ): Transport { function makeRequest(request: TransportRequest): PromiseLike { @@ -19,7 +16,8 @@ export function makeFetchTransport( body: request.body, method: 'POST', referrerPolicy: 'origin', - ...options.requestOptions, + headers: options.headers, + ...options.fetchOptions, }; return nativeFetch(options.url, requestOptions).then(response => ({ diff --git a/packages/browser/src/transports/types.ts b/packages/browser/src/transports/types.ts new file mode 100644 index 000000000000..4f4b7ef2c98a --- /dev/null +++ b/packages/browser/src/transports/types.ts @@ -0,0 +1,8 @@ +import { BaseTransportOptions } from '@sentry/types'; + +export interface BrowserTransportOptions extends BaseTransportOptions { + /** Fetch API init parameters. Used by the FetchTransport */ + fetchOptions?: RequestInit; + /** Custom headers for the transport. Used by the XHRTransport and FetchTransport */ + headers?: { [key: string]: string }; +} diff --git a/packages/browser/src/transports/xhr.ts b/packages/browser/src/transports/xhr.ts index 4c4982225b85..9a8b10c6187d 100644 --- a/packages/browser/src/transports/xhr.ts +++ b/packages/browser/src/transports/xhr.ts @@ -1,7 +1,9 @@ import { createTransport } from '@sentry/core'; -import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; +import { Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; import { SyncPromise } from '@sentry/utils'; +import { BrowserTransportOptions } from './types'; + /** * The DONE ready state for XmlHttpRequest * @@ -12,14 +14,10 @@ import { SyncPromise } from '@sentry/utils'; */ const XHR_READYSTATE_DONE = 4; -export interface XHRTransportOptions extends BaseTransportOptions { - headers?: { [key: string]: string }; -} - /** * Creates a Transport that uses the XMLHttpRequest API to send events to Sentry. */ -export function makeXHRTransport(options: XHRTransportOptions): Transport { +export function makeXHRTransport(options: BrowserTransportOptions): Transport { function makeRequest(request: TransportRequest): PromiseLike { return new SyncPromise((resolve, reject) => { const xhr = new XMLHttpRequest(); diff --git a/packages/browser/test/unit/transports/fetch.test.ts b/packages/browser/test/unit/transports/fetch.test.ts index 63fe62814628..fd2b9d5f5cfc 100644 --- a/packages/browser/test/unit/transports/fetch.test.ts +++ b/packages/browser/test/unit/transports/fetch.test.ts @@ -1,10 +1,11 @@ import { EventEnvelope, EventItem } from '@sentry/types'; import { createEnvelope, serializeEnvelope } from '@sentry/utils'; -import { FetchTransportOptions, makeFetchTransport } from '../../../src/transports/fetch'; +import { makeFetchTransport } from '../../../src/transports/fetch'; import { FetchImpl } from '../../../src/transports/utils'; +import { BrowserTransportOptions } from '../../../src/transports/types'; -const DEFAULT_FETCH_TRANSPORT_OPTIONS: FetchTransportOptions = { +const DEFAULT_FETCH_TRANSPORT_OPTIONS: BrowserTransportOptions = { url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', recordDroppedEvent: () => undefined, }; @@ -83,7 +84,7 @@ describe('NewFetchTransport', () => { }; const transport = makeFetchTransport( - { ...DEFAULT_FETCH_TRANSPORT_OPTIONS, requestOptions: REQUEST_OPTIONS }, + { ...DEFAULT_FETCH_TRANSPORT_OPTIONS, fetchOptions: REQUEST_OPTIONS }, mockFetch, ); diff --git a/packages/browser/test/unit/transports/xhr.test.ts b/packages/browser/test/unit/transports/xhr.test.ts index 35ca842db3d1..118c7b6b4201 100644 --- a/packages/browser/test/unit/transports/xhr.test.ts +++ b/packages/browser/test/unit/transports/xhr.test.ts @@ -1,9 +1,10 @@ import { EventEnvelope, EventItem } from '@sentry/types'; import { createEnvelope, serializeEnvelope } from '@sentry/utils'; -import { makeXHRTransport, XHRTransportOptions } from '../../../src/transports/xhr'; +import { makeXHRTransport } from '../../../src/transports/xhr'; +import { BrowserTransportOptions } from '../../../src/transports/types'; -const DEFAULT_XHR_TRANSPORT_OPTIONS: XHRTransportOptions = { +const DEFAULT_XHR_TRANSPORT_OPTIONS: BrowserTransportOptions = { url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', recordDroppedEvent: () => undefined, }; @@ -82,7 +83,7 @@ describe('NewXHRTransport', () => { keepalive: 'true', referrer: 'http://example.org', }; - const options: XHRTransportOptions = { + const options: BrowserTransportOptions = { ...DEFAULT_XHR_TRANSPORT_OPTIONS, headers, }; From c274a905b3b783fea0e94c5c227447957a1fd191 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 10 May 2022 09:54:00 -0400 Subject: [PATCH 2/2] yarn fix --- packages/browser/test/unit/transports/fetch.test.ts | 2 +- packages/browser/test/unit/transports/xhr.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/test/unit/transports/fetch.test.ts b/packages/browser/test/unit/transports/fetch.test.ts index fd2b9d5f5cfc..4684b87fdf8b 100644 --- a/packages/browser/test/unit/transports/fetch.test.ts +++ b/packages/browser/test/unit/transports/fetch.test.ts @@ -2,8 +2,8 @@ import { EventEnvelope, EventItem } from '@sentry/types'; import { createEnvelope, serializeEnvelope } from '@sentry/utils'; import { makeFetchTransport } from '../../../src/transports/fetch'; -import { FetchImpl } from '../../../src/transports/utils'; import { BrowserTransportOptions } from '../../../src/transports/types'; +import { FetchImpl } from '../../../src/transports/utils'; const DEFAULT_FETCH_TRANSPORT_OPTIONS: BrowserTransportOptions = { url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', diff --git a/packages/browser/test/unit/transports/xhr.test.ts b/packages/browser/test/unit/transports/xhr.test.ts index 118c7b6b4201..15a5c7ce3d62 100644 --- a/packages/browser/test/unit/transports/xhr.test.ts +++ b/packages/browser/test/unit/transports/xhr.test.ts @@ -1,8 +1,8 @@ import { EventEnvelope, EventItem } from '@sentry/types'; import { createEnvelope, serializeEnvelope } from '@sentry/utils'; -import { makeXHRTransport } from '../../../src/transports/xhr'; import { BrowserTransportOptions } from '../../../src/transports/types'; +import { makeXHRTransport } from '../../../src/transports/xhr'; const DEFAULT_XHR_TRANSPORT_OPTIONS: BrowserTransportOptions = { url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7',