soon-fetch 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 +50 -105
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +41 -45
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
> - ⭐ rapid define a request api
|
|
11
11
|
> - ⌛ timeout disconnect
|
|
12
12
|
> - 🔤 automatic parse or serialization of JSON
|
|
13
|
-
> - 📏 .min size less than **
|
|
13
|
+
> - 📏 .min size less than **3K**, smaller after zip
|
|
14
14
|
> - 💡 smart type tips with Typescript
|
|
15
15
|
|
|
16
16
|
- [Example](#example)
|
|
@@ -33,12 +33,10 @@
|
|
|
33
33
|
|
|
34
34
|
```typescript
|
|
35
35
|
export const soon = createSoon({
|
|
36
|
-
baseURL: "/",
|
|
37
|
-
|
|
36
|
+
baseURL: "/api",
|
|
37
|
+
baseOptions: () => ({
|
|
38
38
|
timeout: 20 * 1000,
|
|
39
|
-
headers:
|
|
40
|
-
Authorization: localStorage.getItem("token") ?? "",
|
|
41
|
-
}),
|
|
39
|
+
headers: { Authorization: localStorage.getItem("token") ?? "" },
|
|
42
40
|
}),
|
|
43
41
|
fetching(url, options) {
|
|
44
42
|
return fetch(url, options).then((res) => res.json());
|
|
@@ -125,7 +123,7 @@ soon.get(url, { timeout: 1000 * 20 });
|
|
|
125
123
|
import { createSoon } from "soon";
|
|
126
124
|
|
|
127
125
|
declare function createSoon<Options extends SoonOptions = SoonOptions>(
|
|
128
|
-
soonInit?: SoonInit<Options
|
|
126
|
+
soonInit?: NoInfer<SoonInit<Options>>
|
|
129
127
|
);
|
|
130
128
|
|
|
131
129
|
// options would overwritten by request level options ,but the headers will be merged
|
|
@@ -133,11 +131,23 @@ export type SoonInit<Options> = {
|
|
|
133
131
|
//**url prefix */
|
|
134
132
|
baseURL?: string;
|
|
135
133
|
/**the fetch http options */
|
|
136
|
-
|
|
134
|
+
baseOptions?: () => Options;
|
|
137
135
|
/** can use return a new fetch request instead*/
|
|
138
136
|
fetching?: (
|
|
137
|
+
//parsed url with query and params
|
|
139
138
|
url: string,
|
|
140
|
-
options
|
|
139
|
+
//parsed options with timeout signal,headers,JSON stringified body
|
|
140
|
+
options: Options & {
|
|
141
|
+
headers: Headers;
|
|
142
|
+
},
|
|
143
|
+
raw: {
|
|
144
|
+
// raw base info defined above
|
|
145
|
+
baseURL?: string;
|
|
146
|
+
baseOptions?: () => Options;
|
|
147
|
+
// raw url,options passed from request
|
|
148
|
+
url: string;
|
|
149
|
+
options?: Options;
|
|
150
|
+
}
|
|
141
151
|
) => Promise<any>;
|
|
142
152
|
};
|
|
143
153
|
```
|
|
@@ -149,8 +159,11 @@ soon.request(url[,options])
|
|
|
149
159
|
```
|
|
150
160
|
|
|
151
161
|
Request data can choose `query` `params` `body` for easy specification
|
|
162
|
+
`body` can pass json without stringified
|
|
152
163
|
|
|
153
164
|
```typescript
|
|
165
|
+
//fetch(input: RequestInfo | URL, init?: RequestInit)
|
|
166
|
+
//RequestInit is the init params type of fetch
|
|
154
167
|
type SoonOptions = {
|
|
155
168
|
/** url search params like `api/info?name=yes` {name:"yes"} passed here*/
|
|
156
169
|
query?:
|
|
@@ -163,46 +176,6 @@ type SoonOptions = {
|
|
|
163
176
|
params?: Record<string, string | number>;
|
|
164
177
|
/** unit ms */
|
|
165
178
|
timeout?: number;
|
|
166
|
-
/***** vanilla fetch props *****/
|
|
167
|
-
//body can pass json without stringified
|
|
168
|
-
body?: any;
|
|
169
|
-
signal?: AbortSignal;
|
|
170
|
-
method?:
|
|
171
|
-
| "get"
|
|
172
|
-
| "GET"
|
|
173
|
-
| "delete"
|
|
174
|
-
| "DELETE"
|
|
175
|
-
| "head"
|
|
176
|
-
| "HEAD"
|
|
177
|
-
| "options"
|
|
178
|
-
| "OPTIONS"
|
|
179
|
-
| "post"
|
|
180
|
-
| "POST"
|
|
181
|
-
| "put"
|
|
182
|
-
| "PUT"
|
|
183
|
-
| "patch"
|
|
184
|
-
| "PATCH"
|
|
185
|
-
| "purge"
|
|
186
|
-
| "PURGE"
|
|
187
|
-
| "link"
|
|
188
|
-
| "LINK"
|
|
189
|
-
| "unlink"
|
|
190
|
-
| "UNLINK";
|
|
191
|
-
mode?: "cors" | "no-cors" | "same-origin";
|
|
192
|
-
cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached";
|
|
193
|
-
credentials?: "include" | "same-origin" | "omit";
|
|
194
|
-
headers?: Headers;
|
|
195
|
-
redirect?: "manual" | "follow" | "error";
|
|
196
|
-
referrerPolicy?:
|
|
197
|
-
| "no-referrer"
|
|
198
|
-
| "no-referrer-when-downgrade"
|
|
199
|
-
| "origin"
|
|
200
|
-
| "origin-when-cross-origin"
|
|
201
|
-
| "same-origin"
|
|
202
|
-
| "strict-origin"
|
|
203
|
-
| "strict-origin-when-cross-origin"
|
|
204
|
-
| "unsafe-url";
|
|
205
|
-
integrity?: string;
|
|
206
179
|
};
|
|
207
180
|
```
|
|
208
181
|
|
|
@@ -215,8 +188,8 @@ Default : return raw fetch Response , you can customize it in `fetching` params
|
|
|
215
188
|
If you like this library , you can give a **star** on github.
|
|
216
189
|
GitHub: https://github.com/leafio/soon-fetch
|
|
217
190
|
|
|
218
|
-
> I'm looking for a frontend job
|
|
219
|
-
Email: leafnote@outlook.com
|
|
191
|
+
> I'm looking for a frontend job , expecting an offer or chance for me .
|
|
192
|
+
> Email: leafnote@outlook.com
|
|
220
193
|
|
|
221
194
|
[English](#soon-fetch) | [中文](#soon-fetch-1) | [Installation](#安装-installation)
|
|
222
195
|
|
|
@@ -224,13 +197,13 @@ Email: leafnote@outlook.com
|
|
|
224
197
|
|
|
225
198
|
#### soon-fetch
|
|
226
199
|
|
|
227
|
-
**极轻量的请求库,不到
|
|
200
|
+
**极轻量的请求库,不到 3K**
|
|
228
201
|
|
|
229
202
|
> - 🌐 自动解析 rest Url 的参数
|
|
230
203
|
> - ⭐ 快捷定义请求 api
|
|
231
204
|
> - ⌛ 超时断开
|
|
232
205
|
> - 🔤 自动处理 JSON
|
|
233
|
-
> - 📏 不到 **
|
|
206
|
+
> - 📏 不到 **3K** , zip 后会更小
|
|
234
207
|
> - 💡 用 typescript 有智能类型提醒
|
|
235
208
|
|
|
236
209
|
- [示例](#示例)
|
|
@@ -254,13 +227,11 @@ Email: leafnote@outlook.com
|
|
|
254
227
|
> [github: soon-admin-react-nextjs ](https://github.com/leafio/soon-admin-react-nextjs)
|
|
255
228
|
|
|
256
229
|
```typescript
|
|
257
|
-
export const soon = createSoon
|
|
258
|
-
baseURL: "/",
|
|
259
|
-
|
|
230
|
+
export const soon = createSoon({
|
|
231
|
+
baseURL: "/api",
|
|
232
|
+
baseOptions: () => ({
|
|
260
233
|
timeout: 20 * 1000,
|
|
261
|
-
headers:
|
|
262
|
-
Authorization: localStorage.getItem("token") ?? "",
|
|
263
|
-
}),
|
|
234
|
+
headers: { Authorization: localStorage.getItem("token") ?? "" },
|
|
264
235
|
}),
|
|
265
236
|
fetching(url, options) {
|
|
266
237
|
return fetch(url, options).then((res) => res.json());
|
|
@@ -346,18 +317,30 @@ soon.get(url, { timeout: 1000 * 20 });
|
|
|
346
317
|
import { createSoon } from "soon";
|
|
347
318
|
|
|
348
319
|
declare function createSoon<Options extends SoonOptions = SoonOptions>(
|
|
349
|
-
soonInit?: SoonInit<Options
|
|
320
|
+
soonInit?: NoInfer<SoonInit<Options>>
|
|
350
321
|
);
|
|
351
322
|
|
|
352
323
|
// 实例级options会被请求级options覆盖,但headers仅同key的被覆盖,其他合并
|
|
353
324
|
export type SoonInit<Options> = {
|
|
354
325
|
baseURL?: string;
|
|
355
326
|
//默认的options
|
|
356
|
-
|
|
327
|
+
baseOptions?: () => Options;
|
|
357
328
|
//可返回自定义的fetch请求
|
|
358
329
|
fetching?: (
|
|
330
|
+
//根据 baseURL query params 解析后的url
|
|
359
331
|
url: string,
|
|
360
|
-
|
|
332
|
+
//包含超时signal,传来的JSON类型body已被stringified,并包含content-type json 的 headers
|
|
333
|
+
options: Options & {
|
|
334
|
+
headers: Headers;
|
|
335
|
+
},
|
|
336
|
+
raw: {
|
|
337
|
+
// 上方定义的 baseURL baseOptions
|
|
338
|
+
baseURL?: string;
|
|
339
|
+
baseOptions?: () => Options;
|
|
340
|
+
// 请求时传来的 url options
|
|
341
|
+
url: string;
|
|
342
|
+
options?: Options;
|
|
343
|
+
}
|
|
361
344
|
) => Promise<any>;
|
|
362
345
|
};
|
|
363
346
|
```
|
|
@@ -369,9 +352,12 @@ soon.request(url[,options])
|
|
|
369
352
|
```
|
|
370
353
|
|
|
371
354
|
请求数据可以选择 _`query`_ _`params`_ _`body`_ ,易于传递。
|
|
355
|
+
`body` 可直接传递 JSON 而不必 stringified
|
|
372
356
|
|
|
373
357
|
```typescript
|
|
374
|
-
|
|
358
|
+
//fetch(input: RequestInfo | URL, init?: RequestInit)
|
|
359
|
+
//RequestInit 为原生 fetch 的选项类型
|
|
360
|
+
type SoonOptions = RequestInit & {
|
|
375
361
|
/** url ?后的参数 `api/info?name=yes` 传递 {name:"yes"}*/
|
|
376
362
|
query?:
|
|
377
363
|
| Record<
|
|
@@ -383,47 +369,6 @@ type SoonOptions = {
|
|
|
383
369
|
params?: Record<string, string | number>;
|
|
384
370
|
/** unit 毫秒 */
|
|
385
371
|
timeout?: number;
|
|
386
|
-
|
|
387
|
-
/*** 原生fetch 参数*/
|
|
388
|
-
//可直接传递JSON而不必stringified
|
|
389
|
-
body?: any;
|
|
390
|
-
signal?: AbortSignal;
|
|
391
|
-
method?:
|
|
392
|
-
| "get"
|
|
393
|
-
| "GET"
|
|
394
|
-
| "delete"
|
|
395
|
-
| "DELETE"
|
|
396
|
-
| "head"
|
|
397
|
-
| "HEAD"
|
|
398
|
-
| "options"
|
|
399
|
-
| "OPTIONS"
|
|
400
|
-
| "post"
|
|
401
|
-
| "POST"
|
|
402
|
-
| "put"
|
|
403
|
-
| "PUT"
|
|
404
|
-
| "patch"
|
|
405
|
-
| "PATCH"
|
|
406
|
-
| "purge"
|
|
407
|
-
| "PURGE"
|
|
408
|
-
| "link"
|
|
409
|
-
| "LINK"
|
|
410
|
-
| "unlink"
|
|
411
|
-
| "UNLINK";
|
|
412
|
-
mode?: "cors" | "no-cors" | "same-origin";
|
|
413
|
-
cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached";
|
|
414
|
-
credentials?: "include" | "same-origin" | "omit";
|
|
415
|
-
headers?: Headers;
|
|
416
|
-
redirect?: "manual" | "follow" | "error";
|
|
417
|
-
referrerPolicy?:
|
|
418
|
-
| "no-referrer"
|
|
419
|
-
| "no-referrer-when-downgrade"
|
|
420
|
-
| "origin"
|
|
421
|
-
| "origin-when-cross-origin"
|
|
422
|
-
| "same-origin"
|
|
423
|
-
| "strict-origin"
|
|
424
|
-
| "strict-origin-when-cross-origin"
|
|
425
|
-
| "unsafe-url";
|
|
426
|
-
integrity?: string;
|
|
427
372
|
};
|
|
428
373
|
```
|
|
429
374
|
|
|
@@ -436,7 +381,7 @@ type SoonOptions = {
|
|
|
436
381
|
喜欢 soon-fetch 的话 , 在 github 上给个 **star** 吧.
|
|
437
382
|
GitHub: https://github.com/leafio/soon-fetch
|
|
438
383
|
|
|
439
|
-
>
|
|
384
|
+
> 我目前在找前端的工作,有岗位机会的话,可以联系我。
|
|
440
385
|
> Email: leafnote@outlook.com
|
|
441
386
|
|
|
442
387
|
[English](#soon-fetch) | [中文](#soon-fetch-1) | [Installation](#安装-installation)
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=t=>{
|
|
1
|
+
"use strict";const t=t=>{const e=[],s=t.match(/\:([^:\/\d]+)\/?/g);return s&&s.forEach((t=>{e.push(t.replace(/\//g,"").replace(/:/g,""))})),e},e=(t="")=>t.startsWith("http"),s=(t="")=>e(t)?t:((t="")=>{let e=t;return t&&(e=((t="")=>t.endsWith("/")?t.slice(0,-1):t)(e),t.startsWith("/")||(e="/"+e)),e})(t),r=t=>{if(!t)return[];if(t instanceof URLSearchParams||"string"==typeof t||Array.isArray(t))return Array.from(new URLSearchParams(t).entries());const e=[];return Object.keys(t).forEach((s=>{const r=t[s];(Array.isArray(r)?r:[r]).forEach((t=>{e.push([s,null!=t?t:""])}))})),e},n=(n,a)=>{const{query:o,params:c,baseURL:i}=a;let h=n.trim();t(n).forEach((t=>{c&&(h=h.replace(":"+t,""+c[t]))}));const[u,p]=h.split("?");let f=new URLSearchParams([...r(p),...r(o)]),l=((t,r)=>{let n=s(t);return e(n)||(n=s(r)+n),n})(u,i);return f.size&&(l=l+"?"+f),l},a=(...t)=>{const e=new Headers;return t.forEach((t=>{t&&new Headers(t).forEach(((t,s)=>{e.set(s,t)}))})),e};function o(t,e,s){const{baseURL:r,fetching:o}=s||{};let c;s&&(c=s.baseOptions?s.baseOptions:s.options);const i={url:t,options:e,baseURL:r,baseOptions:c},[h,u]=function(t){const{url:e,options:s,baseURL:r,baseOptions:o}=t,c=o?o():{},{headers:i}=c,h=s||{},{headers:u}=h;let p=a(i,u),f=Object.assign({},c,s,{headers:p}),l=f.timeout;const b=[];l&&b.push(AbortSignal.timeout(l));const g=f.signal;g&&b.push(g),f.signal=AbortSignal.any(b);let y=n(e,Object.assign({},f,{baseURL:r})),d=!1;const m=f.body;return m&&(m instanceof Blob||m instanceof ArrayBuffer||m instanceof FormData||"string"==typeof m||(d=!0,f.body=JSON.stringify(m))),d&&f.headers.set("Content-Type","application/json"),[y,f]}(i);return o?o(h,u,i):fetch(h,u)}exports.createSoon=function(e={}){const s={};s.baseInit=e;const r=(t,e)=>o(t,e,s.baseInit),n=["get","post","put","delete","patch"];return[...n,"head","options"].forEach((t=>{s[t]=(e,s)=>r(e,Object.assign({},s,{method:t}))})),s.request=r,s.API=(e,s)=>{const a={},o=!!t(e).length;return n.forEach((t=>{const n=t.toUpperCase();a[n]=()=>(...n)=>{let[a,c]=n;const i="get"===t?"query":"body";return r(e,Object.assign({},s,{method:t,params:o?a:void 0,[i]:o?c:a}))}})),a},s};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,41 @@
|
|
|
1
|
-
type SoonOptions = {
|
|
2
|
-
query?: Record<string, string | number | boolean | string
|
|
3
|
-
params?: Record<string, string | number>;
|
|
4
|
-
timeout?: number;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
type OptionQuery<Args> = Args extends undefined ? [] : keyof Args extends never ? [] : Partial<Args> extends Args ? [query?: Args] : NonNullable<Args> | undefined extends Args ? [query?: Args] : [query: Args];
|
|
43
|
-
type OptionBody<Args> = Args extends undefined ? [] : keyof Args extends never ? [] : Partial<Args> extends Args ? [body?: Args] : NonNullable<Args> | undefined extends Args ? [body?: Args] : [body: Args];
|
|
44
|
-
|
|
45
|
-
export { type SoonInit, type SoonOptions, createSoon };
|
|
1
|
+
type SoonOptions = RequestInit & {
|
|
2
|
+
query?: Record<string, string | number | boolean | null | undefined | (string | number | boolean | null | undefined)[]> | URLSearchParams;
|
|
3
|
+
params?: Record<string, string | number>;
|
|
4
|
+
timeout?: number;
|
|
5
|
+
};
|
|
6
|
+
type SoonInit<Options> = {
|
|
7
|
+
baseURL?: string;
|
|
8
|
+
options?: () => Options;
|
|
9
|
+
baseOptions?: () => Options;
|
|
10
|
+
fetching?: (url: string, options: Options & {
|
|
11
|
+
headers: Headers;
|
|
12
|
+
}, raw: {
|
|
13
|
+
url: string;
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
options?: Options;
|
|
16
|
+
baseOptions?: () => Options;
|
|
17
|
+
}) => Promise<any>;
|
|
18
|
+
};
|
|
19
|
+
declare function createSoon<Options extends SoonOptions = SoonOptions>(soonInit?: NoInfer<SoonInit<Options>>): {
|
|
20
|
+
request: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
21
|
+
get: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
22
|
+
post: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
23
|
+
put: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
24
|
+
patch: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
25
|
+
delete: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
26
|
+
head: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
27
|
+
options: <T = any>(url: string, options?: Options) => Promise<T>;
|
|
28
|
+
API: <Url extends string>(url: Url, options?: Options) => {
|
|
29
|
+
GET: <Req = undefined, Res = any>() => (...arg: [...OptionParams<{ [key in GetUrlKey<Url>]: string | number; }>, ...OptionQuery<Req>]) => Promise<Res>;
|
|
30
|
+
POST: <Req = undefined, Res = any>() => (...arg: [...OptionParams<{ [key in GetUrlKey<Url>]: string | number; }>, ...OptionBody<Req>]) => Promise<Res>;
|
|
31
|
+
PATCH: <Req = undefined, Res = any>() => (...arg: [...OptionParams<{ [key in GetUrlKey<Url>]: string | number; }>, ...OptionBody<Req>]) => Promise<Res>;
|
|
32
|
+
DELETE: <Req = undefined, Res = any>() => (...arg: [...OptionParams<{ [key in GetUrlKey<Url>]: string | number; }>, ...OptionBody<Req>]) => Promise<Res>;
|
|
33
|
+
PUT: <Req = undefined, Res = any>() => (...arg: [...OptionParams<{ [key in GetUrlKey<Url>]: string | number; }>, ...OptionBody<Req>]) => Promise<Res>;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type GetUrlKey<Url> = Url extends `${string}/:${infer Key}/${infer Right}` ? `${Key}` | GetUrlKey<`/${Right}`> : Url extends `${string}/:${infer Key}` ? `${Key}` : never;
|
|
37
|
+
type OptionParams<Args> = Args extends undefined ? [] : keyof Args extends never ? [] : Partial<Args> extends Args ? [params?: Args] : NonNullable<Args> | undefined extends Args ? [params?: Args] : [params: Args];
|
|
38
|
+
type OptionQuery<Args> = Args extends undefined ? [] : keyof Args extends never ? [] : Partial<Args> extends Args ? [query?: Args] : NonNullable<Args> | undefined extends Args ? [query?: Args] : [query: Args];
|
|
39
|
+
type OptionBody<Args> = Args extends undefined ? [] : keyof Args extends never ? [] : Partial<Args> extends Args ? [body?: Args] : NonNullable<Args> | undefined extends Args ? [body?: Args] : [body: Args];
|
|
40
|
+
|
|
41
|
+
export { type SoonInit, type SoonOptions, createSoon };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>{
|
|
1
|
+
const t=t=>{const e=[],s=t.match(/\:([^:\/\d]+)\/?/g);return s&&s.forEach((t=>{e.push(t.replace(/\//g,"").replace(/:/g,""))})),e},e=(t="")=>t.startsWith("http"),s=(t="")=>e(t)?t:((t="")=>{let e=t;return t&&(e=((t="")=>t.endsWith("/")?t.slice(0,-1):t)(e),t.startsWith("/")||(e="/"+e)),e})(t),r=t=>{if(!t)return[];if(t instanceof URLSearchParams||"string"==typeof t||Array.isArray(t))return Array.from(new URLSearchParams(t).entries());const e=[];return Object.keys(t).forEach((s=>{const r=t[s];(Array.isArray(r)?r:[r]).forEach((t=>{e.push([s,null!=t?t:""])}))})),e},n=(n,a)=>{const{query:o,params:c,baseURL:i}=a;let h=n.trim();t(n).forEach((t=>{c&&(h=h.replace(":"+t,""+c[t]))}));const[p,u]=h.split("?");let f=new URLSearchParams([...r(u),...r(o)]),l=((t,r)=>{let n=s(t);return e(n)||(n=s(r)+n),n})(p,i);return f.size&&(l=l+"?"+f),l},a=(...t)=>{const e=new Headers;return t.forEach((t=>{t&&new Headers(t).forEach(((t,s)=>{e.set(s,t)}))})),e};function o(t,e,s){const{baseURL:r,fetching:o}=s||{};let c;s&&(c=s.baseOptions?s.baseOptions:s.options);const i={url:t,options:e,baseURL:r,baseOptions:c},[h,p]=function(t){const{url:e,options:s,baseURL:r,baseOptions:o}=t,c=o?o():{},{headers:i}=c,h=s||{},{headers:p}=h;let u=a(i,p),f=Object.assign({},c,s,{headers:u}),l=f.timeout;const b=[];l&&b.push(AbortSignal.timeout(l));const g=f.signal;g&&b.push(g),f.signal=AbortSignal.any(b);let y=n(e,Object.assign({},f,{baseURL:r})),d=!1;const m=f.body;return m&&(m instanceof Blob||m instanceof ArrayBuffer||m instanceof FormData||"string"==typeof m||(d=!0,f.body=JSON.stringify(m))),d&&f.headers.set("Content-Type","application/json"),[y,f]}(i);return o?o(h,p,i):fetch(h,p)}function c(e={}){const s={};s.baseInit=e;const r=(t,e)=>o(t,e,s.baseInit),n=["get","post","put","delete","patch"];return[...n,"head","options"].forEach((t=>{s[t]=(e,s)=>r(e,Object.assign({},s,{method:t}))})),s.request=r,s.API=(e,s)=>{const a={},o=!!t(e).length;return n.forEach((t=>{const n=t.toUpperCase();a[n]=()=>(...n)=>{let[a,c]=n;const i="get"===t?"query":"body";return r(e,Object.assign({},s,{method:t,params:o?a:void 0,[i]:o?c:a}))}})),a},s}export{c as createSoon};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soon-fetch",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "a
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "a 3Kb request lib alternative to axios",
|
|
5
5
|
"homepage": "https://github.com/leafio/soon-fetch",
|
|
6
6
|
"main": "./dist/index.cjs.js",
|
|
7
7
|
"module": "/dist/index.js",
|