@ahoo-wang/fetcher 0.0.9 → 0.1.1
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 +5 -4
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.es.js +97 -64
- package/dist/index.umd.js +1 -1
- package/dist/requestBodyInterceptor.d.ts +31 -0
- package/dist/requestBodyInterceptor.d.ts.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,9 @@ A modern HTTP client library based on the Fetch API, designed to simplify and op
|
|
|
15
15
|
- **Timeout Settings**: Request timeout can be configured.
|
|
16
16
|
- **Request Interceptors**: Supports modifying requests before they are sent.
|
|
17
17
|
- **Response Interceptors**: Supports processing responses after they are returned.
|
|
18
|
+
- **Error Interceptors**: Supports handling errors during the request lifecycle.
|
|
18
19
|
- **Modular Design**: Clear code structure for easy maintenance and extension.
|
|
20
|
+
- **Automatic Request Body Conversion**: Automatically converts plain objects to JSON and sets appropriate Content-Type headers.
|
|
19
21
|
- **TypeScript Support**: Complete TypeScript type definitions.
|
|
20
22
|
|
|
21
23
|
## Installation
|
|
@@ -63,11 +65,10 @@ fetcher
|
|
|
63
65
|
console.error(error);
|
|
64
66
|
});
|
|
65
67
|
|
|
66
|
-
// POST request with JSON body
|
|
68
|
+
// POST request with JSON body (automatically converted to JSON string)
|
|
67
69
|
fetcher
|
|
68
70
|
.post('/users', {
|
|
69
|
-
body:
|
|
70
|
-
headers: { 'Content-Type': 'application/json' },
|
|
71
|
+
body: { name: 'John Doe', email: 'john@example.com' },
|
|
71
72
|
})
|
|
72
73
|
.then(response => {
|
|
73
74
|
console.log(response.data);
|
|
@@ -125,7 +126,7 @@ Core HTTP client class that provides various HTTP methods.
|
|
|
125
126
|
#### Constructor
|
|
126
127
|
|
|
127
128
|
```typescript
|
|
128
|
-
new Fetcher(options
|
|
129
|
+
new Fetcher(options: FetcherOptions = defaultOptions)
|
|
129
130
|
```
|
|
130
131
|
|
|
131
132
|
**Parameters:**
|
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,MAAM,eAAe,CAAC;
|
|
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,MAAM,eAAe,CAAC;AAGpD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CAAG;AAWrB;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,cAAc,EAAE,WAAW;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;;;;;;;;;GAUG;AACH,qBAAa,OAAQ,YAAW,cAAc,EAAE,cAAc;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAkB;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,UAAU,CAAa;IAC/B,YAAY,EAAE,mBAAmB,CAA6B;IAE9D;;;;OAIG;gBACS,OAAO,GAAE,cAA+B;IASpD;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAmCzE;;;;;;;;;;OAUG;YACW,YAAY;IAoC1B;;;;;;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;IAOpB;;;;;;OAMG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;OAMG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;OAMG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;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;IAOpB;;;;;;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;CAMrB"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function y(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
4
|
function T(r, e) {
|
|
5
|
-
return
|
|
5
|
+
return y(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class b {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -22,11 +22,11 @@ class E {
|
|
|
22
22
|
* @returns 完整的URL字符串
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
|
-
build(e, t,
|
|
26
|
-
let
|
|
27
|
-
if (
|
|
28
|
-
const
|
|
29
|
-
|
|
25
|
+
build(e, t, n) {
|
|
26
|
+
let s = T(this.baseURL, e), o = this.interpolateUrl(s, t);
|
|
27
|
+
if (n) {
|
|
28
|
+
const i = new URLSearchParams(n).toString();
|
|
29
|
+
i && (o += "?" + i);
|
|
30
30
|
}
|
|
31
31
|
return o;
|
|
32
32
|
}
|
|
@@ -39,26 +39,26 @@ class E {
|
|
|
39
39
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
40
40
|
*/
|
|
41
41
|
interpolateUrl(e, t) {
|
|
42
|
-
return t ? e.replace(/{([^}]+)}/g, (
|
|
43
|
-
const o = t[
|
|
42
|
+
return t ? e.replace(/{([^}]+)}/g, (n, s) => {
|
|
43
|
+
const o = t[s];
|
|
44
44
|
if (o === void 0)
|
|
45
|
-
throw new Error(`Missing required path parameter: ${
|
|
45
|
+
throw new Error(`Missing required path parameter: ${s}`);
|
|
46
46
|
return String(o);
|
|
47
47
|
}) : e;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
function E(r, e) {
|
|
51
51
|
return typeof r < "u" ? r : e;
|
|
52
52
|
}
|
|
53
|
-
class
|
|
54
|
-
constructor(e, t,
|
|
55
|
-
const
|
|
56
|
-
super(
|
|
53
|
+
class d extends Error {
|
|
54
|
+
constructor(e, t, n) {
|
|
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, d.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
|
|
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 || {}), P = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(P || {});
|
|
60
|
+
const h = "Content-Type";
|
|
61
|
+
var f = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(f || {});
|
|
62
62
|
class a {
|
|
63
63
|
constructor() {
|
|
64
64
|
this.interceptors = [];
|
|
@@ -92,30 +92,63 @@ class a {
|
|
|
92
92
|
*/
|
|
93
93
|
async intercept(e) {
|
|
94
94
|
let t = e;
|
|
95
|
-
for (let
|
|
96
|
-
|
|
95
|
+
for (let n of this.interceptors)
|
|
96
|
+
n && (t = await n.intercept(t));
|
|
97
97
|
return t;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
class
|
|
100
|
+
class O {
|
|
101
101
|
constructor() {
|
|
102
102
|
this.request = new a(), this.response = new a(), this.error = new a();
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
class w {
|
|
106
|
+
/**
|
|
107
|
+
* 尝试转换请求体为合法的 fetch API body 类型
|
|
108
|
+
*
|
|
109
|
+
* 根据 Fetch API 规范,body 可以是多种类型,但对于普通对象,需要转换为 JSON 字符串
|
|
110
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body
|
|
111
|
+
*
|
|
112
|
+
* 支持的类型:
|
|
113
|
+
* - a string
|
|
114
|
+
* - ArrayBuffer
|
|
115
|
+
* - TypedArray
|
|
116
|
+
* - DataView
|
|
117
|
+
* - Blob
|
|
118
|
+
* - File
|
|
119
|
+
* - URLSearchParams
|
|
120
|
+
* - FormData
|
|
121
|
+
* - ReadableStream
|
|
122
|
+
*
|
|
123
|
+
* 对于不支持的 object 类型(如普通对象),将自动转换为 JSON 字符串
|
|
124
|
+
*
|
|
125
|
+
* @param request 请求参数
|
|
126
|
+
* @returns 转换后的请求
|
|
127
|
+
*/
|
|
128
|
+
intercept(e) {
|
|
129
|
+
if (e.body === void 0 || e.body === null || typeof e.body != "object" || e.body instanceof ArrayBuffer || ArrayBuffer.isView(e.body) || // 包括 TypedArray 和 DataView
|
|
130
|
+
e.body instanceof Blob || e.body instanceof File || e.body instanceof URLSearchParams || e.body instanceof FormData || e.body instanceof ReadableStream)
|
|
131
|
+
return e;
|
|
132
|
+
const t = { ...e };
|
|
133
|
+
t.body = JSON.stringify(e.body), t.headers || (t.headers = {});
|
|
134
|
+
const n = t.headers;
|
|
135
|
+
return n[h] || (n[h] = f.APPLICATION_JSON), t;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const l = {
|
|
139
|
+
[h]: f.APPLICATION_JSON
|
|
140
|
+
}, A = {
|
|
108
141
|
baseURL: "",
|
|
109
|
-
headers:
|
|
142
|
+
headers: l
|
|
110
143
|
};
|
|
111
|
-
class
|
|
144
|
+
class g {
|
|
112
145
|
/**
|
|
113
146
|
* 创建Fetcher实例
|
|
114
147
|
*
|
|
115
148
|
* @param options - Fetcher配置选项
|
|
116
149
|
*/
|
|
117
|
-
constructor(e =
|
|
118
|
-
this.headers =
|
|
150
|
+
constructor(e = A) {
|
|
151
|
+
this.headers = l, this.interceptors = new O(), this.urlBuilder = new b(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new w());
|
|
119
152
|
}
|
|
120
153
|
/**
|
|
121
154
|
* 发起HTTP请求
|
|
@@ -125,25 +158,25 @@ class A {
|
|
|
125
158
|
* @returns Promise<Response> HTTP响应
|
|
126
159
|
*/
|
|
127
160
|
async fetch(e, t = {}) {
|
|
128
|
-
const
|
|
161
|
+
const n = {
|
|
129
162
|
...this.headers || {},
|
|
130
163
|
...t.headers || {}
|
|
131
164
|
};
|
|
132
|
-
let
|
|
165
|
+
let s = {
|
|
133
166
|
...t,
|
|
134
|
-
headers: Object.keys(
|
|
167
|
+
headers: Object.keys(n).length > 0 ? n : void 0
|
|
135
168
|
};
|
|
136
|
-
const o = this.urlBuilder.build(
|
|
137
|
-
e,
|
|
138
|
-
t.pathParams,
|
|
139
|
-
t.queryParams
|
|
140
|
-
);
|
|
141
|
-
i = await this.interceptors.request.intercept(i);
|
|
142
169
|
try {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
s = await this.interceptors.request.intercept(s);
|
|
171
|
+
const o = this.urlBuilder.build(
|
|
172
|
+
e,
|
|
173
|
+
t.pathParams,
|
|
174
|
+
t.queryParams
|
|
175
|
+
);
|
|
176
|
+
let i = await this.timeoutFetch(o, s);
|
|
177
|
+
return i = await this.interceptors.response.intercept(i), i;
|
|
178
|
+
} catch (o) {
|
|
179
|
+
throw await this.interceptors.error.intercept(o);
|
|
147
180
|
}
|
|
148
181
|
}
|
|
149
182
|
/**
|
|
@@ -158,25 +191,25 @@ class A {
|
|
|
158
191
|
* @throws FetchTimeoutError 当请求超时时抛出
|
|
159
192
|
*/
|
|
160
193
|
async timeoutFetch(e, t) {
|
|
161
|
-
const
|
|
162
|
-
if (!
|
|
194
|
+
const n = E(t.timeout, this.timeout);
|
|
195
|
+
if (!n)
|
|
163
196
|
return fetch(e, t);
|
|
164
|
-
const
|
|
197
|
+
const s = new AbortController(), o = {
|
|
165
198
|
...t,
|
|
166
|
-
signal:
|
|
199
|
+
signal: s.signal
|
|
167
200
|
};
|
|
168
|
-
let
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
},
|
|
201
|
+
let i = null;
|
|
202
|
+
const p = new Promise((S, m) => {
|
|
203
|
+
i = setTimeout(() => {
|
|
204
|
+
i && clearTimeout(i);
|
|
205
|
+
const u = new d(e, t, n);
|
|
206
|
+
s.abort(u), m(u);
|
|
207
|
+
}, n);
|
|
175
208
|
});
|
|
176
209
|
try {
|
|
177
|
-
return await Promise.race([fetch(e, o),
|
|
210
|
+
return await Promise.race([fetch(e, o), p]);
|
|
178
211
|
} finally {
|
|
179
|
-
|
|
212
|
+
i && clearTimeout(i);
|
|
180
213
|
}
|
|
181
214
|
}
|
|
182
215
|
/**
|
|
@@ -272,16 +305,16 @@ class A {
|
|
|
272
305
|
}
|
|
273
306
|
}
|
|
274
307
|
export {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
308
|
+
h as ContentTypeHeader,
|
|
309
|
+
f as ContentTypeValues,
|
|
310
|
+
d as FetchTimeoutError,
|
|
311
|
+
g as Fetcher,
|
|
312
|
+
O as FetcherInterceptors,
|
|
280
313
|
c as HttpMethod,
|
|
281
314
|
a as InterceptorManager,
|
|
282
|
-
|
|
283
|
-
|
|
315
|
+
P as RequestField,
|
|
316
|
+
b as UrlBuilder,
|
|
284
317
|
T as combineURLs,
|
|
285
|
-
|
|
286
|
-
|
|
318
|
+
y as isAbsoluteURL,
|
|
319
|
+
E as resolveTimeout
|
|
287
320
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(i,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(i=typeof globalThis<"u"?globalThis:i||self,h(i.Fetcher={}))})(this,(function(i){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function m(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class p{constructor(e){this.baseURL=e}build(e,t,n){let o=m(this.baseURL,e),c=this.interpolateUrl(o,t);if(n){const s=new URLSearchParams(n).toString();s&&(c+="?"+s)}return c}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(n,o)=>{const c=t[o];if(c===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(c)}):e}}function y(r,e){return typeof r<"u"?r:e}class d extends Error{constructor(e,t,n){const o=`Request timeout of ${n}ms exceeded for ${t?.method||"GET"} ${e}`;super(o),this.name="FetchTimeoutError",this.url=e,this.request=t,Object.setPrototypeOf(this,d.prototype)}}var a=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(a||{}),T=(r=>(r.METHOD="method",r.BODY="body",r))(T||{});const u="Content-Type";var f=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(f||{});class l{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 n of this.interceptors)n&&(t=await n.intercept(t));return t}}class b{constructor(){this.request=new l,this.response=new l,this.error=new l}}class O{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 n=t.headers;return n[u]||(n[u]=f.APPLICATION_JSON),t}}const E={[u]:f.APPLICATION_JSON},w={baseURL:"",headers:E};class S{constructor(e=w){this.headers=E,this.interceptors=new b,this.urlBuilder=new p(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new O)}async fetch(e,t={}){const n={...this.headers||{},...t.headers||{}};let o={...t,headers:Object.keys(n).length>0?n:void 0};try{o=await this.interceptors.request.intercept(o);const c=this.urlBuilder.build(e,t.pathParams,t.queryParams);let s=await this.timeoutFetch(c,o);return s=await this.interceptors.response.intercept(s),s}catch(c){throw await this.interceptors.error.intercept(c)}}async timeoutFetch(e,t){const n=y(t.timeout,this.timeout);if(!n)return fetch(e,t);const o=new AbortController,c={...t,signal:o.signal};let s=null;const A=new Promise((U,g)=>{s=setTimeout(()=>{s&&clearTimeout(s);const P=new d(e,t,n);o.abort(P),g(P)},n)});try{return await Promise.race([fetch(e,c),A])}finally{s&&clearTimeout(s)}}async get(e,t={}){return this.fetch(e,{...t,method:a.GET})}async post(e,t={}){return this.fetch(e,{...t,method:a.POST})}async put(e,t={}){return this.fetch(e,{...t,method:a.PUT})}async delete(e,t={}){return this.fetch(e,{...t,method:a.DELETE})}async patch(e,t={}){return this.fetch(e,{...t,method:a.PATCH})}async head(e,t={}){return this.fetch(e,{...t,method:a.HEAD})}async options(e,t={}){return this.fetch(e,{...t,method:a.OPTIONS})}}i.ContentTypeHeader=u,i.ContentTypeValues=f,i.FetchTimeoutError=d,i.Fetcher=S,i.FetcherInterceptors=b,i.HttpMethod=a,i.InterceptorManager=l,i.RequestField=T,i.UrlBuilder=p,i.combineURLs=m,i.isAbsoluteURL=h,i.resolveTimeout=y,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RequestInterceptor } from './interceptor';
|
|
2
|
+
import { FetcherRequest } from './fetcher';
|
|
3
|
+
/**
|
|
4
|
+
* 请求体拦截器,负责将普通对象转换为JSON字符串
|
|
5
|
+
*/
|
|
6
|
+
export declare class RequestBodyInterceptor implements RequestInterceptor {
|
|
7
|
+
/**
|
|
8
|
+
* 尝试转换请求体为合法的 fetch API body 类型
|
|
9
|
+
*
|
|
10
|
+
* 根据 Fetch API 规范,body 可以是多种类型,但对于普通对象,需要转换为 JSON 字符串
|
|
11
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body
|
|
12
|
+
*
|
|
13
|
+
* 支持的类型:
|
|
14
|
+
* - a string
|
|
15
|
+
* - ArrayBuffer
|
|
16
|
+
* - TypedArray
|
|
17
|
+
* - DataView
|
|
18
|
+
* - Blob
|
|
19
|
+
* - File
|
|
20
|
+
* - URLSearchParams
|
|
21
|
+
* - FormData
|
|
22
|
+
* - ReadableStream
|
|
23
|
+
*
|
|
24
|
+
* 对于不支持的 object 类型(如普通对象),将自动转换为 JSON 字符串
|
|
25
|
+
*
|
|
26
|
+
* @param request 请求参数
|
|
27
|
+
* @returns 转换后的请求
|
|
28
|
+
*/
|
|
29
|
+
intercept(request: FetcherRequest): FetcherRequest;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=requestBodyInterceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C;;GAEG;AACH,qBAAa,sBAAuB,YAAW,kBAAkB;IAC/D;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc;CA0CnD"}
|