@ahoo-wang/fetcher 0.5.2 → 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 +7 -7
- package/README.zh-CN.md +6 -6
- package/dist/fetcher.d.ts +1 -1
- 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 +43 -42
- package/dist/index.umd.js +1 -1
- package/dist/namedFetcher.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
|
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 实例
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ 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
11
|
* Fetcher request configuration interface
|
|
12
12
|
*
|
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":"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,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC;IAC3B;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE5B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;GAUG;AACH,qBAAa,OAAQ,YAAW,cAAc,EAAE,cAAc;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAmB;IACnD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,UAAU,CAAa;IAC/B,YAAY,EAAE,mBAAmB,CAA6B;IAE9D;;;;OAIG;gBACS,OAAO,GAAE,cAAgC;IASrD;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAQzE;;;;;;;;OAQG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC;IA4CzB;;;;;;;;;;OAUG;YACW,YAAY;IA2C1B;;;;;;;OAOG;YACW,WAAW;IAWzB;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;CAGrB"}
|
|
@@ -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
|
@@ -23,12 +23,13 @@ class P {
|
|
|
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中的占位符参数
|
|
@@ -39,24 +40,24 @@ class P {
|
|
|
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,14 +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
|
+
}, i = this.urlBuilder.build(e, t.path, t.query);
|
|
184
185
|
let n = {
|
|
185
186
|
fetcher: this,
|
|
186
|
-
url:
|
|
187
|
-
request:
|
|
187
|
+
url: i,
|
|
188
|
+
request: o,
|
|
188
189
|
response: void 0,
|
|
189
190
|
error: void 0
|
|
190
191
|
};
|
|
@@ -215,25 +216,25 @@ class S {
|
|
|
215
216
|
* @throws FetchTimeoutError Thrown when the request times out
|
|
216
217
|
*/
|
|
217
218
|
async timeoutFetch(e) {
|
|
218
|
-
const t = e.url, s = e.request,
|
|
219
|
-
if (!
|
|
219
|
+
const t = e.url, s = e.request, o = s.timeout, i = O(o, this.timeout);
|
|
220
|
+
if (!i)
|
|
220
221
|
return fetch(t, s);
|
|
221
222
|
const n = new AbortController(), u = {
|
|
222
223
|
...s,
|
|
223
224
|
signal: n.signal
|
|
224
225
|
};
|
|
225
226
|
let a = null;
|
|
226
|
-
const
|
|
227
|
+
const E = new Promise((L, w) => {
|
|
227
228
|
a = setTimeout(() => {
|
|
228
229
|
a && clearTimeout(a);
|
|
229
|
-
const m = new
|
|
230
|
-
n.abort(m),
|
|
231
|
-
},
|
|
230
|
+
const m = new l(e, i);
|
|
231
|
+
n.abort(m), w(m);
|
|
232
|
+
}, i);
|
|
232
233
|
});
|
|
233
234
|
try {
|
|
234
235
|
return await Promise.race([
|
|
235
236
|
fetch(t, u),
|
|
236
|
-
|
|
237
|
+
E
|
|
237
238
|
]);
|
|
238
239
|
} finally {
|
|
239
240
|
a && clearTimeout(a);
|
|
@@ -324,7 +325,7 @@ class S {
|
|
|
324
325
|
return this.methodFetch(c.OPTIONS, e, t);
|
|
325
326
|
}
|
|
326
327
|
}
|
|
327
|
-
const
|
|
328
|
+
const f = "default";
|
|
328
329
|
class R {
|
|
329
330
|
constructor() {
|
|
330
331
|
this.registrar = /* @__PURE__ */ new Map();
|
|
@@ -398,7 +399,7 @@ class R {
|
|
|
398
399
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
399
400
|
*/
|
|
400
401
|
get default() {
|
|
401
|
-
return this.requiredGet(
|
|
402
|
+
return this.requiredGet(f);
|
|
402
403
|
}
|
|
403
404
|
/**
|
|
404
405
|
* Set the default Fetcher instance
|
|
@@ -409,7 +410,7 @@ class R {
|
|
|
409
410
|
* fetcherRegistrar.default = fetcher;
|
|
410
411
|
*/
|
|
411
412
|
set default(e) {
|
|
412
|
-
this.register(
|
|
413
|
+
this.register(f, e);
|
|
413
414
|
}
|
|
414
415
|
/**
|
|
415
416
|
* Get a copy of all registered fetchers
|
|
@@ -425,8 +426,8 @@ class R {
|
|
|
425
426
|
return new Map(this.registrar);
|
|
426
427
|
}
|
|
427
428
|
}
|
|
428
|
-
const
|
|
429
|
-
class
|
|
429
|
+
const U = new R();
|
|
430
|
+
class I extends S {
|
|
430
431
|
/**
|
|
431
432
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
432
433
|
*
|
|
@@ -445,27 +446,27 @@ class N extends S {
|
|
|
445
446
|
* });
|
|
446
447
|
*/
|
|
447
448
|
constructor(e, t = T) {
|
|
448
|
-
super(t), this.name = e,
|
|
449
|
+
super(t), this.name = e, U.register(e, this);
|
|
449
450
|
}
|
|
450
451
|
}
|
|
451
|
-
const
|
|
452
|
+
const N = new I(f);
|
|
452
453
|
export {
|
|
453
454
|
d as ContentTypeHeader,
|
|
454
455
|
p as ContentTypeValues,
|
|
455
|
-
f as
|
|
456
|
+
f as DEFAULT_FETCHER_NAME,
|
|
457
|
+
T as DEFAULT_OPTIONS,
|
|
458
|
+
l as FetchTimeoutError,
|
|
456
459
|
S as Fetcher,
|
|
457
460
|
F as FetcherInterceptors,
|
|
458
461
|
R as FetcherRegistrar,
|
|
459
462
|
c as HttpMethod,
|
|
460
463
|
h as InterceptorManager,
|
|
461
|
-
|
|
462
|
-
|
|
464
|
+
I as NamedFetcher,
|
|
465
|
+
A as RequestField,
|
|
463
466
|
P as UrlBuilder,
|
|
464
467
|
g as combineURLs,
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
L as fetcher,
|
|
468
|
-
I as fetcherRegistrar,
|
|
468
|
+
N as fetcher,
|
|
469
|
+
U as fetcherRegistrar,
|
|
469
470
|
b as isAbsoluteURL,
|
|
470
471
|
O as resolveTimeout
|
|
471
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/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
|
}
|