-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathprepareEvent.ts
57 lines (49 loc) · 1.84 KB
/
prepareEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Scope } from '@sentry/core';
import type { CaptureContext, EventHint, Scope as ScopeInterface, ScopeContext } from '@sentry/core';
/**
* This type makes sure that we get either a CaptureContext, OR an EventHint.
* It does not allow mixing them, which could lead to unexpected outcomes, e.g. this is disallowed:
* { user: { id: '123' }, mechanism: { handled: false } }
*/
export type ExclusiveEventHintOrCaptureContext =
| (CaptureContext & Partial<{ [key in keyof EventHint]: never }>)
| (EventHint & Partial<{ [key in keyof ScopeContext]: never }>);
/**
* Parse either an `EventHint` directly, or convert a `CaptureContext` to an `EventHint`.
* This is used to allow to update method signatures that used to accept a `CaptureContext` but should now accept an `EventHint`.
*/
export function parseEventHintOrCaptureContext(
hint: ExclusiveEventHintOrCaptureContext | undefined,
): EventHint | undefined {
if (!hint) {
return undefined;
}
// If you pass a Scope or `() => Scope` as CaptureContext, we just return this as captureContext
if (hintIsScopeOrFunction(hint)) {
return { captureContext: hint };
}
if (hintIsScopeContext(hint)) {
return {
captureContext: hint,
};
}
return hint;
}
function hintIsScopeOrFunction(
hint: CaptureContext | EventHint,
): hint is ScopeInterface | ((scope: ScopeInterface) => ScopeInterface) {
return hint instanceof Scope || typeof hint === 'function';
}
type ScopeContextProperty = keyof ScopeContext;
const captureContextKeys: readonly ScopeContextProperty[] = [
'user',
'level',
'extra',
'contexts',
'tags',
'fingerprint',
'propagationContext',
] as const;
function hintIsScopeContext(hint: Partial<ScopeContext> | EventHint): hint is Partial<ScopeContext> {
return Object.keys(hint).some(key => captureContextKeys.includes(key as ScopeContextProperty));
}