Skip to content

Commit

Permalink
fix: Use correct types for event context data and allow for context r…
Browse files Browse the repository at this point in the history
…emoval (#2910)
  • Loading branch information
kamilogorek authored Oct 1, 2020
1 parent 1a17399 commit bb40f48
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
19 changes: 12 additions & 7 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import {
Breadcrumb,
CaptureContext,
Context,
Contexts,
Event,
EventHint,
EventProcessor,
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Context = Record<string, unknown>;
export type Contexts = Record<string, Context>;
8 changes: 4 additions & 4 deletions packages/types/src/event.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -31,11 +33,9 @@ export interface Event {
};
stacktrace?: Stacktrace;
breadcrumbs?: Breadcrumb[];
contexts?: {
[key: string]: Record<any, any>;
};
contexts?: Contexts;
tags?: { [key: string]: string };
extra?: { [key: string]: any };
extra?: Extras;
user?: User;
type?: EventType;
spans?: Span[];
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
9 changes: 5 additions & 4 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 { Context, Contexts } from './context';
import { EventProcessor } from './eventprocessor';
import { Extra, Extras } from './extra';
import { Severity } from './severity';
Expand All @@ -13,8 +14,8 @@ export type CaptureContext = Scope | Partial<ScopeContext> | ((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[];
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit bb40f48

Please sign in to comment.