Skip to content

Commit 59ffba3

Browse files
author
Luca Forstner
authored
feat(core): Add setClient() and getClient() to Scope (#10055)
1 parent 5462b04 commit 59ffba3

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

packages/core/src/hub.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,33 @@ export class Hub implements HubInterface {
124124
*/
125125
public constructor(
126126
client?: Client,
127-
scope: Scope = new Scope(),
128-
isolationScope = new Scope(),
127+
scope?: Scope,
128+
isolationScope?: Scope,
129129
private readonly _version: number = API_VERSION,
130130
) {
131-
this._stack = [{ scope }];
131+
let assignedScope;
132+
if (!scope) {
133+
assignedScope = new Scope();
134+
assignedScope.setClient(client);
135+
} else {
136+
assignedScope = scope;
137+
}
138+
139+
let assignedIsolationScope;
140+
if (!isolationScope) {
141+
assignedIsolationScope = new Scope();
142+
assignedIsolationScope.setClient(client);
143+
} else {
144+
assignedIsolationScope = isolationScope;
145+
}
146+
147+
this._stack = [{ scope: assignedScope }];
148+
132149
if (client) {
133150
this.bindClient(client);
134151
}
135152

136-
this._isolationScope = isolationScope;
153+
this._isolationScope = assignedIsolationScope;
137154
}
138155

139156
/**

packages/core/src/scope.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
Attachment,
44
Breadcrumb,
55
CaptureContext,
6+
Client,
67
Context,
78
Contexts,
89
Event,
@@ -100,6 +101,9 @@ export class Scope implements ScopeInterface {
100101
/** Request Mode Session Status */
101102
protected _requestSession?: RequestSession;
102103

104+
/** The client on this scope */
105+
protected _client?: Client;
106+
103107
// NOTE: Any field which gets added here should get added not only to the constructor but also to the `clone` method.
104108

105109
public constructor() {
@@ -144,10 +148,25 @@ export class Scope implements ScopeInterface {
144148
newScope._attachments = [...this._attachments];
145149
newScope._sdkProcessingMetadata = { ...this._sdkProcessingMetadata };
146150
newScope._propagationContext = { ...this._propagationContext };
151+
newScope._client = this._client;
147152

148153
return newScope;
149154
}
150155

156+
/** Update the client on the scope. */
157+
public setClient(client: Client | undefined): void {
158+
this._client = client;
159+
}
160+
161+
/**
162+
* Get the client assigned to this scope.
163+
*
164+
* It is generally recommended to use the global function `Sentry.getClient()` instead, unless you know what you are doing.
165+
*/
166+
public getClient(): Client | undefined {
167+
return this._client;
168+
}
169+
151170
/**
152171
* Add internal on change listener. Used for sub SDKs that need to store the scope.
153172
* @hidden

packages/core/test/lib/scope.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Attachment, Breadcrumb } from '@sentry/types';
1+
import type { Attachment, Breadcrumb, Client } from '@sentry/types';
22
import { applyScopeDataToEvent } from '../../src';
33
import { Scope, getGlobalScope, setGlobalScope } from '../../src/scope';
44

5-
describe('Unit | Scope', () => {
5+
describe('Scope', () => {
66
beforeEach(() => {
77
setGlobalScope(undefined);
88
});
@@ -187,4 +187,29 @@ describe('Unit | Scope', () => {
187187
});
188188
/* eslint-enable deprecation/deprecation */
189189
});
190+
191+
describe('setClient() and getClient()', () => {
192+
it('allows storing and retrieving client objects', () => {
193+
const fakeClient = {} as Client;
194+
const scope = new Scope();
195+
scope.setClient(fakeClient);
196+
expect(scope.getClient()).toBe(fakeClient);
197+
});
198+
199+
it('defaults to not having a client', () => {
200+
const scope = new Scope();
201+
expect(scope.getClient()).toBeUndefined();
202+
});
203+
});
204+
205+
describe('.clone()', () => {
206+
it('will clone a client on the scope', () => {
207+
const fakeClient = {} as Client;
208+
const scope = new Scope();
209+
scope.setClient(fakeClient);
210+
211+
const clonedScope = scope.clone();
212+
expect(clonedScope.getClient()).toBe(fakeClient);
213+
});
214+
});
190215
});

packages/node-experimental/src/sdk/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
Attachment,
33
Breadcrumb,
4-
Client,
54
Contexts,
65
Event,
76
EventHint,
@@ -36,8 +35,6 @@ export interface Scope extends BaseScope {
3635
isolationScope: typeof this | undefined;
3736
// @ts-expect-error typeof this is what we want here
3837
clone(scope?: Scope): typeof this;
39-
setClient(client: Client): void;
40-
getClient(): Client | undefined;
4138
captureException(exception: unknown, hint?: EventHint): string;
4239
captureMessage(
4340
message: string,

packages/types/src/scope.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Attachment } from './attachment';
22
import type { Breadcrumb } from './breadcrumb';
3+
import type { Client } from './client';
34
import type { Context, Contexts } from './context';
45
import type { EventProcessor } from './eventprocessor';
56
import type { Extra, Extras } from './extra';
@@ -47,6 +48,18 @@ export interface ScopeData {
4748
* Holds additional event information. {@link Scope.applyToEvent} will be called by the client before an event is sent.
4849
*/
4950
export interface Scope {
51+
/**
52+
* Update the client on the scope.
53+
*/
54+
setClient(client: Client | undefined): void;
55+
56+
/**
57+
* Get the client assigned to this scope.
58+
*
59+
* It is generally recommended to use the global function `Sentry.getClient()` instead, unless you know what you are doing.
60+
*/
61+
getClient(): Client | undefined;
62+
5063
/** Add new event processor that will be called after {@link applyToEvent}. */
5164
addEventProcessor(callback: EventProcessor): this;
5265

0 commit comments

Comments
 (0)