flex-plugins-api-utils
Advanced tools
Comparing version 0.3.1 to 0.4.0
import { AxiosInstance } from 'axios'; | ||
interface RequestOption { | ||
cacheable?: boolean; | ||
cacheAge?: number; | ||
} | ||
export interface AuthConfig { | ||
@@ -12,2 +16,3 @@ username: string; | ||
protected readonly client: AxiosInstance; | ||
protected readonly cacheAge: number; | ||
constructor(config: HttpConfig); | ||
@@ -19,6 +24,8 @@ private static prettyPrint; | ||
private static transformResponseError; | ||
list<R>(uri: string): Promise<R[]>; | ||
get<R>(uri: string): Promise<R>; | ||
list<R>(uri: string, option?: RequestOption): Promise<R[]>; | ||
get<R>(uri: string, option?: RequestOption): Promise<R>; | ||
post<R>(uri: string, data: object): Promise<R>; | ||
delete(uri: string): Promise<void>; | ||
private getRequestOption; | ||
} | ||
export {}; |
@@ -62,2 +62,3 @@ "use strict"; | ||
var qs_1 = __importDefault(require("qs")); | ||
var axios_cache_adapter_1 = require("axios-cache-adapter"); | ||
var axios_1 = __importDefault(require("axios")); | ||
@@ -68,2 +69,4 @@ var logger_1 = __importDefault(require("./logger")); | ||
function Http(config) { | ||
this.cacheAge = 15 * 60 * 1000; | ||
var cache = axios_cache_adapter_1.setupCache({ maxAge: 0 }); | ||
this.client = axios_1.default.create({ | ||
@@ -78,2 +81,3 @@ baseURL: config.baseURL, | ||
}, | ||
adapter: cache.adapter, | ||
}); | ||
@@ -112,3 +116,7 @@ this.client.interceptors.request.use(Http.transformRequest); | ||
var data = resp.data; | ||
logger_1.default.debug("Request resulted in " + resp.status + " with data\n" + Http.prettyPrint(data)); | ||
var servedFromCache = resp.request.fromCache === true ? '(served from cache) ' : ''; | ||
var pretty = Http.prettyPrint(data); | ||
var url = resp.config.baseURL + "/" + resp.config.url; | ||
var method = resp.request.method || ''; | ||
logger_1.default.debug(method + " request to " + url + " " + servedFromCache + "responded with statusCode " + resp.status + " and data\n" + pretty + "\n"); | ||
return data; | ||
@@ -130,13 +138,13 @@ }; | ||
}; | ||
Http.prototype.list = function (uri) { | ||
Http.prototype.list = function (uri, option) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2, this.get(uri)]; | ||
return [2, this.get(uri, option)]; | ||
}); | ||
}); | ||
}; | ||
Http.prototype.get = function (uri) { | ||
Http.prototype.get = function (uri, option) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2, this.client.get(uri)]; | ||
return [2, this.client.get(uri, this.getRequestOption(option))]; | ||
}); | ||
@@ -159,2 +167,14 @@ }); | ||
}; | ||
Http.prototype.getRequestOption = function (option) { | ||
var opt = {}; | ||
if (!option) { | ||
return opt; | ||
} | ||
if (option.cacheable) { | ||
opt.cache = { | ||
maxAge: option.cacheAge || this.cacheAge, | ||
}; | ||
} | ||
return opt; | ||
}; | ||
return Http; | ||
@@ -161,0 +181,0 @@ }()); |
{ | ||
"name": "flex-plugins-api-utils", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Flex Plugins API Utils", | ||
@@ -53,2 +53,3 @@ "keywords": [ | ||
"axios": "^0.19.2", | ||
"axios-cache-adapter": "^2.5.0", | ||
"chalk": "^4.0.0", | ||
@@ -61,3 +62,3 @@ "qs": "^6.9.4", | ||
}, | ||
"gitHead": "c8acd1233b79ee5c27c2fbffb2da06ebef8fb36f" | ||
"gitHead": "7b5ef71a5953deae4234abc8ebf2c0e6bf86c6a4" | ||
} |
Sorry, the diff of this file is not supported yet
26244
394
5
+ Addedaxios-cache-adapter@^2.5.0
+ Addedaxios@0.21.4(transitive)
+ Addedaxios-cache-adapter@2.7.3(transitive)
+ Addedcache-control-esm@1.0.0(transitive)
+ Addedcharenc@0.0.2(transitive)
+ Addedcrypt@0.0.2(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedmd5@2.3.0(transitive)