|
| 1 | +import { |
| 2 | + BaseTransportOptions, |
| 3 | + getEnvelopeEndpointWithUrlEncodedAuth, |
| 4 | + initAPIDetails, |
| 5 | + NewTransport, |
| 6 | + NoopTransport, |
| 7 | +} from '@sentry/core'; |
| 8 | +import { Transport, TransportOptions } from '@sentry/types'; |
| 9 | +import { supportsFetch } from '@sentry/utils'; |
| 10 | + |
| 11 | +import { BrowserOptions } from '../client'; |
| 12 | +import { FetchTransport } from './fetch'; |
| 13 | +import { makeNewFetchTransport } from './new-fetch'; |
| 14 | +import { makeNewXHRTransport } from './new-xhr'; |
| 15 | +import { XHRTransport } from './xhr'; |
| 16 | + |
| 17 | +export interface BrowserTransportOptions extends BaseTransportOptions { |
| 18 | + // options to pass into fetch request |
| 19 | + fetchParams: Record<string, string>; |
| 20 | + headers?: Record<string, string>; |
| 21 | + sendClientReports?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * TODO: additional doc (since this is not part of Client anymore) |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | +// TODO(v7): refactor to only return newTransport |
| 29 | +export function setupBrowserTransport(options: BrowserOptions): { transport: Transport; newTransport?: NewTransport } { |
| 30 | + if (!options.dsn) { |
| 31 | + // We return the noop transport here in case there is no Dsn. |
| 32 | + return { transport: new NoopTransport() }; |
| 33 | + } |
| 34 | + |
| 35 | + const transportOptions: TransportOptions = { |
| 36 | + ...options.transportOptions, |
| 37 | + dsn: options.dsn, |
| 38 | + tunnel: options.tunnel, |
| 39 | + sendClientReports: options.sendClientReports, |
| 40 | + _metadata: options._metadata, |
| 41 | + }; |
| 42 | + |
| 43 | + const api = initAPIDetails(transportOptions.dsn, transportOptions._metadata, transportOptions.tunnel); |
| 44 | + const url = getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel); |
| 45 | + |
| 46 | + if (options.transport) { |
| 47 | + return { transport: new options.transport(transportOptions) }; |
| 48 | + } |
| 49 | + if (supportsFetch()) { |
| 50 | + const requestOptions: RequestInit = { ...transportOptions.fetchParameters }; |
| 51 | + const newTransport = makeNewFetchTransport({ requestOptions, url }); |
| 52 | + const fetchTransport = new FetchTransport(transportOptions); |
| 53 | + return { transport: fetchTransport, newTransport }; |
| 54 | + } |
| 55 | + |
| 56 | + const newTransport = makeNewXHRTransport({ |
| 57 | + url, |
| 58 | + headers: transportOptions.headers, |
| 59 | + }); |
| 60 | + const transport = new XHRTransport(transportOptions); |
| 61 | + return { transport, newTransport }; |
| 62 | +} |
0 commit comments