Skip to content

Commit 894293b

Browse files
committed
port BrowserBackend to BrowserClient
1 parent c7e18d3 commit 894293b

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

packages/browser/src/client.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
2-
import { Event, EventHint } from '@sentry/types';
3-
import { getGlobalObject, logger } from '@sentry/utils';
1+
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails, Scope, SDK_VERSION } from '@sentry/core';
2+
import { Event, EventHint, Severity, Transport, TransportOptions } from '@sentry/types';
3+
import { getGlobalObject, logger, supportsFetch } from '@sentry/utils';
44

55
import { BrowserBackend, BrowserOptions } from './backend';
6+
import { eventFromException, eventFromMessage } from './eventbuilder';
67
import { IS_DEBUG_BUILD } from './flags';
78
import { injectReportDialog, ReportDialogOptions } from './helpers';
89
import { Breadcrumbs } from './integrations';
10+
import { FetchTransport, makeNewFetchTransport, makeNewXHRTransport, XHRTransport } from './transports';
911

1012
/**
1113
* The Sentry Browser SDK Client.
1214
*
1315
* @see BrowserOptions for documentation on configuration options.
1416
* @see SentryClient for usage documentation.
17+
* TODO: remove BrowserBackend
1518
*/
1619
export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
1720
/**
@@ -32,6 +35,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
3235
version: SDK_VERSION,
3336
};
3437

38+
// TODO: remove BrowserBackend param
3539
super(BrowserBackend, options);
3640
}
3741

@@ -58,6 +62,20 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
5862
});
5963
}
6064

65+
/**
66+
* @inheritDoc
67+
*/
68+
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
69+
return eventFromException(exception, hint, this._options.attachStacktrace);
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
76+
return eventFromMessage(message, level, hint, this._options.attachStacktrace);
77+
}
78+
6179
/**
6280
* @inheritDoc
6381
*/
@@ -76,4 +94,40 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
7694
}
7795
super._sendEvent(event);
7896
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
protected _setupTransport(): Transport {
102+
if (!this._options.dsn) {
103+
// We return the noop transport here in case there is no Dsn.
104+
return super._setupTransport();
105+
}
106+
107+
const transportOptions: TransportOptions = {
108+
...this._options.transportOptions,
109+
dsn: this._options.dsn,
110+
tunnel: this._options.tunnel,
111+
sendClientReports: this._options.sendClientReports,
112+
_metadata: this._options._metadata,
113+
};
114+
115+
const api = initAPIDetails(transportOptions.dsn, transportOptions._metadata, transportOptions.tunnel);
116+
const url = getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel);
117+
118+
if (this._options.transport) {
119+
return new this._options.transport(transportOptions);
120+
}
121+
if (supportsFetch()) {
122+
const requestOptions: RequestInit = { ...transportOptions.fetchParameters };
123+
this._newTransport = makeNewFetchTransport({ requestOptions, url });
124+
return new FetchTransport(transportOptions);
125+
}
126+
127+
this._newTransport = makeNewXHRTransport({
128+
url,
129+
headers: transportOptions.headers,
130+
});
131+
return new XHRTransport(transportOptions);
132+
}
79133
}

0 commit comments

Comments
 (0)