@alphatango/httpclient
Advanced tools
Comparing version 0.0.6 to 0.0.7
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { Options as CacheOptions } from 'axios-extensions/lib/cacheAdapterEnhancer'; | ||
import { Options as RetryOptions } from 'axios-extensions/lib/retryAdapterEnhancer'; | ||
export default class HttpClient { | ||
@@ -6,2 +8,4 @@ private readonly logFunction; | ||
private readonly client; | ||
private readonly enableRetry; | ||
private readonly enableCache; | ||
/** | ||
@@ -45,5 +49,32 @@ * Create a new Instance of the HttpClient | ||
export interface HttpClientOptions { | ||
/** | ||
* An Axios Instance (disables caching and retrying) | ||
*/ | ||
client?: AxiosInstance; | ||
/** | ||
* A function that returns a bearer token | ||
*/ | ||
tokenResolver?: () => Promise<string>; | ||
/** | ||
* Logger function | ||
*/ | ||
logFunction?: (...msg: any[]) => void; | ||
/** | ||
* enable caching (false by default) | ||
*/ | ||
enableCache?: boolean; | ||
/** | ||
* Cache options | ||
* @link https://github.com/kuitos/axios-extensions#cacheadapterenhancer | ||
*/ | ||
cacheOptions?: CacheOptions; | ||
/** | ||
* Enable automatic retries | ||
*/ | ||
enableRetry?: boolean; | ||
/** | ||
* Retry options | ||
* @link https://github.com/kuitos/axios-extensions#cacheadapterenhancer | ||
*/ | ||
retryOptions?: RetryOptions; | ||
} |
@@ -28,2 +28,4 @@ "use strict"; | ||
const uuid = __importStar(require("uuid")); | ||
const cacheAdapterEnhancer_1 = __importDefault(require("axios-extensions/lib/cacheAdapterEnhancer")); | ||
const retryAdapterEnhancer_1 = __importDefault(require("axios-extensions/lib/retryAdapterEnhancer")); | ||
const invalidToken = 'Invalid token'; | ||
@@ -36,6 +38,19 @@ let requestId = ''; | ||
constructor(options) { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
this.logFunction = (_a = options === null || options === void 0 ? void 0 : options.logFunction) !== null && _a !== void 0 ? _a : console.log; | ||
this.tokenResolverFunction = options === null || options === void 0 ? void 0 : options.tokenResolver; | ||
this.client = (_b = options === null || options === void 0 ? void 0 : options.client) !== null && _b !== void 0 ? _b : axios_1.default.create(); | ||
this.enableCache = (_b = options === null || options === void 0 ? void 0 : options.enableCache) !== null && _b !== void 0 ? _b : false; | ||
this.enableRetry = (_c = options === null || options === void 0 ? void 0 : options.enableRetry) !== null && _c !== void 0 ? _c : false; | ||
this.client = (_d = options === null || options === void 0 ? void 0 : options.client) !== null && _d !== void 0 ? _d : axios_1.default.create({ | ||
adapter: (() => { | ||
let adapters = axios_1.default.defaults.adapter; | ||
if (this.enableCache) { | ||
adapters = cacheAdapterEnhancer_1.default(adapters, options === null || options === void 0 ? void 0 : options.cacheOptions); | ||
} | ||
if (this.enableRetry) { | ||
adapters = retryAdapterEnhancer_1.default(adapters, options === null || options === void 0 ? void 0 : options.retryOptions); | ||
} | ||
return adapters; | ||
})(), | ||
}); | ||
this.client.interceptors.request.use((config) => { | ||
@@ -108,8 +123,7 @@ requestId = uuid.v4(); | ||
async get(url, config = { responseType: 'json' }) { | ||
config = { | ||
return this.client.get(url, { | ||
responseType: 'json', | ||
...config, | ||
headers: await this.createHeadersWithResolvedToken(config.headers), | ||
}; | ||
return this.client.get(url, config); | ||
}); | ||
} | ||
@@ -116,0 +130,0 @@ /** |
{ | ||
"name": "@alphatango/httpclient", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A wrapper around axios with retry and caching logic, bearer token injection through custom resolvers and extended logging", | ||
@@ -27,3 +27,4 @@ "main": "lib/httpClient.js", | ||
"dependencies": { | ||
"axios": "^0.19.0", | ||
"axios": "0.19.2", | ||
"axios-extensions": "^3.1.3", | ||
"uuid": "^8.3.0" | ||
@@ -30,0 +31,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15352
249
4
+ Addedaxios-extensions@^3.1.3
+ Addedaxios-extensions@3.1.7(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addedtslib@2.8.1(transitive)
Updatedaxios@0.19.2