Skip to content

Commit 3747ddb

Browse files
fix: token not getting auto refreshed when api throws 401 error
1 parent d03eb7c commit 3747ddb

File tree

37 files changed

+1317
-435
lines changed

37 files changed

+1317
-435
lines changed

lib/HighLevel.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ import { Logger, LogLevelType } from './logging';
3939
import { WebhookManager } from './webhook';
4040
import { UserType } from './constants';
4141

42-
// Extend AxiosRequestConfig to support retry tracking
42+
// Extend AxiosRequestConfig to support retry tracking and security requirements
4343
declare module 'axios' {
4444
interface AxiosRequestConfig {
4545
__isRetryRequest?: boolean;
46+
__secutiryRequirements?: string[];
47+
__preferredTokenType?: 'company' | 'location';
4648
}
4749
}
4850

@@ -308,18 +310,6 @@ export class HighLevel {
308310
return Date.now() + bufferTime >= sessionData.expire_at;
309311
}
310312

311-
/**
312-
* Check if a token is expired (with 1 minute buffer for 401 retries)
313-
* @param sessionData - Session data containing expiration info
314-
* @returns True if token is expired
315-
*/
316-
private isTokenExpired(sessionData: ISessionData): boolean {
317-
if (!sessionData.expire_at) return false;
318-
319-
const bufferTime = 30 * 1000;
320-
return Date.now() + bufferTime >= sessionData.expire_at;
321-
}
322-
323313
/**
324314
* Refresh token if expired and store the new token
325315
* @param resourceId - Resource ID for the session
@@ -651,26 +641,27 @@ export class HighLevel {
651641
if (error.response?.status === 401 && !originalRequest.__isRetryRequest) {
652642
this.logger.warn('401 Unauthorized - Attempting token refresh');
653643

654-
// Try to extract resourceId from the original request
644+
// Try to extract resourceId from the original request using stored security requirements
645+
const securityRequirements = originalRequest.__secutiryRequirements || [];
646+
const preferredTokenType = originalRequest.__preferredTokenType;
647+
655648
const resourceId = this.extractResourceId(
656-
[], // No security requirements available in error context
649+
securityRequirements,
657650
originalRequest.headers || {},
658651
originalRequest.params || {},
659652
originalRequest.data || {},
660-
undefined // No preference available in error context
653+
preferredTokenType
661654
);
662655

663656
if (resourceId) {
664657
try {
665658
const sessionData = await this.sessionStorage.getSession(resourceId);
666659

667-
if (sessionData && this.isTokenExpired(sessionData)) {
660+
if (sessionData) {
668661
this.logger.info(`Token expired for ${resourceId}, attempting refresh`);
669662

670663
const newToken = await this.refreshTokenIfNeeded(resourceId, sessionData);
671664
if (newToken) {
672-
// Mark as retry request and update authorization header
673-
originalRequest.__isRetryRequest = true;
674665
originalRequest.headers = originalRequest.headers || {};
675666
originalRequest.headers.Authorization = newToken;
676667

@@ -680,6 +671,8 @@ export class HighLevel {
680671
}
681672
} catch (refreshError) {
682673
this.logger.error('Failed to refresh token on 401:', refreshError);
674+
} finally {
675+
originalRequest.__isRetryRequest = true;
683676
}
684677
}
685678
}

lib/code/associations/associations.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export class Associations {
3434
const config: AxiosRequestConfig = {
3535
method: 'POST',
3636
url,
37-
params: queryParams,
37+
params: { ...queryParams, ...allParams },
3838
headers: {
3939
...headerParams,
4040
...options?.headers
4141
},
4242
data: requestBody,
43+
// Store security requirements for error handling
44+
__secutiryRequirements: securityRequirements,
4345
...options
4446
};
4547

@@ -143,11 +145,13 @@ export class Associations {
143145
const config: AxiosRequestConfig = {
144146
method: 'GET',
145147
url,
146-
params: queryParams,
148+
params: { ...queryParams, ...allParams },
147149
headers: {
148150
...headerParams,
149151
...options?.headers
150152
},
153+
// Store security requirements for error handling
154+
__secutiryRequirements: securityRequirements,
151155
...options
152156
};
153157

@@ -230,11 +234,13 @@ export class Associations {
230234
const config: AxiosRequestConfig = {
231235
method: 'DELETE',
232236
url,
233-
params: queryParams,
237+
params: { ...queryParams, ...allParams },
234238
headers: {
235239
...headerParams,
236240
...options?.headers
237241
},
242+
// Store security requirements for error handling
243+
__secutiryRequirements: securityRequirements,
238244
...options
239245
};
240246

@@ -317,11 +323,13 @@ export class Associations {
317323
const config: AxiosRequestConfig = {
318324
method: 'GET',
319325
url,
320-
params: queryParams,
326+
params: { ...queryParams, ...allParams },
321327
headers: {
322328
...headerParams,
323329
...options?.headers
324330
},
331+
// Store security requirements for error handling
332+
__secutiryRequirements: securityRequirements,
325333
...options
326334
};
327335

@@ -404,11 +412,13 @@ export class Associations {
404412
const config: AxiosRequestConfig = {
405413
method: 'GET',
406414
url,
407-
params: queryParams,
415+
params: { ...queryParams, ...allParams },
408416
headers: {
409417
...headerParams,
410418
...options?.headers
411419
},
420+
// Store security requirements for error handling
421+
__secutiryRequirements: securityRequirements,
412422
...options
413423
};
414424

@@ -485,12 +495,14 @@ export class Associations {
485495
const config: AxiosRequestConfig = {
486496
method: 'PUT',
487497
url,
488-
params: queryParams,
498+
params: { ...queryParams, ...allParams },
489499
headers: {
490500
...headerParams,
491501
...options?.headers
492502
},
493503
data: requestBody,
504+
// Store security requirements for error handling
505+
__secutiryRequirements: securityRequirements,
494506
...options
495507
};
496508

@@ -566,11 +578,13 @@ export class Associations {
566578
const config: AxiosRequestConfig = {
567579
method: 'DELETE',
568580
url,
569-
params: queryParams,
581+
params: { ...queryParams, ...allParams },
570582
headers: {
571583
...headerParams,
572584
...options?.headers
573585
},
586+
// Store security requirements for error handling
587+
__secutiryRequirements: securityRequirements,
574588
...options
575589
};
576590

@@ -646,11 +660,13 @@ export class Associations {
646660
const config: AxiosRequestConfig = {
647661
method: 'GET',
648662
url,
649-
params: queryParams,
663+
params: { ...queryParams, ...allParams },
650664
headers: {
651665
...headerParams,
652666
...options?.headers
653667
},
668+
// Store security requirements for error handling
669+
__secutiryRequirements: securityRequirements,
654670
...options
655671
};
656672

@@ -714,12 +730,14 @@ export class Associations {
714730
const config: AxiosRequestConfig = {
715731
method: 'POST',
716732
url,
717-
params: queryParams,
733+
params: { ...queryParams, ...allParams },
718734
headers: {
719735
...headerParams,
720736
...options?.headers
721737
},
722738
data: requestBody,
739+
// Store security requirements for error handling
740+
__secutiryRequirements: securityRequirements,
723741
...options
724742
};
725743

@@ -809,11 +827,13 @@ export class Associations {
809827
const config: AxiosRequestConfig = {
810828
method: 'GET',
811829
url,
812-
params: queryParams,
830+
params: { ...queryParams, ...allParams },
813831
headers: {
814832
...headerParams,
815833
...options?.headers
816834
},
835+
// Store security requirements for error handling
836+
__secutiryRequirements: securityRequirements,
817837
...options
818838
};
819839

lib/code/blogs/blogs.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ export class Blogs {
6060
const config: AxiosRequestConfig = {
6161
method: 'GET',
6262
url,
63-
params: queryParams,
63+
params: { ...queryParams, ...allParams },
6464
headers: {
6565
...headerParams,
6666
...options?.headers
6767
},
68+
// Store security requirements for error handling
69+
__secutiryRequirements: securityRequirements,
6870
...options
6971
};
7072

@@ -128,12 +130,14 @@ export class Blogs {
128130
const config: AxiosRequestConfig = {
129131
method: 'PUT',
130132
url,
131-
params: queryParams,
133+
params: { ...queryParams, ...allParams },
132134
headers: {
133135
...headerParams,
134136
...options?.headers
135137
},
136138
data: requestBody,
139+
// Store security requirements for error handling
140+
__secutiryRequirements: securityRequirements,
137141
...options
138142
};
139143

@@ -197,12 +201,14 @@ export class Blogs {
197201
const config: AxiosRequestConfig = {
198202
method: 'POST',
199203
url,
200-
params: queryParams,
204+
params: { ...queryParams, ...allParams },
201205
headers: {
202206
...headerParams,
203207
...options?.headers
204208
},
205209
data: requestBody,
210+
// Store security requirements for error handling
211+
__secutiryRequirements: securityRequirements,
206212
...options
207213
};
208214

@@ -292,11 +298,13 @@ export class Blogs {
292298
const config: AxiosRequestConfig = {
293299
method: 'GET',
294300
url,
295-
params: queryParams,
301+
params: { ...queryParams, ...allParams },
296302
headers: {
297303
...headerParams,
298304
...options?.headers
299305
},
306+
// Store security requirements for error handling
307+
__secutiryRequirements: securityRequirements,
300308
...options
301309
};
302310

@@ -386,11 +394,13 @@ export class Blogs {
386394
const config: AxiosRequestConfig = {
387395
method: 'GET',
388396
url,
389-
params: queryParams,
397+
params: { ...queryParams, ...allParams },
390398
headers: {
391399
...headerParams,
392400
...options?.headers
393401
},
402+
// Store security requirements for error handling
403+
__secutiryRequirements: securityRequirements,
394404
...options
395405
};
396406

@@ -501,11 +511,13 @@ export class Blogs {
501511
const config: AxiosRequestConfig = {
502512
method: 'GET',
503513
url,
504-
params: queryParams,
514+
params: { ...queryParams, ...allParams },
505515
headers: {
506516
...headerParams,
507517
...options?.headers
508518
},
519+
// Store security requirements for error handling
520+
__secutiryRequirements: securityRequirements,
509521
...options
510522
};
511523

@@ -602,11 +614,13 @@ export class Blogs {
602614
const config: AxiosRequestConfig = {
603615
method: 'GET',
604616
url,
605-
params: queryParams,
617+
params: { ...queryParams, ...allParams },
606618
headers: {
607619
...headerParams,
608620
...options?.headers
609621
},
622+
// Store security requirements for error handling
623+
__secutiryRequirements: securityRequirements,
610624
...options
611625
};
612626

0 commit comments

Comments
 (0)