Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scope): Add client to scope #3768

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class Hub implements HubInterface {
*/
public bindClient(client?: Client): void {
const top = this.getStackTop();
// TODO: Only bind client to scope, don't store in stack
if (top.scope) top.scope.bindClient(client);
top.client = client;
if (client && client.setupIntegrations) {
client.setupIntegrations();
Expand Down
19 changes: 19 additions & 0 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
Breadcrumb,
CaptureContext,
Client,
Context,
Contexts,
Event,
Expand Down Expand Up @@ -75,6 +76,9 @@ export class Scope implements ScopeInterface {
/** Request Mode Session Status */
protected _requestSession?: RequestSession;

/** Client */
protected _client?: Client;

/**
* Inherit values from the parent scope.
* @param scope to clone.
Expand All @@ -94,6 +98,7 @@ export class Scope implements ScopeInterface {
newScope._fingerprint = scope._fingerprint;
newScope._eventProcessors = [...scope._eventProcessors];
newScope._requestSession = scope._requestSession;
newScope._client = scope._client;
}
return newScope;
}
Expand Down Expand Up @@ -398,6 +403,20 @@ export class Scope implements ScopeInterface {
return this;
}

/**
* @inheritdoc
*/
public getClient(): Client | undefined {
return this._client;
}

/**
* @inheritdoc
*/
public bindClient(client?: Client): void {
this._client = client;
}

/**
* Applies the current context and fingerprint to the event.
* Note that breadcrumbs will be added by the client.
Expand Down
8 changes: 8 additions & 0 deletions packages/hub/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe('Hub', () => {
expect(hub.isOlderThan(0)).toBeFalsy();
});

test('binds client to scope', () => {
const testClient: any = { bla: 'a' };
const hub = new Hub(testClient);

expect(hub.getScope()?.getClient()).toEqual(testClient);
expect(hub.getScope()?.getClient()).toEqual(hub.getClient());
});

describe('pushScope', () => {
test('simple', () => {
const localScope = new Scope();
Expand Down
29 changes: 28 additions & 1 deletion packages/hub/test/scope.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event, EventHint, RequestSessionStatus, Severity } from '@sentry/types';
import { Client, Event, EventHint, RequestSessionStatus, Severity } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

import { addGlobalEventProcessor, Scope } from '../src';
Expand Down Expand Up @@ -135,6 +135,22 @@ describe('Scope', () => {
expect((scope as any)._level).toEqual(Severity.Critical);
expect((scope as any)._user).toEqual({ id: '1' });
});

test('getClient with no client', () => {
const scope = new Scope();
const client = scope.getClient();
expect(client).toBeUndefined();
});

test('bindClient', () => {
const scope = new Scope();
const client = {
captureEvent: (_: Event) => undefined,
} as Client;

scope.bindClient(client);
expect(scope.getClient()).toEqual(client);
});
});

describe('clone', () => {
Expand Down Expand Up @@ -185,6 +201,17 @@ describe('Scope', () => {
expect(parentScope.getRequestSession()).toEqual({ status: RequestSessionStatus.Ok });
expect(scope.getRequestSession()).toEqual({ status: RequestSessionStatus.Ok });
});

test('client', () => {
const parentScope = new Scope();
const client = {
captureEvent: (_: Event) => undefined,
} as Client;
parentScope.bindClient(client);

const newScope = Scope.clone(parentScope);
expect(newScope.getClient()).toEqual(client);
});
});

describe('applyToEvent', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Breadcrumb } from './breadcrumb';
import { Client } from './client';
import { Context, Contexts } from './context';
import { EventProcessor } from './eventprocessor';
import { Extra, Extras } from './extra';
Expand Down Expand Up @@ -155,4 +156,14 @@ export interface Scope {
* Clears all currently set Breadcrumbs.
*/
clearBreadcrumbs(): this;

/**
* Returns the client binded to the scope
*/
getClient(): Client | undefined;

/**
* Binds a client to the scope
*/
bindClient(client: Client): void;
}