|
| 1 | +import type { Client, EventHint, Hub, Integration, IntegrationClass, Scope, SeverityLevel } from '@sentry/types'; |
| 2 | + |
| 3 | +import { |
| 4 | + addBreadcrumb, |
| 5 | + captureEvent, |
| 6 | + endSession, |
| 7 | + getCurrentScope, |
| 8 | + getIsolationScope, |
| 9 | + setContext, |
| 10 | + setExtra, |
| 11 | + setExtras, |
| 12 | + setTag, |
| 13 | + setTags, |
| 14 | + setUser, |
| 15 | + startSession, |
| 16 | + withScope, |
| 17 | +} from '@sentry/core'; |
| 18 | +import { getClient } from './api'; |
| 19 | + |
| 20 | +/** |
| 21 | + * This is for legacy reasons, and returns a proxy object instead of a hub to be used. |
| 22 | + * @deprecated Use the methods directly. |
| 23 | + */ |
| 24 | +export function getCurrentHub(): Hub { |
| 25 | + return { |
| 26 | + isOlderThan(_version: number): boolean { |
| 27 | + return false; |
| 28 | + }, |
| 29 | + |
| 30 | + bindClient(client: Client): void { |
| 31 | + const scope = getCurrentScope(); |
| 32 | + scope.setClient(client); |
| 33 | + }, |
| 34 | + |
| 35 | + pushScope(): Scope { |
| 36 | + // TODO: This does not work and is actually deprecated |
| 37 | + return getCurrentScope(); |
| 38 | + }, |
| 39 | + |
| 40 | + popScope(): boolean { |
| 41 | + // TODO: This does not work and is actually deprecated |
| 42 | + return false; |
| 43 | + }, |
| 44 | + |
| 45 | + withScope, |
| 46 | + getClient: <C extends Client>() => getClient() as C | undefined, |
| 47 | + getScope: getCurrentScope, |
| 48 | + getIsolationScope, |
| 49 | + captureException: (exception: unknown, hint?: EventHint) => { |
| 50 | + return getCurrentScope().captureException(exception, hint); |
| 51 | + }, |
| 52 | + captureMessage: (message: string, level?: SeverityLevel, hint?: EventHint) => { |
| 53 | + return getCurrentScope().captureMessage(message, level, hint); |
| 54 | + }, |
| 55 | + captureEvent, |
| 56 | + addBreadcrumb, |
| 57 | + setUser, |
| 58 | + setTags, |
| 59 | + setTag, |
| 60 | + setExtra, |
| 61 | + setExtras, |
| 62 | + setContext, |
| 63 | + |
| 64 | + getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null { |
| 65 | + return getClient().getIntegrationByName<T>(integration.id) || null; |
| 66 | + }, |
| 67 | + |
| 68 | + startSession, |
| 69 | + |
| 70 | + endSession, |
| 71 | + |
| 72 | + captureSession(endSession?: boolean): void { |
| 73 | + // both send the update and pull the session from the scope |
| 74 | + if (endSession) { |
| 75 | + // eslint-disable-next-line deprecation/deprecation |
| 76 | + return this.endSession(); |
| 77 | + } |
| 78 | + |
| 79 | + // only send the update |
| 80 | + _sendSessionUpdate(); |
| 81 | + }, |
| 82 | + |
| 83 | + shouldSendDefaultPii(): boolean { |
| 84 | + const client = getClient(); |
| 85 | + const options = client.getOptions(); |
| 86 | + return Boolean(options.sendDefaultPii); |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Sends the current Session on the scope |
| 93 | + */ |
| 94 | +function _sendSessionUpdate(): void { |
| 95 | + const scope = getCurrentScope(); |
| 96 | + const client = getClient(); |
| 97 | + |
| 98 | + const session = scope.getSession(); |
| 99 | + if (session) { |
| 100 | + client.captureSession(session); |
| 101 | + } |
| 102 | +} |
0 commit comments