egg/lib/core/httpclient_next.js
fengmk2 610a39e7f4
👌 IMPROVE: Drop httpclient callback and thunk style (#5052)
And remove mz and mz-modules deps
2022-10-31 09:57:50 +08:00

38 lines
874 B
JavaScript

const { HttpClient } = require('urllib-next');
const ms = require('humanize-ms');
class HttpClientNext extends HttpClient {
constructor(app) {
normalizeConfig(app);
const config = app.config.httpclient;
super({
app,
defaultArgs: config.request,
});
this.app = app;
}
async request(url, options) {
options = options || {};
if (options.ctx && options.ctx.tracer) {
options.tracer = options.ctx.tracer;
} else {
options.tracer = options.tracer || this.app.tracer;
}
return await super.request(url, options);
}
async curl(...args) {
return await this.request(...args);
}
}
function normalizeConfig(app) {
const config = app.config.httpclient;
if (typeof config.request.timeout === 'string') {
config.request.timeout = ms(config.request.timeout);
}
}
module.exports = HttpClientNext;