8000 [v7] Extract client creation to separate initClient function · xiaohuoni/sentry-javascript@d53cc72 · GitHub
[go: up one dir, main page]

Skip to content

Commit d53cc72

Browse files
committed
[v7] Extract client creation to separate initClient function
1 parent 6aa4f65 commit d53cc72

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

packages/browser/src/client.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { BaseClient, SDK_VERSION } from '@sentry/core';
2-
import { CaptureContext, SentryEvent, Options } from '@sentry/types';
3-
import { supportsFetch } from '@sentry/utils';
4-
import { FetchTransport } from '@sentry/transport-fetch';
5-
import { XHRTransport } from '@sentry/transport-xhr';
2+
import { CaptureContext, ClientConfig, SentryEvent, Options } from '@sentry/types';
63
import { eventFromException, eventFromMessage } from '@sentry/eventbuilder-browser';
74

85
/**
@@ -55,7 +52,6 @@ export class BrowserClient extends BaseClient<BrowserOptions> {
5552
],
5653
version: SDK_VERSION,
5754
};
58-
options.transport = options.transport ?? (supportsFetch() ? FetchTransport : XHRTransport);
5955

6056
super(options);
6157
}

packages/browser/src/sdk.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ClientLike } from '@sentry/types';
22
import { captureException, getCarrier, getCurrentClient } from '@sentry/minimal';
3-
import { addInstrumentationHandler, getGlobalObject, logger } from '@sentry/utils';
3+
import { addInstrumentationHandler, getGlobalObject, logger, supportsFetch } from '@sentry/utils';
44
import { Dsn, getReportDialogEndpoint, ReportDialogOptions } from '@sentry/transport-base';
55
import { InboundFilters } from '@sentry/integration-common-inboundfilters';
66
import { UserAgent } from '@sentry/integration-browser-useragent';
@@ -16,6 +16,8 @@ import { LinkedErrors } from '@sentry/integration-browser-linkederrors';
1616
import { OnError, OnUnhandledRejection } from '@sentry/integration-browser-globalhandlers';
1717

1818
import { BrowserClient, BrowserOptions } from './client';
19+
import { FetchTransport } from '@sentry/transport-fetch';
20+
import { XHRTransport } from '@sentry/transport-xhr';
1921

2022
export const defaultIntegrations = [
2123
new EventTargetWrap(),
@@ -91,29 +93,37 @@ export const defaultIntegrations = [
9193
* @see {@link BrowserOptions} for documentation on configuration options.
9294
*/
9395
export function init(options: BrowserOptions = {}): ClientLike {
96+
const carrier = getCarrier();
97+
const client = initClient(options);
98+
carrier.client = client;
99+
100+
if (options.autoSessionTracking) {
101+
startSessionTracking(client);
102+
}
103+
104+
return client;
105+
}
106+
107+
export function initClient(options: BrowserOptions = {}): ClientLike {
94108
if (options.defaultIntegrations === undefined) {
95109
options.defaultIntegrations = defaultIntegrations;
96110
}
111+
97112
if (options.release === undefined) {
98113
const window = getGlobalObject<Window>();
99114
// This supports the variable that sentry-webpack-plugin injects
100115
if (window.SENTRY_RELEASE && window.SENTRY_RELEASE.id) {
101116
options.release = window.SENTRY_RELEASE.id;
102117
}
103118
}
119+
104120
if (options.autoSessionTracking === undefined) {
105121
options.autoSessionTracking = true;
106< 8000 /code>122
}
107123

108-
const carrier = getCarrier();
109-
const client = new BrowserClient(options);
110-
carrier.client = client;
124+
options.transport = options.transport ?? (supportsFetch() ? FetchTransport : XHRTransport);
111125

112-
if (options.autoSessionTracking) {
113-
startSessionTracking(client);
114-
}
115-
116-
return client;
126+
return new BrowserClient(options);
117127
}
118128

119129
/**

packages/node/src/client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export class NodeClient extends BaseClient<NodeOptions> {
5656
],
5757
version: SDK_VERSION,
5858
};
59-
options.transport = options.transport ?? HTTPTransport;
6059

6160
super(options);
6261
}

packages/node/src/sdk.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { LinkedErrors } from '@sentry/integration-node-linkederrors';
77
import { ContextLines } from '@sentry/integration-node-contextlines';
88
import { OnUncaughtException, OnUnhandledRejection } from '@sentry/integration-node-globalhandlers';
99
import { ConsoleBreadcrumbs, HTTPBreadcrumbs } from '@sentry/integration-node-breadcrumbs';
10+
import { HTTPTransport } from '@sentry/transport-http';
11+
import { ClientLike } from '@sentry/types';
1012

1113
import { NodeClient, NodeOptions } from './client';
12-
import { ClientLike } from '@sentry/types';
1314

1415
export const defaultIntegrations = [
1516
new ConsoleBreadcrumbs(),
@@ -77,6 +78,15 @@ export const defaultIntegrations = [
7778
* @see {@link NodeOptions} for documentation on configuration options.
7879
*/
7980
export function init(options: NodeOptions = {}): ClientLike {
81+
// TODO: Reevaluate whether stickin it on the domain is still necessary
82+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
83+
const carrier = (domain as any).active ? getCarrier((domain as any).active) : getCarrier();
84+
const client = initClient(options);
85+
carrier.client = client;
86+
return client;
87+
}
88+
89+
export function initClient(options: NodeOptions = {}): ClientLike {
8090
if (options.defaultIntegrations === undefined) {
8191
options.defaultIntegrations = defaultIntegrations;
8292
}
@@ -108,10 +118,7 @@ export function init(options: NodeOptions = {}): ClientLike {
108118
options.environment = process.env.SENTRY_ENVIRONMENT;
109119
}
110120

111-
// TODO: Reevaluate whether stickin it on the domain is still necessary
112-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
113-
const carrier = (domain as any).active ? getCarrier((domain as any).active) : getCarrier();
114-
const client = new NodeClient(options);
115-
carrier.client = client;
116-
return client;
121+
options.transport = options.transport ?? HTTPTransport;
122+
123+
return new NodeClient(options);
117124
}

0 commit comments

Comments
 (0)
0