@aircall/http
Advanced tools
Comparing version 1.3.4 to 1.4.1
@@ -1,5 +0,98 @@ | ||
export { HttpService } from './HttpService'; | ||
export type { AxiosError } from 'axios'; | ||
export * from './constants'; | ||
export * from './typing'; | ||
//# sourceMappingURL=index.d.ts.map | ||
import { AxiosRequestConfig, AxiosError, AxiosPromise, AxiosInstance, AxiosResponse } from 'axios'; | ||
export { AxiosError } from 'axios'; | ||
interface ResponseLogPayload { | ||
data: any; | ||
statusCode: number; | ||
headers: any; | ||
} | ||
interface RequestLogPayload { | ||
request_base_url: AxiosRequestConfig['baseURL']; | ||
request_body: AxiosRequestConfig['data']; | ||
request_headers: AxiosRequestConfig['headers']; | ||
request_method: AxiosRequestConfig['method']; | ||
request_params: AxiosRequestConfig['params']; | ||
request_path: AxiosRequestConfig['url']; | ||
retry_count: AxiosRequestConfig['retryCount']; | ||
} | ||
interface ErrorLogPayload { | ||
code?: string; | ||
config: AxiosError['config']; | ||
message: string; | ||
name: string; | ||
stack?: string; | ||
statusCode?: number; | ||
data?: any; | ||
} | ||
type Primitive = string | number | boolean; | ||
type SearchParams = Record<string, Primitive | Primitive[] | null | undefined>; | ||
interface HttpRetryConfig { | ||
maxRetry?: number; | ||
retryCondition?: <T extends AxiosError>(error: T) => boolean; | ||
onRetryAttempt?: <T extends AxiosError>(error: T) => Promise<any> | any; | ||
delay?: number | ((retryCount: number) => number); | ||
} | ||
type HTTP_LOG_TYPE = 'REQUEST' | 'SUCCESS' | 'ERROR'; | ||
interface HttpServiceOptions { | ||
adapter?: (config: AxiosRequestConfig) => AxiosPromise; | ||
apiBaseUrl?: AxiosRequestConfig['baseURL']; | ||
headers?: AxiosRequestConfig['headers']; | ||
logger?: Logger; | ||
retryStrategy?: HttpRetryConfig; | ||
withCredentials?: AxiosRequestConfig['withCredentials']; | ||
} | ||
interface Logger { | ||
info: LogFunction; | ||
debug: LogFunction; | ||
warn: LogFunction; | ||
error: LogFunction; | ||
} | ||
type LogFunction = (...args: any[]) => void; | ||
declare module 'axios' { | ||
interface AxiosRequestConfig { | ||
'http-retry'?: HttpRetryConfig; | ||
retryCount?: number; | ||
} | ||
} | ||
declare class HttpService { | ||
private options; | ||
axiosInstance: AxiosInstance; | ||
private logger; | ||
constructor(options: HttpServiceOptions); | ||
setLogger(logger: Logger): void; | ||
static getResponseLogPayload: (response: AxiosResponse) => ResponseLogPayload; | ||
static getRequestLogPayload: (config: AxiosRequestConfig) => RequestLogPayload; | ||
static getErrorLogPayload: (error: AxiosError) => ErrorLogPayload; | ||
/** | ||
* Interceptor sending a logging action for each request. | ||
*/ | ||
private logRequestInterceptor; | ||
/** | ||
* Interceptor sending a logging action for each received response. | ||
*/ | ||
private logResponseInterceptor; | ||
/** | ||
* Interceptor sending a logging action for each received error. | ||
* @todo check this later | ||
*/ | ||
logErrorInterceptor: (error: AxiosError) => Promise<AxiosError>; | ||
private getHttpRetryConfig; | ||
retryStrategyInterceptor: (error: AxiosError) => Promise<AxiosResponse<any>>; | ||
private handleResponse; | ||
get<T = any>(path: string, config?: AxiosRequestConfig): Promise<T>; | ||
post<T = any>(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<T>; | ||
patch<T = any>(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<T>; | ||
put<T = any>(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<T>; | ||
delete<T = any>(path: string, config?: AxiosRequestConfig): Promise<T>; | ||
} | ||
declare enum ErrorCode { | ||
INVALID_API_RESPONSE_CODE = 600, | ||
UNAUTHORIZED_API_RESPONSE_CODE = 401 | ||
} | ||
declare const INVALID_API_RESPONSE_MESSAGE = "An internal error occured"; | ||
declare const DEFAULT_MAX_RETRY = 3; | ||
declare const DEFAULT_DELAY = 300; | ||
export { DEFAULT_DELAY, DEFAULT_MAX_RETRY, ErrorCode, ErrorLogPayload, HTTP_LOG_TYPE, HttpRetryConfig, HttpService, HttpServiceOptions, INVALID_API_RESPONSE_MESSAGE, LogFunction, Logger, RequestLogPayload, ResponseLogPayload, SearchParams }; |
@@ -0,8 +1,183 @@ | ||
'use strict'; | ||
'use strict' | ||
var axios = require('axios'); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./http.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./http.cjs.development.js') | ||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var axios__default = /*#__PURE__*/_interopDefault(axios); | ||
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
// src/constants.ts | ||
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => { | ||
ErrorCode2[ErrorCode2["INVALID_API_RESPONSE_CODE"] = 600] = "INVALID_API_RESPONSE_CODE"; | ||
ErrorCode2[ErrorCode2["UNAUTHORIZED_API_RESPONSE_CODE"] = 401] = "UNAUTHORIZED_API_RESPONSE_CODE"; | ||
return ErrorCode2; | ||
})(ErrorCode || {}); | ||
var INVALID_API_RESPONSE_MESSAGE = "An internal error occured"; | ||
var DEFAULT_MAX_RETRY = 3; | ||
var DEFAULT_DELAY = 300; | ||
// src/logger.ts | ||
var defaultLogger = console; | ||
// src/utils.ts | ||
var sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay)); | ||
// src/HttpService.ts | ||
var namespace = "http-retry"; | ||
function defaultRetryCondition() { | ||
return false; | ||
} | ||
function noop() { | ||
return; | ||
} | ||
var _HttpService = class { | ||
constructor(options) { | ||
this.options = options; | ||
const { apiBaseUrl, logger, headers, adapter, withCredentials } = this.options; | ||
this.logger = logger || defaultLogger; | ||
this.axiosInstance = axios__default.default.create({ | ||
adapter, | ||
baseURL: apiBaseUrl, | ||
headers: { | ||
...headers, | ||
"Content-Type": "application/json; charset=UTF-8", | ||
Accept: "application/json, text/plain, */*" | ||
}, | ||
withCredentials | ||
}); | ||
this.axiosInstance.interceptors.request.use(this.logRequestInterceptor); | ||
this.axiosInstance.interceptors.response.use(void 0, this.retryStrategyInterceptor); | ||
this.axiosInstance.interceptors.response.use( | ||
this.logResponseInterceptor, | ||
this.logErrorInterceptor | ||
); | ||
} | ||
axiosInstance; | ||
logger; | ||
setLogger(logger) { | ||
this.logger = logger; | ||
} | ||
/** | ||
* Interceptor sending a logging action for each request. | ||
*/ | ||
logRequestInterceptor = (config) => { | ||
this.logger.info(`HTTP | ${config.method} | REQUEST`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(config) | ||
}); | ||
return config; | ||
}; | ||
/** | ||
* Interceptor sending a logging action for each received response. | ||
*/ | ||
logResponseInterceptor = (response) => { | ||
this.logger.debug(`HTTP | ${response.config.method} | SUCCESS`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(response.config), | ||
response: _HttpService.getResponseLogPayload(response) | ||
}); | ||
return response; | ||
}; | ||
/** | ||
* Interceptor sending a logging action for each received error. | ||
* @todo check this later | ||
*/ | ||
logErrorInterceptor = (error) => { | ||
this.logger.error(`HTTP | ${error.config.method} | ERROR`.toUpperCase(), { | ||
request: _HttpService.getRequestLogPayload(error.config), | ||
response: _HttpService.getErrorLogPayload(error) | ||
}); | ||
return Promise.reject(error); | ||
}; | ||
getHttpRetryConfig(baseConfig, axiosConfig) { | ||
return Object.assign({}, baseConfig, axiosConfig[namespace]); | ||
} | ||
retryStrategyInterceptor = async (error) => { | ||
const { retryStrategy } = this.options; | ||
if (!retryStrategy) { | ||
return Promise.reject(error); | ||
} | ||
const { config } = error; | ||
const { | ||
onRetryAttempt = noop, | ||
retryCondition = defaultRetryCondition, | ||
maxRetry = DEFAULT_MAX_RETRY, | ||
delay = DEFAULT_DELAY | ||
} = this.getHttpRetryConfig(retryStrategy, config); | ||
config.retryCount = config.retryCount ?? 0; | ||
const shouldRetry = retryCondition(error) && config.retryCount < maxRetry; | ||
const delayDuration = typeof delay === "function" ? delay(config.retryCount) : delay; | ||
if (!shouldRetry) { | ||
return Promise.reject(error); | ||
} | ||
config.retryCount++; | ||
if (delayDuration > 0) { | ||
await sleep(delayDuration); | ||
} | ||
try { | ||
await onRetryAttempt(error); | ||
return await this.axiosInstance.request(error.config); | ||
} catch { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
handleResponse = (response) => { | ||
return response.data; | ||
}; | ||
async get(path, config) { | ||
return this.axiosInstance.get(path, config).then(this.handleResponse); | ||
} | ||
async post(path, payload = {}, config) { | ||
return this.axiosInstance.post(path, payload, config).then(this.handleResponse); | ||
} | ||
async patch(path, payload = {}, config) { | ||
return this.axiosInstance.patch(path, payload, config).then(this.handleResponse); | ||
} | ||
async put(path, payload = {}, config) { | ||
return this.axiosInstance.put(path, payload, config).then(this.handleResponse); | ||
} | ||
async delete(path, config) { | ||
return this.axiosInstance.delete(path, config).then(this.handleResponse); | ||
} | ||
}; | ||
var HttpService = _HttpService; | ||
__publicField(HttpService, "getResponseLogPayload", (response) => { | ||
return { | ||
data: response.data, | ||
statusCode: response.status, | ||
headers: response.headers | ||
}; | ||
}); | ||
__publicField(HttpService, "getRequestLogPayload", (config) => { | ||
return { | ||
request_base_url: config.baseURL, | ||
request_body: config.data, | ||
request_headers: config.headers, | ||
request_method: config.method, | ||
request_params: config.params, | ||
request_path: config.url, | ||
retry_count: config.retryCount | ||
}; | ||
}); | ||
__publicField(HttpService, "getErrorLogPayload", (error) => { | ||
return { | ||
code: error.code, | ||
config: error.config, | ||
message: error.message, | ||
name: error.name, | ||
stack: error.stack, | ||
statusCode: error.response?.status, | ||
data: error.response?.data | ||
}; | ||
}); | ||
exports.DEFAULT_DELAY = DEFAULT_DELAY; | ||
exports.DEFAULT_MAX_RETRY = DEFAULT_MAX_RETRY; | ||
exports.ErrorCode = ErrorCode; | ||
exports.HttpService = HttpService; | ||
exports.INVALID_API_RESPONSE_MESSAGE = INVALID_API_RESPONSE_MESSAGE; |
{ | ||
"name": "@aircall/http", | ||
"version": "1.3.4", | ||
"version": "1.4.1", | ||
"main": "dist/index.js", | ||
"module": "dist/http.esm.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
@@ -17,14 +17,9 @@ "sideEffects": false, | ||
], | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"size-limit": [ | ||
{ | ||
"path": "dist/http.cjs.production.min.js", | ||
"path": "dist/index.js", | ||
"limit": "24 KB" | ||
}, | ||
{ | ||
"path": "dist/http.esm.js", | ||
"path": "dist/index.mjs", | ||
"limit": "15 KB" | ||
@@ -37,17 +32,18 @@ } | ||
"devDependencies": { | ||
"@types/jest": "29", | ||
"@types/jest": "29.5.2", | ||
"jest": "29.5.0", | ||
"ts-jest": "29.1.0", | ||
"axios-mock-adapter": "1.21.1", | ||
"@size-limit/preset-small-lib": "8.1.0", | ||
"eslint": "8.30.0", | ||
"@aircall/eslint-config": "1.2.4", | ||
"husky": "8.0.1", | ||
"@aircall/eslint-config": "1.2.5", | ||
"size-limit": "8.1.0", | ||
"@aircall/tsconfig": "1.2.2", | ||
"tsdx": "0.14.1", | ||
"@aircall/tsconfig": "1.3.1", | ||
"tsup": "6.7.0", | ||
"tslib": "2.4.0" | ||
}, | ||
"scripts": { | ||
"dev": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test --passWithNoTests", | ||
"dev": "pnpm build --watch", | ||
"build": "tsup --config ../../tsup.config.ts", | ||
"test": "jest --passWithNoTests", | ||
"posttest": "pnpm run size", | ||
@@ -54,0 +50,0 @@ "lint": "eslint --ext ts src", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
1
10711
11
3
268