@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
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Ok } from '@chzky/core';
|
|
2
|
+
/** ## `loading_control` : loading控制器
|
|
3
|
+
使用` Ref<boolean, boolean>` 来控制loading状态 ,当reqwest开始执行的时候启动loading.当reqwest结束的时候关闭loading
|
|
4
|
+
+ 如果请求时间超过`maximum`时间,loading状态会变为false,并执行`maximun_handler`回调
|
|
5
|
+
@example Usage
|
|
6
|
+
```ts
|
|
7
|
+
const loading = ref(false)
|
|
8
|
+
const [_cnacel_controler, interceptor] = loading_control(loading, { maximum: 2000 })
|
|
9
|
+
|
|
10
|
+
let change_count = 0
|
|
11
|
+
watch(loading, nval => {
|
|
12
|
+
if (change_count === 0) assert(nval) //先变true(开始执行)
|
|
13
|
+
if (change_count === 1) assert(!nval) //后变false(执行结束)
|
|
14
|
+
change_count++
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
await mock_reqwest.clone().use(interceptor).dispatch()
|
|
18
|
+
```
|
|
19
|
+
@returns [ `controller` - 控制取消的`AbortController` , `interceptor` - 拦截器 ]
|
|
20
|
+
@category ReqwestInterceptor
|
|
21
|
+
*/
|
|
22
|
+
export function loading_control(ref, conf) {
|
|
23
|
+
let { maximum, maximun_handler = () => {
|
|
24
|
+
/* 默认空回调 */
|
|
25
|
+
}, } = conf !== null && conf !== void 0 ? conf : {};
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
let timer;
|
|
28
|
+
controller.signal.addEventListener('abort', () => {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
if (ref.value)
|
|
31
|
+
ref.value = false;
|
|
32
|
+
});
|
|
33
|
+
return [
|
|
34
|
+
controller,
|
|
35
|
+
{
|
|
36
|
+
request: config => {
|
|
37
|
+
if (maximum === void 0)
|
|
38
|
+
maximum = config.timeout || 0;
|
|
39
|
+
ref.value = true;
|
|
40
|
+
if (maximum <= 0)
|
|
41
|
+
return Ok();
|
|
42
|
+
timer = setTimeout(() => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
maximun_handler();
|
|
45
|
+
ref.value = false;
|
|
46
|
+
}, maximum);
|
|
47
|
+
return Ok();
|
|
48
|
+
},
|
|
49
|
+
response: response => {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
if (ref.value)
|
|
52
|
+
ref.value = false;
|
|
53
|
+
return Ok(response);
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chzky/reqwest",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"author": "https://github.com/HYliangkai",
|
|
5
5
|
"main": "./esm/mod.js",
|
|
6
6
|
"module": "./esm/mod.js",
|
|
@@ -12,16 +12,9 @@
|
|
|
12
12
|
},
|
|
13
13
|
"access": "public",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@chzky/
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"vue-demi": "0.14.10",
|
|
19
|
-
"@deno/shim-deno": "~0.18.0",
|
|
20
|
-
"@deno/shim-prompts": "~0.1.0",
|
|
21
|
-
"@deno/shim-timers": "~0.1.0"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/node": "^20.9.0"
|
|
15
|
+
"@chzky/core": "*",
|
|
16
|
+
"axios": "^1.12.2",
|
|
17
|
+
"vue-demi": "0.14.10"
|
|
25
18
|
},
|
|
26
19
|
"_generatedBy": "dnt@dev"
|
|
27
20
|
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Object {
|
|
3
|
+
/**
|
|
4
|
+
* Determines whether an object has a property with the specified name.
|
|
5
|
+
* @param o An object.
|
|
6
|
+
* @param v A property name.
|
|
7
|
+
*/
|
|
8
|
+
hasOwn(o: object, v: PropertyKey): boolean;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
1
12
|
declare global {
|
|
2
13
|
interface PromiseConstructor {
|
|
3
14
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_dnt.polyfills.d.ts","sourceRoot":"","sources":["../src/_dnt.polyfills.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_dnt.polyfills.d.ts","sourceRoot":"","sources":["../src/_dnt.polyfills.ts"],"names":[],"mappings":"AAeA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd;;;;WAIG;QACH,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC;KAC5C;CACF;AAED,OAAO,EAAE,CAAC;AACV,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,kBAAkB;QAC1B;;;WAGG;QACH,aAAa,CAAC,CAAC,KAAK;YAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;YAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,CAAC;KAC3H;CACF"}
|
package/script/_dnt.polyfills.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// https://github.com/tc39/proposal-accessible-object-hasownproperty/blob/main/polyfill.js
|
|
4
|
+
if (!Object.hasOwn) {
|
|
5
|
+
Object.defineProperty(Object, "hasOwn", {
|
|
6
|
+
value: function (object, property) {
|
|
7
|
+
if (object == null) {
|
|
8
|
+
throw new TypeError("Cannot convert undefined or null to object");
|
|
9
|
+
}
|
|
10
|
+
return Object.prototype.hasOwnProperty.call(Object(object), property);
|
|
11
|
+
},
|
|
12
|
+
configurable: true,
|
|
13
|
+
enumerable: false,
|
|
14
|
+
writable: true,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
2
17
|
// https://github.com/tc39/proposal-promise-with-resolvers/blob/3a78801e073e99217dbeb2c43ba7212f3bdc8b83/polyfills.js#L1C1-L9C2
|
|
3
18
|
if (Promise.withResolvers === undefined) {
|
|
4
19
|
Promise.withResolvers = () => {
|
package/script/_dnt.shims.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export { Deno } from "@deno/shim-deno";
|
|
3
|
-
export { alert, confirm, prompt } from "@deno/shim-prompts";
|
|
4
|
-
import { setInterval, setTimeout } from "@deno/shim-timers";
|
|
5
|
-
export { setInterval, setTimeout } from "@deno/shim-timers";
|
|
6
|
-
export declare const dntGlobalThis: Omit<typeof globalThis, "alert" | "confirm" | "prompt" | "setInterval" | "setTimeout" | "Deno"> & {
|
|
7
|
-
Deno: typeof Deno;
|
|
8
|
-
alert: typeof globalThis.alert;
|
|
9
|
-
confirm: typeof globalThis.confirm;
|
|
10
|
-
prompt: typeof globalThis.prompt;
|
|
11
|
-
setInterval: typeof setInterval;
|
|
12
|
-
setTimeout: typeof setTimeout;
|
|
13
|
-
};
|
|
1
|
+
export declare const dntGlobalThis: Omit<typeof globalThis, never>;
|
|
14
2
|
//# sourceMappingURL=_dnt.shims.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,gCAA2C,CAAC"}
|
package/script/_dnt.shims.js
CHANGED
|
@@ -1,26 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.dntGlobalThis =
|
|
4
|
-
const
|
|
5
|
-
var shim_deno_2 = require("@deno/shim-deno");
|
|
6
|
-
Object.defineProperty(exports, "Deno", { enumerable: true, get: function () { return shim_deno_2.Deno; } });
|
|
7
|
-
const shim_prompts_1 = require("@deno/shim-prompts");
|
|
8
|
-
var shim_prompts_2 = require("@deno/shim-prompts");
|
|
9
|
-
Object.defineProperty(exports, "alert", { enumerable: true, get: function () { return shim_prompts_2.alert; } });
|
|
10
|
-
Object.defineProperty(exports, "confirm", { enumerable: true, get: function () { return shim_prompts_2.confirm; } });
|
|
11
|
-
Object.defineProperty(exports, "prompt", { enumerable: true, get: function () { return shim_prompts_2.prompt; } });
|
|
12
|
-
const shim_timers_1 = require("@deno/shim-timers");
|
|
13
|
-
var shim_timers_2 = require("@deno/shim-timers");
|
|
14
|
-
Object.defineProperty(exports, "setInterval", { enumerable: true, get: function () { return shim_timers_2.setInterval; } });
|
|
15
|
-
Object.defineProperty(exports, "setTimeout", { enumerable: true, get: function () { return shim_timers_2.setTimeout; } });
|
|
16
|
-
const dntGlobals = {
|
|
17
|
-
Deno: shim_deno_1.Deno,
|
|
18
|
-
alert: shim_prompts_1.alert,
|
|
19
|
-
confirm: shim_prompts_1.confirm,
|
|
20
|
-
prompt: shim_prompts_1.prompt,
|
|
21
|
-
setInterval: shim_timers_1.setInterval,
|
|
22
|
-
setTimeout: shim_timers_1.setTimeout,
|
|
23
|
-
};
|
|
3
|
+
exports.dntGlobalThis = void 0;
|
|
4
|
+
const dntGlobals = {};
|
|
24
5
|
exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
25
6
|
function createMergeProxy(baseObj, extObj) {
|
|
26
7
|
return new Proxy(baseObj, {
|
package/script/lib/mod.d.ts
CHANGED
package/script/lib/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/lib/mod.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/lib/mod.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA"}
|
package/script/lib/mod.js
CHANGED
|
@@ -15,4 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./reqwest/mod.js"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
18
|
+
__exportStar(require("./utils/mod.js"), exports);
|
|
@@ -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"}
|
|
@@ -4,22 +4,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Reqwest = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
7
|
const core_js_1 = require("./core.js");
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
9
|
/** ## `Reqwest` : 基于`Axios`二次封装的请求库 */
|
|
10
10
|
class Reqwest {
|
|
11
|
-
constructor(
|
|
12
|
-
|
|
11
|
+
constructor(
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: <>
|
|
13
|
+
interceptors_req = [],
|
|
14
|
+
// biome-ignore lint/suspicious/noExplicitAny: <>
|
|
15
|
+
interceptors_res = [], cof = {}) {
|
|
16
|
+
Object.defineProperty(this, "interceptors_req", {
|
|
13
17
|
enumerable: true,
|
|
14
18
|
configurable: true,
|
|
15
19
|
writable: true,
|
|
16
|
-
value:
|
|
20
|
+
value: interceptors_req
|
|
17
21
|
});
|
|
18
|
-
Object.defineProperty(this, "
|
|
22
|
+
Object.defineProperty(this, "interceptors_res", {
|
|
19
23
|
enumerable: true,
|
|
20
24
|
configurable: true,
|
|
21
25
|
writable: true,
|
|
22
|
-
value:
|
|
26
|
+
value: interceptors_res
|
|
23
27
|
});
|
|
24
28
|
Object.defineProperty(this, "cof", {
|
|
25
29
|
enumerable: true,
|
|
@@ -27,7 +31,7 @@ class Reqwest {
|
|
|
27
31
|
writable: true,
|
|
28
32
|
value: cof
|
|
29
33
|
});
|
|
30
|
-
Object.defineProperty(this, "
|
|
34
|
+
Object.defineProperty(this, "internal_axios", {
|
|
31
35
|
enumerable: true,
|
|
32
36
|
configurable: true,
|
|
33
37
|
writable: true,
|
|
@@ -35,38 +39,144 @@ class Reqwest {
|
|
|
35
39
|
});
|
|
36
40
|
cof && this.conf(cof);
|
|
37
41
|
}
|
|
38
|
-
/** 实现{@link NewAble}接口 */
|
|
39
|
-
static new() {
|
|
40
|
-
return new Reqwest();
|
|
41
|
-
}
|
|
42
|
-
/** 实现{@link Copy}接口 */
|
|
43
|
-
clone() {
|
|
44
|
-
return new Reqwest(this._interceptors_req, this._interceptors_res, this.cof);
|
|
45
|
-
}
|
|
46
42
|
/** ### `conf` : 更新配置 */
|
|
47
43
|
conf(conf) {
|
|
48
44
|
for (const key in conf) {
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
if (Object.hasOwn(conf, key)) {
|
|
46
|
+
this.internal_axios.defaults[key] =
|
|
47
|
+
conf[key];
|
|
48
|
+
}
|
|
51
49
|
}
|
|
52
|
-
this.cof = this.
|
|
50
|
+
this.cof = this.internal_axios.defaults;
|
|
53
51
|
return this;
|
|
54
52
|
}
|
|
55
|
-
/** ### `request` : 增加请求拦截器,拦截器会按顺序执行
|
|
56
|
-
+ 该方法会返回一个新的Reqwest,并且与原Reqwest保持独立 */
|
|
57
53
|
request(...interceptors) {
|
|
58
|
-
return new Reqwest([...this.
|
|
54
|
+
return new Reqwest([...this.interceptors_req, ...interceptors], this.interceptors_res, this.cof);
|
|
59
55
|
}
|
|
60
56
|
response(...interceptor) {
|
|
61
|
-
return new Reqwest(this.
|
|
57
|
+
return new Reqwest(this.interceptors_req, [...this.interceptors_res, ...interceptor], this.cof);
|
|
62
58
|
}
|
|
63
|
-
/** ### `use` : 增加拦截器,返回一个新的Reqwest */
|
|
64
59
|
use(interceptor) {
|
|
65
|
-
return new Reqwest([...this.
|
|
60
|
+
return new Reqwest([...this.interceptors_req, interceptor.request], [...this.interceptors_res, interceptor.response], this.cof);
|
|
66
61
|
}
|
|
67
|
-
/** ### `dispatch` : 发送请求 */
|
|
68
62
|
dispatch() {
|
|
69
63
|
return core_js_1.normal_dispatch.call(this);
|
|
70
64
|
}
|
|
65
|
+
clone() {
|
|
66
|
+
return new Reqwest(this.interceptors_req, this.interceptors_res, this.cof);
|
|
67
|
+
}
|
|
68
|
+
/** ### `new`
|
|
69
|
+
实现{@link NewAble}接口 */
|
|
70
|
+
static new() {
|
|
71
|
+
return new Reqwest();
|
|
72
|
+
}
|
|
73
|
+
/** ### `default`
|
|
74
|
+
实现{@link Default}接口 : 以默认配置生成一个`Reqwest`
|
|
75
|
+
#### 默认配置:
|
|
76
|
+
+ `timeout` : `6000ms`
|
|
77
|
+
*/
|
|
78
|
+
static default() {
|
|
79
|
+
return new Reqwest().conf({
|
|
80
|
+
timeout: 6000,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** ## `sse` : 进行[SSE](https://developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events)请求
|
|
84
|
+
api使用见[rxjs-webSocket](https://rxjs.dev/api/webSocket/webSocket)
|
|
85
|
+
+ 实际请求只有在第一次`gen.next()`时才会发出请求
|
|
86
|
+
+ 由于请求带来的错误不会抛出,而是返回`{done:true,value:underfine}`
|
|
87
|
+
@example for await to take
|
|
88
|
+
```ts
|
|
89
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
90
|
+
|
|
91
|
+
const abort = new AbortController()
|
|
92
|
+
|
|
93
|
+
const gen = Reqwest.sse({
|
|
94
|
+
abort,
|
|
95
|
+
url: sse_url,
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
let index = 0
|
|
99
|
+
// 开始循环时才会调用接口获取数据
|
|
100
|
+
for await (const item of gen) {
|
|
101
|
+
if (index === 10) abort.abort() //主动终止数据获取
|
|
102
|
+
index++
|
|
103
|
+
const message = parse_sse_data(item)
|
|
104
|
+
assert(typeof message.data === 'string')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const { done, value } = await gen.next()
|
|
108
|
+
assert(done)
|
|
109
|
+
assert.equal(value, undefined)
|
|
110
|
+
```
|
|
111
|
+
@example AsyncVector to take
|
|
112
|
+
```ts
|
|
113
|
+
const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
|
|
114
|
+
|
|
115
|
+
const abort = new AbortController()
|
|
116
|
+
|
|
117
|
+
const gen = Reqwest.sse({
|
|
118
|
+
abort,
|
|
119
|
+
url: sse_url,
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
const vec = AsyncVec(gen, false)
|
|
123
|
+
|
|
124
|
+
const result = await vec
|
|
125
|
+
.map(item => parse_sse_data(item))
|
|
126
|
+
.filter(i => Boolean(i.data))
|
|
127
|
+
.take(10)
|
|
128
|
+
.collect()
|
|
129
|
+
|
|
130
|
+
const { done, value } = await gen.next()
|
|
131
|
+
|
|
132
|
+
assert(done)
|
|
133
|
+
assert.equal(value, undefined)
|
|
134
|
+
assert.equal(result.length, 10)
|
|
135
|
+
assert(result.every(i => i.data.length > 1))
|
|
136
|
+
```
|
|
137
|
+
*/
|
|
138
|
+
static sse(conf) {
|
|
139
|
+
return (0, core_js_1.see_reqwest)(conf);
|
|
140
|
+
}
|
|
141
|
+
/** ## `sse` : 进行[WebSocket](https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket)请求
|
|
142
|
+
@example Usage
|
|
143
|
+
```ts
|
|
144
|
+
const MOCK_URL = 'wss://echo.websocket.org'
|
|
145
|
+
|
|
146
|
+
const { promise, resolve } = Promise.withResolvers<void>()
|
|
147
|
+
|
|
148
|
+
const abort = new AbortController()
|
|
149
|
+
|
|
150
|
+
type TansShape = { type: 'event' | 'message'; data: JsonX }
|
|
151
|
+
|
|
152
|
+
const wss = Reqwest.ws({
|
|
153
|
+
url: MOCK_URL,
|
|
154
|
+
abort: abort.signal,
|
|
155
|
+
deserialize: data => result(() => JSON.parse(data.data) as TansShape).unwrap_or(data.data),
|
|
156
|
+
serialize: (data: TansShape) => JSON.stringify(data),
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
wss.subscribe({
|
|
160
|
+
complete: resolve,
|
|
161
|
+
next: data => {
|
|
162
|
+
if (typeof data === 'string') return
|
|
163
|
+
const rdata = data as TansShape
|
|
164
|
+
if (!rdata.data) return
|
|
165
|
+
|
|
166
|
+
abort.abort() // 触发complete函数,执行complete回调
|
|
167
|
+
assert(rdata.data)
|
|
168
|
+
assert.equal(rdata.type, 'message')
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
wss.next({ type: 'event', data: false })
|
|
173
|
+
wss.next({ type: 'message', data: true })
|
|
174
|
+
|
|
175
|
+
await promise
|
|
176
|
+
```
|
|
177
|
+
*/
|
|
178
|
+
static ws(conf) {
|
|
179
|
+
return (0, core_js_1.ws_reqwest)(conf);
|
|
180
|
+
}
|
|
71
181
|
}
|
|
72
182
|
exports.Reqwest = Reqwest;
|
|
@@ -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"}
|