diff --git a/packages/core/package.json b/packages/core/package.json index 978136c..d6c4e10 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,7 +1,7 @@ { "name": "@cordisjs/plugin-http", "description": "Fetch-based axios-style HTTP client", - "version": "0.6.0", + "version": "0.6.2", "type": "module", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 7c3ea49..416de96 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -21,6 +21,7 @@ declare module 'cordis' { 'http/file'(this: HTTP, url: string, options: FileOptions): Awaitable 'http/config'(this: HTTP, config: HTTP.Config): void 'http/fetch-init'(this: HTTP, url: URL, init: RequestInit, config: HTTP.Config): void + 'http/after-fetch'(this: HTTP, data: HTTP.AfterFetch): void 'http/websocket-init'(this: HTTP, url: URL, init: ClientOptions, config: HTTP.Config): void } } @@ -115,6 +116,14 @@ export namespace HTTP { headers: Headers } + export interface AfterFetch { + url: URL + init: RequestInit + config: RequestConfig + result?: globalThis.Response + error?: any + } + export type Decoder = (raw: globalThis.Response) => Awaitable export type Error = HTTPError @@ -319,11 +328,13 @@ export class HTTP extends Service { } this.ctx.emit(this, 'http/fetch-init', url, init, config) const raw = await fetch(url, init).catch((cause) => { + this.ctx.emit(this, 'http/after-fetch', { url, init, config, error: cause }) if (HTTP.Error.is(cause)) throw cause const error = new HTTP.Error(`fetch ${url} failed`) error.cause = cause throw error }) + this.ctx.emit(this, 'http/after-fetch', { url, init, config, result: raw }) const response: HTTP.Response = { data: null, diff --git a/packages/proxy-agent/package.json b/packages/proxy-agent/package.json index 6da32f9..3d9e152 100644 --- a/packages/proxy-agent/package.json +++ b/packages/proxy-agent/package.json @@ -47,7 +47,7 @@ "cordis": "^3.17.4" }, "peerDependencies": { - "@cordisjs/plugin-http": "^0.6.0", + "@cordisjs/plugin-http": "^0.6.2", "cordis": "^3.17.4" }, "dependencies": {