1- import { getCurrentHub , Hub , Scope } from '@sentry/hub' ;
21import {
32 Breadcrumb ,
43 CaptureContext ,
@@ -9,25 +8,19 @@ import {
98 Primitive ,
109 Severity ,
1110 SeverityLevel ,
12- Transaction ,
1311 TransactionContext ,
1412 User ,
1513} from '@sentry/types' ;
1614
17- /**
18- * This calls a function on the current hub.
19- * @param method function to call on hub.
20- * @param args to pass to function.
21- */
22- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23- function callOnHub < T > ( method : string , ...args : any [ ] ) : T {
24- const hub = getCurrentHub ( ) ;
25- if ( hub && hub [ method as keyof Hub ] ) {
26- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27- return ( hub [ method as keyof Hub ] as any ) ( ...args ) ;
28- }
29- throw new Error ( `No hub defined or ${ method } was not found on the hub, please open a bug report.` ) ;
30- }
15+ import { getCurrentHub , Hub } from './hub' ;
16+ import { Scope } from './scope' ;
17+
18+ // Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`,
19+ // where HUB_FUNCTION is some method on the Hub class.
20+ //
21+ // This is done to make sure the top level SDK methods stay in sync with the hub methods.
22+ // Although every method here has an explicit return type, some of them (that map to void returns) do not
23+ // contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable.
3124
3225/**
3326 * Captures an exception event and sends it to Sentry.
@@ -36,14 +29,8 @@ function callOnHub<T>(method: string, ...args: any[]): T {
3629 * @returns The generated eventId.
3730 */
3831// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
39- export function captureException ( exception : any , captureContext ?: CaptureContext ) : string {
40- const syntheticException = new Error ( 'Sentry syntheticException' ) ;
41-
42- return callOnHub ( 'captureException' , exception , {
43- captureContext,
44- originalException : exception ,
45- syntheticException,
46- } ) ;
32+ export function captureException ( exception : any , captureContext ?: CaptureContext ) : ReturnType < Hub [ 'captureException' ] > {
33+ return getCurrentHub ( ) . captureException ( exception , { captureContext } ) ;
4734}
4835
4936/**
@@ -57,19 +44,12 @@ export function captureMessage(
5744 message : string ,
5845 // eslint-disable-next-line deprecation/deprecation
5946 captureContext ?: CaptureContext | Severity | SeverityLevel ,
60- ) : string {
61- const syntheticException = new Error ( message ) ;
62-
47+ ) : ReturnType < Hub [ 'captureMessage' ] > {
6348 // This is necessary to provide explicit scopes upgrade, without changing the original
6449 // arity of the `captureMessage(message, level)` method.
6550 const level = typeof captureContext === 'string' ? captureContext : undefined ;
6651 const context = typeof captureContext !== 'string' ? { captureContext } : undefined ;
67-
68- return callOnHub ( 'captureMessage' , message , level , {
69- originalException : message ,
70- syntheticException,
71- ...context ,
72- } ) ;
52+ return getCurrentHub ( ) . captureMessage ( message , level , context ) ;
7353}
7454
7555/**
@@ -78,16 +58,16 @@ export function captureMessage(
7858 * @param event The event to send to Sentry.
7959 * @returns The generated eventId.
8060 */
81- export function captureEvent ( event : Event ) : string {
82- return callOnHub ( ' captureEvent' , event ) ;
61+ export function captureEvent ( event : Event ) : ReturnType < Hub [ 'captureEvent' ] > {
62+ return getCurrentHub ( ) . captureEvent ( event ) ;
8363}
8464
8565/**
8666 * Callback to set context information onto the scope.
8767 * @param callback Callback function that receives Scope.
8868 */
89- export function configureScope ( callback : ( scope : Scope ) => void ) : void {
90- callOnHub < void > ( ' configureScope' , callback ) ;
69+ export function configureScope ( callback : ( scope : Scope ) => void ) : ReturnType < Hub [ 'configureScope' ] > {
70+ getCurrentHub ( ) . configureScope ( callback ) ;
9171}
9272
9373/**
@@ -98,8 +78,8 @@ export function configureScope(callback: (scope: Scope) => void): void {
9878 *
9979 * @param breadcrumb The breadcrumb to record.
10080 */
101- export function addBreadcrumb ( breadcrumb : Breadcrumb ) : void {
102- callOnHub < void > ( ' addBreadcrumb' , breadcrumb ) ;
81+ export function addBreadcrumb ( breadcrumb : Breadcrumb ) : ReturnType < Hub [ 'addBreadcrumb' ] > {
82+ getCurrentHub ( ) . addBreadcrumb ( breadcrumb ) ;
10383}
10484
10585/**
@@ -108,33 +88,33 @@ export function addBreadcrumb(breadcrumb: Breadcrumb): void {
10888 * @param context Any kind of data. This data will be normalized.
10989 */
11090// eslint-disable-next-line @typescript-eslint/no-explicit-any
111- export function setContext ( name : string , context : { [ key : string ] : any } | null ) : void {
112- callOnHub < void > ( ' setContext' , name , context ) ;
91+ export function setContext ( name : string , context : { [ key : string ] : any } | null ) : ReturnType < Hub [ 'setContext' ] > {
92+ getCurrentHub ( ) . setContext ( name , context ) ;
11393}
11494
11595/**
11696 * Set an object that will be merged sent as extra data with the event.
11797 * @param extras Extras object to merge into current context.
11898 */
119- export function setExtras ( extras : Extras ) : void {
120- callOnHub < void > ( ' setExtras' , extras ) ;
99+ export function setExtras ( extras : Extras ) : ReturnType < Hub [ 'setExtras' ] > {
100+ getCurrentHub ( ) . setExtras ( extras ) ;
121101}
122102
123103/**
124- * Set an object that will be merged sent as tags data with the event.
125- * @param tags Tags context object to merge into current context.
104+ * Set key:value that will be sent as extra data with the event.
105+ * @param key String of extra
106+ * @param extra Any kind of data. This data will be normalized.
126107 */
127- export function setTags ( tags : { [ key : string ] : Primitive } ) : void {
128- callOnHub < void > ( 'setTags' , tags ) ;
108+ export function setExtra ( key : string , extra : Extra ) : ReturnType < Hub [ 'setExtra' ] > {
109+ getCurrentHub ( ) . setExtra ( key , extra ) ;
129110}
130111
131112/**
132- * Set key:value that will be sent as extra data with the event.
133- * @param key String of extra
134- * @param extra Any kind of data. This data will be normalized.
113+ * Set an object that will be merged sent as tags data with the event.
114+ * @param tags Tags context object to merge into current context.
135115 */
136- export function setExtra ( key : string , extra : Extra ) : void {
137- callOnHub < void > ( 'setExtra' , key , extra ) ;
116+ export function setTags ( tags : { [ key : string ] : Primitive } ) : ReturnType < Hub [ 'setTags' ] > {
117+ getCurrentHub ( ) . setTags ( tags ) ;
138118}
139119
140120/**
@@ -145,17 +125,17 @@ export function setExtra(key: string, extra: Extra): void {
145125 * @param key String key of tag
146126 * @param value Value of tag
147127 */
148- export function setTag ( key : string , value : Primitive ) : void {
149- callOnHub < void > ( ' setTag' , key , value ) ;
128+ export function setTag ( key : string , value : Primitive ) : ReturnType < Hub [ 'setTag' ] > {
129+ getCurrentHub ( ) . setTag ( key , value ) ;
150130}
151131
152132/**
153133 * Updates user context information for future events.
154134 *
155135 * @param user User context object to be set in the current context. Pass `null` to unset the user.
156136 */
157- export function setUser ( user : User | null ) : void {
158- callOnHub < void > ( ' setUser' , user ) ;
137+ export function setUser ( user : User | null ) : ReturnType < Hub [ 'setUser' ] > {
138+ getCurrentHub ( ) . setUser ( user ) ;
159139}
160140
161141/**
@@ -171,23 +151,8 @@ export function setUser(user: User | null): void {
171151 *
172152 * @param callback that will be enclosed into push/popScope.
173153 */
174- export function withScope ( callback : ( scope : Scope ) => void ) : void {
175- callOnHub < void > ( 'withScope' , callback ) ;
176- }
177-
178- /**
179- * Calls a function on the latest client. Use this with caution, it's meant as
180- * in "internal" helper so we don't need to expose every possible function in
181- * the shim. It is not guaranteed that the client actually implements the
182- * function.
183- *
184- * @param method The method to call on the client/client.
185- * @param args Arguments to pass to the client/fontend.
186- * @hidden
187- */
188- // eslint-disable-next-line @typescript-eslint/no-explicit-any
189- export function _callOnClient ( method : string , ...args : any [ ] ) : void {
190- callOnHub < void > ( '_invokeClient' , method , ...args ) ;
154+ export function withScope ( callback : ( scope : Scope ) => void ) : ReturnType < Hub [ 'withScope' ] > {
155+ getCurrentHub ( ) . withScope ( callback ) ;
191156}
192157
193158/**
@@ -210,6 +175,6 @@ export function _callOnClient(method: string, ...args: any[]): void {
210175export function startTransaction (
211176 context : TransactionContext ,
212177 customSamplingContext ?: CustomSamplingContext ,
213- ) : Transaction {
214- return callOnHub ( ' startTransaction' , { ...context } , customSamplingContext ) ;
178+ ) : ReturnType < Hub [ 'startTransaction' ] > {
179+ return getCurrentHub ( ) . startTransaction ( { ...context } , customSamplingContext ) ;
215180}
0 commit comments