@iquji-community/web-network 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -2
- package/README.zh-CN.md +16 -0
- package/dist/http-transport.d.ts +13 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +105 -0
- package/dist/test-support.d.ts +8 -0
- package/dist/test-support.js +13 -0
- package/package.json +40 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Iquji contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @iquji-community/web-network
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[简体中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
HTTP requests and typed JSON responses with timeouts, cancellation and injectable fetch.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @iquji-community/web-network
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createJsonTransport } from "@iquji-community/web-network";
|
|
13
|
+
|
|
14
|
+
const request = createJsonTransport({ baseUrl: "https://api.example.com" });
|
|
15
|
+
export const getStatus = () => request<{ status: string }>("/status");
|
|
16
|
+
```
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @iquji-community/web-network
|
|
2
|
+
|
|
3
|
+
[English](README.md)
|
|
4
|
+
|
|
5
|
+
提供 HTTP 请求和带类型的 JSON 响应,支持超时、取消和注入 fetch。
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @iquji-community/web-network
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createJsonTransport } from "@iquji-community/web-network";
|
|
13
|
+
|
|
14
|
+
const request = createJsonTransport({ baseUrl: "https://api.example.com" });
|
|
15
|
+
export const getStatus = () => request<{ status: string }>("/status");
|
|
16
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type FetchImplementation = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
2
|
+
export type HttpTransportOptions = {
|
|
3
|
+
fetchImplementation?: FetchImplementation;
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
};
|
|
6
|
+
export type HttpRequestOptions = RequestInit & {
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
};
|
|
9
|
+
export declare class HttpTimeoutError extends Error {
|
|
10
|
+
readonly timeoutMs: number;
|
|
11
|
+
constructor(timeoutMs: number);
|
|
12
|
+
}
|
|
13
|
+
export declare function createHttpTransport({ fetchImplementation, timeoutMs, }?: HttpTransportOptions): (input: RequestInfo | URL, options?: HttpRequestOptions) => Promise<Response>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type HttpTransportOptions } from "./http-transport.js";
|
|
2
|
+
export { createHttpTransport, type FetchImplementation, type HttpRequestOptions, HttpTimeoutError, type HttpTransportOptions, } from "./http-transport.js";
|
|
3
|
+
export type JsonTransportOptions = HttpTransportOptions & {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
resolveHeaders?: () => HeadersInit | Promise<HeadersInit>;
|
|
6
|
+
credentials?: RequestCredentials;
|
|
7
|
+
cache?: RequestCache;
|
|
8
|
+
};
|
|
9
|
+
export type JsonRequest = {
|
|
10
|
+
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
11
|
+
body?: unknown;
|
|
12
|
+
headers?: HeadersInit;
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
};
|
|
16
|
+
export declare class JsonTransportError extends Error {
|
|
17
|
+
readonly status: number;
|
|
18
|
+
constructor(status: number);
|
|
19
|
+
}
|
|
20
|
+
export declare function createJsonTransport({ baseUrl, fetchImplementation, timeoutMs, resolveHeaders, credentials, cache, }: JsonTransportOptions): <ResponseBody>(path: string, options?: JsonRequest) => Promise<ResponseBody>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/http-transport.ts
|
|
2
|
+
class HttpTimeoutError extends Error {
|
|
3
|
+
timeoutMs;
|
|
4
|
+
constructor(timeoutMs) {
|
|
5
|
+
super(`Request timed out after ${timeoutMs} ms`);
|
|
6
|
+
this.name = "TimeoutError";
|
|
7
|
+
this.timeoutMs = timeoutMs;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function validateTimeout(timeoutMs) {
|
|
11
|
+
if (timeoutMs !== undefined && (!Number.isFinite(timeoutMs) || timeoutMs < 0 || timeoutMs > 2147483647)) {
|
|
12
|
+
throw new RangeError("HTTP timeout must be between 0 and 2147483647 ms");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function createHttpTransport({
|
|
16
|
+
fetchImplementation = globalThis.fetch,
|
|
17
|
+
timeoutMs
|
|
18
|
+
} = {}) {
|
|
19
|
+
validateTimeout(timeoutMs);
|
|
20
|
+
return async function request(input, options = {}) {
|
|
21
|
+
const { timeoutMs: requestTimeout = timeoutMs, ...init } = options;
|
|
22
|
+
validateTimeout(requestTimeout);
|
|
23
|
+
const sourceSignal = init.signal === undefined && input instanceof Request ? input.signal : init.signal;
|
|
24
|
+
sourceSignal?.throwIfAborted();
|
|
25
|
+
const timeoutController = requestTimeout ? new AbortController : undefined;
|
|
26
|
+
const signal = timeoutController ? sourceSignal ? AbortSignal.any([sourceSignal, timeoutController.signal]) : timeoutController.signal : sourceSignal;
|
|
27
|
+
if (timeoutController && signal)
|
|
28
|
+
init.signal = signal;
|
|
29
|
+
const timer = timeoutController ? setTimeout(() => timeoutController.abort(new HttpTimeoutError(requestTimeout ?? 0)), requestTimeout) : undefined;
|
|
30
|
+
let abortListener;
|
|
31
|
+
try {
|
|
32
|
+
if (!signal)
|
|
33
|
+
return await fetchImplementation(input, init);
|
|
34
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
35
|
+
abortListener = () => reject(signal.reason);
|
|
36
|
+
signal.addEventListener("abort", abortListener, { once: true });
|
|
37
|
+
});
|
|
38
|
+
return await Promise.race([fetchImplementation(input, init), aborted]);
|
|
39
|
+
} finally {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
if (signal && abortListener)
|
|
42
|
+
signal.removeEventListener("abort", abortListener);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/index.ts
|
|
48
|
+
class JsonTransportError extends Error {
|
|
49
|
+
status;
|
|
50
|
+
constructor(status) {
|
|
51
|
+
super(`Request failed with status ${status}`);
|
|
52
|
+
this.name = "JsonTransportError";
|
|
53
|
+
this.status = status;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function createJsonTransport({
|
|
57
|
+
baseUrl,
|
|
58
|
+
fetchImplementation,
|
|
59
|
+
timeoutMs,
|
|
60
|
+
resolveHeaders,
|
|
61
|
+
credentials,
|
|
62
|
+
cache
|
|
63
|
+
}) {
|
|
64
|
+
const normalizedBaseUrl = baseUrl.replace(/\/+$/u, "");
|
|
65
|
+
if (!normalizedBaseUrl)
|
|
66
|
+
throw new Error("A transport base URL is required");
|
|
67
|
+
const transportOptions = {};
|
|
68
|
+
if (fetchImplementation)
|
|
69
|
+
transportOptions.fetchImplementation = fetchImplementation;
|
|
70
|
+
if (timeoutMs !== undefined)
|
|
71
|
+
transportOptions.timeoutMs = timeoutMs;
|
|
72
|
+
const transport = createHttpTransport(transportOptions);
|
|
73
|
+
return async function request(path, options = {}) {
|
|
74
|
+
const headers = new Headers(await resolveHeaders?.());
|
|
75
|
+
new Headers(options.headers).forEach((value, key) => {
|
|
76
|
+
headers.set(key, value);
|
|
77
|
+
});
|
|
78
|
+
const init = {
|
|
79
|
+
method: options.method ?? "GET",
|
|
80
|
+
headers
|
|
81
|
+
};
|
|
82
|
+
if (credentials)
|
|
83
|
+
init.credentials = credentials;
|
|
84
|
+
if (cache)
|
|
85
|
+
init.cache = cache;
|
|
86
|
+
if (options.signal)
|
|
87
|
+
init.signal = options.signal;
|
|
88
|
+
if (options.timeoutMs !== undefined)
|
|
89
|
+
init.timeoutMs = options.timeoutMs;
|
|
90
|
+
if (options.body !== undefined) {
|
|
91
|
+
headers.set("content-type", "application/json");
|
|
92
|
+
init.body = JSON.stringify(options.body);
|
|
93
|
+
}
|
|
94
|
+
const response = await transport(`${normalizedBaseUrl}/${path.replace(/^\/+/, "")}`, init);
|
|
95
|
+
if (!response.ok)
|
|
96
|
+
throw new JsonTransportError(response.status);
|
|
97
|
+
return await response.json();
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
createJsonTransport,
|
|
102
|
+
createHttpTransport,
|
|
103
|
+
JsonTransportError,
|
|
104
|
+
HttpTimeoutError
|
|
105
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FetchImplementation } from "./index.js";
|
|
2
|
+
export type FetchRecord = {
|
|
3
|
+
request: Request;
|
|
4
|
+
};
|
|
5
|
+
export declare function createFetchRecorder(responder: (request: Request) => Response | Promise<Response>): {
|
|
6
|
+
fetchImplementation: FetchImplementation;
|
|
7
|
+
records: FetchRecord[];
|
|
8
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/test-support.ts
|
|
2
|
+
function createFetchRecorder(responder) {
|
|
3
|
+
const records = [];
|
|
4
|
+
const fetchImplementation = async (input, init) => {
|
|
5
|
+
const request = new Request(input, init);
|
|
6
|
+
records.push({ request });
|
|
7
|
+
return responder(request);
|
|
8
|
+
};
|
|
9
|
+
return { fetchImplementation, records };
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
createFetchRecorder
|
|
13
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iquji-community/web-network",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"author": {
|
|
4
|
+
"name": "IQuji Community",
|
|
5
|
+
"email": "community@iqu.lol"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.2.1",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"README.zh-CN.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./test-support": {
|
|
22
|
+
"types": "./dist/test-support.d.ts",
|
|
23
|
+
"import": "./dist/test-support.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"peerDependencies": {},
|
|
28
|
+
"description": "可注入 fetch 的 HTTP 传输、取消和 JSON 适配器。",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"registry": "https://registry.npmjs.org/"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/haydardsx/iquji-community-web.git",
|
|
36
|
+
"directory": "network"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://haydardsx.github.io/iquji-community-web/",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/haydardsx/iquji-community-web/issues"
|
|
41
|
+
}
|
|
42
|
+
}
|