@chzky/reqwest 0.0.1 → 0.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 +314 -0
- package/esm/_dnt.polyfills.d.ts +11 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -10
- package/esm/_dnt.polyfills.js +24 -66
- package/esm/_dnt.shims.d.ts +1 -13
- package/esm/_dnt.shims.d.ts.map +1 -1
- package/esm/_dnt.shims.js +57 -1
- package/esm/lib/mod.d.ts +1 -1
- package/esm/lib/mod.d.ts.map +1 -1
- package/esm/lib/mod.js +1 -1
- package/esm/lib/reqwest/class.d.ts +116 -78
- package/esm/lib/reqwest/class.d.ts.map +1 -1
- package/esm/lib/reqwest/class.js +175 -1
- package/esm/lib/reqwest/core.d.ts +5 -0
- package/esm/lib/reqwest/core.d.ts.map +1 -1
- package/esm/lib/reqwest/core.js +154 -244
- package/esm/lib/reqwest/factor.d.ts +2 -2
- package/esm/lib/reqwest/factor.d.ts.map +1 -1
- package/esm/lib/reqwest/factor.js +32 -1
- package/esm/lib/reqwest/interface.d.ts +166 -42
- package/esm/lib/reqwest/interface.d.ts.map +1 -1
- package/esm/lib/reqwest/interface.js +1 -1
- package/esm/lib/reqwest/mod.d.ts +1 -1
- package/esm/lib/reqwest/mod.d.ts.map +1 -1
- package/esm/lib/reqwest/mod.js +2 -2
- package/esm/lib/{helper → utils}/mod.d.ts +1 -0
- package/esm/lib/utils/mod.d.ts.map +1 -0
- package/esm/lib/{helper → utils}/mod.js +1 -0
- package/esm/lib/utils/other/mod.d.ts +2 -0
- package/esm/lib/utils/other/mod.d.ts.map +1 -0
- package/esm/lib/utils/other/mod.js +1 -0
- package/esm/lib/utils/other/sseParse.d.ts +34 -0
- package/esm/lib/utils/other/sseParse.d.ts.map +1 -0
- package/esm/lib/utils/other/sseParse.js +42 -0
- package/{script/lib/helper → esm/lib/utils}/request/RESTful.d.ts +43 -11
- package/esm/lib/utils/request/RESTful.d.ts.map +1 -0
- package/esm/lib/utils/request/RESTful.js +129 -0
- package/esm/lib/utils/request/baseConf.d.ts +22 -0
- package/esm/lib/utils/request/baseConf.d.ts.map +1 -0
- package/esm/lib/utils/request/baseConf.js +37 -0
- package/esm/lib/utils/request/factors.d.ts +30 -0
- package/esm/lib/utils/request/factors.d.ts.map +1 -0
- package/esm/lib/utils/request/factors.js +39 -0
- package/esm/lib/{helper → utils}/request/input.d.ts +4 -3
- package/esm/lib/utils/request/input.d.ts.map +1 -0
- package/esm/lib/utils/request/input.js +24 -0
- package/esm/lib/utils/request/mod.d.ts +7 -0
- package/esm/lib/utils/request/mod.d.ts.map +1 -0
- package/esm/lib/utils/request/mod.js +6 -0
- package/esm/lib/utils/request/process.d.ts +16 -0
- package/esm/lib/utils/request/process.d.ts.map +1 -0
- package/esm/lib/utils/request/process.js +37 -0
- package/esm/lib/utils/request/wait.d.ts +10 -0
- package/esm/lib/utils/request/wait.d.ts.map +1 -0
- package/esm/lib/utils/request/wait.js +14 -0
- package/esm/lib/utils/response/blobToUrl.d.ts +10 -0
- package/esm/lib/utils/response/blobToUrl.d.ts.map +1 -0
- package/esm/lib/utils/response/blobToUrl.js +21 -0
- package/esm/lib/utils/response/mod.d.ts +5 -0
- package/esm/lib/utils/response/mod.d.ts.map +1 -0
- package/esm/lib/utils/response/mod.js +4 -0
- package/esm/lib/utils/response/openLink.d.ts +7 -0
- package/esm/lib/utils/response/openLink.d.ts.map +1 -0
- package/esm/lib/utils/response/openLink.js +20 -0
- package/esm/lib/utils/response/output.d.ts +16 -0
- package/esm/lib/utils/response/output.d.ts.map +1 -0
- package/esm/lib/utils/response/output.js +24 -0
- package/esm/lib/utils/response/pick.d.ts +23 -0
- package/esm/lib/utils/response/pick.d.ts.map +1 -0
- package/esm/lib/utils/response/pick.js +25 -0
- package/esm/lib/utils/use/mod.d.ts.map +1 -0
- package/esm/lib/utils/use/mod.js +2 -0
- package/esm/lib/utils/use/useCancel.d.ts +33 -0
- package/esm/lib/utils/use/useCancel.d.ts.map +1 -0
- package/esm/lib/utils/use/useCancel.js +41 -0
- package/esm/lib/utils/use/useLoading.d.ts +34 -0
- package/esm/lib/utils/use/useLoading.d.ts.map +1 -0
- package/esm/lib/utils/use/useLoading.js +57 -0
- package/package.json +4 -11
- package/script/_dnt.polyfills.d.ts +11 -0
- package/script/_dnt.polyfills.d.ts.map +1 -1
- package/script/_dnt.polyfills.js +15 -0
- package/script/_dnt.shims.d.ts +1 -13
- package/script/_dnt.shims.d.ts.map +1 -1
- package/script/_dnt.shims.js +2 -21
- package/script/lib/mod.d.ts +1 -1
- package/script/lib/mod.d.ts.map +1 -1
- package/script/lib/mod.js +1 -1
- package/script/lib/reqwest/class.d.ts +116 -78
- package/script/lib/reqwest/class.d.ts.map +1 -1
- package/script/lib/reqwest/class.js +135 -25
- package/script/lib/reqwest/core.d.ts +5 -0
- package/script/lib/reqwest/core.d.ts.map +1 -1
- package/script/lib/reqwest/core.js +119 -23
- package/script/lib/reqwest/factor.d.ts +2 -2
- package/script/lib/reqwest/factor.d.ts.map +1 -1
- package/script/lib/reqwest/factor.js +2 -2
- package/script/lib/reqwest/interface.d.ts +160 -11
- package/script/lib/reqwest/interface.d.ts.map +1 -1
- package/script/lib/reqwest/mod.d.ts +1 -1
- package/script/lib/reqwest/mod.d.ts.map +1 -1
- package/script/lib/{helper → utils}/mod.d.ts +1 -0
- package/script/lib/utils/mod.d.ts.map +1 -0
- package/script/lib/{helper → utils}/mod.js +1 -0
- package/script/lib/utils/other/mod.d.ts +2 -0
- package/script/lib/utils/other/mod.d.ts.map +1 -0
- package/script/lib/{helper/response → utils/other}/mod.js +1 -2
- package/script/lib/utils/other/sseParse.d.ts +34 -0
- package/script/lib/utils/other/sseParse.d.ts.map +1 -0
- package/script/lib/utils/other/sseParse.js +45 -0
- package/{esm/lib/helper → script/lib/utils}/request/RESTful.d.ts +43 -11
- package/script/lib/utils/request/RESTful.d.ts.map +1 -0
- package/script/lib/utils/request/RESTful.js +140 -0
- package/script/lib/utils/request/baseConf.d.ts +22 -0
- package/script/lib/utils/request/baseConf.d.ts.map +1 -0
- package/script/lib/utils/request/baseConf.js +42 -0
- package/script/lib/utils/request/factors.d.ts +30 -0
- package/script/lib/utils/request/factors.d.ts.map +1 -0
- package/script/lib/utils/request/factors.js +42 -0
- package/script/lib/{helper → utils}/request/input.d.ts +4 -3
- package/script/lib/utils/request/input.d.ts.map +1 -0
- package/script/lib/{helper → utils}/request/input.js +10 -10
- package/script/lib/utils/request/mod.d.ts +7 -0
- package/script/lib/utils/request/mod.d.ts.map +1 -0
- package/script/lib/utils/request/mod.js +22 -0
- package/script/lib/utils/request/process.d.ts +16 -0
- package/script/lib/utils/request/process.d.ts.map +1 -0
- package/script/lib/utils/request/process.js +41 -0
- package/script/lib/utils/request/wait.d.ts +10 -0
- package/script/lib/utils/request/wait.d.ts.map +1 -0
- package/script/lib/utils/request/wait.js +17 -0
- package/script/lib/utils/response/blobToUrl.d.ts +10 -0
- package/script/lib/utils/response/blobToUrl.d.ts.map +1 -0
- package/script/lib/utils/response/blobToUrl.js +24 -0
- package/script/lib/utils/response/mod.d.ts +5 -0
- package/script/lib/utils/response/mod.d.ts.map +1 -0
- package/script/lib/{helper/request → utils/response}/mod.js +4 -3
- package/script/lib/utils/response/openLink.d.ts +7 -0
- package/script/lib/utils/response/openLink.d.ts.map +1 -0
- package/script/lib/{helper/use/useLoading.js → utils/response/openLink.js} +17 -22
- package/script/lib/utils/response/output.d.ts +16 -0
- package/script/lib/utils/response/output.d.ts.map +1 -0
- package/script/lib/utils/response/output.js +28 -0
- package/script/lib/utils/response/pick.d.ts +23 -0
- package/script/lib/utils/response/pick.d.ts.map +1 -0
- package/script/lib/utils/response/pick.js +28 -0
- package/script/lib/utils/use/mod.d.ts.map +1 -0
- package/script/lib/{helper → utils}/use/mod.js +2 -2
- package/script/lib/utils/use/useCancel.d.ts +33 -0
- package/script/lib/utils/use/useCancel.d.ts.map +1 -0
- package/script/lib/utils/use/useCancel.js +44 -0
- package/script/lib/utils/use/useLoading.d.ts +34 -0
- package/script/lib/utils/use/useLoading.d.ts.map +1 -0
- package/script/lib/utils/use/useLoading.js +60 -0
- package/esm/lib/helper/mod.d.ts.map +0 -1
- package/esm/lib/helper/request/RESTful.d.ts.map +0 -1
- package/esm/lib/helper/request/RESTful.js +0 -1
- package/esm/lib/helper/request/baseConf.d.ts +0 -15
- package/esm/lib/helper/request/baseConf.d.ts.map +0 -1
- package/esm/lib/helper/request/baseConf.js +0 -1
- package/esm/lib/helper/request/input.d.ts.map +0 -1
- package/esm/lib/helper/request/input.js +0 -1
- package/esm/lib/helper/request/mod.d.ts +0 -4
- package/esm/lib/helper/request/mod.d.ts.map +0 -1
- package/esm/lib/helper/request/mod.js +0 -3
- package/esm/lib/helper/response/mod.d.ts +0 -3
- package/esm/lib/helper/response/mod.d.ts.map +0 -1
- package/esm/lib/helper/response/mod.js +0 -2
- package/esm/lib/helper/response/output.d.ts +0 -5
- package/esm/lib/helper/response/output.d.ts.map +0 -1
- package/esm/lib/helper/response/output.js +0 -1
- package/esm/lib/helper/response/pick.d.ts +0 -7
- package/esm/lib/helper/response/pick.d.ts.map +0 -1
- package/esm/lib/helper/response/pick.js +0 -1
- package/esm/lib/helper/use/mod.d.ts.map +0 -1
- package/esm/lib/helper/use/mod.js +0 -2
- package/esm/lib/helper/use/useCancel.d.ts +0 -4
- package/esm/lib/helper/use/useCancel.d.ts.map +0 -1
- package/esm/lib/helper/use/useCancel.js +0 -1
- package/esm/lib/helper/use/useLoading.d.ts +0 -15
- package/esm/lib/helper/use/useLoading.d.ts.map +0 -1
- package/esm/lib/helper/use/useLoading.js +0 -1
- package/script/lib/helper/mod.d.ts.map +0 -1
- package/script/lib/helper/request/RESTful.d.ts.map +0 -1
- package/script/lib/helper/request/RESTful.js +0 -86
- package/script/lib/helper/request/baseConf.d.ts +0 -15
- package/script/lib/helper/request/baseConf.d.ts.map +0 -1
- package/script/lib/helper/request/baseConf.js +0 -27
- package/script/lib/helper/request/input.d.ts.map +0 -1
- package/script/lib/helper/request/mod.d.ts +0 -4
- package/script/lib/helper/request/mod.d.ts.map +0 -1
- package/script/lib/helper/response/mod.d.ts +0 -3
- package/script/lib/helper/response/mod.d.ts.map +0 -1
- package/script/lib/helper/response/output.d.ts +0 -5
- package/script/lib/helper/response/output.d.ts.map +0 -1
- package/script/lib/helper/response/output.js +0 -15
- package/script/lib/helper/response/pick.d.ts +0 -7
- package/script/lib/helper/response/pick.d.ts.map +0 -1
- package/script/lib/helper/response/pick.js +0 -12
- package/script/lib/helper/use/mod.d.ts.map +0 -1
- package/script/lib/helper/use/useCancel.d.ts +0 -4
- package/script/lib/helper/use/useCancel.d.ts.map +0 -1
- package/script/lib/helper/use/useCancel.js +0 -15
- package/script/lib/helper/use/useLoading.d.ts +0 -15
- package/script/lib/helper/use/useLoading.d.ts.map +0 -1
- /package/esm/lib/{helper → utils}/use/mod.d.ts +0 -0
- /package/script/lib/{helper → utils}/use/mod.d.ts +0 -0
|
@@ -1,84 +1,122 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import type { AsyncResult, UnexpectedError } from '@chzky/core';
|
|
2
|
+
import type { AxiosResponse, CreateAxiosDefaults } from 'axios';
|
|
3
|
+
import type { ExtendWebSocketSubject, MergeErrors, RequestInterceptor, ReqwestInterceptor, ReqwestInterface, ReqwestSSEConfig, ReqwestWsConfig, ResponseInterceptor, ResponseResult } from './interface.js';
|
|
4
4
|
/** ## `Reqwest` : 基于`Axios`二次封装的请求库 */
|
|
5
|
-
export declare class Reqwest<O = AxiosResponse<unknown, unknown>, E = never, ER = UnexpectedError> implements
|
|
6
|
-
private
|
|
7
|
-
private
|
|
5
|
+
export declare class Reqwest<O = AxiosResponse<unknown, unknown>, E = never, ER = UnexpectedError> implements ReqwestInterface<O, E, ER> {
|
|
6
|
+
private readonly interceptors_req;
|
|
7
|
+
private readonly interceptors_res;
|
|
8
8
|
private cof;
|
|
9
|
-
private
|
|
10
|
-
constructor(
|
|
11
|
-
/** 实现{@link NewAble}接口 */
|
|
12
|
-
static new(): Reqwest;
|
|
13
|
-
/** 实现{@link Copy}接口 */
|
|
14
|
-
clone(): Reqwest<O, E, ER>;
|
|
9
|
+
private readonly internal_axios;
|
|
10
|
+
constructor(interceptors_req?: any[], interceptors_res?: any[], cof?: CreateAxiosDefaults);
|
|
15
11
|
/** ### `conf` : 更新配置 */
|
|
16
|
-
conf(conf: CreateAxiosDefaults):
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/** ### `response` : 增加响应拦截器,拦截器会按顺序执行
|
|
21
|
-
+ 该方法会返回一个新的Reqwest,并且与原Reqwest保持独立 */
|
|
22
|
-
response<OO, EE>(interceptor: ResponseInterceptor<O, OO, EE>): Reqwest<OO, E, EE | ER>;
|
|
23
|
-
response<OO, EE, O2, E2>(...interceptor: [ResponseInterceptor<O, OO, EE>, ResponseInterceptor<OO, O2, E2>]): Reqwest<O2, E, E2 | EE | ER>;
|
|
24
|
-
response<OO, EE, O2, E2, O3, E3>(...interceptor: [
|
|
25
|
-
ResponseInterceptor<O, OO, EE>,
|
|
26
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
27
|
-
ResponseInterceptor<O2, O3, E3>
|
|
28
|
-
]): Reqwest<O3, E, E3 | E2 | EE | ER>;
|
|
29
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4>(...interceptor: [
|
|
30
|
-
ResponseInterceptor<O, OO, EE>,
|
|
31
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
32
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
33
|
-
ResponseInterceptor<O3, O4, E4>
|
|
34
|
-
]): Reqwest<O4, E, E4 | E3 | E2 | EE | ER>;
|
|
35
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5>(...interceptor: [
|
|
36
|
-
ResponseInterceptor<O, OO, EE>,
|
|
37
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
38
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
39
|
-
ResponseInterceptor<O3, O4, E4>
|
|
40
|
-
]): Reqwest<O5, E, E5 | E4 | E3 | E2 | EE | ER>;
|
|
41
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6>(...interceptor: [
|
|
42
|
-
ResponseInterceptor<O, OO, EE>,
|
|
43
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
44
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
45
|
-
ResponseInterceptor<O3, O4, E4>,
|
|
46
|
-
ResponseInterceptor<O4, O5, E5>,
|
|
47
|
-
ResponseInterceptor<O5, O6, E6>
|
|
48
|
-
]): Reqwest<O6, E, E6 | E5 | E4 | E3 | E2 | EE | ER>;
|
|
49
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7>(...interceptor: [
|
|
50
|
-
ResponseInterceptor<O, OO, EE>,
|
|
51
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
52
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
53
|
-
ResponseInterceptor<O3, O4, E4>,
|
|
54
|
-
ResponseInterceptor<O4, O5, E5>,
|
|
55
|
-
ResponseInterceptor<O5, O6, E6>,
|
|
56
|
-
ResponseInterceptor<O6, O7, E7>
|
|
57
|
-
]): Reqwest<O7, E, E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
|
|
58
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7, O8, E8>(...interceptor: [
|
|
59
|
-
ResponseInterceptor<O, OO, EE>,
|
|
60
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
61
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
62
|
-
ResponseInterceptor<O3, O4, E4>,
|
|
63
|
-
ResponseInterceptor<O4, O5, E5>,
|
|
64
|
-
ResponseInterceptor<O5, O6, E6>,
|
|
65
|
-
ResponseInterceptor<O6, O7, E7>,
|
|
66
|
-
ResponseInterceptor<O7, O8, E8>
|
|
67
|
-
]): Reqwest<O8, E, E8 | E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
|
|
68
|
-
response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7, O8, E8, O9, E9>(...interceptor: [
|
|
69
|
-
ResponseInterceptor<O, OO, EE>,
|
|
70
|
-
ResponseInterceptor<OO, O2, E2>,
|
|
71
|
-
ResponseInterceptor<O2, O3, E3>,
|
|
72
|
-
ResponseInterceptor<O3, O4, E4>,
|
|
73
|
-
ResponseInterceptor<O4, O5, E5>,
|
|
74
|
-
ResponseInterceptor<O5, O6, E6>,
|
|
75
|
-
ResponseInterceptor<O6, O7, E7>,
|
|
76
|
-
ResponseInterceptor<O7, O8, E8>,
|
|
77
|
-
ResponseInterceptor<O8, O9, E9>
|
|
78
|
-
]): Reqwest<O9, E, E9 | E8 | E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
|
|
79
|
-
/** ### `use` : 增加拦截器,返回一个新的Reqwest */
|
|
80
|
-
use<OO, EC, EE>(interceptor: ReqwestInterceptor<O, EC, OO, EE>): Reqwest<OO, EC | E, EE | ER>;
|
|
81
|
-
/** ### `dispatch` : 发送请求 */
|
|
12
|
+
conf(conf: CreateAxiosDefaults): ReqwestInterface<O, E, ER>;
|
|
13
|
+
request<I extends RequestInterceptor<unknown>[]>(...interceptors: I): ReqwestInterface<O, MergeErrors<I> | E, ER>;
|
|
14
|
+
response<T extends ResponseInterceptor<unknown, unknown, unknown>[]>(...interceptor: T): ResponseResult<O, E, ER, T>;
|
|
15
|
+
use<OO, EC, EE>(interceptor: ReqwestInterceptor<O, EC, OO, EE>): ReqwestInterface<OO, EC | E, EE | ER>;
|
|
82
16
|
dispatch(): AsyncResult<O, E | ER>;
|
|
17
|
+
clone(): ReqwestInterface<O, E, ER>;
|
|
18
|
+
/** ### `new`
|
|
19
|
+
实现{@link NewAble}接口 */
|
|
20
|
+
static new(): ReqwestInterface;
|
|
21
|
+
/** ### `default`
|
|
22
|
+
实现{@link Default}接口 : 以默认配置生成一个`Reqwest`
|
|
23
|
+
#### 默认配置:
|
|
24
|
+
+ `timeout` : `6000ms`
|
|
25
|
+
*/
|
|
26
|
+
static default(): ReqwestInterface;
|
|
27
|
+
/** ## `sse` : 进行[SSE](https://developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events)请求
|
|
28
|
+
api使用见[rxjs-webSocket](https://rxjs.dev/api/webSocket/webSocket)
|
|
29
|
+
+ 实际请求只有在第一次`gen.next()`时才会发出请求
|
|
30
|
+
+ 由于请求带来的错误不会抛出,而是返回`{done:true,value:underfine}`
|
|
31
|
+
@example for await to take
|
|
32
|
+
```ts
|
|
33
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
34
|
+
|
|
35
|
+
const abort = new AbortController()
|
|
36
|
+
|
|
37
|
+
const gen = Reqwest.sse({
|
|
38
|
+
abort,
|
|
39
|
+
url: sse_url,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
let index = 0
|
|
43
|
+
// 开始循环时才会调用接口获取数据
|
|
44
|
+
for await (const item of gen) {
|
|
45
|
+
if (index === 10) abort.abort() //主动终止数据获取
|
|
46
|
+
index++
|
|
47
|
+
const message = parse_sse_data(item)
|
|
48
|
+
assert(typeof message.data === 'string')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { done, value } = await gen.next()
|
|
52
|
+
assert(done)
|
|
53
|
+
assert.equal(value, undefined)
|
|
54
|
+
```
|
|
55
|
+
@example AsyncVector to take
|
|
56
|
+
```ts
|
|
57
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
58
|
+
|
|
59
|
+
const abort = new AbortController()
|
|
60
|
+
|
|
61
|
+
const gen = Reqwest.sse({
|
|
62
|
+
abort,
|
|
63
|
+
url: sse_url,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const vec = AsyncVec(gen, false)
|
|
67
|
+
|
|
68
|
+
const result = await vec
|
|
69
|
+
.map(item => parse_sse_data(item))
|
|
70
|
+
.filter(i => Boolean(i.data))
|
|
71
|
+
.take(10)
|
|
72
|
+
.collect()
|
|
73
|
+
|
|
74
|
+
const { done, value } = await gen.next()
|
|
75
|
+
|
|
76
|
+
assert(done)
|
|
77
|
+
assert.equal(value, undefined)
|
|
78
|
+
assert.equal(result.length, 10)
|
|
79
|
+
assert(result.every(i => i.data.length > 1))
|
|
80
|
+
```
|
|
81
|
+
*/
|
|
82
|
+
static sse(conf: ReqwestSSEConfig): AsyncGenerator<Uint8Array, undefined, void>;
|
|
83
|
+
/** ## `sse` : 进行[WebSocket](https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket)请求
|
|
84
|
+
@example Usage
|
|
85
|
+
```ts
|
|
86
|
+
const MOCK_URL = 'wss://echo.websocket.org'
|
|
87
|
+
|
|
88
|
+
const { promise, resolve } = Promise.withResolvers<void>()
|
|
89
|
+
|
|
90
|
+
const abort = new AbortController()
|
|
91
|
+
|
|
92
|
+
type TansShape = { type: 'event' | 'message'; data: JsonX }
|
|
93
|
+
|
|
94
|
+
const wss = Reqwest.ws({
|
|
95
|
+
url: MOCK_URL,
|
|
96
|
+
abort: abort.signal,
|
|
97
|
+
deserialize: data => result(() => JSON.parse(data.data) as TansShape).unwrap_or(data.data),
|
|
98
|
+
serialize: (data: TansShape) => JSON.stringify(data),
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
wss.subscribe({
|
|
102
|
+
complete: resolve,
|
|
103
|
+
next: data => {
|
|
104
|
+
if (typeof data === 'string') return
|
|
105
|
+
const rdata = data as TansShape
|
|
106
|
+
if (!rdata.data) return
|
|
107
|
+
|
|
108
|
+
abort.abort() // 触发complete函数,执行complete回调
|
|
109
|
+
assert(rdata.data)
|
|
110
|
+
assert.equal(rdata.type, 'message')
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
wss.next({ type: 'event', data: false })
|
|
115
|
+
wss.next({ type: 'message', data: true })
|
|
116
|
+
|
|
117
|
+
await promise
|
|
118
|
+
```
|
|
119
|
+
*/
|
|
120
|
+
static ws<A, B>(conf: ReqwestWsConfig<A, B>): ExtendWebSocketSubject<A, B>;
|
|
83
121
|
}
|
|
84
122
|
//# sourceMappingURL=class.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["../../../src/lib/reqwest/class.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["../../../src/lib/reqwest/class.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAA;AAC/D,OAAO,KAAK,EACX,sBAAsB,EACtB,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,cAAc,EACd,MAAM,gBAAgB,CAAA;AAKvB,uCAAuC;AACvC,qBAAa,OAAO,CACnB,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,EACnC,CAAC,GAAG,KAAK,EACT,EAAE,GAAG,eAAe,CACnB,YAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IAMrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IAEjC,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,GAAG;IAPZ,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAmB;gBAIhC,gBAAgB,GAAE,GAAG,EAAO,EAE5B,gBAAgB,GAAE,GAAG,EAAO,EACrC,GAAG,GAAE,mBAAwB;IAKtC,wBAAwB;IACxB,IAAI,CAAC,IAAI,EAAE,mBAAmB,GAAG,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IAW3D,OAAO,CAAC,CAAC,SAAS,kBAAkB,CAAC,OAAO,CAAC,EAAE,EAC9C,GAAG,YAAY,EAAE,CAAC,GAChB,gBAAgB,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;IAQ9C,QAAQ,CAAC,CAAC,SAAS,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAClE,GAAG,WAAW,EAAE,CAAC,GACf,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAQ9B,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EACb,WAAW,EAAE,kBAAkB,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,GAC5C,gBAAgB,CAAC,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC;IAQxC,QAAQ,IAAI,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC;IAIlC,KAAK,IAAI,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IAQnC;0BACsB;IACtB,MAAM,CAAC,GAAG,IAAI,gBAAgB;IAI9B;;;;OAIG;IACH,MAAM,CAAC,OAAO,IAAI,gBAAgB;IAMlC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsDG;IACH,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,gBAAgB,GAAG,cAAc,CAAC,UAAU,EAAE,SAAS,EAAE,IAAI,CAAC;IAI/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;CAG1E"}
|
package/esm/lib/reqwest/class.js
CHANGED
|
@@ -1 +1,175 @@
|
|
|
1
|
-
|
|
1
|
+
import { normal_dispatch, see_reqwest, ws_reqwest } from './core.js';
|
|
2
|
+
import Axios from 'axios';
|
|
3
|
+
/** ## `Reqwest` : 基于`Axios`二次封装的请求库 */
|
|
4
|
+
export class Reqwest {
|
|
5
|
+
constructor(
|
|
6
|
+
// biome-ignore lint/suspicious/noExplicitAny: <>
|
|
7
|
+
interceptors_req = [],
|
|
8
|
+
// biome-ignore lint/suspicious/noExplicitAny: <>
|
|
9
|
+
interceptors_res = [], cof = {}) {
|
|
10
|
+
Object.defineProperty(this, "interceptors_req", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: interceptors_req
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(this, "interceptors_res", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: interceptors_res
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "cof", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: cof
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "internal_axios", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: Axios.create({})
|
|
33
|
+
});
|
|
34
|
+
cof && this.conf(cof);
|
|
35
|
+
}
|
|
36
|
+
/** ### `conf` : 更新配置 */
|
|
37
|
+
conf(conf) {
|
|
38
|
+
for (const key in conf) {
|
|
39
|
+
if (Object.hasOwn(conf, key)) {
|
|
40
|
+
this.internal_axios.defaults[key] =
|
|
41
|
+
conf[key];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this.cof = this.internal_axios.defaults;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
request(...interceptors) {
|
|
48
|
+
return new Reqwest([...this.interceptors_req, ...interceptors], this.interceptors_res, this.cof);
|
|
49
|
+
}
|
|
50
|
+
response(...interceptor) {
|
|
51
|
+
return new Reqwest(this.interceptors_req, [...this.interceptors_res, ...interceptor], this.cof);
|
|
52
|
+
}
|
|
53
|
+
use(interceptor) {
|
|
54
|
+
return new Reqwest([...this.interceptors_req, interceptor.request], [...this.interceptors_res, interceptor.response], this.cof);
|
|
55
|
+
}
|
|
56
|
+
dispatch() {
|
|
57
|
+
return normal_dispatch.call(this);
|
|
58
|
+
}
|
|
59
|
+
clone() {
|
|
60
|
+
return new Reqwest(this.interceptors_req, this.interceptors_res, this.cof);
|
|
61
|
+
}
|
|
62
|
+
/** ### `new`
|
|
63
|
+
实现{@link NewAble}接口 */
|
|
64
|
+
static new() {
|
|
65
|
+
return new Reqwest();
|
|
66
|
+
}
|
|
67
|
+
/** ### `default`
|
|
68
|
+
实现{@link Default}接口 : 以默认配置生成一个`Reqwest`
|
|
69
|
+
#### 默认配置:
|
|
70
|
+
+ `timeout` : `6000ms`
|
|
71
|
+
*/
|
|
72
|
+
static default() {
|
|
73
|
+
return new Reqwest().conf({
|
|
74
|
+
timeout: 6000,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** ## `sse` : 进行[SSE](https://developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events)请求
|
|
78
|
+
api使用见[rxjs-webSocket](https://rxjs.dev/api/webSocket/webSocket)
|
|
79
|
+
+ 实际请求只有在第一次`gen.next()`时才会发出请求
|
|
80
|
+
+ 由于请求带来的错误不会抛出,而是返回`{done:true,value:underfine}`
|
|
81
|
+
@example for await to take
|
|
82
|
+
```ts
|
|
83
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
84
|
+
|
|
85
|
+
const abort = new AbortController()
|
|
86
|
+
|
|
87
|
+
const gen = Reqwest.sse({
|
|
88
|
+
abort,
|
|
89
|
+
url: sse_url,
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
let index = 0
|
|
93
|
+
// 开始循环时才会调用接口获取数据
|
|
94
|
+
for await (const item of gen) {
|
|
95
|
+
if (index === 10) abort.abort() //主动终止数据获取
|
|
96
|
+
index++
|
|
97
|
+
const message = parse_sse_data(item)
|
|
98
|
+
assert(typeof message.data === 'string')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const { done, value } = await gen.next()
|
|
102
|
+
assert(done)
|
|
103
|
+
assert.equal(value, undefined)
|
|
104
|
+
```
|
|
105
|
+
@example AsyncVector to take
|
|
106
|
+
```ts
|
|
107
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
108
|
+
|
|
109
|
+
const abort = new AbortController()
|
|
110
|
+
|
|
111
|
+
const gen = Reqwest.sse({
|
|
112
|
+
abort,
|
|
113
|
+
url: sse_url,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const vec = AsyncVec(gen, false)
|
|
117
|
+
|
|
118
|
+
const result = await vec
|
|
119
|
+
.map(item => parse_sse_data(item))
|
|
120
|
+
.filter(i => Boolean(i.data))
|
|
121
|
+
.take(10)
|
|
122
|
+
.collect()
|
|
123
|
+
|
|
124
|
+
const { done, value } = await gen.next()
|
|
125
|
+
|
|
126
|
+
assert(done)
|
|
127
|
+
assert.equal(value, undefined)
|
|
128
|
+
assert.equal(result.length, 10)
|
|
129
|
+
assert(result.every(i => i.data.length > 1))
|
|
130
|
+
```
|
|
131
|
+
*/
|
|
132
|
+
static sse(conf) {
|
|
133
|
+
return see_reqwest(conf);
|
|
134
|
+
}
|
|
135
|
+
/** ## `sse` : 进行[WebSocket](https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket)请求
|
|
136
|
+
@example Usage
|
|
137
|
+
```ts
|
|
138
|
+
const MOCK_URL = 'wss://echo.websocket.org'
|
|
139
|
+
|
|
140
|
+
const { promise, resolve } = Promise.withResolvers<void>()
|
|
141
|
+
|
|
142
|
+
const abort = new AbortController()
|
|
143
|
+
|
|
144
|
+
type TansShape = { type: 'event' | 'message'; data: JsonX }
|
|
145
|
+
|
|
146
|
+
const wss = Reqwest.ws({
|
|
147
|
+
url: MOCK_URL,
|
|
148
|
+
abort: abort.signal,
|
|
149
|
+
deserialize: data => result(() => JSON.parse(data.data) as TansShape).unwrap_or(data.data),
|
|
150
|
+
serialize: (data: TansShape) => JSON.stringify(data),
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
wss.subscribe({
|
|
154
|
+
complete: resolve,
|
|
155
|
+
next: data => {
|
|
156
|
+
if (typeof data === 'string') return
|
|
157
|
+
const rdata = data as TansShape
|
|
158
|
+
if (!rdata.data) return
|
|
159
|
+
|
|
160
|
+
abort.abort() // 触发complete函数,执行complete回调
|
|
161
|
+
assert(rdata.data)
|
|
162
|
+
assert.equal(rdata.type, 'message')
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
wss.next({ type: 'event', data: false })
|
|
167
|
+
wss.next({ type: 'message', data: true })
|
|
168
|
+
|
|
169
|
+
await promise
|
|
170
|
+
```
|
|
171
|
+
*/
|
|
172
|
+
static ws(conf) {
|
|
173
|
+
return ws_reqwest(conf);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import { ExtendWebSocketSubject, ReqwestSSEConfig, ReqwestWsConfig } from './interface.js';
|
|
2
|
+
/** 正常的dispatch */
|
|
1
3
|
export declare function normal_dispatch(this: any): Promise<any>;
|
|
2
4
|
/** 请求拦截器运行核心 */
|
|
3
5
|
export declare function request_interceptor_run(conf: any, arrs: any[]): Promise<void>;
|
|
4
6
|
/** 响应拦截器运行核心 */
|
|
5
7
|
export declare function response_interceptor_run(res: any, arrs: any[]): Promise<any>;
|
|
8
|
+
/** sse请求转Generator */
|
|
9
|
+
export declare const see_reqwest: (conf: ReqwestSSEConfig) => AsyncGenerator<Uint8Array, undefined, void>;
|
|
10
|
+
export declare const ws_reqwest: <A, B>(conf: ReqwestWsConfig<A, B>) => ExtendWebSocketSubject<A, B>;
|
|
6
11
|
//# sourceMappingURL=core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/lib/reqwest/core.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/lib/reqwest/core.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAG1F,kBAAkB;AAClB,wBAAgB,eAAe,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAuCvD;AAED,gBAAgB;AAChB,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAMnF;AAED,gBAAgB;AAChB,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAQlF;AAiBD,sBAAsB;AACtB,eAAO,MAAM,WAAW,SAAU,gBAAgB,gDA2CjD,CAAA;AAMD,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,QAAQ,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,KAWzC,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAC7C,CAAA"}
|