@iquji-web/web-network 0.1.2 → 0.1.3
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/README.md +65 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,27 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @iquji-web/web-network
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
简体中文 | [English](README.en.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Iquji Web 网络传输抽象层,提供原生 HTTP 传输器、JSON 适配器、可注入 Fetch 实现、统一超时控制与取消信号。
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @iquji-web/web-network
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 核心特性与使用
|
|
18
|
+
|
|
19
|
+
### 1. JSON 传输客户端 (`createJsonTransport`)
|
|
8
20
|
|
|
9
21
|
```ts
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
import { createJsonTransport, JsonTransportError } from "@iquji-web/web-network";
|
|
23
|
+
|
|
24
|
+
const api = createJsonTransport({
|
|
25
|
+
baseUrl: "https://api.example.com",
|
|
26
|
+
timeoutMs: 10000,
|
|
27
|
+
resolveHeaders: () => ({
|
|
28
|
+
Authorization: `Bearer ${localStorage.getItem("token") || ""}`,
|
|
29
|
+
}),
|
|
18
30
|
});
|
|
31
|
+
|
|
32
|
+
// 发起强类型 JSON 请求
|
|
33
|
+
try {
|
|
34
|
+
const user = await api<{ id: string; name: string }>("/v1/user/profile", {
|
|
35
|
+
method: "GET",
|
|
36
|
+
});
|
|
37
|
+
console.log(user.name);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof JsonTransportError) {
|
|
40
|
+
console.error(`HTTP 错误响应码: ${error.status}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
19
43
|
```
|
|
20
44
|
|
|
21
|
-
|
|
45
|
+
### 2. 底层 HTTP 传输器 (`createHttpTransport`)
|
|
22
46
|
|
|
23
|
-
|
|
47
|
+
支持注入自定义 fetch(例如在测试替身或 Node.js 环境中):
|
|
24
48
|
|
|
25
|
-
|
|
49
|
+
```ts
|
|
50
|
+
import { createHttpTransport, HttpTimeoutError } from "@iquji-web/web-network";
|
|
51
|
+
|
|
52
|
+
const transport = createHttpTransport({
|
|
53
|
+
timeoutMs: 5000,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const response = await transport.request("https://api.example.com/health");
|
|
58
|
+
console.log(response.status);
|
|
59
|
+
} catch (err) {
|
|
60
|
+
if (err instanceof HttpTimeoutError) {
|
|
61
|
+
console.warn("请求超时");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
26
67
|
|
|
27
|
-
|
|
68
|
+
## 开发与构建
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
bun install --frozen-lockfile
|
|
72
|
+
bun run build # 编译构建到 dist/
|
|
73
|
+
bun run typecheck # 类型校验
|
|
74
|
+
bun run test # 单元测试
|
|
75
|
+
bun run check # 完整自检
|
|
76
|
+
```
|