kbfetch 0.0.15 → 0.0.16

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 CHANGED
@@ -1,76 +1,108 @@
1
- # kbfetch
1
+ # kbfetch 库使用说明
2
2
 
3
- 一个轻量基于fetch封装的API请求工具库。
3
+ `kbfetch` 一个轻量基于fetch封装的API请求工具库。
4
4
 
5
- ## 安装
5
+ ## 安装引用
6
6
 
7
- ```
7
+ ```bash
8
8
  npm install kbfetch
9
9
  ```
10
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);
11
+ ```javascript
12
+ import kbfetch from 'kbfetch'
21
13
  ```
22
14
 
23
- `KbFetchInit` 类型扩展了标准的 `RequestInit`,添加了一些可选的便捷属性:
15
+ ## 接口和方法
24
16
 
25
- - `baseUrl` - API的基础网址
26
- - `timeout` - 请求超时时间(毫秒)
27
- - `before` - 修改/记录请求参数的回调函数
28
- - `after` - 响应结果的回调函数
29
- - `parser` - 在返回resolve前解析响应的回调函数
17
+ `kbfetch` 提供了以下方法:
30
18
 
31
- 示例使用部分属性:
19
+ - `baseUrl`: 字符串,表示默认的基本 URL。
20
+ - `before(init: kbfetchInit): kbfetchInit`: 对请求初始化对象进行预处理的函数。
21
+ - `after(res: Promise<Response & { data: any }>): any`: 对响应结果进行后处理的函数。
22
+ - `parser(res: Response): any`: 对响应数据进行解析的函数。
23
+ - `timeout: number`: 请求超时时间(毫秒)。
32
24
 
33
- ```ts
34
- const response = await fetch('/users', {
35
- baseUrl: 'https://example.com/api',
36
- timeout: 10000,
37
- parser: res => res.json()
38
- });
39
- ```
25
+ ## 使用示例
40
26
 
41
- 同时导出了一个默认的`fetch`实例`afetch`,包含一些常用的默认配置。
27
+ 发起 GET 请求的示例:
42
28
 
43
- ## API
29
+ ```javascript
30
+ import kbfetch from 'kbfetch'
31
+ kbfetch.g('https://api.example.com/data', { id: 1 })
32
+ // OR
33
+ kbfetch.get('https://api.example.com/data', {
34
+ params: { id: 1 }
35
+ })
36
+ ```
44
37
 
45
- ### 类型
38
+ 发起 POST 请求的示例:
46
39
 
47
- - `KbFetchInit` - 扩展了便捷请求属性的RequestInit
40
+ ```js
41
+ kbfetch.post('https://api.example.com/data', { id: 1 })
42
+ ```
48
43
 
49
- ### 函数
44
+ 取消请求示例:
45
+ `timeout!=-1时,canAbort可以不设置`
50
46
 
51
- - `fetch` - 使用提供的初始化选项发起请求
52
- - `afetch` - 配置好默认值的fetch实例
47
+ ```js
48
+ const ft = kbfetch.post('https://api.example.com/data', { id: 1 }, { canAbort: true })
49
+ ft.abort()
50
+ ```
53
51
 
54
- ### 配置
52
+ ```js
53
+ const ft = kbfetch.g('https://api.example.com/data', { params: { id: 1 }, timeout: 0 })
54
+ ft.abort()
55
+ ```
55
56
 
56
- 可以使用`createKbFetch`创建自定义的`fetch`实例:
57
+ ## 自定义选项
57
58
 
58
- ```ts
59
- import { createKbFetch } from 'kbfetch';
59
+ `KbFetchInit` 类型扩展了标准的 `RequestInit`,添加了一些可选的便捷属性:
60
60
 
61
- const customFetch = createKbFetch({
62
- baseUrl: 'https://api.example.com'
63
- });
61
+ - `baseUrl` - API的基础网址
62
+ - `timeout` - 请求超时时间(毫秒)
63
+ - `before` - 修改/记录请求参数的回调函数
64
+ - `parser` - 在返回resolve前解析响应的回调函数
65
+ - `after` - 响应结果的回调函数
66
+
67
+ ```typescript
68
+ import kbfetch, { createKbFetch } from 'kbfetch'
69
+ // 请求单独配置
70
+ kbfetch.post(url, data, kbFetchInit)
71
+ kbfetch.get(url, params, kbFetchInit)
72
+ // 创建自定义实例
73
+ const ft = createKbFetch(KbFetchInit)
74
+ // ft.get ft.post
64
75
  ```
