@@ -3,10 +3,26 @@ import { Dsn, urlEncode } from '@sentry/utils';
3
3
4
4
const SENTRY_API_VERSION = '7' ;
5
5
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
+
6
20
/**
7
21
* Helper class to provide urls, headers and metadata that can be used to form
8
22
* different types of requests to Sentry endpoints.
9
23
* Supports both envelopes and regular event requests.
24
+ *
25
+ * @deprecated Please use APIDetails
10
26
**/
11
27
export class API {
12
28
/** The DSN as passed to Sentry.init() */
@@ -41,13 +57,12 @@ export class API {
41
57
42
58
/** Returns the prefix to construct Sentry ingestion API endpoints. */
43
59
public getBaseApiEndpoint ( ) : string {
44
- const dsn = this . getDsn ( ) ;
45
- return getBaseApiEndpoint ( dsn ) ;
60
+ return getBaseApiEndpoint ( this . _dsnObject ) ;
46
61
}
47
62
48
63
/** Returns the store endpoint URL. */
49
64
public getStoreEndpoint ( ) : string {
50
- return this . _getIngestEndpoint ( 'store' ) ;
65
+ return getStoreEndpoint ( this . _dsnObject ) ;
51
66
}
52
67
53
68
/**
@@ -56,7 +71,7 @@ export class API {
56
71
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
57
72
*/
58
73
public getStoreEndpointWithUrlEncodedAuth ( ) : string {
59
- return ` ${ this . getStoreEndpoint ( ) } ? ${ this . _encodedAuth ( ) } ` ;
74
+ return getStoreEndpointWithUrlEncodedAuth ( this . _dsnObject ) ;
60
75
}
61
76
62
77
/**
@@ -65,64 +80,18 @@ export class API {
65
80
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
66
81
*/
67
82
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 } ` : '' } /api/${ dsn . projectId } /store/` ;
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 ) ;
113
84
}
85
+ }
114
86
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 ;
126
95
}
127
96
128
97
/** Returns the prefix to construct Sentry ingestion API endpoints. */
@@ -132,6 +101,67 @@ function getBaseApiEndpoint(dsn: Dsn): string {
132
101
return `${ protocol } //${ dsn . host } ${ port } ${ dsn . path ? `/${ dsn . path } ` : '' } /api/` ;
133
102
}
134
103
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
+
135
165
/** Returns the url to the report dialog endpoint. */
136
166
export function getReportDialogEndpoint (
137
167
dsnLike : DsnLike ,
0 commit comments