@@ -70,16 +70,22 @@ export function ConcurrencyQueue ({ axios, config }) {
7070 }
7171
7272 // Request interceptor to queue the request
73- const requestHandler = request => {
73+ const requestHandler = ( request ) => {
7474 if ( typeof request . data === 'function' ) {
7575 request . formdata = request . data
7676 request . data = transformFormData ( request )
7777 }
7878 request . retryCount = request . retryCount || 0
7979 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
83+ }
8084 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
8188 }
82-
8389 if ( request . cancelToken === undefined ) {
8490 const source = Axios . CancelToken . source ( )
8591 request . cancelToken = source . token
@@ -102,26 +108,62 @@ export function ConcurrencyQueue ({ axios, config }) {
102108 } )
103109 }
104110
105- const delay = ( time ) => {
111+ const delay = ( time , isRefreshToken = false ) => {
106112 if ( ! this . paused ) {
107113 this . paused = true
108114 // Check for current running request.
109115 // Wait for running queue to complete.
110116 // Wait and prosed the Queued request.
111117 if ( this . running . length > 0 ) {
112118 setTimeout ( ( ) => {
113- delay ( time )
119+ delay ( time , isRefreshToken )
114120 } , time )
115121 }
116122 return new Promise ( resolve => setTimeout ( ( ) => {
117123 this . paused = false
118- for ( let i = 0 ; i < this . config . maxRequests ; i ++ ) {
119- this . initialShift ( )
124+ if ( isRefreshToken ) {
125+ return refreshToken ( )
126+ } else {
127+ for ( let i = 0 ; i < this . config . maxRequests ; i ++ ) {
128+ this . initialShift ( )
129+ }
120130 }
121131 } , time ) )
122132 }
123133 }
124-
134+ const refreshToken = ( ) => {
135+ return config . refreshToken ( ) . then ( ( token ) => {
136+ if ( token . authorization ) {
137+ axios . defaults . headers . authorization = token . authorization
138+ axios . defaults . authorization = token . authorization
139+ axios . httpClientParams . authorization = token . authorization
140+ axios . httpClientParams . headers . authorization = token . authorization
141+ this . config . authorization = token . authorization
142+ } else if ( token . authtoken ) {
143+ axios . defaults . headers . authtoken = token . authtoken
144+ axios . defaults . authtoken = token . authtoken
145+ axios . httpClientParams . authtoken = token . authtoken
146+ axios . httpClientParams . headers . authtoken = token . authtoken
147+ this . config . authtoken = token . authtoken
148+ }
149+ } ) . catch ( ( error ) => {
150+ throw error
151+ } ) . finally ( ( ) => {
152+ this . queue . forEach ( ( queueItem ) => {
153+ if ( this . config . authorization ) {
154+ queueItem . request . headers . authorization = this . config . authorization
155+ queueItem . request . authorization = this . config . authorization
156+ }
157+ if ( this . config . authtoken ) {
158+ queueItem . request . headers . authtoken = this . config . authtoken
159+ queueItem . request . authtoken = this . config . authtoken
160+ }
161+ } )
162+ for ( let i = 0 ; i < this . config . maxRequests ; i ++ ) {
163+ this . initialShift ( )
164+ }
165+ } )
166+ }
125167 // Response interceptor used for
126168 const responseHandler = ( response ) => {
127169 response . config . onComplete ( )
@@ -150,7 +192,7 @@ export function ConcurrencyQueue ({ axios, config }) {
150192 } else {
151193 return Promise . reject ( responseHandler ( error ) )
152194 }
153- } else if ( response . status === 429 ) {
195+ } else if ( response . status === 429 || ( response . status === 401 && this . config . refreshToken ) ) {
154196 retryErrorType = `Error with status: ${ response . status } `
155197 networkError ++
156198
@@ -159,7 +201,7 @@ export function ConcurrencyQueue ({ axios, config }) {
159201 }
160202 this . running . shift ( )
161203 // Cool down the running requests
162- delay ( wait )
204+ delay ( wait , response . status === 401 )
163205 error . config . retryCount = networkError
164206
165207 return axios ( updateRequestConfig ( error , retryErrorType , wait ) )
0 commit comments