Skip to content

Commit 7a3f8fc

Browse files
committed
feat: Expose prio scope functions
1 parent aead4d1 commit 7a3f8fc

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

packages/browser/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ export {
2020
captureEvent,
2121
captureMessage,
2222
configureScope,
23-
withScope,
2423
getHubFromCarrier,
2524
getCurrentHub,
2625
Hub,
2726
Scope,
27+
setContext,
28+
setExtras,
29+
setTags,
30+
setUser,
31+
withScope,
2832
} from '@sentry/core';
2933

3034
export { BrowserOptions } from './backend';

packages/core/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export {
44
captureEvent,
55
captureMessage,
66
configureScope,
7+
setContext,
8+
setExtras,
9+
setTags,
10+
setUser,
711
withScope,
812
} from '@sentry/minimal';
913
export { addGlobalEventProcessor, getCurrentHub, Hub, getHubFromCarrier, Scope } from '@sentry/hub';

packages/hub/src/hub.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Integration,
99
IntegrationClass,
1010
Severity,
11+
User,
1112
} from '@sentry/types';
1213
import { consoleSandbox, dynamicRequire, getGlobalObject, logger, uuid4 } from '@sentry/utils';
1314

@@ -225,13 +226,56 @@ export class Hub implements HubInterface {
225226
top.scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS));
226227
}
227228

229+
/**
230+
* @inheritDoc
231+
*/
232+
public setUser(user: User | null): void {
233+
const top = this.getStackTop();
234+
if (!top.scope) {
235+
return;
236+
}
237+
top.scope.setUser(user);
238+
}
239+
240+
/**
241+
* @inheritDoc
242+
*/
243+
public setTags(tags: { [key: string]: string }): void {
244+
const top = this.getStackTop();
245+
if (!top.scope) {
246+
return;
247+
}
248+
top.scope.setTags(tags);
249+
}
250+
251+
/**
252+
* @inheritDoc
253+
*/
254+
public setExtras(extras: { [key: string]: any }): void {
255+
const top = this.getStackTop();
256+
if (!top.scope) {
257+
return;
258+
}
259+
top.scope.setExtras(extras);
260+
}
261+
262+
/**
263+
* @inheritDoc
264+
*/
265+
public setContext(name: string, context: { [key: string]: any } | null): void {
266+
const top = this.getStackTop();
267+
if (!top.scope) {
268+
return;
269+
}
270+
top.scope.setContext(name, context);
271+
}
272+
228273
/**
229274
* @inheritDoc
230275
*/
231276
public configureScope(callback: (scope: Scope) => void): void {
232277
const top = this.getStackTop();
233278
if (top.scope && top.client) {
234-
// TODO: freeze flag
235279
callback(top.scope);
236280
}
237281
}

packages/minimal/src/index.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub, Hub, Scope } from '@sentry/hub';
2-
import { Breadcrumb, Event, Severity } from '@sentry/types';
2+
import { Breadcrumb, Event, Severity, User } from '@sentry/types';
33

44
/**
55
* This calls a function on the current hub.
@@ -64,6 +64,14 @@ export function captureEvent(event: Event): string {
6464
return callOnHub('captureEvent', event);
6565
}
6666

67+
/**
68+
* Callback to set context information onto the scope.
69+
* @param callback Callback function that receives Scope.
70+
*/
71+
export function configureScope(callback: (scope: Scope) => void): void {
72+
callOnHub<void>('configureScope', callback);
73+
}
74+
6775
/**
6876
* Records a new breadcrumb which will be attached to future events.
6977
*
@@ -77,11 +85,37 @@ export function addBreadcrumb(breadcrumb: Breadcrumb): void {
7785
}
7886

7987
/**
80-
* Callback to set context information onto the scope.
81-
* @param callback Callback function that receives Scope.
88+
* Sets context data with the given name.
89+
* @param name of the context
90+
* @param context Any kind of data. This data will be normailzed.
8291
*/
83-
export function configureScope(callback: (scope: Scope) => void): void {
84-
callOnHub<void>('configureScope', callback);
92+
export function setContext(name: string, context: { [key: string]: any } | null): void {
93+
callOnHub<void>('setContext', name, context);
94+
}
95+
96+
/**
97+
* Set an object that will be merged sent as extra data with the event.
98+
* @param extras Extras object to merge into current context.
99+
*/
100+
export function setExtras(extras: { [key: string]: any }): void {
101+
callOnHub<void>('setExtras', extras);
102+
}
103+
104+
/**
105+
* Updates user context information for future events.
106+
*
107+
* @param user User context object to be set in the current context. Pass `null` to unset the user.
108+
*/
109+
export function setUser(user: User | null): void {
110+
callOnHub<void>('setUser', user);
111+
}
112+
113+
/**
114+
* Set an object that will be merged sent as tags data with the event.
115+
* @param tags Tags context object to merge into current context.
116+
*/
117+
export function setTags(tags: { [key: string]: string }): void {
118+
callOnHub<void>('setTags', tags);
85119
}
86120

87121
/**

packages/node/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export {
2424
getHubFromCarrier,
2525
Hub,
2626
Scope,
27+
setContext,
28+
setExtras,
29+
setTags,
30+
setUser,
2731
withScope,
2832
} from '@sentry/core';
2933

packages/types/src/hub.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Event, EventHint } from './event';
44
import { Integration, IntegrationClass } from './integration';
55
import { Scope } from './scope';
66
import { Severity } from './severity';
7+
import { User } from './user';
78

89
/**
910
* Internal class used to make sure we always have the latest internal functions
@@ -109,6 +110,33 @@ export interface Hub {
109110
* @param hint May contain additional information about the original breadcrumb.
110111
*/
111112
addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void;
113+
114+
/**
115+
* Updates user context information for future events.
116+
*
117+
* @param user User context object to be set in the current context. Pass `null` to unset the user.
118+
*/
119+
setUser(user: User | null): void;
120+
121+
/**
122+
* Set an object that will be merged sent as tags data with the event.
123+
* @param tags Tags context object to merge into current context.
124+
*/
125+
setTags(tags: { [key: string]: string }): void;
126+
127+
/**
128+
* Set an object that will be merged sent as extra data with the event.
129+
* @param extras Extras object to merge into current context.
130+
*/
131+
setExtras(extras: { [key: string]: any }): void;
132+
133+
/**
134+
* Sets context data with the given name.
135+
* @param name of the context
136+
* @param context Any kind of data. This data will be normailzed.
137+
*/
138+
setContext(name: string, context: { [key: string]: any } | null): void;
139+
112140
/**
113141
* Callback to set context information onto the scope.
114142
*

0 commit comments

Comments
 (0)