Comparing version 0.2.5 to 0.3.0
@@ -14,4 +14,4 @@ import { Context, Service } from 'cordis'; | ||
'http/config'(config: HTTP.Config): void; | ||
'http/fetch-init'(init: RequestInit, config: HTTP.Config): void; | ||
'http/websocket-init'(init: ClientOptions, config: HTTP.Config): void; | ||
'http/fetch-init'(url: URL, init: RequestInit, config: HTTP.Config): void; | ||
'http/websocket-init'(url: URL, init: ClientOptions, config: HTTP.Config): void; | ||
} | ||
@@ -21,5 +21,7 @@ } | ||
declare class HTTPError extends Error { | ||
code?: "ETIMEDOUT" | undefined; | ||
[kHTTPError]: boolean; | ||
response?: HTTP.Response; | ||
static is(error: any): error is HTTPError; | ||
constructor(message?: string, code?: "ETIMEDOUT" | undefined); | ||
} | ||
@@ -72,2 +74,5 @@ export declare namespace HTTP { | ||
type Error = HTTPError; | ||
namespace Error { | ||
type Code = 'ETIMEDOUT'; | ||
} | ||
} | ||
@@ -74,0 +79,0 @@ export interface HTTP { |
@@ -10,2 +10,6 @@ var __defProp = Object.defineProperty; | ||
var HTTPError = class extends Error { | ||
constructor(message, code) { | ||
super(message); | ||
this.code = code; | ||
} | ||
static { | ||
@@ -139,7 +143,7 @@ __name(this, "HTTPError"); | ||
clearTimeout(timer); | ||
controller.abort(new Error("context disposed")); | ||
controller.abort(new HTTPError("context disposed", "ETIMEDOUT")); | ||
}); | ||
if (config.timeout) { | ||
timer = setTimeout(() => { | ||
controller.abort(new Error("timeout")); | ||
controller.abort(new HTTPError("request timeout", "ETIMEDOUT")); | ||
}, config.timeout); | ||
@@ -164,4 +168,6 @@ } | ||
} | ||
caller.emit("http/fetch-init", init, config); | ||
caller.emit("http/fetch-init", url, init, config); | ||
const raw = await fetch(url, init).catch((cause) => { | ||
if (_HTTP.Error.is(cause)) | ||
throw cause; | ||
const error = new _HTTP.Error(`fetch ${url} failed`); | ||
@@ -225,3 +231,3 @@ error.cause = cause; | ||
}; | ||
caller.emit("http/websocket-init", options, config); | ||
caller.emit("http/websocket-init", url, options, config); | ||
} | ||
@@ -228,0 +234,0 @@ const socket = new WebSocket(url, options); |
{ | ||
"name": "undios", | ||
"description": "Fetch-based axios-style HTTP client", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"type": "module", | ||
@@ -52,7 +52,7 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"cordis": "^3.12.0", | ||
"cordis": "^3.13.0", | ||
"undici": "^6.6.2" | ||
}, | ||
"peerDependencies": { | ||
"cordis": "^3.12.0" | ||
"cordis": "^3.13.0" | ||
}, | ||
@@ -59,0 +59,0 @@ "dependencies": { |
@@ -19,4 +19,4 @@ import { Context, Service } from 'cordis' | ||
'http/config'(config: HTTP.Config): void | ||
'http/fetch-init'(init: RequestInit, config: HTTP.Config): void | ||
'http/websocket-init'(init: ClientOptions, config: HTTP.Config): void | ||
'http/fetch-init'(url: URL, init: RequestInit, config: HTTP.Config): void | ||
'http/websocket-init'(url: URL, init: ClientOptions, config: HTTP.Config): void | ||
} | ||
@@ -34,2 +34,6 @@ } | ||
} | ||
constructor(message?: string, public code?: HTTP.Error.Code) { | ||
super(message) | ||
} | ||
} | ||
@@ -108,2 +112,6 @@ | ||
export type Error = HTTPError | ||
export namespace Error { | ||
export type Code = 'ETIMEDOUT' | ||
} | ||
} | ||
@@ -242,7 +250,7 @@ | ||
clearTimeout(timer) | ||
controller.abort(new Error('context disposed')) | ||
controller.abort(new HTTPError('context disposed', 'ETIMEDOUT')) | ||
}) | ||
if (config.timeout) { | ||
timer = setTimeout(() => { | ||
controller.abort(new Error('timeout')) | ||
controller.abort(new HTTPError('request timeout', 'ETIMEDOUT')) | ||
}, config.timeout) | ||
@@ -268,4 +276,5 @@ } | ||
} | ||
caller.emit('http/fetch-init', init, config) | ||
caller.emit('http/fetch-init', url, init, config) | ||
const raw = await fetch(url, init).catch((cause) => { | ||
if (HTTP.Error.is(cause)) throw cause | ||
const error = new HTTP.Error(`fetch ${url} failed`) | ||
@@ -338,3 +347,3 @@ error.cause = cause | ||
} | ||
caller.emit('http/websocket-init', options, config) | ||
caller.emit('http/websocket-init', url, options, config) | ||
} | ||
@@ -341,0 +350,0 @@ const socket = new WebSocket(url, options as never) |
Sorry, the diff of this file is not supported yet
38302
995