@aircall/http
Advanced tools
Comparing version 1.5.1 to 1.6.1
@@ -1,3 +0,3 @@ | ||
import { AxiosRequestConfig, AxiosError, AxiosPromise, AxiosInstance, AxiosResponse } from 'axios'; | ||
export { AxiosError } from 'axios'; | ||
import { AxiosRequestConfig, AxiosError, CreateAxiosDefaults, AxiosInstance, AxiosResponse } from 'axios'; | ||
export { AxiosError, AxiosRequestConfig } from 'axios'; | ||
@@ -36,9 +36,7 @@ interface ResponseLogPayload { | ||
type HTTP_LOG_TYPE = 'REQUEST' | 'SUCCESS' | 'ERROR'; | ||
interface HttpServiceOptions { | ||
adapter?: (config: AxiosRequestConfig) => AxiosPromise; | ||
interface HttpServiceOptions extends Pick<CreateAxiosDefaults, 'adapter' | 'fetchOptions' | 'withCredentials' | 'headers'> { | ||
apiBaseUrl?: AxiosRequestConfig['baseURL']; | ||
headers?: AxiosRequestConfig['headers']; | ||
retryStrategy?: HttpRetryConfig; | ||
logger?: Logger; | ||
retryStrategy?: HttpRetryConfig; | ||
withCredentials?: AxiosRequestConfig['withCredentials']; | ||
shouldSkipLogging?: (config: AxiosRequestConfig) => boolean; | ||
} | ||
@@ -63,2 +61,3 @@ interface Logger { | ||
private logger; | ||
private shouldSkipLogging; | ||
constructor(options: HttpServiceOptions); | ||
@@ -81,3 +80,3 @@ setLogger(logger: Logger): void; | ||
*/ | ||
logErrorInterceptor: (error: AxiosError) => Promise<AxiosError>; | ||
logErrorInterceptor: (error: any) => Promise<AxiosError | void>; | ||
private getHttpRetryConfig; | ||
@@ -90,3 +89,3 @@ retryStrategyInterceptor: (error: AxiosError) => Promise<AxiosResponse<any>>; | ||
put<T = any>(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<T>; | ||
delete<T = any>(path: string, config?: AxiosRequestConfig): Promise<T>; | ||
delete<T = any>(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<T>; | ||
} | ||
@@ -93,0 +92,0 @@ |
@@ -38,13 +38,13 @@ 'use strict'; | ||
this.options = options; | ||
const { apiBaseUrl, logger, headers, adapter, withCredentials } = this.options; | ||
const { apiBaseUrl, logger, shouldSkipLogging, ...axiosOptions } = this.options; | ||
this.logger = logger || defaultLogger; | ||
this.shouldSkipLogging = shouldSkipLogging || (() => false); | ||
this.axiosInstance = axios__default.default.create({ | ||
adapter, | ||
...axiosOptions, | ||
baseURL: apiBaseUrl, | ||
headers: { | ||
...headers, | ||
...axiosOptions.headers, | ||
"Content-Type": "application/json; charset=UTF-8", | ||
Accept: "application/json, text/plain, */*" | ||
}, | ||
withCredentials | ||
} | ||
}); | ||
@@ -60,2 +60,3 @@ this.axiosInstance.interceptors.request.use(this.logRequestInterceptor); | ||
logger; | ||
shouldSkipLogging; | ||
setLogger(logger) { | ||
@@ -97,5 +98,7 @@ this.logger = logger; | ||
logRequestInterceptor = (config) => { | ||
this.logger.info(`HTTP | ${config.method} | REQUEST`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(config) | ||
}); | ||
if (!this.shouldSkipLogging(config)) { | ||
this.logger.info(`HTTP | ${config.method} | REQUEST`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(config) | ||
}); | ||
} | ||
return config; | ||
@@ -107,6 +110,8 @@ }; | ||
logResponseInterceptor = (response) => { | ||
this.logger.debug(`HTTP | ${response.config.method} | SUCCESS`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(response.config), | ||
response: _HttpService.getResponseLogPayload(response) | ||
}); | ||
if (!this.shouldSkipLogging(response.config)) { | ||
this.logger.debug(`HTTP | ${response.config.method} | SUCCESS`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(response.config), | ||
response: _HttpService.getResponseLogPayload(response) | ||
}); | ||
} | ||
return response; | ||
@@ -119,6 +124,11 @@ }; | ||
logErrorInterceptor = (error) => { | ||
this.logger.error(`HTTP | ${error.config.method} | ERROR`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(error.config), | ||
response: _HttpService.getErrorLogPayload(error) | ||
}); | ||
if (axios.isAxiosError(error) && axios.isCancel(error)) { | ||
return Promise.resolve(); | ||
} | ||
if (axios.isAxiosError(error) && error.config && !this.shouldSkipLogging(error.config)) { | ||
this.logger.error(`HTTP | ${error.config.method} | ERROR`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(error.config), | ||
response: _HttpService.getErrorLogPayload(error) | ||
}); | ||
} | ||
return Promise.reject(error); | ||
@@ -131,3 +141,3 @@ }; | ||
const { retryStrategy } = this.options; | ||
if (!retryStrategy) { | ||
if (!retryStrategy || !axios.isAxiosError(error) || !error.config) { | ||
return Promise.reject(error); | ||
@@ -174,4 +184,4 @@ } | ||
} | ||
async delete(path, config) { | ||
return this.axiosInstance.delete(path, config).then(this.handleResponse); | ||
async delete(path, payload = {}, config) { | ||
return this.axiosInstance.delete(path, { ...config, data: payload }).then(this.handleResponse); | ||
} | ||
@@ -178,0 +188,0 @@ }; |
{ | ||
"name": "@aircall/http", | ||
"version": "1.5.1", | ||
"version": "1.6.1", | ||
"main": "dist/index.js", | ||
@@ -23,11 +23,11 @@ "module": "dist/index.mjs", | ||
"path": "dist/index.mjs", | ||
"limit": "15 KB" | ||
"limit": "20 KB" | ||
} | ||
], | ||
"dependencies": { | ||
"axios": "^0.27.2" | ||
"axios": "1.7.3" | ||
}, | ||
"devDependencies": { | ||
"@aircall/test-sequencer": "1.0.0", | ||
"@aircall/tsconfig": "1.4.0", | ||
"@aircall/test-sequencer": "1.0.2", | ||
"@aircall/tsconfig": "1.4.2", | ||
"@size-limit/preset-small-lib": "8.1.0", | ||
@@ -46,7 +46,6 @@ "@types/jest": "29.5.2", | ||
"dev": "pnpm build --watch", | ||
"posttest": "pnpm run size", | ||
"size": "size-limit", | ||
"test:ci": "pnpm run test --testSequencer @aircall/test-sequencer/parallel-ci-sequencer.js --passWithNoTests", | ||
"test:ci": "pnpm run test --shard ${CI_NODE_INDEX}/${CI_NODE_TOTAL} --passWithNoTests --silent", | ||
"test": "jest --passWithNoTests" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20510
442
+ Addedaxios@1.7.3(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
- Removedaxios@0.27.2(transitive)
Updatedaxios@1.7.3