65
76
 
66
- 可以提供以下选项进行配置:
67
-
68
- - `baseUrl` - API的基地址
69
- - `timeout` - 全局超时时间
70
- - `before` - 全局before回调函数
71
- - `after` - 全局after回调函数
72
- - `parser` - 全局parser回调函数
77
+ fetch SSE 实现示例:
78
+
79
+ ```javascript
80
+ import kbfetch from 'kbfetch'
81
+ const sse = (name: string, params: { ondata: (data: string) => any } & Record<string, any>) => {
82
+ const { ondata, ..._p } = params
83
+ return kbfetch.post(name, _p, {
84
+ timeout: 0,
85
+ parser: rb => {
86
+ if (!rb.body) return
87
+ const reader = rb.body.getReader()
88
+ const push = () => {
89
+ // done 为数据流是否接收完成,boolean
90
+ // value 为返回数据,Uint8Array
91
+ return reader.read().then(({ done, value }) => {
92
+ if (done) return
93
+ ondata(new TextDecoder().decode(value))
94
+ // 持续读取流信息
95
+ return push()
96
+ })
97
+ }
98
+ // 开始读取流信息
99
+ return push()
100
+ },
101
+ after: v => v
102
+ })
103
+ }
104
+ ```
73
105
 
74
- ## 许可
106
+ 在这个示例中,我们自定义了 `before` 和 `after` 函数,以修改请求初始化对象和响应结果。
75
107
 
76
- MIT
108
+ 更多关于 `kbfetchInit` 对象的详细信息,请参考原始的 TypeScript 定义文件。
package/dist/index.js CHANGED
@@ -48,8 +48,7 @@ var defaultKbFetch = {
48
48
  if (!url.startsWith("http://") && !url.startsWith("https://"))
49
49
  url = baseUrl + url;
50
50
  const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) }));
51
- const res = after(response);
52
- return Object.assign(res, {
51
+ return Object.assign(after(response), {
53
52
  abort: () => abortController?.abort()
54
53
  });
55
54
  },
@@ -69,9 +68,7 @@ var defaultKbFetch = {
69
68
  var createKbFetch = (config) => ({ ...defaultKbFetch, ...config });
70
69
  var kbFetch = defaultKbFetch;
71
70
  var kbfetch_default = kbFetch;
72
- var afetch = defaultKbFetch;
73
71
  export {
74
72
  kbfetch_default as default,
75
- createKbFetch,
76
- afetch
73
+ createKbFetch
77
74
  };
@@ -1,13 +1,12 @@
1
1
  import { DefaultKbFetch, KbFetchInit } from './help';
2
- declare const defaultKbFetch: DefaultKbFetch;
3
- export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) => {
4
- baseUrl: string;
5
- before: (init: KbFetchInit) => KbFetchInit;
2
+ export declare const createKbFetch: (config: Partial<Pick<DefaultKbFetch, 'baseUrl' | 'timeout' | 'before' | 'parser' | 'after'>>) => {
6
3
  after: (res: Promise<Response & {
7
4
  data: any;
8
5
  }>) => any;
9
- parser: (res: Response) => any;
6
+ before: (init: KbFetchInit) => KbFetchInit;
10
7
  timeout: number;
8
+ parser: (res: Response) => any;
9
+ baseUrl: string;
11
10
  do: <T>(url: string, init?: KbFetchInit) => Promise<T> & {
12
11
  abort: () => void;
13
12
  };
@@ -37,4 +36,3 @@ export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) =>
37
36
  };
38
37
  declare const kbFetch: DefaultKbFetch;
39
38
  export default kbFetch;
40
- export declare const afetch: DefaultKbFetch;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./dist/index.js",
4
4
  "module": "./dist/index.js",
5
5
  "type": "module",
6
- "version": "0.0.15",
6
+ "version": "0.0.16",
7
7
  "license": "MIT",
8
8
  "types": "./dist/typings/index.d.ts",
9
9
  "files": [