@@ -3,10 +3,26 @@ import { Dsn, urlEncode } from '@sentry/utils';
33
44const  SENTRY_API_VERSION  =  '7' ; 
55
6+ /** 
7+  * Stores details about a Sentry SDK 
8+  */ 
9+ export  interface  APIDetails  { 
10+   /** The DSN as passed to Sentry.init() */ 
11+   initDsn : DsnLike ; 
12+   /** Metadata about the SDK (name, version, etc) for inclusion in envelope headers */ 
13+   metadata : SdkMetadata ; 
14+   /** The internally used Dsn object. */ 
15+   readonly  dsn : Dsn ; 
16+   /** The envelope tunnel to use. */ 
17+   readonly  tunnel ?: string ; 
18+ } 
19+ 
620/** 
721 * Helper class to provide urls, headers and metadata that can be used to form 
822 * different types of requests to Sentry endpoints. 
923 * Supports both envelopes and regular event requests. 
24+  * 
25+  * @deprecated  Please use APIDetails 
1026 **/ 
1127export  class  API  { 
1228  /** The DSN as passed to Sentry.init() */ 
@@ -41,13 +57,12 @@ export class API {
4157
4258  /** Returns the prefix to construct Sentry ingestion API endpoints. */ 
4359  public  getBaseApiEndpoint ( ) : string  { 
44-     const  dsn  =  this . getDsn ( ) ; 
45-     return  getBaseApiEndpoint ( dsn ) ; 
60+     return  getBaseApiEndpoint ( this . _dsnObject ) ; 
4661  } 
4762
4863  /** Returns the store endpoint URL. */ 
4964  public  getStoreEndpoint ( ) : string  { 
50-     return  this . _getIngestEndpoint ( 'store' ) ; 
65+     return  getStoreEndpoint ( this . _dsnObject ) ; 
5166  } 
5267
5368  /** 
@@ -56,7 +71,7 @@ export class API {
5671   * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. 
5772   */ 
5873  public  getStoreEndpointWithUrlEncodedAuth ( ) : string  { 
59-     return  ` ${ this . getStoreEndpoint ( ) } ? ${ this . _encodedAuth ( ) } ` ; 
74+     return  getStoreEndpointWithUrlEncodedAuth ( this . _dsnObject ) ; 
6075  } 
6176
6277  /** 
@@ -65,64 +80,18 @@ export class API {
6580   * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. 
6681   */ 
6782  public  getEnvelopeEndpointWithUrlEncodedAuth ( ) : string  { 
68-     if  ( this . forceEnvelope ( ) )  { 
69-       return  this . _tunnel  as  string ; 
70-     } 
71- 
72-     return  `${ this . _getEnvelopeEndpoint ( ) } ${ this . _encodedAuth ( ) }  ; 
73-   } 
74- 
75-   /** Returns only the path component for the store endpoint. */ 
76-   public  getStoreEndpointPath ( ) : string  { 
77-     const  dsn  =  this . getDsn ( ) ; 
78-     return  `${ dsn . path  ? `/${ dsn . path }   : '' } ${ dsn . projectId }  ; 
79-   } 
80- 
81-   /** 
82-    * Returns an object that can be used in request headers. 
83-    * This is needed for node and the old /store endpoint in sentry 
84-    */ 
85-   public  getRequestHeaders ( clientName : string ,  clientVersion : string ) : {  [ key : string ] : string  }  { 
86-     // CHANGE THIS to use metadata but keep clientName and clientVersion compatible 
87-     const  dsn  =  this . getDsn ( ) ; 
88-     const  header  =  [ `Sentry sentry_version=${ SENTRY_API_VERSION }  ] ; 
89-     header . push ( `sentry_client=${ clientName } ${ clientVersion }  ) ; 
90-     header . push ( `sentry_key=${ dsn . publicKey }  ) ; 
91-     if  ( dsn . pass )  { 
92-       header . push ( `sentry_secret=${ dsn . pass }  ) ; 
93-     } 
94-     return  { 
95-       'Content-Type' : 'application/json' , 
96-       'X-Sentry-Auth' : header . join ( ', ' ) , 
97-     } ; 
98-   } 
99- 
100-   /** Returns the envelope endpoint URL. */ 
101-   private  _getEnvelopeEndpoint ( ) : string  { 
102-     return  this . _getIngestEndpoint ( 'envelope' ) ; 
103-   } 
104- 
105-   /** Returns the ingest API endpoint for target. */ 
106-   private  _getIngestEndpoint ( target : 'store'  |  'envelope' ) : string  { 
107-     if  ( this . _tunnel )  { 
108-       return  this . _tunnel ; 
109-     } 
110-     const  base  =  this . getBaseApiEndpoint ( ) ; 
111-     const  dsn  =  this . getDsn ( ) ; 
112-     return  `${ base } ${ dsn . projectId } ${ target }  ; 
83+     return  getEnvelopeEndpointWithUrlEncodedAuth ( this . _dsnObject ,  this . _tunnel ) ; 
11384  } 
85+ } 
11486
115-   /** Returns a URL-encoded string with auth config suitable for a query string. */ 
116-   private  _encodedAuth ( ) : string  { 
117-     const  dsn  =  this . getDsn ( ) ; 
118-     const  auth  =  { 
119-       // We send only the minimum set of required information. See 
120-       // https://github.com/getsentry/sentry-javascript/issues/2572. 
121-       sentry_key : dsn . publicKey , 
122-       sentry_version : SENTRY_API_VERSION , 
123-     } ; 
124-     return  urlEncode ( auth ) ; 
125-   } 
87+ /** Initializes API Details */ 
88+ export  function  initAPIDetails ( dsn : DsnLike ,  metadata ?: SdkMetadata ,  tunnel ?: string ) : APIDetails  { 
89+   return  { 
90+     initDsn : dsn , 
91+     metadata : metadata  ||  { } , 
92+     dsn : new  Dsn ( dsn ) , 
93+     tunnel, 
94+   }  as  APIDetails ; 
12695} 
12796
12897/** Returns the prefix to construct Sentry ingestion API endpoints. */ 
@@ -132,6 +101,67 @@ function getBaseApiEndpoint(dsn: Dsn): string {
132101  return  `${ protocol } ${ dsn . host } ${ port } ${ dsn . path  ? `/${ dsn . path }   : '' }  ; 
133102} 
134103
104+ /** Returns the ingest API endpoint for target. */ 
105+ function  _getIngestEndpoint ( dsn : Dsn ,  target : 'store'  |  'envelope' ) : string  { 
106+   return  `${ getBaseApiEndpoint ( dsn ) } ${ dsn . projectId } ${ target }  ; 
107+ } 
108+ 
109+ /** Returns a URL-encoded string with auth config suitable for a query string. */ 
110+ function  _encodedAuth ( dsn : Dsn ) : string  { 
111+   return  urlEncode ( { 
112+     // We send only the minimum set of required information. See 
113+     // https://github.com/getsentry/sentry-javascript/issues/2572. 
114+     sentry_key : dsn . publicKey , 
115+     sentry_version : SENTRY_API_VERSION , 
116+   } ) ; 
117+ } 
118+ 
119+ /** Returns the store endpoint URL. */ 
120+ function  getStoreEndpoint ( dsn : Dsn ) : string  { 
121+   return  _getIngestEndpoint ( dsn ,  'store' ) ; 
122+ } 
123+ 
124+ /** 
125+  * Returns the store endpoint URL with auth in the query string. 
126+  * 
127+  * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. 
128+  */ 
129+ export  function  getStoreEndpointWithUrlEncodedAuth ( dsn : Dsn ) : string  { 
130+   return  `${ getStoreEndpoint ( dsn ) } ${ _encodedAuth ( dsn ) }  ; 
131+ } 
132+ 
133+ /** Returns the envelope endpoint URL. */ 
134+ function  _getEnvelopeEndpoint ( dsn : Dsn ) : string  { 
135+   return  _getIngestEndpoint ( dsn ,  'envelope' ) ; 
136+ } 
137+ 
138+ /** 
139+  * Returns the envelope endpoint URL with auth in the query string. 
140+  * 
141+  * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. 
142+  */ 
143+ export  function  getEnvelopeEndpointWithUrlEncodedAuth ( dsn : Dsn ,  tunnel ?: string ) : string  { 
144+   return  tunnel  ? tunnel  : `${ _getEnvelopeEndpoint ( dsn ) } ${ _encodedAuth ( dsn ) }  ; 
145+ } 
146+ 
147+ /** 
148+  * Returns an object that can be used in request headers. 
149+  * This is needed for node and the old /store endpoint in sentry 
150+  */ 
151+ export  function  getRequestHeaders ( dsn : Dsn ,  clientName : string ,  clientVersion : string ) : {  [ key : string ] : string  }  { 
152+   // CHANGE THIS to use metadata but keep clientName and clientVersion compatible 
153+   const  header  =  [ `Sentry sentry_version=${ SENTRY_API_VERSION }  ] ; 
154+   header . push ( `sentry_client=${ clientName } ${ clientVersion }  ) ; 
155+   header . push ( `sentry_key=${ dsn . publicKey }  ) ; 
156+   if  ( dsn . pass )  { 
157+     header . push ( `sentry_secret=${ dsn . pass }  ) ; 
158+   } 
159+   return  { 
160+     'Content-Type' : 'application/json' , 
161+     'X-Sentry-Auth' : header . join ( ', ' ) , 
162+   } ; 
163+ } 
164+ 
135165/** Returns the url to the report dialog endpoint. */ 
136166export  function  getReportDialogEndpoint ( 
137167  dsnLike : DsnLike , 
0 commit comments