Skip to content

Commit 5e1c49a

Browse files
authored
Merge pull request #285 from contentstack/feat/DX-1364
feat: retry the requests that were pending due to token expiration
2 parents aa892ba + 7efbab2 commit 5e1c49a

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

lib/core/concurrency-queue.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,23 @@ export function ConcurrencyQueue ({ axios, config }) {
130130
//Refresh Access Token
131131
const refreshAccessToken = async () => {
132132
try {
133+
// Try to refresh the token
133134
await new OAuthHandler(axios).refreshAccessToken();
134135
this.paused = false; // Resume the request queue once the token is refreshed
136+
137+
// Retry the requests that were pending due to token expiration
135138
this.running.forEach(({ request, resolve, reject }) => {
136-
resolve(request); // Retry the queued requests
139+
// Retry the request
140+
axios(request).then(resolve).catch(reject);
137141
});
138-
this.running = []; // Clear the running queue
142+
this.running = []; // Clear the running queue after retrying requests
139143
} catch (error) {
140-
this.paused = false; // Ensure we stop queueing requests on failure
144+
this.paused = false; // stop queueing requests on failure
141145
this.running.forEach(({ reject }) => reject(error)); // Reject all queued requests
142146
this.running = []; // Clear the running queue
143147
}
144-
};
148+
}
149+
145150

146151
const delay = (time, isRefreshToken = false) => {
147152
if (!this.paused) {

lib/core/oauthHandler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ export default class OAuthHandler {
8989
*/
9090
async authorize() {
9191
try {
92+
if (!this.OAuthBaseURL) {
93+
throw new Error('OAuthBaseURL is not set');
94+
}
9295
const baseUrl = `${this.OAuthBaseURL}/#!/apps/${this.appId}/authorize`;
9396
const authUrl = new URL(baseUrl);
9497
authUrl.searchParams.set('response_type', 'code'); // Using set() to avoid duplicate parameters

types/oauthHandler.d.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ interface OAuthResponse {
55
expires_in: number;
66
organization_uid: string;
77
user_uid: string;
8-
token_type: string,
9-
location: string,
10-
region: string,
11-
authorization_type: string,
12-
stack_api_key: string
8+
token_type: string;
9+
location: string;
10+
region: string;
11+
authorization_type: string;
12+
stack_api_key: string;
1313
}
1414

1515
export default class OAuthHandler {
@@ -45,6 +45,60 @@ export default class OAuthHandler {
4545
*/
4646
getAccessToken(): string;
4747

48+
/**
49+
* Get the current refresh token
50+
* @returns The refresh token
51+
*/
52+
getRefreshToken(): string;
53+
54+
/**
55+
* Get the current organization UID
56+
* @returns The organization UID
57+
*/
58+
getOrganizationUID(): string;
59+
60+
/**
61+
* Get the current user UID
62+
* @returns The user UID
63+
*/
64+
getUserUID(): string;
65+
66+
/**
67+
* Get the token expiry time
68+
* @returns The token expiry time
69+
*/
70+
getTokenExpiryTime(): string;
71+
72+
/**
73+
* Set the access token
74+
* @param token - The access token
75+
*/
76+
setAccessToken(token: string): void;
77+
78+
/**
79+
* Set the refresh token
80+
* @param token - The refresh token
81+
*/
82+
setRefreshToken(token: string): void;
83+
84+
/**
85+
* Set organization UID
86+
* @param organizationUID - The organization UID
87+
*/
88+
setOrganizationUID(organizationUID: string): void;
89+
90+
/**
91+
* Set user UID
92+
* @param userUID - The user UID
93+
*/
94+
setUserUID(userUID: string): void;
95+
96+
/**
97+
* Set expiry time
98+
* @param expiryTime - The expiry time
99+
*/
100+
setTokenExpiryTime(expiryTime: Date): void;
101+
48102
/**
49103
* Handle the OAuth redirect URL and exchange the authorization code for a token
50104
* @param url - The redirect URL containing the authorization code

0 commit comments

Comments
 (0)