@ahoo-wang/fetcher 0.5.1 → 0.5.5
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 +9 -9
- package/README.zh-CN.md +8 -8
- package/dist/fetcher.d.ts +79 -4
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherRegistrar.d.ts +1 -1
- package/dist/fetcherRegistrar.d.ts.map +1 -1
- package/dist/index.es.js +46 -49
- package/dist/index.umd.js +1 -1
- package/dist/namedFetcher.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts +5 -5
- package/dist/urlBuilder.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -5,18 +5,18 @@
|
|
|
5
5
|
[](https://codecov.io/gh/Ahoo-Wang/fetcher)
|
|
6
6
|
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
9
|
|
|
9
|
-
A modern, lightweight HTTP client
|
|
10
|
-
|
|
11
|
-
settings, and request/response interceptors.
|
|
10
|
+
A modern, ultra-lightweight (1.9kB) HTTP client with built-in path parameters, query parameters, and Axios-like API. 86%
|
|
11
|
+
smaller than Axios while providing the same powerful features.
|
|
12
12
|
|
|
13
13
|
## 🌟 Features
|
|
14
14
|
|
|
15
|
-
-
|
|
16
|
-
- **🧭 Path & Query Parameters**:
|
|
17
|
-
- **⏱️ Timeout Control**: Configurable request timeouts with proper error handling
|
|
15
|
+
- **⚡ Ultra-Lightweight**: Only 1.9kB min+gzip - 86% smaller than Axios
|
|
16
|
+
- **🧭 Path & Query Parameters**: Built-in support for path (`{id}`) and query parameters
|
|
18
17
|
- **🔗 Interceptor System**: Request, response, and error interceptors for middleware patterns
|
|
19
|
-
-
|
|
18
|
+
- **⏱️ Timeout Control**: Configurable request timeouts with proper error handling
|
|
19
|
+
- **🔄 Fetch API Compatible**: Fully compatible with the native Fetch API
|
|
20
20
|
- **🛡️ TypeScript Support**: Complete TypeScript definitions for type-safe development
|
|
21
21
|
- **🧩 Modular Architecture**: Lightweight core with optional extension packages
|
|
22
22
|
- **📦 Named Fetcher Support**: Automatic registration and retrieval of fetcher instances
|
|
@@ -50,8 +50,8 @@ const fetcher = new Fetcher({
|
|
|
50
50
|
|
|
51
51
|
// GET request with path and query parameters
|
|
52
52
|
const response = await fetcher.get('/users/{id}', {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
path: { id: 123 },
|
|
54
|
+
query: { include: 'profile' },
|
|
55
55
|
});
|
|
56
56
|
const userData = await response.json();
|
|
57
57
|
|
package/README.zh-CN.md
CHANGED
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
[](https://codecov.io/gh/Ahoo-Wang/fetcher)
|
|
6
6
|
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
API,支持路径参数、查询参数、超时设置和请求/响应拦截器。
|
|
10
|
+
一个现代、超轻量级(1.9kB)的 HTTP 客户端,内置路径参数、查询参数和类似 Axios 的 API。比 Axios 小 86%,同时提供相同的强大功能。
|
|
11
11
|
|
|
12
12
|
## 🌟 特性
|
|
13
13
|
|
|
14
|
-
-
|
|
15
|
-
- **🧭
|
|
16
|
-
- **⏱️ 超时控制**:可配置的请求超时和适当的错误处理
|
|
14
|
+
- **⚡ 超轻量级**:仅 1.9kB min+gzip - 比 Axios 小 86%
|
|
15
|
+
- **🧭 路径和查询参数**:内置支持路径(`{id}`)和查询参数
|
|
17
16
|
- **🔗 拦截器系统**:请求、响应和错误拦截器的中间件模式
|
|
18
|
-
-
|
|
17
|
+
- **⏱️ 超时控制**:可配置的请求超时和适当的错误处理
|
|
18
|
+
- **🔄 Fetch API 兼容**:与原生 Fetch API 完全兼容
|
|
19
19
|
- **🛡️ TypeScript 支持**:完整的 TypeScript 类型定义,提升开发体验
|
|
20
20
|
- **🧩 模块化架构**:轻量级核心和可选的扩展包
|
|
21
21
|
- **📦 命名 Fetcher 支持**:自动注册和检索 fetcher 实例
|
|
@@ -49,8 +49,8 @@ const fetcher = new Fetcher({
|
|
|
49
49
|
|
|
50
50
|
// 带路径和查询参数的 GET 请求
|
|
51
51
|
const response = await fetcher.get('/users/{id}', {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
path: { id: 123 },
|
|
53
|
+
query: { include: 'profile' },
|
|
54
54
|
});
|
|
55
55
|
const userData = await response.json();
|
|
56
56
|
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -6,13 +6,88 @@ import { FetcherInterceptors, FetchExchange } from './interceptor';
|
|
|
6
6
|
*/
|
|
7
7
|
export interface FetcherOptions extends BaseURLCapable, HeadersCapable, TimeoutCapable {
|
|
8
8
|
}
|
|
9
|
-
export declare const
|
|
9
|
+
export declare const DEFAULT_OPTIONS: FetcherOptions;
|
|
10
10
|
/**
|
|
11
|
-
* Fetcher request
|
|
11
|
+
* Fetcher request configuration interface
|
|
12
|
+
*
|
|
13
|
+
* This interface defines all the configuration options available for making HTTP requests
|
|
14
|
+
* with the Fetcher client. It extends the standard RequestInit interface while adding
|
|
15
|
+
* Fetcher-specific features like path parameters, query parameters, and timeout control.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const request: FetcherRequest = {
|
|
20
|
+
* method: 'GET',
|
|
21
|
+
* path: { id: 123 },
|
|
22
|
+
* query: { include: 'profile' },
|
|
23
|
+
* headers: { 'Authorization': 'Bearer token' },
|
|
24
|
+
* timeout: 5000
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* const response = await fetcher.fetch('/users/{id}', request);
|
|
28
|
+
* ```
|
|
12
29
|
*/
|
|
13
30
|
export interface FetcherRequest extends TimeoutCapable, Omit<RequestInit, 'body'> {
|
|
14
|
-
|
|
15
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Path parameters for URL templating
|
|
33
|
+
*
|
|
34
|
+
* An object containing key-value pairs that will be used to replace placeholders
|
|
35
|
+
* in the URL path. Placeholders are specified using curly braces, e.g., '/users/{id}'.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // With URL '/users/{id}/posts/{postId}'
|
|
40
|
+
* const request = {
|
|
41
|
+
* path: { id: 123, postId: 456 }
|
|
42
|
+
* };
|
|
43
|
+
* // Results in URL: '/users/123/posts/456'
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
path?: Record<string, any>;
|
|
47
|
+
/**
|
|
48
|
+
* Query parameters for URL query string
|
|
49
|
+
*
|
|
50
|
+
* An object containing key-value pairs that will be serialized and appended
|
|
51
|
+
* to the URL as query parameters. Arrays are serialized as multiple parameters
|
|
52
|
+
* with the same name, and objects are JSON-stringified.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const request = {
|
|
57
|
+
* query: {
|
|
58
|
+
* limit: 10,
|
|
59
|
+
* filter: 'active',
|
|
60
|
+
* tags: ['important', 'urgent']
|
|
61
|
+
* }
|
|
62
|
+
* };
|
|
63
|
+
* // Results in query string: '?limit=10&filter=active&tags=important&tags=urgent'
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
query?: Record<string, any>;
|
|
67
|
+
/**
|
|
68
|
+
* Request body
|
|
69
|
+
*
|
|
70
|
+
* The body of the request. Can be a string, Blob, ArrayBuffer, FormData,
|
|
71
|
+
* URLSearchParams, or a plain object. Plain objects are automatically
|
|
72
|
+
* converted to JSON and the appropriate Content-Type header is set.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Plain object (automatically converted to JSON)
|
|
77
|
+
* const request = {
|
|
78
|
+
* method: 'POST',
|
|
79
|
+
* body: { name: 'John', email: 'john@example.com' }
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* // FormData
|
|
83
|
+
* const formData = new FormData();
|
|
84
|
+
* formData.append('name', 'John');
|
|
85
|
+
* const request = {
|
|
86
|
+
* method: 'POST',
|
|
87
|
+
* body: formData
|
|
88
|
+
* };
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
16
91
|
body?: BodyInit | Record<string, any> | null;
|
|
17
92
|
}
|
|
18
93
|
/**
|
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,cAAc,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EACL,cAAc,EAGd,cAAc,EAEd,YAAY,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnE;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CACjB;AAMD,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"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"}
|
|
@@ -2,7 +2,7 @@ import { Fetcher } from './fetcher';
|
|
|
2
2
|
/**
|
|
3
3
|
* Default fetcher name used when no name is specified
|
|
4
4
|
*/
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const DEFAULT_FETCHER_NAME = "default";
|
|
6
6
|
/**
|
|
7
7
|
* FetcherRegistrar is a registry for managing multiple Fetcher instances.
|
|
8
8
|
* It allows registering, retrieving, and unregistering Fetcher instances by name.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcherRegistrar.d.ts","sourceRoot":"","sources":["../src/fetcherRegistrar.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"fetcherRegistrar.d.ts","sourceRoot":"","sources":["../src/fetcherRegistrar.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,oBAAoB,YAAY,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAgB;IAC3B;;;OAGG;IACH,OAAO,CAAC,SAAS,CAAmC;IAEpD;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAI9C;;;;;;;;;;OAUG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIjC;;;;;;;;;;OAUG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAItC;;;;;;;;;;;;;OAaG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAQlC;;;;;;;OAOG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;;;;;OAOG;IACH,IAAI,OAAO,CAAC,OAAO,EAAE,OAAO,EAE3B;IAED;;;;;;;;;OASG;IACH,IAAI,QAAQ,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAEnC;CACF;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,gBAAgB,kBAAyB,CAAC"}
|
package/dist/index.es.js
CHANGED
|
@@ -17,46 +17,47 @@ class P {
|
|
|
17
17
|
* 构建完整的URL,包括路径参数替换和查询参数添加
|
|
18
18
|
*
|
|
19
19
|
* @param url - 需要构建的URL路径
|
|
20
|
-
* @param
|
|
21
|
-
* @param
|
|
20
|
+
* @param path - 路径参数对象,用于替换URL中的占位符(如{id})
|
|
21
|
+
* @param query - 查询参数对象,将被添加到URL查询字符串中
|
|
22
22
|
* @returns 完整的URL字符串
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(e, t, s) {
|
|
26
|
-
|
|
26
|
+
const o = g(this.baseURL, e);
|
|
27
|
+
let i = this.interpolateUrl(o, t);
|
|
27
28
|
if (s) {
|
|
28
29
|
const n = new URLSearchParams(s).toString();
|
|
29
|
-
n && (
|
|
30
|
+
n && (i += "?" + n);
|
|
30
31
|
}
|
|
31
|
-
return
|
|
32
|
+
return i;
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* 替换url中的占位符参数
|
|
35
36
|
*
|
|
36
37
|
* @param url - 包含占位符的路径字符串,如 "http://localhost/users/{id}/posts/{postId}"
|
|
37
|
-
* @param
|
|
38
|
+
* @param path - 路径参数对象,用于替换路径中的占位符
|
|
38
39
|
* @returns 替换占位符后的路径字符串
|
|
39
40
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
40
41
|
*/
|
|
41
42
|
interpolateUrl(e, t) {
|
|
42
|
-
return t ? e.replace(/{([^}]+)}/g, (s,
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
45
|
-
throw new Error(`Missing required path parameter: ${
|
|
46
|
-
return String(
|
|
43
|
+
return t ? e.replace(/{([^}]+)}/g, (s, o) => {
|
|
44
|
+
const i = t[o];
|
|
45
|
+
if (i === void 0)
|
|
46
|
+
throw new Error(`Missing required path parameter: ${o}`);
|
|
47
|
+
return String(i);
|
|
47
48
|
}) : e;
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
function O(r, e) {
|
|
51
52
|
return typeof r < "u" ? r : e;
|
|
52
53
|
}
|
|
53
|
-
class
|
|
54
|
+
class l extends Error {
|
|
54
55
|
constructor(e, t) {
|
|
55
|
-
const s = e.request?.method || "GET",
|
|
56
|
-
super(
|
|
56
|
+
const s = e.request?.method || "GET", o = `Request timeout of ${t}ms exceeded for ${s} ${e.url}`;
|
|
57
|
+
super(o), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this, l.prototype);
|
|
57
58
|
}
|
|
58
59
|
}
|
|
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
|
+
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 || {}), A = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(A || {});
|
|
60
61
|
const d = "Content-Type";
|
|
61
62
|
var p = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(p || {});
|
|
62
63
|
class h {
|
|
@@ -92,7 +93,7 @@ class h {
|
|
|
92
93
|
*/
|
|
93
94
|
async intercept(e) {
|
|
94
95
|
let t = e;
|
|
95
|
-
for (
|
|
96
|
+
for (const s of this.interceptors)
|
|
96
97
|
s && (t = await s.intercept(t));
|
|
97
98
|
return t;
|
|
98
99
|
}
|
|
@@ -102,7 +103,7 @@ class F {
|
|
|
102
103
|
this.request = new h(), this.response = new h(), this.error = new h();
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
|
-
class
|
|
106
|
+
class q {
|
|
106
107
|
/**
|
|
107
108
|
* 尝试转换请求体为合法的 fetch API body 类型
|
|
108
109
|
*
|
|
@@ -132,8 +133,8 @@ class A {
|
|
|
132
133
|
return e;
|
|
133
134
|
const s = { ...t };
|
|
134
135
|
s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
|
|
135
|
-
const
|
|
136
|
-
return
|
|
136
|
+
const o = s.headers;
|
|
137
|
+
return o[d] || (o[d] = p.APPLICATION_JSON), { ...e, request: s };
|
|
137
138
|
}
|
|
138
139
|
}
|
|
139
140
|
const y = {
|
|
@@ -149,7 +150,7 @@ class S {
|
|
|
149
150
|
* @param options - Fetcher configuration options
|
|
150
151
|
*/
|
|
151
152
|
constructor(e = T) {
|
|
152
|
-
this.headers = y, this.interceptors = new F(), this.urlBuilder = new P(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new
|
|
153
|
+
this.headers = y, this.interceptors = new F(), this.urlBuilder = new P(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new q());
|
|
153
154
|
}
|
|
154
155
|
/**
|
|
155
156
|
* Make an HTTP request
|
|
@@ -177,18 +178,14 @@ class S {
|
|
|
177
178
|
const s = {
|
|
178
179
|
...this.headers || {},
|
|
179
180
|
...t.headers || {}
|
|
180
|
-
},
|
|
181
|
+
}, o = {
|
|
181
182
|
...t,
|
|
182
183
|
headers: Object.keys(s).length > 0 ? s : void 0
|
|
183
|
-
},
|
|
184
|
-
e,
|
|
185
|
-
t.pathParams,
|
|
186
|
-
t.queryParams
|
|
187
|
-
);
|
|
184
|
+
}, i = this.urlBuilder.build(e, t.path, t.query);
|
|
188
185
|
let n = {
|
|
189
186
|
fetcher: this,
|
|
190
|
-
url:
|
|
191
|
-
request:
|
|
187
|
+
url: i,
|
|
188
|
+
request: o,
|
|
192
189
|
response: void 0,
|
|
193
190
|
error: void 0
|
|
194
191
|
};
|
|
@@ -219,25 +216,25 @@ class S {
|
|
|
219
216
|
* @throws FetchTimeoutError Thrown when the request times out
|
|
220
217
|
*/
|
|
221
218
|
async timeoutFetch(e) {
|
|
222
|
-
const t = e.url, s = e.request,
|
|
223
|
-
if (!
|
|
219
|
+
const t = e.url, s = e.request, o = s.timeout, i = O(o, this.timeout);
|
|
220
|
+
if (!i)
|
|
224
221
|
return fetch(t, s);
|
|
225
222
|
const n = new AbortController(), u = {
|
|
226
223
|
...s,
|
|
227
224
|
signal: n.signal
|
|
228
225
|
};
|
|
229
226
|
let a = null;
|
|
230
|
-
const
|
|
227
|
+
const E = new Promise((L, w) => {
|
|
231
228
|
a = setTimeout(() => {
|
|
232
229
|
a && clearTimeout(a);
|
|
233
|
-
const m = new
|
|
234
|
-
n.abort(m),
|
|
235
|
-
},
|
|
230
|
+
const m = new l(e, i);
|
|
231
|
+
n.abort(m), w(m);
|
|
232
|
+
}, i);
|
|
236
233
|
});
|
|
237
234
|
try {
|
|
238
235
|
return await Promise.race([
|
|
239
236
|
fetch(t, u),
|
|
240
|
-
|
|
237
|
+
E
|
|
241
238
|
]);
|
|
242
239
|
} finally {
|
|
243
240
|
a && clearTimeout(a);
|
|
@@ -328,7 +325,7 @@ class S {
|
|
|
328
325
|
return this.methodFetch(c.OPTIONS, e, t);
|
|
329
326
|
}
|
|
330
327
|
}
|
|
331
|
-
const
|
|
328
|
+
const f = "default";
|
|
332
329
|
class R {
|
|
333
330
|
constructor() {
|
|
334
331
|
this.registrar = /* @__PURE__ */ new Map();
|
|
@@ -402,7 +399,7 @@ class R {
|
|
|
402
399
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
403
400
|
*/
|
|
404
401
|
get default() {
|
|
405
|
-
return this.requiredGet(
|
|
402
|
+
return this.requiredGet(f);
|
|
406
403
|
}
|
|
407
404
|
/**
|
|
408
405
|
* Set the default Fetcher instance
|
|
@@ -413,7 +410,7 @@ class R {
|
|
|
413
410
|
* fetcherRegistrar.default = fetcher;
|
|
414
411
|
*/
|
|
415
412
|
set default(e) {
|
|
416
|
-
this.register(
|
|
413
|
+
this.register(f, e);
|
|
417
414
|
}
|
|
418
415
|
/**
|
|
419
416
|
* Get a copy of all registered fetchers
|
|
@@ -429,8 +426,8 @@ class R {
|
|
|
429
426
|
return new Map(this.registrar);
|
|
430
427
|
}
|
|
431
428
|
}
|
|
432
|
-
const
|
|
433
|
-
class
|
|
429
|
+
const U = new R();
|
|
430
|
+
class I extends S {
|
|
434
431
|
/**
|
|
435
432
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
436
433
|
*
|
|
@@ -449,27 +446,27 @@ class N extends S {
|
|
|
449
446
|
* });
|
|
450
447
|
*/
|
|
451
448
|
constructor(e, t = T) {
|
|
452
|
-
super(t), this.name = e,
|
|
449
|
+
super(t), this.name = e, U.register(e, this);
|
|
453
450
|
}
|
|
454
451
|
}
|
|
455
|
-
const
|
|
452
|
+
const N = new I(f);
|
|
456
453
|
export {
|
|
457
454
|
d as ContentTypeHeader,
|
|
458
455
|
p as ContentTypeValues,
|
|
459
|
-
f as
|
|
456
|
+
f as DEFAULT_FETCHER_NAME,
|
|
457
|
+
T as DEFAULT_OPTIONS,
|
|
458
|
+
l as FetchTimeoutError,
|
|
460
459
|
S as Fetcher,
|
|
461
460
|
F as FetcherInterceptors,
|
|
462
461
|
R as FetcherRegistrar,
|
|
463
462
|
c as HttpMethod,
|
|
464
463
|
h as InterceptorManager,
|
|
465
|
-
|
|
466
|
-
|
|
464
|
+
I as NamedFetcher,
|
|
465
|
+
A as RequestField,
|
|
467
466
|
P as UrlBuilder,
|
|
468
467
|
g as combineURLs,
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
L as fetcher,
|
|
472
|
-
I as fetcherRegistrar,
|
|
468
|
+
N as fetcher,
|
|
469
|
+
U as fetcherRegistrar,
|
|
473
470
|
b as isAbsoluteURL,
|
|
474
471
|
O as resolveTimeout
|
|
475
472
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n.Fetcher={}))})(this,(function(n){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function
|
|
1
|
+
(function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n.Fetcher={}))})(this,(function(n){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function p(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class b{constructor(e){this.baseURL=e}build(e,t,s){const o=p(this.baseURL,e);let c=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(c+="?"+i)}return c}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const c=t[o];if(c===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(c)}):e}}function g(r,e){return typeof r<"u"?r:e}class f extends Error{constructor(e,t){const s=e.request?.method||"GET",o=`Request timeout of ${t}ms exceeded for ${s} ${e.url}`;super(o),this.name="FetchTimeoutError",this.exchange=e,Object.setPrototypeOf(this,f.prototype)}}var u=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(u||{}),F=(r=>(r.METHOD="method",r.BODY="body",r))(F||{});const l="Content-Type";var m=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(m||{});class T{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(const s of this.interceptors)s&&(t=await s.intercept(t));return t}}class w{constructor(){this.request=new T,this.response=new T,this.error=new T}}class q{intercept(e){const t=e.request;if(t.body===void 0||t.body===null||typeof t.body!="object"||t.body instanceof ArrayBuffer||ArrayBuffer.isView(t.body)||t.body instanceof Blob||t.body instanceof File||t.body instanceof URLSearchParams||t.body instanceof FormData||t.body instanceof ReadableStream)return e;const s={...t};s.body=JSON.stringify(t.body),s.headers||(s.headers={});const o=s.headers;return o[l]||(o[l]=m.APPLICATION_JSON),{...e,request:s}}}const P={[l]:m.APPLICATION_JSON},E={baseURL:"",headers:P};class A{constructor(e=E){this.headers=P,this.interceptors=new w,this.urlBuilder=new b(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new q)}async fetch(e,t={}){const s=await this.request(e,t);if(!s.response)throw new Error(`Request to ${s.url} failed with no response`);return s.response}async request(e,t={}){const s={...this.headers||{},...t.headers||{}},o={...t,headers:Object.keys(s).length>0?s:void 0},c=this.urlBuilder.build(e,t.path,t.query);let i={fetcher:this,url:c,request:o,response:void 0,error:void 0};try{const d={...i};i=await this.interceptors.request.intercept(d),i.response=await this.timeoutFetch(i);const a={...i};return i=await this.interceptors.response.intercept(a),i}catch(d){if(i.error=d,i=await this.interceptors.error.intercept(i),i.response)return i;throw i.error}}async timeoutFetch(e){const t=e.url,s=e.request,o=s.timeout,c=g(o,this.timeout);if(!c)return fetch(t,s);const i=new AbortController,d={...s,signal:i.signal};let a=null;const I=new Promise((D,N)=>{a=setTimeout(()=>{a&&clearTimeout(a);const U=new f(e,c);i.abort(U),N(U)},c)});try{return await Promise.race([fetch(t,d),I])}finally{a&&clearTimeout(a)}}async methodFetch(e,t,s={}){return this.fetch(t,{...s,method:e})}async get(e,t={}){return this.methodFetch(u.GET,e,t)}async post(e,t={}){return this.methodFetch(u.POST,e,t)}async put(e,t={}){return this.methodFetch(u.PUT,e,t)}async delete(e,t={}){return this.methodFetch(u.DELETE,e,t)}async patch(e,t={}){return this.methodFetch(u.PATCH,e,t)}async head(e,t={}){return this.methodFetch(u.HEAD,e,t)}async options(e,t={}){return this.methodFetch(u.OPTIONS,e,t)}}const y="default";class O{constructor(){this.registrar=new Map}register(e,t){this.registrar.set(e,t)}unregister(e){return this.registrar.delete(e)}get(e){return this.registrar.get(e)}requiredGet(e){const t=this.get(e);if(!t)throw new Error(`Fetcher ${e} not found`);return t}get default(){return this.requiredGet(y)}set default(e){this.register(y,e)}get fetchers(){return new Map(this.registrar)}}const R=new O;class S extends A{constructor(e,t=E){super(t),this.name=e,R.register(e,this)}}const L=new S(y);n.ContentTypeHeader=l,n.ContentTypeValues=m,n.DEFAULT_FETCHER_NAME=y,n.DEFAULT_OPTIONS=E,n.FetchTimeoutError=f,n.Fetcher=A,n.FetcherInterceptors=w,n.FetcherRegistrar=O,n.HttpMethod=u,n.InterceptorManager=T,n.NamedFetcher=S,n.RequestField=F,n.UrlBuilder=b,n.combineURLs=p,n.fetcher=L,n.fetcherRegistrar=R,n.isAbsoluteURL=h,n.resolveTimeout=g,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"namedFetcher.d.ts","sourceRoot":"","sources":["../src/namedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,
|
|
1
|
+
{"version":3,"file":"namedFetcher.d.ts","sourceRoot":"","sources":["../src/namedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAmB,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGrE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,YAAa,SAAQ,OAAQ,YAAW,YAAY;IAC/D;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;;;;;;;;;;OAgBG;gBACS,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAAgC;CAKpE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,OAAO,cAAyC,CAAC"}
|
package/dist/urlBuilder.d.ts
CHANGED
|
@@ -19,20 +19,20 @@ export declare class UrlBuilder implements BaseURLCapable {
|
|
|
19
19
|
* 构建完整的URL,包括路径参数替换和查询参数添加
|
|
20
20
|
*
|
|
21
21
|
* @param url - 需要构建的URL路径
|
|
22
|
-
* @param
|
|
23
|
-
* @param
|
|
22
|
+
* @param path - 路径参数对象,用于替换URL中的占位符(如{id})
|
|
23
|
+
* @param query - 查询参数对象,将被添加到URL查询字符串中
|
|
24
24
|
* @returns 完整的URL字符串
|
|
25
25
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
26
26
|
*/
|
|
27
|
-
build(url: string,
|
|
27
|
+
build(url: string, path?: Record<string, any>, query?: Record<string, any>): string;
|
|
28
28
|
/**
|
|
29
29
|
* 替换url中的占位符参数
|
|
30
30
|
*
|
|
31
31
|
* @param url - 包含占位符的路径字符串,如 "http://localhost/users/{id}/posts/{postId}"
|
|
32
|
-
* @param
|
|
32
|
+
* @param path - 路径参数对象,用于替换路径中的占位符
|
|
33
33
|
* @returns 替换占位符后的路径字符串
|
|
34
34
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
35
35
|
*/
|
|
36
|
-
interpolateUrl(url: string,
|
|
36
|
+
interpolateUrl(url: string, path?: Record<string, any>): string;
|
|
37
37
|
}
|
|
38
38
|
//# sourceMappingURL=urlBuilder.d.ts.map
|
package/dist/urlBuilder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;GAOG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;OAQG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,
|
|
1
|
+
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;GAOG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;OAQG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM;IAYT;;;;;;;OAOG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;CAWhE"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.5.5",
|
|
4
|
+
"description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
7
7
|
"http",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "vite build",
|
|
46
46
|
"test": "vitest run --coverage",
|
|
47
|
-
"lint": "
|
|
47
|
+
"lint": "eslint . --fix",
|
|
48
48
|
"clean": "rm -rf dist"
|
|
49
49
|
}
|
|
50
50
|
}
|