@aircall/http
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -6,2 +6,10 @@ # Change Log | ||
## [0.4.5](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/http@0.4.4...@aircall/http@0.4.5) (2021-10-20) | ||
**Note:** Version bump only for package @aircall/http | ||
## [0.4.4](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/http@0.4.3...@aircall/http@0.4.4) (2021-10-20) | ||
@@ -8,0 +16,0 @@ |
@@ -31,2 +31,3 @@ import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
logErrorInterceptor: (error: AxiosError<any>) => Promise<AxiosError<any>>; | ||
private getHttpRetryConfig; | ||
retryStrategyInterceptor: (error: AxiosError<any>) => Promise<AxiosResponse<any>>; | ||
@@ -33,0 +34,0 @@ private handleResponse; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
const namespace = 'http-retry'; | ||
/* istanbul ignore next */ | ||
@@ -72,4 +73,4 @@ function defaultRetryCondition() { | ||
} | ||
const { onRetryAttempt = noop, retryCondition = defaultRetryCondition, maxRetry = constants_1.DEFAULT_MAX_RETRY, delay = constants_1.DEFAULT_DELAY } = retryStrategy; | ||
const { config } = error; | ||
const { onRetryAttempt = noop, retryCondition = defaultRetryCondition, maxRetry = constants_1.DEFAULT_MAX_RETRY, delay = constants_1.DEFAULT_DELAY } = this.getHttpRetryConfig(retryStrategy, config); | ||
config.retryCount = (_a = config.retryCount) !== null && _a !== void 0 ? _a : 0; | ||
@@ -125,2 +126,5 @@ const shouldRetry = retryCondition(error) && config.retryCount < maxRetry; | ||
} | ||
getHttpRetryConfig(baseConfig, axiosConfig) { | ||
return Object.assign({}, baseConfig, axiosConfig[namespace]); | ||
} | ||
get(path, config) { | ||
@@ -127,0 +131,0 @@ return this.axiosInstance.get(path, config).then(this.handleResponse); |
@@ -13,2 +13,8 @@ import { AxiosRequestConfig, AxiosError } from 'axios'; | ||
export declare type SearchParams = Record<string, Primitive | Primitive[] | null | undefined>; | ||
export interface HttpRetryConfig { | ||
maxRetry?: number; | ||
retryCondition?: <T extends HttpError>(error: T) => boolean; | ||
onRetryAttempt?: <T extends HttpError>(error: T) => Promise<any>; | ||
delay?: number; | ||
} | ||
export declare type HTTP_LOG_TYPE = 'REQUEST' | 'SUCCESS' | 'ERROR'; | ||
@@ -22,9 +28,10 @@ export declare type HttpError<T = any> = AxiosError<T>; | ||
handleError?: (error: AxiosError) => Promise<AxiosError>; | ||
retryStrategy?: { | ||
maxRetry?: number; | ||
retryCondition?: <T extends HttpError>(error: T) => boolean; | ||
onRetryAttempt?: <T extends HttpError>(error: T) => Promise<any>; | ||
delay?: number; | ||
}; | ||
retryStrategy?: HttpRetryConfig; | ||
} | ||
declare module 'axios' { | ||
interface AxiosRequestConfig { | ||
'http-retry'?: HttpRetryConfig; | ||
retryCount?: number; | ||
} | ||
} | ||
export {}; |
{ | ||
"name": "@aircall/http", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"main": "dist/index.js", | ||
@@ -14,3 +14,3 @@ "types": "dist/index.d.ts", | ||
}, | ||
"gitHead": "b0bd5427ce5c3c765f9bb4cfc4fcec5974680dfd", | ||
"gitHead": "dc06c5950d530a5afe68c03785ca08708824fa70", | ||
"dependencies": { | ||
@@ -17,0 +17,0 @@ "@aircall/logger": "^2.5.3", |
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { Logger } from '@aircall/logger'; | ||
import { HttpServiceOptions, RequestLogPayload, HTTP_LOG_TYPE } from './typing/HttpService'; | ||
import { | ||
HttpServiceOptions, | ||
RequestLogPayload, | ||
HTTP_LOG_TYPE, | ||
HttpRetryConfig | ||
} from './typing/HttpService'; | ||
import { DEFAULT_DELAY, DEFAULT_MAX_RETRY } from './constants'; | ||
import { sleep } from './utils'; | ||
const namespace = 'http-retry'; | ||
/* istanbul ignore next */ | ||
@@ -158,2 +165,9 @@ function defaultRetryCondition(): boolean { | ||
private getHttpRetryConfig( | ||
baseConfig: HttpRetryConfig, | ||
axiosConfig: AxiosRequestConfig | ||
): HttpRetryConfig { | ||
return Object.assign({}, baseConfig, axiosConfig[namespace]); | ||
} | ||
public retryStrategyInterceptor = async (error: AxiosError): Promise<AxiosResponse<any>> => { | ||
@@ -166,2 +180,3 @@ const { retryStrategy } = this.options; | ||
const { config } = error; | ||
const { | ||
@@ -172,5 +187,4 @@ onRetryAttempt = noop, | ||
delay = DEFAULT_DELAY | ||
} = retryStrategy; | ||
} = this.getHttpRetryConfig(retryStrategy, config); | ||
const { config }: { config: AxiosRequestConfig & { retryCount?: number } } = error; | ||
config.retryCount = config.retryCount ?? 0; | ||
@@ -177,0 +191,0 @@ const shouldRetry = retryCondition(error) && config.retryCount < maxRetry; |
@@ -16,2 +16,11 @@ import { AxiosRequestConfig, AxiosError } from 'axios'; | ||
export interface HttpRetryConfig { | ||
maxRetry?: number; | ||
retryCondition?: <T extends HttpError>(error: T) => boolean; | ||
onRetryAttempt?: <T extends HttpError>(error: T) => Promise<any>; | ||
// Delay between each retry. | ||
// Zero means that no delay will be applied | ||
delay?: number; | ||
} | ||
export type HTTP_LOG_TYPE = 'REQUEST' | 'SUCCESS' | 'ERROR'; | ||
@@ -25,10 +34,10 @@ export type HttpError<T = any> = AxiosError<T>; | ||
handleError?: (error: AxiosError) => Promise<AxiosError>; | ||
retryStrategy?: { | ||
maxRetry?: number; | ||
retryCondition?: <T extends HttpError>(error: T) => boolean; | ||
onRetryAttempt?: <T extends HttpError>(error: T) => Promise<any>; | ||
// Delay between each retry. | ||
// Zero means that no delay will be applied | ||
delay?: number; | ||
}; | ||
retryStrategy?: HttpRetryConfig; | ||
} | ||
declare module 'axios' { | ||
export interface AxiosRequestConfig { | ||
'http-retry'?: HttpRetryConfig; | ||
retryCount?: number; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
41743
790