kbfetch 0.0.5 → 0.0.7
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 +76 -0
- package/dist/index.js +7 -2
- package/dist/typings/help.d.ts +5 -2
- package/dist/typings/index.d.ts +12 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# kbfetch
|
|
2
|
+
|
|
3
|
+
一个轻量基于fetch封装的API请求工具库。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install kbfetch
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
kbfetch导出了一些类型定义和默认的fetch助手函数:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import fetch, { KbFetchInit } from 'kbfetch';
|
|
17
|
+
|
|
18
|
+
const response = await fetch('/api/users', {
|
|
19
|
+
method: 'GET'
|
|
20
|
+
} as KbFetchInit);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`KbFetchInit` 类型扩展了标准的 `RequestInit`,添加了一些可选的便捷属性:
|
|
24
|
+
|
|
25
|
+
- `baseUrl` - API的基础网址
|
|
26
|
+
- `timeout` - 请求超时时间(毫秒)
|
|
27
|
+
- `before` - 修改/记录请求参数的回调函数
|
|
28
|
+
- `after` - 响应结果的回调函数
|
|
29
|
+
- `parser` - 在返回resolve前解析响应的回调函数
|
|
30
|
+
|
|
31
|
+
示例使用部分属性:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const response = await fetch('/users', {
|
|
35
|
+
baseUrl: 'https://example.com/api',
|
|
36
|
+
timeout: 10000,
|
|
37
|
+
parser: res => res.json()
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
同时导出了一个默认的`fetch`实例`afetch`,包含一些常用的默认配置。
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### 类型
|
|
46
|
+
|
|
47
|
+
- `KbFetchInit` - 扩展了便捷请求属性的RequestInit
|
|
48
|
+
|
|
49
|
+
### 函数
|
|
50
|
+
|
|
51
|
+
- `fetch` - 使用提供的初始化选项发起请求
|
|
52
|
+
- `afetch` - 配置好默认值的fetch实例
|
|
53
|
+
|
|
54
|
+
### 配置
|
|
55
|
+
|
|
56
|
+
可以使用`createKbFetch`创建自定义的`fetch`实例:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { createKbFetch } from 'kbfetch';
|
|
60
|
+
|
|
61
|
+
const customFetch = createKbFetch({
|
|
62
|
+
baseUrl: 'https://api.example.com'
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
可以提供以下选项进行配置:
|
|
67
|
+
|
|
68
|
+
- `baseUrl` - API的基地址
|
|
69
|
+
- `timeout` - 全局超时时间
|
|
70
|
+
- `before` - 全局before回调函数
|
|
71
|
+
- `after` - 全局after回调函数
|
|
72
|
+
- `parser` - 全局parser回调函数
|
|
73
|
+
|
|
74
|
+
## 许可
|
|
75
|
+
|
|
76
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,11 @@ var help = {
|
|
|
20
20
|
if (contentType.includes("application/json"))
|
|
21
21
|
return res.json();
|
|
22
22
|
return res.text();
|
|
23
|
+
},
|
|
24
|
+
obj2header: (obj) => {
|
|
25
|
+
const headers = new Headers;
|
|
26
|
+
Object.entries(obj).forEach(([k, v]) => headers.append(k, v));
|
|
27
|
+
return headers;
|
|
23
28
|
}
|
|
24
29
|
};
|
|
25
30
|
var help_default = help;
|
|
@@ -35,8 +40,8 @@ var defaultKbFetch = {
|
|
|
35
40
|
const _t = this;
|
|
36
41
|
let { before = _t.before, ..._init } = init || {};
|
|
37
42
|
_init = before(_init);
|
|
38
|
-
let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, ...requestInit } = _init;
|
|
39
|
-
timeout && Object.assign(requestInit, { signal: help_default.createTimeoutSignal(timeout) });
|
|
43
|
+
let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, headers = {}, ...requestInit } = _init;
|
|
44
|
+
timeout && Object.assign(requestInit, { signal: help_default.createTimeoutSignal(timeout), headers: help_default.obj2header(headers) });
|
|
40
45
|
if (!url.startsWith("http://") && !url.startsWith("https://"))
|
|
41
46
|
url = baseUrl + url;
|
|
42
47
|
const response = fetch(url, requestInit).then(async (respnse) => {
|
package/dist/typings/help.d.ts
CHANGED
|
@@ -4,17 +4,20 @@ type After = <T = any>(res: Promise<Response & {
|
|
|
4
4
|
data: T;
|
|
5
5
|
}>) => any;
|
|
6
6
|
type _KbFetchInit = {
|
|
7
|
-
timeout?: number;
|
|
8
7
|
before?: Before;
|
|
9
8
|
parser?: Parser;
|
|
10
9
|
after?: After;
|
|
11
10
|
baseUrl?: string;
|
|
12
11
|
flags?: Record<string, any>;
|
|
13
12
|
};
|
|
14
|
-
export type KbFetchInit = Omit<RequestInit, 'timeout'> &
|
|
13
|
+
export type KbFetchInit = Omit<RequestInit, 'timeout' | 'headers'> & {
|
|
14
|
+
timeout?: number;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
} & _KbFetchInit;
|
|
15
17
|
declare const help: {
|
|
16
18
|
synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => string;
|
|
17
19
|
createTimeoutSignal: (timeout: number) => AbortSignal;
|
|
18
20
|
parseResBody: (res: Response) => Promise<any>;
|
|
21
|
+
obj2header: (obj: Record<string, string>) => Headers;
|
|
19
22
|
};
|
|
20
23
|
export default help;
|
package/dist/typings/index.d.ts
CHANGED
|
@@ -9,8 +9,10 @@ declare const defaultKbFetch: {
|
|
|
9
9
|
timeout: number;
|
|
10
10
|
do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
|
|
11
11
|
get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
|
|
12
|
-
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
|
|
12
|
+
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
|
|
13
13
|
timeout?: number;
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
} & {
|
|
14
16
|
before?: (init: KbFetchInit) => KbFetchInit;
|
|
15
17
|
parser?: (res: Response) => any;
|
|
16
18
|
after?: <T_3 = any>(res: Promise<Response & {
|
|
@@ -33,8 +35,10 @@ export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) =>
|
|
|
33
35
|
timeout: number;
|
|
34
36
|
do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
|
|
35
37
|
get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
|
|
36
|
-
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
|
|
38
|
+
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
|
|
37
39
|
timeout?: number;
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
} & {
|
|
38
42
|
before?: (init: KbFetchInit) => KbFetchInit;
|
|
39
43
|
parser?: (res: Response) => any;
|
|
40
44
|
after?: <T_3 = any>(res: Promise<Response & {
|
|
@@ -57,8 +61,10 @@ declare const kbFetch: {
|
|
|
57
61
|
timeout: number;
|
|
58
62
|
do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
|
|
59
63
|
get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
|
|
60
|
-
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
|
|
64
|
+
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
|
|
61
65
|
timeout?: number;
|
|
66
|
+
headers?: Record<string, string>;
|
|
67
|
+
} & {
|
|
62
68
|
before?: (init: KbFetchInit) => KbFetchInit;
|
|
63
69
|
parser?: (res: Response) => any;
|
|
64
70
|
after?: <T_3 = any>(res: Promise<Response & {
|
|
@@ -82,8 +88,10 @@ export declare const afetch: {
|
|
|
82
88
|
timeout: number;
|
|
83
89
|
do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
|
|
84
90
|
get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
|
|
85
|
-
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
|
|
91
|
+
g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
|
|
86
92
|
timeout?: number;
|
|
93
|
+
headers?: Record<string, string>;
|
|
94
|
+
} & {
|
|
87
95
|
before?: (init: KbFetchInit) => KbFetchInit;
|
|
88
96
|
parser?: (res: Response) => any;
|
|
89
97
|
after?: <T_3 = any>(res: Promise<Response & {
|