Skip to content

Commit 012c349

Browse files
authored
feat(core): Return client from init method (#12585)
fixes #12495
1 parent 6e07ec7 commit 012c349

File tree

42 files changed

+215
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+215
-86
lines changed

packages/angular/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
functionToStringIntegration,
1414
inboundFiltersIntegration,
1515
} from '@sentry/core';
16-
import type { Integration } from '@sentry/types';
16+
import type { Client, Integration } from '@sentry/types';
1717
import { logger } from '@sentry/utils';
1818

1919
import { IS_DEBUG_BUILD } from './flags';
@@ -44,7 +44,7 @@ export function getDefaultIntegrations(): Integration[] {
4444
/**
4545
* Inits the Angular SDK
4646
*/
47-
export function init(options: BrowserOptions): void {
47+
export function init(options: BrowserOptions): Client | undefined {
4848
const opts = {
4949
defaultIntegrations: getDefaultIntegrations(),
5050
...options,
@@ -53,7 +53,7 @@ export function init(options: BrowserOptions): void {
5353
applySdkMetadata(opts, 'angular');
5454

5555
checkAndSetAngularVersion();
56-
browserInit(opts);
56+
return browserInit(opts);
5757
}
5858

5959
function checkAndSetAngularVersion(): void {

packages/angular/test/sdk.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ describe('init', () => {
2626

2727
expect(angularDefaultIntegrations).toEqual(browserDefaultIntegrationsWithoutBrowserApiErrors);
2828
});
29+
30+
it('returns client from init', () => {
31+
expect(init({})).not.toBeUndefined();
32+
});
2933
});

packages/astro/src/client/sdk.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
setTag,
77
} from '@sentry/browser';
88
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
9-
import type { Integration } from '@sentry/types';
9+
import type { Client, Integration } from '@sentry/types';
1010

1111
// Tree-shakable guard to remove all code related to tracing
1212
declare const __SENTRY_TRACING__: boolean;
@@ -16,17 +16,19 @@ declare const __SENTRY_TRACING__: boolean;
1616
*
1717
* @param options Configuration options for the SDK.
1818
*/
19-
export function init(options: BrowserOptions): void {
19+
export function init(options: BrowserOptions): Client | undefined {
2020
const opts = {
2121
defaultIntegrations: getDefaultIntegrations(options),
2222
...options,
2323
};
2424

2525
applySdkMetadata(opts, 'astro', ['astro', 'browser']);
2626

27-
initBrowserSdk(opts);
27+
const client = initBrowserSdk(opts);
2828

2929
setTag('runtime', 'browser');
30+
31+
return client;
3032
}
3133

3234
function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefined {

packages/astro/src/server/sdk.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { applySdkMetadata } from '@sentry/core';
2-
import type { NodeOptions } from '@sentry/node';
2+
import type { NodeClient, NodeOptions } from '@sentry/node';
33
import { init as initNodeSdk, setTag } from '@sentry/node';
44

55
/**
66
*
77
* @param options
88
*/
9-
export function init(options: NodeOptions): void {
9+
export function init(options: NodeOptions): NodeClient | undefined {
1010
const opts = {
1111
...options,
1212
};
1313

1414
applySdkMetadata(opts, 'astro', ['astro', 'node']);
1515

16-
initNodeSdk(opts);
16+
const client = initNodeSdk(opts);
1717

1818
setTag('runtime', 'node');
19+
20+
return client;
1921
}

packages/astro/test/client/sdk.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,9 @@ describe('Sentry client SDK', () => {
122122
expect(getActiveSpan()).toBeUndefined();
123123
});
124124
});
125+
126+
it('returns client from init', () => {
127+
expect(init({})).not.toBeUndefined();
128+
});
125129
});
126130
});

packages/astro/test/server/sdk.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ describe('Sentry server SDK', () => {
4646

4747
expect(SentryNode.getIsolationScope().getScopeData().tags).toEqual({ runtime: 'node' });
4848
});
49+
50+
it('returns client from init', () => {
51+
expect(init({})).not.toBeUndefined();
52+
});
4953
});
5054
});

