@ahoo-wang/fetcher 0.1.5 → 0.2.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.zh-CN.md +258 -0
- package/dist/fetcher.d.ts +2 -1
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.es.js +54 -47
- package/dist/index.umd.js +1 -1
- package/package.json +3 -3
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# @ahoo-wang/fetcher
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
4
|
+
[](https://github.com/Ahoo-Wang/fetcher/actions)
|
|
5
|
+
[](https://codecov.io/gh/Ahoo-Wang/Fetcher)
|
|
6
|
+
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
+
|
|
9
|
+
一个基于 Fetch API 的现代 HTTP 客户端库,旨在简化和优化与后端 RESTful API 的交互。它提供了类似 Axios 的 API,支持路径参数、查询参数、超时设置和请求/响应拦截器。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- **Fetch API 兼容**:Fetcher 的 API 与原生 Fetch API 完全兼容,易于上手。
|
|
14
|
+
- **路径和查询参数**:支持请求中的路径参数和查询参数,路径参数用 `{}` 包装。
|
|
15
|
+
- **超时设置**:可以配置请求超时。
|
|
16
|
+
- **请求拦截器**:支持在发送请求前修改请求。
|
|
17
|
+
- **响应拦截器**:支持在返回响应后处理响应。
|
|
18
|
+
- **错误拦截器**:支持在请求生命周期中处理错误。
|
|
19
|
+
- **模块化设计**:代码结构清晰,易于维护和扩展。
|
|
20
|
+
- **自动请求体转换**:自动将普通对象转换为 JSON 并设置适当的 Content-Type 头部。
|
|
21
|
+
- **TypeScript 支持**:完整的 TypeScript 类型定义。
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
使用 pnpm:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @ahoo-wang/fetcher
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
使用 npm:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @ahoo-wang/fetcher
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
使用 yarn:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
yarn add @ahoo-wang/fetcher
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 使用
|
|
44
|
+
|
|
45
|
+
### 基本用法
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
49
|
+
|
|
50
|
+
const fetcher = new Fetcher({
|
|
51
|
+
baseURL: 'https://api.example.com',
|
|
52
|
+
timeout: 5000,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// 带路径参数和查询参数的 GET 请求
|
|
56
|
+
fetcher
|
|
57
|
+
.get('/users/{id}', {
|
|
58
|
+
pathParams: { id: 123 },
|
|
59
|
+
queryParams: { include: 'profile' },
|
|
60
|
+
})
|
|
61
|
+
.then(response => {
|
|
62
|
+
console.log(response.data);
|
|
63
|
+
})
|
|
64
|
+
.catch(error => {
|
|
65
|
+
console.error(error);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// 带 JSON 体的 POST 请求(自动转换为 JSON 字符串)
|
|
69
|
+
fetcher
|
|
70
|
+
.post('/users', {
|
|
71
|
+
body: { name: 'John Doe', email: 'john@example.com' },
|
|
72
|
+
})
|
|
73
|
+
.then(response => {
|
|
74
|
+
console.log(response.data);
|
|
75
|
+
})
|
|
76
|
+
.catch(error => {
|
|
77
|
+
console.error(error);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 拦截器用法
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
85
|
+
|
|
86
|
+
const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
87
|
+
|
|
88
|
+
// 添加请求拦截器
|
|
89
|
+
const requestInterceptorId = fetcher.interceptors.request.use({
|
|
90
|
+
intercept(request) {
|
|
91
|
+
// 修改请求配置,例如添加认证头部
|
|
92
|
+
return {
|
|
93
|
+
...request,
|
|
94
|
+
headers: {
|
|
95
|
+
...request.headers,
|
|
96
|
+
Authorization: 'Bearer token',
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// 添加响应拦截器
|
|
103
|
+
const responseInterceptorId = fetcher.interceptors.response.use({
|
|
104
|
+
intercept(response) {
|
|
105
|
+
// 处理响应数据,例如解析 JSON
|
|
106
|
+
return response;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// 添加错误拦截器
|
|
111
|
+
const errorInterceptorId = fetcher.interceptors.error.use({
|
|
112
|
+
intercept(error) {
|
|
113
|
+
// 处理错误,例如记录日志
|
|
114
|
+
console.error('请求失败:', error);
|
|
115
|
+
return error;
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API 参考
|
|
121
|
+
|
|
122
|
+
### Fetcher 类
|
|
123
|
+
|
|
124
|
+
提供各种 HTTP 方法的核心 HTTP 客户端类。
|
|
125
|
+
|
|
126
|
+
#### 构造函数
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
new Fetcher(options?: FetcherOptions)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**参数:**
|
|
133
|
+
|
|
134
|
+
- `options.baseURL`:基础 URL
|
|
135
|
+
- `options.timeout`:以毫秒为单位的请求超时
|
|
136
|
+
- `options.headers`:默认请求头部
|
|
137
|
+
|
|
138
|
+
#### 方法
|
|
139
|
+
|
|
140
|
+
- `fetch(url: string, request?: FetcherRequest): Promise<Response>` - 通用 HTTP 请求方法
|
|
141
|
+
- `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET 请求
|
|
142
|
+
- `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST 请求
|
|
143
|
+
- `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT 请求
|
|
144
|
+
- `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE 请求
|
|
145
|
+
- `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH 请求
|
|
146
|
+
- `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD 请求
|
|
147
|
+
- `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS 请求
|
|
148
|
+
|
|
149
|
+
### UrlBuilder 类
|
|
150
|
+
|
|
151
|
+
用于构建带参数的完整 URL 的 URL 构建器。
|
|
152
|
+
|
|
153
|
+
#### 方法
|
|
154
|
+
|
|
155
|
+
- `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - 构建完整 URL
|
|
156
|
+
|
|
157
|
+
### InterceptorManager 类
|
|
158
|
+
|
|
159
|
+
用于管理同一类型多个拦截器的拦截器管理器。
|
|
160
|
+
|
|
161
|
+
#### 方法
|
|
162
|
+
|
|
163
|
+
- `use(interceptor: T): number` - 添加拦截器,返回拦截器 ID
|
|
164
|
+
- `eject(index: number): void` - 按 ID 移除拦截器
|
|
165
|
+
- `clear(): void` - 清除所有拦截器
|
|
166
|
+
- `intercept(data: R): Promise<R>` - 顺序执行所有拦截器
|
|
167
|
+
|
|
168
|
+
### FetcherInterceptors 类
|
|
169
|
+
|
|
170
|
+
Fetcher 拦截器集合,包括请求、响应和错误拦截器管理器。
|
|
171
|
+
|
|
172
|
+
#### 属性
|
|
173
|
+
|
|
174
|
+
- `request: RequestInterceptorManager` - 请求拦截器管理器
|
|
175
|
+
- `response: ResponseInterceptorManager` - 响应拦截器管理器
|
|
176
|
+
- `error: ErrorInterceptorManager` - 错误拦截器管理器
|
|
177
|
+
|
|
178
|
+
## 完整示例
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
182
|
+
|
|
183
|
+
// 创建 fetcher 实例
|
|
184
|
+
const fetcher = new Fetcher({
|
|
185
|
+
baseURL: 'https://api.example.com',
|
|
186
|
+
timeout: 10000,
|
|
187
|
+
headers: {
|
|
188
|
+
'Content-Type': 'application/json',
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// 添加请求拦截器 - 添加认证头部
|
|
193
|
+
fetcher.interceptors.request.use({
|
|
194
|
+
intercept(request) {
|
|
195
|
+
return {
|
|
196
|
+
...request,
|
|
197
|
+
headers: {
|
|
198
|
+
...request.headers,
|
|
199
|
+
Authorization: 'Bearer ' + getAuthToken(),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// 添加响应拦截器 - 自动解析 JSON
|
|
206
|
+
fetcher.interceptors.response.use({
|
|
207
|
+
intercept(response) {
|
|
208
|
+
if (response.headers.get('content-type')?.includes('application/json')) {
|
|
209
|
+
return response.json().then(data => {
|
|
210
|
+
return new Response(JSON.stringify(data), response);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return response;
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// 添加错误拦截器 - 统一错误处理
|
|
218
|
+
fetcher.interceptors.error.use({
|
|
219
|
+
intercept(error) {
|
|
220
|
+
if (error.name === 'FetchTimeoutError') {
|
|
221
|
+
console.error('请求超时:', error.message);
|
|
222
|
+
} else {
|
|
223
|
+
console.error('网络错误:', error.message);
|
|
224
|
+
}
|
|
225
|
+
return error;
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// 使用 fetcher 发起请求
|
|
230
|
+
fetcher
|
|
231
|
+
.get('/users/{id}', {
|
|
232
|
+
pathParams: { id: 123 },
|
|
233
|
+
queryParams: { include: 'profile,posts' },
|
|
234
|
+
})
|
|
235
|
+
.then(response => response.json())
|
|
236
|
+
.then(data => {
|
|
237
|
+
console.log('用户数据:', data);
|
|
238
|
+
})
|
|
239
|
+
.catch(error => {
|
|
240
|
+
console.error('获取用户失败:', error);
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## 测试
|
|
245
|
+
|
|
246
|
+
运行测试:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
pnpm test
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## 贡献
|
|
253
|
+
|
|
254
|
+
欢迎任何形式的贡献!请查看 [贡献指南](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) 了解更多详情。
|
|
255
|
+
|
|
256
|
+
## 许可证
|
|
257
|
+
|
|
258
|
+
本项目采用 [Apache-2.0 许可证](https://opensource.org/licenses/Apache-2.0)。
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -9,9 +9,10 @@ export interface FetcherOptions extends BaseURLCapable, HeadersCapable, TimeoutC
|
|
|
9
9
|
/**
|
|
10
10
|
* Fetcher请求选项接口
|
|
11
11
|
*/
|
|
12
|
-
export interface FetcherRequest extends TimeoutCapable, RequestInit {
|
|
12
|
+
export interface FetcherRequest extends TimeoutCapable, Omit<RequestInit, 'body'> {
|
|
13
13
|
pathParams?: Record<string, any>;
|
|
14
14
|
queryParams?: Record<string, any>;
|
|
15
|
+
body?: BodyInit | Record<string, any> | null;
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* HTTP请求客户端类,支持URL构建、超时控制等功能
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AACA,OAAO,EAAqC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"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function T(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function b(r, e) {
|
|
5
|
+
return T(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class E {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -23,10 +23,10 @@ class b {
|
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(e, t, n) {
|
|
26
|
-
let s =
|
|
26
|
+
let s = b(this.baseURL, e), o = this.interpolateUrl(s, t);
|
|
27
27
|
if (n) {
|
|
28
|
-
const
|
|
29
|
-
|
|
28
|
+
const a = new URLSearchParams(n).toString();
|
|
29
|
+
a && (o += "?" + a);
|
|
30
30
|
}
|
|
31
31
|
return o;
|
|
32
32
|
}
|
|
@@ -47,19 +47,19 @@ class b {
|
|
|
47
47
|
}) : e;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
function P(r, e) {
|
|
51
51
|
return typeof r < "u" ? r : e;
|
|
52
52
|
}
|
|
53
|
-
class
|
|
53
|
+
class f extends Error {
|
|
54
54
|
constructor(e, t, n) {
|
|
55
55
|
const s = `Request timeout of ${n}ms exceeded for ${t?.method || "GET"} ${e}`;
|
|
56
|
-
super(s), this.name = "FetchTimeoutError", this.url = e, this.request = t, Object.setPrototypeOf(this,
|
|
56
|
+
super(s), this.name = "FetchTimeoutError", this.url = e, this.request = t, Object.setPrototypeOf(this, f.prototype);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
var c = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(c || {}),
|
|
60
|
-
const
|
|
61
|
-
var
|
|
62
|
-
class
|
|
59
|
+
var c = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(c || {}), O = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(O || {});
|
|
60
|
+
const d = "Content-Type";
|
|
61
|
+
var u = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(u || {});
|
|
62
|
+
class h {
|
|
63
63
|
constructor() {
|
|
64
64
|
this.interceptors = [];
|
|
65
65
|
}
|
|
@@ -97,12 +97,12 @@ class a {
|
|
|
97
97
|
return t;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
class
|
|
100
|
+
class w {
|
|
101
101
|
constructor() {
|
|
102
|
-
this.request = new
|
|
102
|
+
this.request = new h(), this.response = new h(), this.error = new h();
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
class
|
|
105
|
+
class A {
|
|
106
106
|
/**
|
|
107
107
|
* 尝试转换请求体为合法的 fetch API body 类型
|
|
108
108
|
*
|
|
@@ -132,23 +132,23 @@ class w {
|
|
|
132
132
|
const t = { ...e };
|
|
133
133
|
t.body = JSON.stringify(e.body), t.headers || (t.headers = {});
|
|
134
134
|
const n = t.headers;
|
|
135
|
-
return n[
|
|
135
|
+
return n[d] || (n[d] = u.APPLICATION_JSON), t;
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
-
const
|
|
139
|
-
[
|
|
140
|
-
},
|
|
138
|
+
const p = {
|
|
139
|
+
[d]: u.APPLICATION_JSON
|
|
140
|
+
}, S = {
|
|
141
141
|
baseURL: "",
|
|
142
|
-
headers:
|
|
142
|
+
headers: p
|
|
143
143
|
};
|
|
144
|
-
class
|
|
144
|
+
class I {
|
|
145
145
|
/**
|
|
146
146
|
* 创建Fetcher实例
|
|
147
147
|
*
|
|
148
148
|
* @param options - Fetcher配置选项
|
|
149
149
|
*/
|
|
150
|
-
constructor(e =
|
|
151
|
-
this.headers =
|
|
150
|
+
constructor(e = S) {
|
|
151
|
+
this.headers = p, this.interceptors = new w(), this.urlBuilder = new E(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new A());
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
154
154
|
* 发起HTTP请求
|
|
@@ -172,8 +172,11 @@ class g {
|
|
|
172
172
|
e,
|
|
173
173
|
t.pathParams,
|
|
174
174
|
t.queryParams
|
|
175
|
-
)
|
|
176
|
-
|
|
175
|
+
), a = {
|
|
176
|
+
...s,
|
|
177
|
+
body: s.body
|
|
178
|
+
};
|
|
179
|
+
let i = await this.timeoutFetch(o, a);
|
|
177
180
|
return i = await this.interceptors.response.intercept(i), i;
|
|
178
181
|
} catch (o) {
|
|
179
182
|
throw await this.interceptors.error.intercept(o);
|
|
@@ -191,23 +194,27 @@ class g {
|
|
|
191
194
|
* @throws FetchTimeoutError 当请求超时时抛出
|
|
192
195
|
*/
|
|
193
196
|
async timeoutFetch(e, t) {
|
|
194
|
-
const n =
|
|
195
|
-
if (!
|
|
197
|
+
const n = t.timeout, s = P(n, this.timeout);
|
|
198
|
+
if (!s)
|
|
196
199
|
return fetch(e, t);
|
|
197
|
-
const
|
|
200
|
+
const o = new AbortController(), a = {
|
|
198
201
|
...t,
|
|
199
|
-
signal:
|
|
202
|
+
signal: o.signal
|
|
200
203
|
};
|
|
201
204
|
let i = null;
|
|
202
|
-
const
|
|
205
|
+
const m = new Promise((g, y) => {
|
|
203
206
|
i = setTimeout(() => {
|
|
204
207
|
i && clearTimeout(i);
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
const l = new f(
|
|
209
|
+
e,
|
|
210
|
+
t,
|
|
211
|
+
s
|
|
212
|
+
);
|
|
213
|
+
o.abort(l), y(l);
|
|
214
|
+
}, s);
|
|
208
215
|
});
|
|
209
216
|
try {
|
|
210
|
-
return await Promise.race([fetch(e,
|
|
217
|
+
return await Promise.race([fetch(e, a), m]);
|
|
211
218
|
} finally {
|
|
212
219
|
i && clearTimeout(i);
|
|
213
220
|
}
|
|
@@ -305,16 +312,16 @@ class g {
|
|
|
305
312
|
}
|
|
306
313
|
}
|
|
307
314
|
export {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
315
|
+
d as ContentTypeHeader,
|
|
316
|
+
u as ContentTypeValues,
|
|
317
|
+
f as FetchTimeoutError,
|
|
318
|
+
I as Fetcher,
|
|
319
|
+
w as FetcherInterceptors,
|
|
313
320
|
c as HttpMethod,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
321
|
+
h as InterceptorManager,
|
|
322
|
+
O as RequestField,
|
|
323
|
+
E as UrlBuilder,
|
|
324
|
+
b as combineURLs,
|
|
325
|
+
T as isAbsoluteURL,
|
|
326
|
+
P as resolveTimeout
|
|
320
327
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(n=typeof globalThis<"u"?globalThis:n||self,d(n.Fetcher={}))})(this,(function(n){"use strict";function d(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function y(r,e){return d(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class p{constructor(e){this.baseURL=e}build(e,t,i){let o=y(this.baseURL,e),s=this.interpolateUrl(o,t);if(i){const h=new URLSearchParams(i).toString();h&&(s+="?"+h)}return s}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(i,o)=>{const s=t[o];if(s===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(s)}):e}}function T(r,e){return typeof r<"u"?r:e}class u extends Error{constructor(e,t,i){const o=`Request timeout of ${i}ms exceeded for ${t?.method||"GET"} ${e}`;super(o),this.name="FetchTimeoutError",this.url=e,this.request=t,Object.setPrototypeOf(this,u.prototype)}}var c=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(c||{}),b=(r=>(r.METHOD="method",r.BODY="body",r))(b||{});const f="Content-Type";var l=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(l||{});class m{constructor(){this.interceptors=[]}use(e){const t=this.interceptors.length;return this.interceptors.push(e),t}eject(e){this.interceptors[e]&&(this.interceptors[e]=null)}clear(){this.interceptors=[]}async intercept(e){let t=e;for(let i of this.interceptors)i&&(t=await i.intercept(t));return t}}class E{constructor(){this.request=new m,this.response=new m,this.error=new m}}class w{intercept(e){if(e.body===void 0||e.body===null||typeof e.body!="object"||e.body instanceof ArrayBuffer||ArrayBuffer.isView(e.body)||e.body instanceof Blob||e.body instanceof File||e.body instanceof URLSearchParams||e.body instanceof FormData||e.body instanceof ReadableStream)return e;const t={...e};t.body=JSON.stringify(e.body),t.headers||(t.headers={});const i=t.headers;return i[f]||(i[f]=l.APPLICATION_JSON),t}}const P={[f]:l.APPLICATION_JSON},S={baseURL:"",headers:P};class A{constructor(e=S){this.headers=P,this.interceptors=new E,this.urlBuilder=new p(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new w)}async fetch(e,t={}){const i={...this.headers||{},...t.headers||{}};let o={...t,headers:Object.keys(i).length>0?i:void 0};try{o=await this.interceptors.request.intercept(o);const s=this.urlBuilder.build(e,t.pathParams,t.queryParams),h={...o,body:o.body};let a=await this.timeoutFetch(s,h);return a=await this.interceptors.response.intercept(a),a}catch(s){throw await this.interceptors.error.intercept(s)}}async timeoutFetch(e,t){const i=t.timeout,o=T(i,this.timeout);if(!o)return fetch(e,t);const s=new AbortController,h={...t,signal:s.signal};let a=null;const g=new Promise((I,U)=>{a=setTimeout(()=>{a&&clearTimeout(a);const O=new u(e,t,o);s.abort(O),U(O)},o)});try{return await Promise.race([fetch(e,h),g])}finally{a&&clearTimeout(a)}}async get(e,t={}){return this.fetch(e,{...t,method:c.GET})}async post(e,t={}){return this.fetch(e,{...t,method:c.POST})}async put(e,t={}){return this.fetch(e,{...t,method:c.PUT})}async delete(e,t={}){return this.fetch(e,{...t,method:c.DELETE})}async patch(e,t={}){return this.fetch(e,{...t,method:c.PATCH})}async head(e,t={}){return this.fetch(e,{...t,method:c.HEAD})}async options(e,t={}){return this.fetch(e,{...t,method:c.OPTIONS})}}n.ContentTypeHeader=f,n.ContentTypeValues=l,n.FetchTimeoutError=u,n.Fetcher=A,n.FetcherInterceptors=E,n.HttpMethod=c,n.InterceptorManager=m,n.RequestField=b,n.UrlBuilder=p,n.combineURLs=y,n.isAbsoluteURL=d,n.resolveTimeout=T,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core library providing basic HTTP client functionality for Fetcher",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
27
28
|
"import": "./dist/index.es.js",
|
|
28
|
-
"require": "./dist/index.umd.js"
|
|
29
|
-
"types": "./dist/index.d.ts"
|
|
29
|
+
"require": "./dist/index.umd.js"
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"files": [
|