@wevu/web-apis 1.0.0 → 1.1.0
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 +89 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @wevu/web-apis 使用指南
|
|
2
|
+
|
|
3
|
+
## 1. 简介
|
|
4
|
+
|
|
5
|
+
`@wevu/web-apis` 为小程序运行时提供一组 Web API 兼容层,重点解决第三方请求库在小程序环境中缺失 `fetch`、`Request`、`Response`、`AbortController`、`XMLHttpRequest`、`URL` 等全局对象的问题。
|
|
6
|
+
|
|
7
|
+
它主要服务于:
|
|
8
|
+
|
|
9
|
+
- `weapp-vite` 的请求全局注入能力
|
|
10
|
+
- `wevu` 运行时对 Web 风格请求库的兼容
|
|
11
|
+
- `axios`、`graphql-request` 等依赖 Web API 的第三方库
|
|
12
|
+
|
|
13
|
+
## 2. 特性
|
|
14
|
+
|
|
15
|
+
- 按需安装 `fetch`、`Headers`、`Request`、`Response`
|
|
16
|
+
- 提供 `AbortController` / `AbortSignal` 兼容层
|
|
17
|
+
- 提供 `XMLHttpRequest`、`URL`、`URLSearchParams`、`Blob`、`FormData` 兼容层
|
|
18
|
+
- 自动把能力安装到可用的小程序宿主全局对象
|
|
19
|
+
- 默认基于 `@wevu/api` 的请求能力完成底层转发
|
|
20
|
+
|
|
21
|
+
## 3. 安装
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm add @wevu/web-apis
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 4. 快速开始
|
|
28
|
+
|
|
29
|
+
### 4.1 安装完整请求全局
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { installRequestGlobals } from '@wevu/web-apis'
|
|
33
|
+
|
|
34
|
+
installRequestGlobals()
|
|
35
|
+
|
|
36
|
+
const response = await fetch('https://example.com/data')
|
|
37
|
+
console.log(await response.json())
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 4.2 仅安装 Abort 相关能力
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { installAbortGlobals } from '@wevu/web-apis'
|
|
44
|
+
|
|
45
|
+
installAbortGlobals()
|
|
46
|
+
|
|
47
|
+
const controller = new AbortController()
|
|
48
|
+
controller.abort()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 4.3 按目标精简注入
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { installRequestGlobals } from '@wevu/web-apis'
|
|
55
|
+
|
|
56
|
+
installRequestGlobals({
|
|
57
|
+
targets: ['fetch', 'Headers', 'Request', 'Response'],
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 5. 主要导出
|
|
62
|
+
|
|
63
|
+
| 导出 | 说明 |
|
|
64
|
+
| ---------------------------------------------------------- | ---------------------------------------- |
|
|
65
|
+
| `installRequestGlobals` | 按需安装请求相关全局对象 |
|
|
66
|
+
| `installAbortGlobals` | 仅安装 `AbortController` / `AbortSignal` |
|
|
67
|
+
| `fetch` | 基于小程序请求能力适配的 `fetch` 实现 |
|
|
68
|
+
| `HeadersPolyfill` / `RequestPolyfill` / `ResponsePolyfill` | HTTP 相关兼容类 |
|
|
69
|
+
| `URLPolyfill` / `URLSearchParamsPolyfill` | URL 相关兼容类 |
|
|
70
|
+
| `XMLHttpRequestPolyfill` | XHR 兼容实现 |
|
|
71
|
+
|
|
72
|
+
## 6. 适用场景
|
|
73
|
+
|
|
74
|
+
- 在小程序环境中运行 `axios` 的 `fetch` 适配器
|
|
75
|
+
- 在小程序环境中运行 `graphql-request`
|
|
76
|
+
- 给依赖 `URL` / `FormData` / `Blob` 的库补齐基础全局对象
|
|
77
|
+
|
|
78
|
+
## 7. 本地开发
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pnpm --filter @wevu/web-apis build
|
|
82
|
+
pnpm --filter @wevu/web-apis test
|
|
83
|
+
pnpm --filter @wevu/web-apis typecheck
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 8. 相关链接
|
|
87
|
+
|
|
88
|
+
- `@wevu/api`:[../weapi/README.md](../weapi/README.md)
|
|
89
|
+
- `wevu`:[../wevu/README.md](../wevu/README.md)
|
package/package.json
CHANGED