-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
524 lines (476 loc) · 16.4 KB
/
index.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosRequestHeaders,
AxiosResponse,
} from 'axios'
import { Mutex } from 'async-mutex'
import * as debugWebAxios from './adapters/debugWebAxios'
import {
AsyncResponse,
AxiosErrorWithRetriableRequestConfig,
ConfigMetaData,
ConnectionConfig,
ErrorResponse,
ExtendedAxiosInstance,
RefreshTokenResponse,
StepUpPayload,
} from './types'
const ACCESS_TOKEN_EXPIRED = 401
const REFRESH_PATH = '/oauth2/refresh'
const STEP_UP_REQUIRED = 403
const STEPUP_PATH = '/oauth2/stepup'
const DEFAULT_CONFIG = { timeout: 3000 }
const DEFAULT_HEADERS: AxiosRequestHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
const ApiConnector = (() => {
const instances: Record<string, ExtendedAxiosInstance> = {}
function createInstance(name: string, config: ConnectionConfig): ExtendedAxiosInstance {
const {
refreshPath = REFRESH_PATH,
stepupPath = STEPUP_PATH,
autoRefreshToken = true,
useIdempotency = false,
cancelOldRequest,
stepUpAuthEnabled = false,
retryOnTimeout = false,
useResponseTime = false,
useEtag = false,
tron,
headers: inputHeaders,
tokensPersist,
tokenRehydrate,
...axiosConfig
} = config
const { apiKey } = axiosConfig
const headers: AxiosRequestHeaders = {
...DEFAULT_HEADERS,
// Add X-ApiKey header if it exists
...(apiKey ? { 'X-ApiKey': apiKey } : undefined),
// Add any custom headers
...inputHeaders,
}
const currentExecutingRequests: Record<string, { cancel: () => void }> = {}
const tokens: RefreshTokenResponse = {
accessToken: undefined,
refreshToken: undefined,
}
tokenRehydrate?.().then(
({ accessToken, refreshToken } = { accessToken: undefined, refreshToken: undefined }) => {
tokens.accessToken = accessToken ?? tokens.accessToken
tokens.refreshToken = refreshToken ?? tokens.refreshToken
});
const stepUpPayload: StepUpPayload = {}
/**
* Create mutex for refreshToken
*/
const mutex = new Mutex()
/**
* Create main instance. Can be an axios instance or a debugWebAxios instance
*/
const factory = global.window?.location ? debugWebAxios : axios
const instance = factory.create({
...DEFAULT_CONFIG,
...axiosConfig,
}) as ExtendedAxiosInstance
/**
* Add Headers
*/
instance.defaults.headers.common = headers
/**
* Add Request Interceptors
*/
instance.interceptors.request.use(authenticationInterceptor, undefined, {
synchronous: true,
runWhen: () => autoRefreshToken && !!tokens?.accessToken,
})
instance.interceptors.request.use(idempotencyInterceptor, undefined, {
synchronous: true,
runWhen: ({ method }: AxiosRequestConfig) =>
useIdempotency && ['post', 'put', 'patch'].includes(method ?? ''),
})
instance.interceptors.request.use(cancelRequestInterceptor, undefined, {
synchronous: true,
runWhen: () => cancelOldRequest !== undefined,
})
instance.interceptors.request.use(
(config: AxiosRequestConfig & ConfigMetaData) => {
config.metadata = { ...config.metadata, startTime: +new Date() }
return config
},
undefined,
{
synchronous: true,
runWhen: () => useResponseTime,
},
)
/**
* Add Response Interceptors
*/
instance.interceptors.response.use(
cancelResponseInterceptor,
cancelErrorInterceptor,
{
runWhen: () => cancelOldRequest !== undefined,
},
)
instance.interceptors.response.use(storeTokensInterceptor, refreshTokenInterceptor, {
runWhen: () => autoRefreshToken,
})
instance.interceptors.response.use(undefined, stepUpAuthInterceptor, {
runWhen: () => stepUpAuthEnabled,
})
instance.interceptors.response.use(undefined, timeOutInterceptor, {
runWhen: () => retryOnTimeout,
})
instance.interceptors.response.use(
(response) => {
if (useResponseTime) {
updateResponseTime(response.config as ConfigMetaData)
}
return response
},
(error) => {
if (useResponseTime) {
updateResponseTime(error.config)
}
return Promise.reject(error)
},
{
runWhen: () => useResponseTime,
},
)
/**
* Private interceptor
* Add Authorization header if accessToken was previously set by
* storeTokensInterceptor
*/
function authenticationInterceptor(
requestConfig: AxiosRequestConfig,
): AxiosRequestConfig {
if (autoRefreshToken && requestConfig.headers) {
requestConfig.headers['Authorization'] = `Bearer ${tokens.accessToken}`
}
return requestConfig
}
/**
* Private interceptor
* Add Idempotency-Key to POST, PUT and PATCH methods
*/
function idempotencyInterceptor(
requestConfig: AxiosRequestConfig,
): AxiosRequestConfig {
if (requestConfig.headers) {
const { data, url } = requestConfig
requestConfig.headers['Idempotency-Key'] = idempotencyKeyFrom(data, url)
}
return requestConfig
}
/**
* Private interceptor
* Add cancelToken to request config to be able to cancel repeated
* requests. If the url was already used and the cancelOldRequest flag is true,
* the old request will be canceled and the new request will be performed, or
* the new request will be canceled and the old will be preserved.
*/
function cancelRequestInterceptor(
requestConfig: AxiosRequestConfig,
): AxiosRequestConfig {
const key = requestConfig?.url ?? ''
if (currentExecutingRequests[key]) {
const source = cancelOldRequest
? currentExecutingRequests[key]
: axios.CancelToken.source()
delete currentExecutingRequests[cancelOldRequest ? key : '']
source.cancel()
}
const source = axios.CancelToken.source()
currentExecutingRequests[key] = source
return {
...requestConfig,
cancelToken: source.token,
}
}
/**
* Private interceptor
* Inspect every response and remove pending requests from
* currentExecutingRequests object.
* @param response: AxiosResponse<RefreshTokenResponse>
*/
function cancelResponseInterceptor(
response: AxiosResponse<RefreshTokenResponse>,
): AxiosResponse<RefreshTokenResponse> {
if (cancelOldRequest !== undefined) {
delete currentExecutingRequests[response.request?.responseURL]
}
return response
}
/**
* Private interceptor
* If the request failed because it was canceled, the error will
* be silently discarded. On any other error, it will remove pending
* requests from currentExecutingRequests object.
* @param error: AxiosErrorWithRetriableRequestConfig
*/
function cancelErrorInterceptor(error: AxiosErrorWithRetriableRequestConfig) {
if (cancelOldRequest !== undefined) {
if (axios.isCancel(error)) {
return Promise.resolve()
}
const {
config: { url = '' },
} = error
if (currentExecutingRequests[url]) {
delete currentExecutingRequests[url]
}
}
return Promise.reject(error)
}
/**
* Private interceptor
* Inspect every response for the accessToken, refreshToken pair
* and store them if found
* @param response: AxiosResponse<RefreshTokenResponse>
*/
function storeTokensInterceptor(
response: AxiosResponse<RefreshTokenResponse>,
): AxiosResponse<RefreshTokenResponse> {
if (autoRefreshToken) {
const { accessToken, refreshToken } = response?.data ?? {}
tokens.accessToken = accessToken ?? tokens.accessToken
tokens.refreshToken = refreshToken ?? tokens.refreshToken
if (accessToken && refreshToken) {
tokensPersist?.(tokens)
}
}
return response
}
/**
* Private interceptor
* If request failed with timeout or 504 timeout.
* @param error: AxiosErrorWithRetriableRequestConfig
*/
function timeOutInterceptor(error: AxiosErrorWithRetriableRequestConfig) {
if (retryOnTimeout) {
const { config, response, code, message } = error
const { status } = response ?? {}
if ((code === 'ECONNABORTED' && message.match(/timeout/)) || status === 504) {
const { timeout, ...request } = config
if ((timeout ?? 1e10) > 6e4) {
return Promise.reject(error)
}
return instance.request({ ...request, timeout: (timeout ?? 1e3) * 5 })
}
}
return Promise.reject(error)
}
/**
* Private interceptor
* If request failed with status 401 accessToken expiration, refresh tokens.
* @param error: AxiosErrorWithRetriableRequestConfig
*/
function refreshTokenInterceptor(error: AxiosErrorWithRetriableRequestConfig) {
const { response: { status, data = {} } = {}, config } = error
if (autoRefreshToken && status === ACCESS_TOKEN_EXPIRED) {
if (config.metadata?.didRetry) {
return Promise.reject(data)
}
config.metadata = { ...config.metadata, didRetry: true }
return refreshToken().then(() => instance.request(config))
}
return Promise.reject(error)
}
/**
* Private interceptor
* If request failed with status 403 stepup required.
* @param error: AxiosErrorWithRetriableRequestConfig
*/
function stepUpAuthInterceptor(error: AxiosErrorWithRetriableRequestConfig) {
if (stepUpAuthEnabled) {
const { response, config } = error
const { status, data } = response || {}
const { transactionId, authenticationMethods } =( data || {}) as StepUpPayload
if (status === STEP_UP_REQUIRED && transactionId) {
config.headers ??= {}
config.headers['X-TransactionId'] = `${transactionId}`
stepUpPayload.transactionId = transactionId
stepUpPayload.authenticationMethods = authenticationMethods
stepUpPayload.config = config
}
}
return Promise.reject(error)
}
/**
* Create a separate axions instance for auth refresh token
*/
const refreshInstance: AxiosInstance = factory.create({
...DEFAULT_CONFIG,
...axiosConfig,
tron,
headers,
})
/**
* The refreshToken handler will be accessible from the instance
* to be called when we need to force an Auth Token refresh.
* It use mutex to prevent multiple refresh requests.
*/
async function refreshToken(): Promise<void> {
const { refreshToken } = tokens
return mutex.runExclusive(() => refreshInstance
.post<RefreshTokenResponse>(refreshPath, { refreshToken })
.then(({ data }) => data)
.then(({ accessToken, refreshToken }) => {
tokens.accessToken = accessToken
tokens.refreshToken = refreshToken
}))
}
/**
* The updateHeaders handler will allow the headers to be consistently
* updated on both the instance and the refreshInstance.
*/
function updateHeaders(headers: Partial<AxiosRequestHeaders>) {
const instanceHeaders = {
...instance.defaults.headers.common,
...(headers as AxiosRequestHeaders),
}
Object.keys(instanceHeaders).forEach(
(key) => instanceHeaders[key] === undefined && delete instanceHeaders[key],
)
instance.defaults.headers.common = instanceHeaders
const refreshHeaders = {
...refreshInstance.defaults.headers.common,
...(headers as AxiosRequestHeaders),
}
Object.keys(refreshHeaders).forEach(
(key) => refreshHeaders[key] === undefined && delete refreshHeaders[key],
)
refreshInstance.defaults.headers.common = refreshHeaders
}
/**
* The stepUp handler takes care of the step up authentication required
* for a step up payment - adds transactionId as a header and calls the
* step up api with credentials to authenticate user
*/
async function stepUp(
username?: string,
passcode?: string,
): Promise<AxiosResponse<unknown, unknown>> {
if (!stepUpPayload.config && !stepUpPayload.transactionId) {
return Promise.reject()
}
const payload =
username && passcode
? { username, passcode }
: { refreshToken: tokens.refreshToken, authenticationMethod: 'BIOMETRIC' }
const headers = {
...(stepUpPayload?.transactionId
? { 'X-TransactionId': `${stepUpPayload.transactionId}` }
: undefined),
}
return instance.post(stepupPath, payload, { headers }).then(() =>
instance
.request(stepUpPayload.config as unknown as AxiosRequestConfig)
.finally(() => {
stepUpPayload.config = undefined
stepUpPayload.transactionId = undefined
stepUpPayload.authenticationMethods = undefined
}),
)
}
/**
* Return the API headers with API key and authorization token,
* if found
*/
function getApiHeaders() {
return {
...(apiKey ? { 'X-ApiKey': apiKey } : undefined),
...(tokens.accessToken
? { Authorization: `Bearer ${tokens.accessToken}` }
: undefined),
}
}
/**
* Add the refreshToken, updateHeaders, getApiHeaders and stepUp handlers
* to the instance and the instance to the ApiConnector on its name
*/
instance.refreshToken = refreshToken
instance.updateHeaders = updateHeaders
instance.getApiHeaders = getApiHeaders
instance.stepUp = stepUp
instances[name] = instance
return instance
}
/**
* Return an axios instance based on the `name`.
* This is a singleton. If the instance do not exists for that name, it will be created
* using the suplied config. If the instance existis and a config is supplied then
* the instance will be recreated with the new config.
* @param name the name of the connection instance.
* @param config the config for that instance.
*/
function getInstance(
name = 'default',
config?: ConnectionConfig,
): ExtendedAxiosInstance {
return config ? createInstance(name, config) : instances[name]
}
return Object.freeze({
getInstance,
})
})()
export { ApiConnector, ConnectionConfig }
/**
* Helper method to contain possible exceptions raised by Axios.
* Example of usage:
* const { response, error } = to(ApiConnector.getInstance().get('/products'))
* @param promise the axios request
* @returns an object with possible response or error
*/
export async function to<T>(promise: Promise<T>): Promise<AsyncResponse<T>> {
try {
const response = await promise
return { response }
} catch (error) {
return { error: (error as ErrorResponse)?.response?.data }
}
}
/**
* Generate an idempotencyKey from the request URL and payload. The returned string
* is a uuidv4 format.
* Inspired from cyrb53, a very fast, high quality, 53-bit hash algorithm.
* @param data request payload; string or object
* @param url request URL, string, optional
* @returns unique 32 bytes uuidv4 hash
*/
export function idempotencyKeyFrom(data: unknown, url?: string): string {
let h1 = 0xdeadbeef,
h2 = 0x41c6ce57,
h3 = 0xfeadcabe,
h4 = 0x93a5f713
const str = typeof data === 'string' ? data + url : JSON.stringify(data) + url
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
h1 = Math.imul(h1 ^ char, 2654435761)
h2 = Math.imul(h2 ^ char, 1597334677)
h3 = Math.imul(h3 ^ char, 5754853343)
h4 = Math.imul(h4 ^ char, 3367900313)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
h3 = Math.imul(h3 ^ (h3 >>> 16), 1500450271) ^ Math.imul(h4 ^ (h4 >>> 13), 9576890767)
h4 = Math.imul(h4 ^ (h4 >>> 16), 1500450271) ^ Math.imul(h3 ^ (h3 >>> 13), 9576890767)
const hash =
(h4 >>> 0).toString(16).padStart(8, '0') +
(h3 >>> 0).toString(16).padStart(8, '0') +
(h2 >>> 0).toString(16).padStart(8, '0') +
(h1 >>> 0).toString(16).padStart(8, '0')
return 'xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, (_, p) => hash[p % 32])
}
function updateResponseTime(config: ConfigMetaData) {
config.metadata = {
...config.metadata,
duration: +new Date() - ((config.metadata?.startTime as number) ?? 0),
}
}