@@ -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,52 @@ 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 } ` ;
133
-
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 ;
143
- }
144
-
145
- // If anything other than 401, throw the error
146
- if ( response . status !== 401 ) {
147
- throw await ApiClientError . fromResponse ( response ) ;
148
- }
121
+ public async sendEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
122
+ if ( ! this . hasCredentials ( ) ) {
123
+ await this . sendUnauthEvents ( events ) ;
124
+ return ;
125
+ }
149
126
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 ) {
127
+ try {
128
+ await this . validateAccessToken ( ) ;
129
+ await this . sendAuthEvents ( events ) ;
130
+ } catch ( error ) {
131
+ if ( error instanceof ApiClientError ) {
132
+ if ( error . response . status !== 401 ) {
155
133
throw error ;
156
134
}
157
-
158
- // For 401 errors, fall through to unauthenticated endpoint
159
- delete headers [ "Authorization" ] ;
160
135
}
136
+ // send unauth events if the token is not valid
137
+ await this . sendUnauthEvents ( events ) ;
138
+ return ;
161
139
}
140
+ }
141
+
142
+ private async sendAuthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
143
+ const authUrl = new URL ( "api/private/v1.0/telemetry/events" , this . options . baseUrl ) ;
144
+ const response = await fetch ( authUrl , {
145
+ method : "POST" ,
146
+ headers : {
147
+ Accept : "application/json" ,
148
+ "Content-Type" : "application/json" ,
149
+ "User-Agent" : this . options . userAgent ,
150
+ Authorization : `Bearer ${ this . accessToken } ` ,
151
+ } ,
152
+ body : JSON . stringify ( events ) ,
153
+ } ) ;
154
+
155
+ if ( ! response . ok ) {
156
+ throw await ApiClientError . fromResponse ( response ) ;
157
+ }
158
+ }
159
+
160
+ private async sendUnauthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
161
+ const headers : Record < string , string > = {
162
+ Accept : "application/json" ,
163
+ "Content-Type" : "application/json" ,
164
+ "User-Agent" : this . options . userAgent ,
165
+ } ;
162
166
163
- // Send to unauthenticated endpoint (either as fallback from 401 or direct if no token)
164
167
const unauthUrl = new URL ( "api/private/unauth/telemetry/events" , this . options . baseUrl ) ;
165
168
const response = await fetch ( unauthUrl , {
166
169
method : "POST" ,
0 commit comments