-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathon-retry-callback.ts
More file actions
57 lines (47 loc) · 1.65 KB
/
on-retry-callback.ts
File metadata and controls
57 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* onRetry Callback Example
*
* Demonstrates using the onRetry option to mutate the request config
* before a failed request is retried. Here we add a custom header
* and verify the mock adapter sees it on the retried request.
*/
import axios from 'axios';
import { createAuthRefresh } from '../src/index';
import { createMockAdapter, MockState } from './_helpers/mock-adapter';
import { assertEqual, assert } from './_helpers/assert';
const state: MockState = { validToken: 'token-v2', refreshCount: 0 };
let retryHeaderSeen = false;
// Custom adapter that also checks for the X-Retry header
const baseAdapter = createMockAdapter(state);
const checkingAdapter = (config: any) => {
if (config.headers?.['X-Retry'] === 'true') {
retryHeaderSeen = true;
}
return baseAdapter(config);
};
const instance = axios.create({
adapter: checkingAdapter,
headers: { Authorization: 'Bearer token-v1' },
});
createAuthRefresh(
instance,
async (failedRequest) => {
state.refreshCount++;
state.validToken = 'token-v2';
failedRequest.response.config.headers['Authorization'] = `Bearer ${state.validToken}`;
},
{
onRetry: (requestConfig) => {
requestConfig.headers['X-Retry'] = 'true';
return requestConfig;
},
},
);
async function main() {
const response = await instance.get('/protected');
assertEqual(response.status, 200, 'Request should succeed after retry');
assertEqual(state.refreshCount, 1, 'Refresh called once');
assert(retryHeaderSeen, 'X-Retry header should be present on retried request');
console.log(' PASS on-retry-callback');
}
main();