packages/aws-serverless/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22
import { hostname } from 'os';
33
import { basename, resolve } from 'path';
44
import { types } from 'util';
5-
import type { NodeOptions } from '@sentry/node';
5+
import type { NodeClient, NodeOptions } from '@sentry/node';
66
import {
77
SDK_VERSION,
88
captureException,
@@ -74,7 +74,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
7474
*
7575
* @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
7676
*/
77-
export function init(options: NodeOptions = {}): void {
77+
export function init(options: NodeOptions = {}): NodeClient | undefined {
7878
const opts = {
7979
_metadata: {} as SdkMetadata,
8080
defaultIntegrations: getDefaultIntegrations(options),
@@ -93,7 +93,7 @@ export function init(options: NodeOptions = {}): void {
9393
version: SDK_VERSION,
9494
};
9595

96-
initWithoutDefaultIntegrations(opts);
96+
return initWithoutDefaultIntegrations(opts);
9797
}
9898

9999
/** */

packages/browser/src/sdk.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
lastEventId,
1010
startSession,
1111
} from '@sentry/core';
12-
import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
12+
import type { Client, DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
1313
import { consoleSandbox, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils';
1414

1515
import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
@@ -139,7 +139,7 @@ declare const __SENTRY_RELEASE__: string | undefined;
139139
*
140140
* @see {@link BrowserOptions} for documentation on configuration options.
141141
*/
142-
export function init(browserOptions: BrowserOptions = {}): void {
142+
export function init(browserOptions: BrowserOptions = {}): Client | undefined {
143143
const options = applyDefaultOptions(browserOptions);
144144

145145
if (shouldShowBrowserExtensionError()) {
@@ -166,11 +166,13 @@ export function init(browserOptions: BrowserOptions = {}): void {
166166
transport: options.transport || makeFetchTransport,
167167
};
168168

169-
initAndBind(BrowserClient, clientOptions);
169+
const client = initAndBind(BrowserClient, clientOptions);
170170

171171
if (options.autoSessionTracking) {
172172
startSessionTracking();
173173
}
174+
175+
return client;
174176
}
175177

176178
/**

packages/browser/test/unit/sdk.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,25 @@ describe('init', () => {
209209

210210
consoleErrorSpy.mockRestore();
211211
});
212+
213+
it("doesn't return a client on initialization error", () => {
214+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
215+
216+
Object.defineProperty(WINDOW, 'chrome', {
217+
value: { runtime: { id: 'mock-extension-id' } },
218+
writable: true,
219+
});
220+
221+
const client = init(options);
222+
223+
expect(client).toBeUndefined();
224+
225+
consoleErrorSpy.mockRestore();
226+
});
227+
});
228+
229+
it('returns a client from init', () => {
230+
const client = init();
231+
expect(client).not.toBeUndefined();
212232
});
213233
});

packages/bun/src/sdk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
linkedErrorsIntegration,
55
requestDataIntegration,
66
} from '@sentry/core';
7+
import type { NodeClient } from '@sentry/node';
78
import {
89
consoleIntegration,
910
contextLinesIntegration,
@@ -91,13 +92,13 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
9192
*
9293
* @see {@link BunOptions} for documentation on configuration options.
9394
*/
94-
export function init(options: BunOptions = {}): void {
95+
export function init(options: BunOptions = {}): NodeClient | undefined {
9596
options.clientClass = BunClient;
9697
options.transport = options.transport || makeFetchTransport;
9798

9899
if (options.defaultIntegrations === undefined) {
99100
options.defaultIntegrations = getDefaultIntegrations(options);
100101
}
101102

102-
initNode(options);
103+
return initNode(options);
103104
}

packages/bun/test/sdk.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ test("calling init shouldn't fail", () => {
88
});
99
expect(true).toBe(true);
1010
});
11+
12+
test('shuold return client from init', () => {
13+
expect(init({})).not.toBeUndefined();
14+
});

packages/core/src/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type ClientClass<F extends Client, O extends ClientOptions> = new (option
1717
export function initAndBind<F extends Client, O extends ClientOptions>(
1818
clientClass: ClientClass<F, O>,
1919
options: O,
20-
): void {
20+
): Client {
2121
if (options.debug === true) {
2222
if (DEBUG_BUILD) {
2323
logger.enable();
@@ -35,6 +35,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
3535
const client = new clientClass(options);
3636
setCurrentClient(client);
3737
client.init();
38+
return client;
3839
}
3940

4041
/**

packages/core/test/lib/sdk.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ describe('SDK', () => {
8282
'afterAllSetup2',
8383
]);
8484
});
85+
86+
test('returns client from init', () => {
87+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
88+
const client = initAndBind(TestClient, options);
89+
expect(client).not.toBeUndefined();
90+
});
8591
});
8692
});
8793

packages/deno/src/sdk.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/consistent-type-imports */
12
import type { ServerRuntimeClientOptions } from '@sentry/core';
23
import {
34
dedupeIntegration,
@@ -6,7 +7,7 @@ import {
67
linkedErrorsIntegration,
78
} from '@sentry/core';
89
import { getIntegrationsToSetup, initAndBind } from '@sentry/core';
9-
import type { Integration, Options, StackParser } from '@sentry/types';
10+
import type { Client, Integration, Options, StackParser } from '@sentry/types';
1011
import { createStackParser, nodeStackLineParser, stackParserFromStackParserOptions } from '@sentry/utils';
1112

1213
import { DenoClient } from './client';
@@ -82,7 +83,7 @@ const defaultStackParser: StackParser = createStackParser(nodeStackLineParser())
8283
*
8384
* @see {@link DenoOptions} for documentation on configuration options.
8485
*/
85-
export function init(options: DenoOptions = {}): void {
86+
export function init(options: DenoOptions = {}): Client {
8687
if (options.defaultIntegrations === undefined) {
8788
options.defaultIntegrations = getDefaultIntegrations(options);
8889
}
@@ -94,5 +95,5 @@ export function init(options: DenoOptions = {}): void {
9495
transport: options.transport || makeFetchTransport,
9596
};
9697

97-
initAndBind(DenoClient, clientOptions);
98+
return initAndBind(DenoClient, clientOptions);
9899
}

packages/deno/test/sdk.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { assertNotEquals } from 'https://deno.land/[email protected]/assert/assert_not_equals.ts';
2+
import { init } from '../build/index.mjs';
3+
4+
Deno.test('init() should return client', () => {
5+
assertNotEquals(init({}), undefined);
6+
});

packages/ember/addon/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, app
99
import { GLOBAL_OBJ } from '@sentry/utils';
1010
import Ember from 'ember';
1111

12-
import type { TransactionSource } from '@sentry/types';
12+
import type { Client, TransactionSource } from '@sentry/types';
1313
import type { EmberSentryConfig, GlobalConfig, OwnConfig } from './types';
1414

1515
function _getSentryInitConfig(): EmberSentryConfig['sentry'] {
@@ -21,7 +21,7 @@ function _getSentryInitConfig(): EmberSentryConfig['sentry'] {
2121
/**
2222
* Initialize the Sentry SDK for Ember.
2323
*/
24-
export function init(_runtimeConfig?: BrowserOptions): void {
24+
export function init(_runtimeConfig?: BrowserOptions): Client | undefined {
2525
const environmentConfig = getOwnConfig<OwnConfig>().sentryConfig;
2626

2727
assert('Missing configuration.', environmentConfig);
@@ -42,11 +42,11 @@ export function init(_runtimeConfig?: BrowserOptions): void {
4242
const sentryInitConfig = _getSentryInitConfig();
4343
Object.assign(sentryInitConfig, initConfig);
4444

45-
Sentry.init(initConfig);
45+
const client = Sentry.init(initConfig);
4646

4747
if (macroCondition(isDevelopingApp())) {
4848
if (environmentConfig.ignoreEmberOnErrorWarning) {
49-
return;
49+
return client;
5050
}
5151
next(null, function () {
5252
warn(
@@ -58,6 +58,8 @@ export function init(_runtimeConfig?: BrowserOptions): void {
5858
);
5959
});
6060
}
61+
62+
return client;
6163
}
6264

6365
type RouteConstructor = new (...args: ConstructorParameters<typeof Route>) => Route;

packages/gatsby/src/sdk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { applySdkMetadata } from '@sentry/core';
22
import { init as reactInit } from '@sentry/react';
33

4+
import type { Client } from '@sentry/types';
45
import type { GatsbyOptions } from './utils/types';
56

67
/**
78
* Inits the Sentry Gatsby SDK.
89
*/
9-
export function init(options: GatsbyOptions): void {
10+
export function init(options: GatsbyOptions): Client | undefined {
1011
applySdkMetadata(options, 'gatsby');
11-
reactInit({
12+
return reactInit({
1213
...options,
1314
});
1415
}

packages/google-cloud-serverless/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NodeOptions } from '@sentry/node';
1+
import type { NodeClient, NodeOptions } from '@sentry/node';
22
import { SDK_VERSION, getDefaultIntegrationsWithoutPerformance, init as initNode } from '@sentry/node';
33
import type { Integration, Options, SdkMetadata } from '@sentry/types';
44

@@ -26,7 +26,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
2626
/**
2727
* @see {@link Sentry.init}
2828
*/
29-
export function init(options: NodeOptions = {}): void {
29+
export function init(options: NodeOptions = {}): NodeClient | undefined {
3030
const opts = {
3131
_metadata: {} as SdkMetadata,
3232
defaultIntegrations: getDefaultIntegrations(options),
@@ -44,5 +44,5 @@ export function init(options: NodeOptions = {}): void {
4444
version: SDK_VERSION,
4545
};
4646

47-
initNode(opts);
47+
return initNode(opts);
4848
}

0 commit comments

Comments
 (0)