UNPKG

egg

Version:

A web application framework for Node.js

43 lines (42 loc) • 1.33 kB
import { ms } from "humanize-ms"; import { HttpClient as HttpClient$1 } from "urllib"; //#region src/lib/core/httpclient.ts var HttpClient = class extends HttpClient$1 { #app; constructor(app, options = {}) { normalizeConfig(app); const config = app.config.httpclient || {}; options.lookup = options.lookup ?? config.lookup; const initOptions = { ...options, defaultArgs: { ...config.request, ...options.defaultArgs } }; super(initOptions); this.#app = app; if (config.interceptors?.length) { const originalDispatcher = this.getDispatcher(); this.setDispatcher(originalDispatcher.compose((dispatch) => (opts, handler) => { const { dispatcher: _dispatcher, ...dispatchOptions } = opts; return dispatch(dispatchOptions, handler); }, ...config.interceptors)); } } async request(url, options) { options = options ?? {}; if (options.ctx?.tracer) options.tracer = options.ctx.tracer; else options.tracer = options.tracer ?? this.#app.tracer; return await super.request(url, options); } async curl(url, options) { return await this.request(url, options); } }; function normalizeConfig(app) { const config = app.config.httpclient; if (typeof config.request?.timeout === "string") config.request.timeout = ms(config.request.timeout); } //#endregion export { HttpClient };