diff --git a/packages/hub/src/scope.ts b/packages/hub/src/scope.ts index 82d470a265db..ee378373b3e2 100644 --- a/packages/hub/src/scope.ts +++ b/packages/hub/src/scope.ts @@ -2,6 +2,8 @@ import { Breadcrumb, CaptureContext, + Context, + Contexts, Event, EventHint, EventProcessor, @@ -40,12 +42,10 @@ export class Scope implements ScopeInterface { protected _tags: { [key: string]: string } = {}; /** Extra */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - protected _extra: { [key: string]: any } = {}; + protected _extra: Extras = {}; /** Contexts */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - protected _contexts: { [key: string]: any } = {}; + protected _contexts: Contexts = {}; /** Fingerprint */ protected _fingerprint?: string[]; @@ -185,9 +185,14 @@ export class Scope implements ScopeInterface { /** * @inheritDoc */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - public setContext(key: string, context: { [key: string]: any } | null): this { - this._contexts = { ...this._contexts, [key]: context }; + public setContext(key: string, context: Context | null): this { + if (context === null) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this._contexts[key]; + } else { + this._contexts = { ...this._contexts, [key]: context }; + } + this._notifyScopeListeners(); return this; } diff --git a/packages/types/src/context.ts b/packages/types/src/context.ts new file mode 100644 index 000000000000..6fbbeb00b6e6 --- /dev/null +++ b/packages/types/src/context.ts @@ -0,0 +1,2 @@ +export type Context = Record; +export type Contexts = Record; diff --git a/packages/types/src/event.ts b/packages/types/src/event.ts index fc1b4205611b..38e7a6ad5a21 100644 --- a/packages/types/src/event.ts +++ b/packages/types/src/event.ts @@ -1,5 +1,7 @@ import { Breadcrumb } from './breadcrumb'; +import { Contexts } from './context'; import { Exception } from './exception'; +import { Extras } from './extra'; import { Request } from './request'; import { CaptureContext } from './scope'; import { SdkInfo } from './sdkinfo'; @@ -31,11 +33,9 @@ export interface Event { }; stacktrace?: Stacktrace; breadcrumbs?: Breadcrumb[]; - contexts?: { - [key: string]: Record; - }; + contexts?: Contexts; tags?: { [key: string]: string }; - extra?: { [key: string]: any }; + extra?: Extras; user?: User; type?: EventType; spans?: Span[]; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index da4fa2f6129f..98a429e9e455 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -1,5 +1,6 @@ export { Breadcrumb, BreadcrumbHint } from './breadcrumb'; export { Client } from './client'; +export { Context, Contexts } from './context'; export { Dsn, DsnComponents, DsnLike, DsnProtocol } from './dsn'; export { ExtendedError } from './error'; export { Event, EventHint } from './event'; diff --git a/packages/types/src/scope.ts b/packages/types/src/scope.ts index 973d45fd7b4e..630d980c8eb7 100644 --- a/packages/types/src/scope.ts +++ b/packages/types/src/scope.ts @@ -1,4 +1,5 @@ import { Breadcrumb } from './breadcrumb'; +import { Context, Contexts } from './context'; import { EventProcessor } from './eventprocessor'; import { Extra, Extras } from './extra'; import { Severity } from './severity'; @@ -13,8 +14,8 @@ export type CaptureContext = Scope | Partial | ((scope: Scope) => export interface ScopeContext { user: User; level: Severity; - extra: { [key: string]: any }; - contexts: { [key: string]: any }; + extra: Extras; + contexts: Contexts; tags: { [key: string]: string }; fingerprint: string[]; } @@ -80,9 +81,9 @@ export interface Scope { /** * Sets context data with the given name. * @param name of the context - * @param context Any kind of data. This data will be normailzed. + * @param context an object containing context data. This data will be normailzed. Pass `null` to unset the context. */ - setContext(name: string, context: { [key: string]: any } | null): this; + setContext(name: string, context: Context | null): this; /** * Sets the Span on the scope.