@@ -89,9 +89,8 @@ export class ApiClient {
89
89
return ! ! ( this . oauth2Client && this . accessToken ) ;
90
90
}
91
91
92
- public async hasValidAccessToken ( ) : Promise < boolean > {
93
- const accessToken = await this . getAccessToken ( ) ;
94
- return accessToken !== undefined ;
92
+ public async validateAccessToken ( ) : Promise < void > {
93
+ await this . getAccessToken ( ) ;
95
94
}
96
95
97
96
public async getIpInfo ( ) : Promise < {
@@ -119,48 +118,57 @@ export class ApiClient {
119
118
} > ;
120
119
}
121
120
122
- async sendEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
123
- const headers : Record < string , string > = {
124
- Accept : "application/json" ,
125
- "Content-Type" : "application/json" ,
126
- "User-Agent" : this . options . userAgent ,
127
- } ;
128
-
129
- const accessToken = await this . getAccessToken ( ) ;
130
- if ( accessToken ) {
131
- const authUrl = new URL ( "api/private/v1.0/telemetry/events" , this . options . baseUrl ) ;
132
- headers [ "Authorization" ] = `Bearer ${ accessToken } ` ;
121
+ public async sendEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
122
+ if ( ! this . options . credentials ) {
123
+ await this . sendUnauthEvents ( events ) ;
124
+ return ;
125
+ }
133
126
134
- try {
135
- const response = await fetch ( authUrl , {
136
- method : "POST" ,
137
- headers,
138
- body : JSON . stringify ( events ) ,
139
- } ) ;
140
-
141
- if ( response . ok ) {
142
- return ;
127
+ try {
128
+ await this . sendAuthEvents ( events ) ;
129
+ } catch ( error ) {
130
+ if ( error instanceof ApiClientError ) {
131
+ if ( error . response . status !== 401 ) {
132
+ throw error ;
143
133
}
134
+ }
144
135
145
- // If anything other than 401, throw the error
146
- if ( response . status !== 401 ) {
147
- throw await ApiClientError . fromResponse ( response ) ;
148
- }
136
+ // send unauth events if any of the following are true:
137
+ // 1: the token is not valid (not ApiClientError)
138
+ // 2: if the api responded with 401 (ApiClientError with status 401)
139
+ await this . sendUnauthEvents ( events ) ;
140
+ }
141
+ }
149
142
150
- // For 401, fall through to unauthenticated endpoint
151
- delete headers [ "Authorization" ] ;
152
- } catch ( error ) {
153
- // If the error is not a 401, rethrow it
154
- if ( ! ( error instanceof ApiClientError ) || error . response . status !== 401 ) {
155
- throw error ;
156
- }
143
+ private async sendAuthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
144
+ const accessToken = await this . getAccessToken ( ) ;
145
+ if ( ! accessToken ) {
146
+ throw new Error ( "No access token available" ) ;
147
+ }
148
+ const authUrl = new URL ( "api/private/v1.0/telemetry/events" , this . options . baseUrl ) ;
149
+ const response = await fetch ( authUrl , {
150
+ method : "POST" ,
151
+ headers : {
152
+ Accept : "application/json" ,
153
+ "Content-Type" : "application/json" ,
154
+ "User-Agent" : this . options . userAgent ,
155
+ Authorization : `Bearer ${ accessToken } ` ,
156
+ } ,
157
+ body : JSON . stringify ( events ) ,
158
+ } ) ;
157
159
158
- // For 401 errors, fall through to unauthenticated endpoint
159
- delete headers [ "Authorization" ] ;
160
- }
160
+ if ( ! response . ok ) {
161
+ throw await ApiClientError . fromResponse ( response ) ;
161
162
}
163
+ }
164
+
165
+ private async sendUnauthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
166
+ const headers : Record < string , string > = {
167
+ Accept : "application/json" ,
168
+ "Content-Type" : "application/json" ,
169
+ "User-Agent" : this . options . userAgent ,
170
+ } ;
162
171
163
- // Send to unauthenticated endpoint (either as fallback from 401 or direct if no token)
164
172
const unauthUrl = new URL ( "api/private/unauth/telemetry/events" , this . options . baseUrl ) ;
165
173
const response = await fetch ( unauthUrl , {
166
174
method : "POST" ,
0 commit comments