@cordisjs/plugin-http
Advanced tools
+41
-41
@@ -7,21 +7,21 @@ import { Context, Service } from 'cordis'; | ||
| interface Context { | ||
| http: HTTP; | ||
| http: Http; | ||
| } | ||
| interface Intercept { | ||
| http: HTTP.Intercept; | ||
| http: Http.Intercept; | ||
| } | ||
| interface Events { | ||
| 'http/fetch'(this: HTTP, url: URL, init: RequestInit, config: HTTP.Config, next: () => Promise<Response>): Promise<Response>; | ||
| 'http/websocket'(this: HTTP, url: URL, init: WebSocketInit, config: HTTP.Config, next: () => WebSocket): WebSocket; | ||
| 'http/fetch'(this: Http, url: URL, init: RequestInit, config: Http.Config, next: () => Promise<Response>): Promise<Response>; | ||
| 'http/websocket'(this: Http, url: URL, init: WebSocketInit, config: Http.Config, next: () => WebSocket): WebSocket; | ||
| } | ||
| } | ||
| declare const kHTTPError: unique symbol; | ||
| declare class HTTPError extends Error { | ||
| code?: HTTP.Error.Code | undefined; | ||
| declare const kHttpError: unique symbol; | ||
| declare class HttpError extends Error { | ||
| code?: Http.Error.Code | undefined; | ||
| response?: Response | undefined; | ||
| [kHTTPError]: boolean; | ||
| static is(error: any): error is HTTPError; | ||
| constructor(message?: string, code?: HTTP.Error.Code | undefined, response?: Response | undefined); | ||
| [kHttpError]: boolean; | ||
| static is(error: any): error is HttpError; | ||
| constructor(message?: string, code?: Http.Error.Code | undefined, response?: Response | undefined); | ||
| } | ||
| export declare namespace HTTP { | ||
| export declare namespace Http { | ||
| interface ResponseTypes { | ||
@@ -37,18 +37,18 @@ json: any; | ||
| interface Request1 { | ||
| <K extends keyof ResponseTypes>(url: string | URL, config: HTTP.RequestConfig & { | ||
| <K extends keyof ResponseTypes>(url: string | URL, config: Http.RequestConfig & { | ||
| responseType: K; | ||
| }): Promise<ResponseTypes[K]>; | ||
| <T = any>(url: string | URL, config: HTTP.RequestConfig & { | ||
| <T = any>(url: string | URL, config: Http.RequestConfig & { | ||
| responseType: Decoder<T>; | ||
| }): Promise<T>; | ||
| <T = any>(url: string | URL, config?: HTTP.RequestConfig): Promise<T>; | ||
| <T = any>(url: string | URL, config?: Http.RequestConfig): Promise<T>; | ||
| } | ||
| interface Request2 { | ||
| <K extends keyof ResponseTypes>(url: string | URL, data: any, config: HTTP.RequestConfig & { | ||
| <K extends keyof ResponseTypes>(url: string | URL, data: any, config: Http.RequestConfig & { | ||
| responseType: K; | ||
| }): Promise<ResponseTypes[K]>; | ||
| <T = any>(url: string | URL, data: any, config: HTTP.RequestConfig & { | ||
| <T = any>(url: string | URL, data: any, config: Http.RequestConfig & { | ||
| responseType: Decoder<T>; | ||
| }): Promise<T>; | ||
| <T = any>(url: string | URL, data?: any, config?: HTTP.RequestConfig): Promise<T>; | ||
| <T = any>(url: string | URL, data?: any, config?: Http.RequestConfig): Promise<T>; | ||
| } | ||
@@ -88,3 +88,3 @@ interface Intercept { | ||
| type Decoder<T = any> = (raw: globalThis.Response) => Awaitable<T>; | ||
| type Error = HTTPError; | ||
| type Error = HttpError; | ||
| namespace Error { | ||
@@ -97,23 +97,23 @@ type Code = 'TIMEOUT' | 'STATUS_ERROR'; | ||
| } | ||
| export interface HTTP { | ||
| (url: string | URL, config?: HTTP.RequestConfig): Promise<Response>; | ||
| config: HTTP.Config; | ||
| get: HTTP.Request1; | ||
| delete: HTTP.Request1; | ||
| patch: HTTP.Request2; | ||
| post: HTTP.Request2; | ||
| put: HTTP.Request2; | ||
| export interface Http { | ||
| (url: string | URL, config?: Http.RequestConfig): Promise<Response>; | ||
| config: Http.Config; | ||
| get: Http.Request1; | ||
| delete: Http.Request1; | ||
| patch: Http.Request2; | ||
| post: Http.Request2; | ||
| put: Http.Request2; | ||
| } | ||
| export declare class HTTP extends Service<HTTP.Intercept> { | ||
| config: HTTP.Config; | ||
| static Error: typeof HTTPError; | ||
| export declare class Http extends Service<Http.Intercept> { | ||
| config: Http.Config; | ||
| static Error: typeof HttpError; | ||
| static undici: typeof import('undici'); | ||
| static Config: z<HTTP.Config>; | ||
| Config: z<HTTP.Config>; | ||
| isError: typeof HTTPError.is; | ||
| static Config: z<Http.Config>; | ||
| Config: z<Http.Config>; | ||
| isError: typeof HttpError.is; | ||
| private _decoders; | ||
| private _proxies; | ||
| constructor(ctx: Context, config?: HTTP.Config); | ||
| constructor(ctx: Context, config?: Http.Config); | ||
| get undici(): typeof import("undici"); | ||
| static mergeConfig: (target: HTTP.Config, source?: HTTP.Config) => { | ||
| static mergeConfig: (target: Http.Config, source?: Http.Config) => { | ||
| headers: { | ||
@@ -126,13 +126,13 @@ [x: string]: any; | ||
| }; | ||
| decoder<K extends keyof HTTP.ResponseTypes>(type: K, decoder: HTTP.Decoder<HTTP.ResponseTypes[K]>): import("cordis").Disposable<Promise<void>>; | ||
| decoder<K extends keyof Http.ResponseTypes>(type: K, decoder: Http.Decoder<Http.ResponseTypes[K]>): import("cordis").Disposable<Promise<void>>; | ||
| proxy(name: string[], factory: (url: URL) => Dispatcher): import("cordis").Disposable<Promise<void>>; | ||
| extend(config?: HTTP.Config): any; | ||
| resolveConfig(init?: HTTP.RequestConfig): HTTP.RequestConfig; | ||
| resolveURL(url: string | URL, config: HTTP.RequestConfig, isWebSocket?: boolean): URL; | ||
| extend(config?: Http.Config): any; | ||
| resolveConfig(init?: Http.RequestConfig): Http.RequestConfig; | ||
| resolveURL(url: string | URL, config: Http.RequestConfig, isWebSocket?: boolean): URL; | ||
| defaultDecoder(response: Response): Promise<any>; | ||
| [Service.invoke](...args: any[]): Promise<Response>; | ||
| private _decode; | ||
| head(url: string | URL, config?: HTTP.RequestConfig): Promise<any>; | ||
| ws(url: string | URL, _config?: HTTP.Config): WebSocket; | ||
| head(url: string | URL, config?: Http.RequestConfig): Promise<any>; | ||
| ws(url: string | URL, _config?: Http.Config): WebSocket; | ||
| } | ||
| export default HTTP; | ||
| export default Http; |
+32
-28
@@ -55,5 +55,5 @@ var __create = Object.create; | ||
| import z from "schemastery"; | ||
| var kHTTPError = /* @__PURE__ */ Symbol.for("cordis.http.error"); | ||
| var kHTTPConfig = /* @__PURE__ */ Symbol.for("cordis.http.config"); | ||
| var HTTPError = class extends Error { | ||
| var kHttpError = /* @__PURE__ */ Symbol.for("cordis.http.error"); | ||
| var kHttpConfig = /* @__PURE__ */ Symbol.for("cordis.http.config"); | ||
| var HttpError = class extends Error { | ||
| constructor(message, code, response) { | ||
@@ -67,7 +67,7 @@ super(message); | ||
| static { | ||
| __name(this, "HTTPError"); | ||
| __name(this, "HttpError"); | ||
| } | ||
| [kHTTPError] = true; | ||
| [kHttpError] = true; | ||
| static is(error) { | ||
| return !!error?.[kHTTPError]; | ||
| return !!error?.[kHttpError]; | ||
| } | ||
@@ -86,5 +86,5 @@ }; | ||
| var validateStatus = /* @__PURE__ */ __name((status) => status < 400, "validateStatus"); | ||
| var _HTTP_decorators, _init, _a; | ||
| _HTTP_decorators = [Inject("logger", false)]; | ||
| var _HTTP = class _HTTP extends (_a = Service) { | ||
| var _Http_decorators, _init, _a; | ||
| _Http_decorators = [Inject("logger")]; | ||
| var _Http = class _Http extends (_a = Service) { | ||
| constructor(ctx, config = {}) { | ||
@@ -110,3 +110,3 @@ super(ctx, "http"); | ||
| download: true, | ||
| onError: ctx.logger?.error | ||
| onError: ctx.logger.error | ||
| }); | ||
@@ -137,5 +137,5 @@ }, { prepend: true }); | ||
| static { | ||
| __name(this, "HTTP"); | ||
| __name(this, "Http"); | ||
| } | ||
| static Error = HTTPError; | ||
| static Error = HttpError; | ||
| static undici; | ||
@@ -153,3 +153,3 @@ static { | ||
| for (const method of ["get", "delete"]) { | ||
| defineProperty(_HTTP.prototype, method, async function(url, config) { | ||
| defineProperty(_Http.prototype, method, async function(url, config) { | ||
| const response = await this(url, { method, validateStatus, ...config }); | ||
@@ -160,3 +160,3 @@ return this._decode(response); | ||
| for (const method of ["patch", "post", "put"]) { | ||
| defineProperty(_HTTP.prototype, method, async function(url, data, config) { | ||
| defineProperty(_Http.prototype, method, async function(url, data, config) { | ||
| const response = await this(url, { method, data, validateStatus, ...config }); | ||
@@ -178,7 +178,7 @@ return this._decode(response); | ||
| }); | ||
| isError = HTTPError.is; | ||
| isError = HttpError.is; | ||
| _decoders = /* @__PURE__ */ Object.create(null); | ||
| _proxies = /* @__PURE__ */ Object.create(null); | ||
| get undici() { | ||
| if (_HTTP.undici) return _HTTP.undici; | ||
| if (_Http.undici) return _Http.undici; | ||
| throw new Error("please install `undici`"); | ||
@@ -214,3 +214,3 @@ } | ||
| return this[Service.extend]({ | ||
| config: _HTTP.mergeConfig(this.config, config) | ||
| config: _Http.mergeConfig(this.config, config) | ||
| }); | ||
@@ -263,3 +263,3 @@ } | ||
| const timer = config.timeout && setTimeout(() => { | ||
| controller.abort(new HTTPError("request timeout", "TIMEOUT")); | ||
| controller.abort(new HttpError("request timeout", "TIMEOUT")); | ||
| }, config.timeout); | ||
@@ -298,10 +298,13 @@ return () => { | ||
| const response = await this.ctx.waterfall(this, "http/fetch", url, init, config, () => { | ||
| this.ctx.logger("http:request").debug("%c %s", method, url.href); | ||
| return this.undici.fetch(url, init); | ||
| }).catch((cause) => { | ||
| if (_HTTP.Error.is(cause)) throw cause; | ||
| const error = new _HTTP.Error(`fetch ${url} failed`); | ||
| this.ctx.logger("http:request").debug("%c %s failed: %o", method, url.href, cause); | ||
| if (_Http.Error.is(cause)) throw cause; | ||
| const error = new _Http.Error(`fetch ${url} failed`); | ||
| error.cause = cause; | ||
| throw error; | ||
| }); | ||
| response[kHTTPConfig] = config; | ||
| this.ctx.logger("http:response").debug("%c %s %s %s", method, url.href, response.status, response.statusText); | ||
| response[kHttpConfig] = config; | ||
| return response; | ||
@@ -313,6 +316,6 @@ } finally { | ||
| async _decode(response) { | ||
| const config = response[kHTTPConfig]; | ||
| const config = response[kHttpConfig]; | ||
| const validateStatus2 = config.validateStatus ?? (() => true); | ||
| if (!validateStatus2(response.status)) { | ||
| throw new _HTTP.Error(response.statusText, "STATUS_ERROR", response); | ||
| throw new _Http.Error(response.statusText, "STATUS_ERROR", response); | ||
| } | ||
@@ -351,2 +354,3 @@ if (!config.responseType) { | ||
| const socket = this.ctx.waterfall(this, "http/websocket", url, init, config, () => { | ||
| this.ctx.logger("http:ws").debug("connect %s", url.href); | ||
| return new this.undici.WebSocket(url, init); | ||
@@ -364,9 +368,9 @@ }); | ||
| _init = __decoratorStart(_a); | ||
| _HTTP = __decorateElement(_init, 0, "HTTP", _HTTP_decorators, _HTTP); | ||
| __runInitializers(_init, 1, _HTTP); | ||
| var HTTP = _HTTP; | ||
| var index_default = HTTP; | ||
| _Http = __decorateElement(_init, 0, "Http", _Http_decorators, _Http); | ||
| __runInitializers(_init, 1, _Http); | ||
| var Http = _Http; | ||
| var index_default = Http; | ||
| export { | ||
| HTTP, | ||
| Http, | ||
| index_default as default | ||
| }; |
+4
-4
| { | ||
| "name": "@cordisjs/plugin-http", | ||
| "description": "Fetch-based axios-style HTTP client", | ||
| "version": "1.4.0", | ||
| "version": "1.5.0", | ||
| "type": "module", | ||
@@ -41,4 +41,4 @@ "main": "lib/index.js", | ||
| "peerDependencies": { | ||
| "cordis": "^4.0.0-rc.3", | ||
| "undici": "^8.1.0" | ||
| "cordis": "^4.0.0-rc.4", | ||
| "undici": "*" | ||
| }, | ||
@@ -52,3 +52,3 @@ "peerDependenciesMeta": { | ||
| "@cordisjs/plugin-logger": "^1.0.2", | ||
| "cordis": "^4.0.0-rc.3", | ||
| "cordis": "^4.0.0-rc.4", | ||
| "undici": "^8.1.0" | ||
@@ -55,0 +55,0 @@ }, |
24526
1.38%494
0.82%