1
1
import Axios from 'axios'
2
+ import OAuthHandler from './oauthHandler'
2
3
const defaultConfig = {
3
4
maxRequests : 5 ,
4
5
retryLimit : 5 ,
@@ -75,17 +76,17 @@ export function ConcurrencyQueue ({ axios, config }) {
75
76
request . formdata = request . data
76
77
request . data = transformFormData ( request )
77
78
}
78
- request . retryCount = request . retryCount || 0
79
- if ( request . headers . authorization && request . headers . authorization !== undefined ) {
80
- if ( this . config . authorization && this . config . authorization !== undefined ) {
81
- request . headers . authorization = this . config . authorization
82
- request . authorization = this . config . authorization
79
+ if ( axios ?. oauth ?. accessToken ) {
80
+ const isTokenExpired = axios . oauth . tokenExpiryTime && Date . now ( ) > axios . oauth . tokenExpiryTime
81
+ if ( isTokenExpired ) {
82
+ return refreshAccessToken ( ) . catch ( ( error ) => {
83
+ throw new Error ( 'Failed to refresh access token: ' + error . message )
84
+ } )
83
85
}
84
- delete request . headers . authtoken
85
- } else if ( request . headers . authtoken && request . headers . authtoken !== undefined && this . config . authtoken && this . config . authtoken !== undefined ) {
86
- request . headers . authtoken = this . config . authtoken
87
- request . authtoken = this . config . authtoken
88
86
}
87
+
88
+ request . retryCount = request ?. retryCount || 0
89
+ setAuthorizationHeaders ( request )
89
90
if ( request . cancelToken === undefined ) {
90
91
const source = Axios . CancelToken . source ( )
91
92
request . cancelToken = source . token
@@ -108,6 +109,44 @@ export function ConcurrencyQueue ({ axios, config }) {
108
109
} )
109
110
}
110
111
112
+ const setAuthorizationHeaders = ( request ) => {
113
+ if ( request . headers . authorization && request . headers . authorization !== undefined ) {
114
+ if ( this . config . authorization && this . config . authorization !== undefined ) {
115
+ request . headers . authorization = this . config . authorization
116
+ request . authorization = this . config . authorization
117
+ }
118
+ delete request . headers . authtoken
119
+ } else if ( request . headers . authtoken && request . headers . authtoken !== undefined && this . config . authtoken && this . config . authtoken !== undefined ) {
120
+ request . headers . authtoken = this . config . authtoken
121
+ request . authtoken = this . config . authtoken
122
+ } else if ( axios ?. oauth ?. accessToken ) {
123
+ // If OAuth access token is available in axios instance
124
+ request . headers . authorization = `Bearer ${ axios . oauth . accessToken } `
125
+ request . authorization = `Bearer ${ axios . oauth . accessToken } `
126
+ delete request . headers . authtoken
127
+ }
128
+ }
129
+
130
+ // Refresh Access Token
131
+ const refreshAccessToken = async ( ) => {
132
+ try {
133
+ // Try to refresh the token
134
+ await new OAuthHandler ( axios ) . refreshAccessToken ( )
135
+ this . paused = false // Resume the request queue once the token is refreshed
136
+
137
+ // Retry the requests that were pending due to token expiration
138
+ this . running . forEach ( ( { request, resolve, reject } ) => {
139
+ // Retry the request
140
+ axios ( request ) . then ( resolve ) . catch ( reject )
141
+ } )
142
+ this . running = [ ] // Clear the running queue after retrying requests
143
+ } catch ( error ) {
144
+ this . paused = false // stop queueing requests on failure
145
+ this . running . forEach ( ( { reject } ) => reject ( error ) ) // Reject all queued requests
146
+ this . running = [ ] // Clear the running queue
147
+ }
148
+ }
149
+
111
150
const delay = ( time , isRefreshToken = false ) => {
112
151
if ( ! this . paused ) {
113
152
this . paused = true
0 commit comments