Skip to content

Commit bb40f48

Browse files
authored
fix: Use correct types for event context data and allow for context removal (#2910)
1 parent 1a17399 commit bb40f48

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

packages/hub/src/scope.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import {
33
Breadcrumb,
44
CaptureContext,
5+
Context,
6+
Contexts,
57
Event,
68
EventHint,
79
EventProcessor,
@@ -40,12 +42,10 @@ export class Scope implements ScopeInterface {
4042
protected _tags: { [key: string]: string } = {};
4143

4244
/** Extra */
43-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44-
protected _extra: { [key: string]: any } = {};
45+
protected _extra: Extras = {};
4546

4647
/** Contexts */
47-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48-
protected _contexts: { [key: string]: any } = {};
48+
protected _contexts: Contexts = {};
4949

5050
/** Fingerprint */
5151
protected _fingerprint?: string[];
@@ -185,9 +185,14 @@ export class Scope implements ScopeInterface {
185185
/**
186186
* @inheritDoc
187187
*/
188-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
189-
public setContext(key: string, context: { [key: string]: any } | null): this {
190-
this._contexts = { ...this._contexts, [key]: context };
188+
public setContext(key: string, context: Context | null): this {
189+
if (context === null) {
190+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
191+
delete this._contexts[key];
192+
} else {
193+
this._contexts = { ...this._contexts, [key]: context };
194+
}
195+
191196
this._notifyScopeListeners();
192197
return this;
193198
}

packages/types/src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type Context = Record<string, unknown>;
2+
export type Contexts = Record<string, Context>;

packages/types/src/event.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Breadcrumb } from './breadcrumb';
2+
import { Contexts } from './context';
23
import { Exception } from './exception';
4+
import { Extras } from './extra';
35
import { Request } from './request';
46
import { CaptureContext } from './scope';
57
import { SdkInfo } from './sdkinfo';
@@ -31,11 +33,9 @@ export interface Event {
3133
};
3234
stacktrace?: Stacktrace;
3335
breadcrumbs?: Breadcrumb[];
34-
contexts?: {
35-
[key: string]: Record<any, any>;
36-
};
36+
contexts?: Contexts;
3737
tags?: { [key: string]: string };
38-
extra?: { [key: string]: any };
38+
extra?: Extras;
3939
user?: User;
4040
type?: EventType;
4141
spans?: Span[];

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { Breadcrumb, BreadcrumbHint } from './breadcrumb';
22
export { Client } from './client';
3+
export { Context, Contexts } from './context';
34
export { Dsn, DsnComponents, DsnLike, DsnProtocol } from './dsn';
45
export { ExtendedError } from './error';
56
export { Event, EventHint } from './event';

packages/types/src/scope.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Breadcrumb } from './breadcrumb';
2+
import { Context, Contexts } from './context';
23
import { EventProcessor } from './eventprocessor';
34
import { Extra, Extras } from './extra';
45
import { Severity } from './severity';
@@ -13,8 +14,8 @@ export type CaptureContext = Scope | Partial<ScopeContext> | ((scope: Scope) =>
1314
export interface ScopeContext {
1415
user: User;
1516
level: Severity;
16-
extra: { [key: string]: any };
17-
contexts: { [key: string]: any };
17+
extra: Extras;
18+
contexts: Contexts;
1819
tags: { [key: string]: string };
1920
fingerprint: string[];
2021
}
@@ -80,9 +81,9 @@ export interface Scope {
8081
/**
8182
* Sets context data with the given name.
8283
* @param name of the context
83-
* @param context Any kind of data. This data will be normailzed.
84+
* @param context an object containing context data. This data will be normailzed. Pass `null` to unset the context.
8485
*/
85-
setContext(name: string, context: { [key: string]: any } | null): this;
86+
setContext(name: string, context: Context | null): this;
8687

8788
/**
8889
* Sets the Span on the scope.

0 commit comments

Comments
 (0)