1212
1313/* eslint-disable @typescript-eslint/no-explicit-any */
1414
15- import type { Client , MetricsAggregator , Scope } from '../types-hoist' ;
16-
15+ import type { Carrier , SentryCarrier , VersionedCarrier } from '../carrier' ;
1716import type { SdkSource } from './env' ;
18- import type { logger } from './logger' ;
1917import { SDK_VERSION } from './version' ;
2018
21- interface SentryCarrier {
22- acs ?: any ;
23- stack ?: any ;
24-
25- globalScope ?: Scope ;
26- defaultIsolationScope ?: Scope ;
27- defaultCurrentScope ?: Scope ;
28- globalMetricsAggregators ?: WeakMap < Client , MetricsAggregator > | undefined ;
29- logger ?: typeof logger ;
30-
31- /** Overwrites TextEncoder used in `@sentry/core`, need for `[email protected] ` and older */ 32- encodePolyfill ?: ( input : string ) => Uint8Array ;
33- /** Overwrites TextDecoder used in `@sentry/core`, need for `[email protected] ` and older */ 34- decodePolyfill ?: ( input : Uint8Array ) => string ;
35- }
36-
3719/** Internal global with common properties and Sentry extensions */
3820export type InternalGlobal = {
3921 navigator ?: { userAgent ?: string } ;
@@ -60,17 +42,14 @@ export type InternalGlobal = {
6042 * file.
6143 */
6244 _sentryDebugIds ?: Record < string , string > ;
63- __SENTRY__ : Record < Exclude < string , 'version' > , SentryCarrier > & {
64- version ?: string ;
65- } ;
6645 /**
6746 * Raw module metadata that is injected by bundler plugins.
6847 *
6948 * Keys are `error.stack` strings, values are the metadata.
7049 */
7150 _sentryModuleMetadata ?: Record < string , any > ;
7251 _sentryEsmLoaderHookRegistered ?: boolean ;
73- } ;
52+ } & Carrier ;
7453
7554/** Get's the global object for the current JavaScript runtime */
7655export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal ;
@@ -86,15 +65,20 @@ export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal;
8665 * @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value
8766 * @returns the singleton
8867 */
89- export function getGlobalSingleton < T > ( name : keyof SentryCarrier , creator : ( ) => T , obj = GLOBAL_OBJ ) : T {
68+ export function getGlobalSingleton < Prop extends keyof SentryCarrier > (
69+ name : Prop ,
70+ creator : ( ) => NonNullable < SentryCarrier [ Prop ] > ,
71+ obj = GLOBAL_OBJ ,
72+ ) : NonNullable < SentryCarrier [ Prop ] > {
9073 const __SENTRY__ = getSentryCarrierObj ( obj ) ;
9174 const versionedCarrier = ( __SENTRY__ [ SDK_VERSION ] = __SENTRY__ [ SDK_VERSION ] || { } ) ;
75+
9276 return versionedCarrier [ name ] || ( versionedCarrier [ name ] = creator ( ) ) ;
9377}
9478
9579function getSentryCarrierObj (
9680 obj : Omit < InternalGlobal , '__SENTRY__' > & Partial < Pick < InternalGlobal , '__SENTRY__' > > ,
97- ) : InternalGlobal [ '__SENTRY__' ] {
81+ ) : VersionedCarrier {
9882 // Set the Sentry carrier, if it does not exist yet
9983 return obj . __SENTRY__ || ( obj . __SENTRY__ = { } ) ;
10084}
0 commit comments