@chzky/reqwest 0.0.1 → 0.1.1

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.
Files changed (179) hide show
  1. package/README.md +314 -0
  2. package/esm/_dnt.polyfills.d.ts +11 -0
  3. package/esm/_dnt.polyfills.d.ts.map +1 -10
  4. package/esm/_dnt.polyfills.js +24 -66
  5. package/esm/_dnt.shims.d.ts +1 -13
  6. package/esm/_dnt.shims.d.ts.map +1 -1
  7. package/esm/_dnt.shims.js +57 -1
  8. package/esm/lib/core/class.d.ts +122 -0
  9. package/esm/lib/core/class.d.ts.map +1 -0
  10. package/esm/lib/core/class.js +173 -0
  11. package/esm/lib/core/core.d.ts +11 -0
  12. package/esm/lib/core/core.d.ts.map +1 -0
  13. package/esm/lib/core/core.js +157 -0
  14. package/esm/lib/{reqwest → core}/factor.d.ts +57 -9
  15. package/esm/lib/core/factor.d.ts.map +1 -0
  16. package/esm/lib/core/factor.js +48 -0
  17. package/esm/lib/core/interface.d.ts +172 -0
  18. package/esm/lib/core/interface.d.ts.map +1 -0
  19. package/esm/lib/core/interface.js +1 -0
  20. package/esm/lib/core/mod.d.ts +5 -0
  21. package/esm/lib/core/mod.d.ts.map +1 -0
  22. package/esm/lib/core/mod.js +3 -0
  23. package/esm/lib/helper/mod.d.ts +2 -0
  24. package/esm/lib/helper/mod.d.ts.map +1 -1
  25. package/esm/lib/helper/mod.js +2 -0
  26. package/esm/lib/helper/other/mod.d.ts +2 -0
  27. package/esm/lib/helper/other/mod.d.ts.map +1 -0
  28. package/esm/lib/helper/other/mod.js +1 -0
  29. package/esm/lib/helper/other/sseParse.d.ts +34 -0
  30. package/esm/lib/helper/other/sseParse.d.ts.map +1 -0
  31. package/esm/lib/helper/other/sseParse.js +42 -0
  32. package/esm/lib/helper/request/RESTful.d.ts +43 -11
  33. package/esm/lib/helper/request/RESTful.d.ts.map +1 -1
  34. package/esm/lib/helper/request/RESTful.js +129 -1
  35. package/esm/lib/helper/request/baseConf.d.ts +11 -4
  36. package/esm/lib/helper/request/baseConf.d.ts.map +1 -1
  37. package/esm/lib/helper/request/baseConf.js +37 -1
  38. package/esm/lib/helper/request/factors.d.ts +31 -0
  39. package/esm/lib/helper/request/factors.d.ts.map +1 -0
  40. package/esm/lib/helper/request/factors.js +40 -0
  41. package/esm/lib/helper/request/input.d.ts +5 -4
  42. package/esm/lib/helper/request/input.d.ts.map +1 -1
  43. package/esm/lib/helper/request/input.js +24 -1
  44. package/esm/lib/helper/request/mod.d.ts +4 -1
  45. package/esm/lib/helper/request/mod.d.ts.map +1 -1
  46. package/esm/lib/helper/request/mod.js +6 -3
  47. package/esm/lib/helper/request/process.d.ts +16 -0
  48. package/esm/lib/helper/request/process.d.ts.map +1 -0
  49. package/esm/lib/helper/request/process.js +37 -0
  50. package/esm/lib/helper/request/wait.d.ts +9 -0
  51. package/esm/lib/helper/request/wait.d.ts.map +1 -0
  52. package/esm/lib/helper/request/wait.js +13 -0
  53. package/esm/lib/helper/response/blobToUrl.d.ts +11 -0
  54. package/esm/lib/helper/response/blobToUrl.d.ts.map +1 -0
  55. package/esm/lib/helper/response/blobToUrl.js +22 -0
  56. package/esm/lib/helper/response/mod.d.ts +3 -1
  57. package/esm/lib/helper/response/mod.d.ts.map +1 -1
  58. package/esm/lib/helper/response/mod.js +4 -2
  59. package/esm/lib/helper/response/openLink.d.ts +8 -0
  60. package/esm/lib/helper/response/openLink.d.ts.map +1 -0
  61. package/esm/lib/helper/response/openLink.js +21 -0
  62. package/esm/lib/helper/response/output.d.ts +14 -3
  63. package/esm/lib/helper/response/output.d.ts.map +1 -1
  64. package/esm/lib/helper/response/output.js +24 -1
  65. package/esm/lib/helper/response/pick.d.ts +19 -2
  66. package/esm/lib/helper/response/pick.d.ts.map +1 -1
  67. package/esm/lib/helper/response/pick.js +26 -1
  68. package/esm/lib/helper/use/mod.js +2 -2
  69. package/esm/lib/helper/use/useCancel.d.ts +32 -2
  70. package/esm/lib/helper/use/useCancel.d.ts.map +1 -1
  71. package/esm/lib/helper/use/useCancel.js +42 -1
  72. package/esm/lib/helper/use/useLoading.d.ts +27 -7
  73. package/esm/lib/helper/use/useLoading.d.ts.map +1 -1
  74. package/esm/lib/helper/use/useLoading.js +58 -1
  75. package/esm/lib/mod.d.ts +1 -1
  76. package/esm/lib/mod.d.ts.map +1 -1
  77. package/esm/lib/mod.js +1 -1
  78. package/package.json +4 -11
  79. package/script/_dnt.polyfills.d.ts +11 -0
  80. package/script/_dnt.polyfills.d.ts.map +1 -1
  81. package/script/_dnt.polyfills.js +15 -0
  82. package/script/_dnt.shims.d.ts +1 -13
  83. package/script/_dnt.shims.d.ts.map +1 -1
  84. package/script/_dnt.shims.js +2 -21
  85. package/script/lib/core/class.d.ts +122 -0
  86. package/script/lib/core/class.d.ts.map +1 -0
  87. package/script/lib/core/class.js +180 -0
  88. package/script/lib/core/core.d.ts +11 -0
  89. package/script/lib/core/core.d.ts.map +1 -0
  90. package/script/lib/core/core.js +165 -0
  91. package/script/lib/{reqwest → core}/factor.d.ts +57 -9
  92. package/script/lib/core/factor.d.ts.map +1 -0
  93. package/script/lib/{reqwest → core}/factor.js +24 -8
  94. package/script/lib/core/interface.d.ts +172 -0
  95. package/script/lib/core/interface.d.ts.map +1 -0
  96. package/script/lib/core/mod.d.ts +5 -0
  97. package/script/lib/core/mod.d.ts.map +1 -0
  98. package/script/lib/{reqwest → core}/mod.js +1 -0
  99. package/script/lib/helper/mod.d.ts +2 -0
  100. package/script/lib/helper/mod.d.ts.map +1 -1
  101. package/script/lib/helper/mod.js +2 -0
  102. package/script/lib/helper/other/mod.d.ts +2 -0
  103. package/script/lib/helper/other/mod.d.ts.map +1 -0
  104. package/script/lib/helper/other/mod.js +17 -0
  105. package/script/lib/helper/other/sseParse.d.ts +34 -0
  106. package/script/lib/helper/other/sseParse.d.ts.map +1 -0
  107. package/script/lib/helper/other/sseParse.js +45 -0
  108. package/script/lib/helper/request/RESTful.d.ts +43 -11
  109. package/script/lib/helper/request/RESTful.d.ts.map +1 -1
  110. package/script/lib/helper/request/RESTful.js +75 -21
  111. package/script/lib/helper/request/baseConf.d.ts +11 -4
  112. package/script/lib/helper/request/baseConf.d.ts.map +1 -1
  113. package/script/lib/helper/request/baseConf.js +25 -10
  114. package/script/lib/helper/request/factors.d.ts +31 -0
  115. package/script/lib/helper/request/factors.d.ts.map +1 -0
  116. package/script/lib/helper/request/factors.js +43 -0
  117. package/script/lib/helper/request/input.d.ts +5 -4
  118. package/script/lib/helper/request/input.d.ts.map +1 -1
  119. package/script/lib/helper/request/input.js +11 -11
  120. package/script/lib/helper/request/mod.d.ts +4 -1
  121. package/script/lib/helper/request/mod.d.ts.map +1 -1
  122. package/script/lib/helper/request/mod.js +6 -3
  123. package/script/lib/helper/request/process.d.ts +16 -0
  124. package/script/lib/helper/request/process.d.ts.map +1 -0
  125. package/script/lib/helper/request/process.js +41 -0
  126. package/script/lib/helper/request/wait.d.ts +9 -0
  127. package/script/lib/helper/request/wait.d.ts.map +1 -0
  128. package/script/lib/helper/request/wait.js +16 -0
  129. package/script/lib/helper/response/blobToUrl.d.ts +11 -0
  130. package/script/lib/helper/response/blobToUrl.d.ts.map +1 -0
  131. package/script/lib/helper/response/blobToUrl.js +25 -0
  132. package/script/lib/helper/response/mod.d.ts +3 -1
  133. package/script/lib/helper/response/mod.d.ts.map +1 -1
  134. package/script/lib/helper/response/mod.js +4 -2
  135. package/script/lib/helper/response/openLink.d.ts +8 -0
  136. package/script/lib/helper/response/openLink.d.ts.map +1 -0
  137. package/script/lib/helper/response/openLink.js +47 -0
  138. package/script/lib/helper/response/output.d.ts +14 -3
  139. package/script/lib/helper/response/output.d.ts.map +1 -1
  140. package/script/lib/helper/response/output.js +20 -7
  141. package/script/lib/helper/response/pick.d.ts +19 -2
  142. package/script/lib/helper/response/pick.d.ts.map +1 -1
  143. package/script/lib/helper/response/pick.js +22 -5
  144. package/script/lib/helper/use/mod.js +2 -2
  145. package/script/lib/helper/use/useCancel.d.ts +32 -2
  146. package/script/lib/helper/use/useCancel.d.ts.map +1 -1
  147. package/script/lib/helper/use/useCancel.js +37 -7
  148. package/script/lib/helper/use/useLoading.d.ts +27 -7
  149. package/script/lib/helper/use/useLoading.d.ts.map +1 -1
  150. package/script/lib/helper/use/useLoading.js +56 -46
  151. package/script/lib/mod.d.ts +1 -1
  152. package/script/lib/mod.d.ts.map +1 -1
  153. package/script/lib/mod.js +1 -1
  154. package/esm/lib/reqwest/class.d.ts +0 -84
  155. package/esm/lib/reqwest/class.d.ts.map +0 -1
  156. package/esm/lib/reqwest/class.js +0 -1
  157. package/esm/lib/reqwest/core.d.ts +0 -6
  158. package/esm/lib/reqwest/core.d.ts.map +0 -1
  159. package/esm/lib/reqwest/core.js +0 -250
  160. package/esm/lib/reqwest/factor.d.ts.map +0 -1
  161. package/esm/lib/reqwest/factor.js +0 -1
  162. package/esm/lib/reqwest/interface.d.ts +0 -48
  163. package/esm/lib/reqwest/interface.d.ts.map +0 -1
  164. package/esm/lib/reqwest/interface.js +0 -1
  165. package/esm/lib/reqwest/mod.d.ts +0 -4
  166. package/esm/lib/reqwest/mod.d.ts.map +0 -1
  167. package/esm/lib/reqwest/mod.js +0 -2
  168. package/script/lib/reqwest/class.d.ts +0 -84
  169. package/script/lib/reqwest/class.d.ts.map +0 -1
  170. package/script/lib/reqwest/class.js +0 -72
  171. package/script/lib/reqwest/core.d.ts +0 -6
  172. package/script/lib/reqwest/core.d.ts.map +0 -1
  173. package/script/lib/reqwest/core.js +0 -72
  174. package/script/lib/reqwest/factor.d.ts.map +0 -1
  175. package/script/lib/reqwest/interface.d.ts +0 -23
  176. package/script/lib/reqwest/interface.d.ts.map +0 -1
  177. package/script/lib/reqwest/mod.d.ts +0 -4
  178. package/script/lib/reqwest/mod.d.ts.map +0 -1
  179. /package/script/lib/{reqwest → core}/interface.js +0 -0
@@ -0,0 +1,173 @@
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` : `6000`ms
71
+ */
72
+ static default() {
73
+ return new Reqwest().conf({ timeout: 6000 });
74
+ }
75
+ /** ## `sse` : 进行[SSE](https://developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events)请求
76
+ api使用见[rxjs-webSocket](https://rxjs.dev/api/webSocket/webSocket)
77
+ + 实际请求只有在第一次`gen.next()`时才会发出请求
78
+ + 由于请求带来的错误不会抛出,而是返回`{done:true,value:underfine}`
79
+ @example for await to take
80
+ ```ts
81
+ const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
82
+
83
+ const abort = new AbortController()
84
+
85
+ const gen = Reqwest.sse({
86
+ abort,
87
+ url: sse_url,
88
+ })
89
+
90
+ let index = 0
91
+ // 开始循环时才会调用接口获取数据
92
+ for await (const item of gen) {
93
+ if (index === 10) abort.abort() //主动终止数据获取
94
+ index++
95
+ const message = parse_sse_data(item)
96
+ assert(typeof message.data === 'string')
97
+ }
98
+
99
+ const { done, value } = await gen.next()
100
+ assert(done)
101
+ assert.equal(value, undefined)
102
+ ```
103
+ @example AsyncVector to take
104
+ ```ts
105
+ const sse_url = 'https://stream.wikimedia.org/v2/stream/recentchange'
106
+
107
+ const abort = new AbortController()
108
+
109
+ const gen = Reqwest.sse({
110
+ abort,
111
+ url: sse_url,
112
+ })
113
+
114
+ const vec = AsyncVec(gen, false)
115
+
116
+ const result = await vec
117
+ .map(item => parse_sse_data(item))
118
+ .filter(i => Boolean(i.data))
119
+ .take(10)
120
+ .collect()
121
+
122
+ const { done, value } = await gen.next()
123
+
124
+ assert(done)
125
+ assert.equal(value, undefined)
126
+ assert.equal(result.length, 10)
127
+ assert(result.every(i => i.data.length > 1))
128
+ ```
129
+ */
130
+ static sse(conf) {
131
+ return see_reqwest(conf);
132
+ }
133
+ /** ## `sse` : 进行[WebSocket](https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket)请求
134
+ @example Usage
135
+ ```ts
136
+ const MOCK_URL = 'wss://echo.websocket.org'
137
+
138
+ const { promise, resolve } = Promise.withResolvers<void>()
139
+
140
+ const abort = new AbortController()
141
+
142
+ type TansShape = { type: 'event' | 'message'; data: JsonX }
143
+
144
+ const wss = Reqwest.ws({
145
+ url: MOCK_URL,
146
+ abort: abort.signal,
147
+ deserialize: data => result(() => JSON.parse(data.data) as TansShape).unwrap_or(data.data),
148
+ serialize: (data: TansShape) => JSON.stringify(data),
149
+ })
150
+
151
+ wss.subscribe({
152
+ complete: resolve,
153
+ next: data => {
154
+ if (typeof data === 'string') return
155
+ const rdata = data as TansShape
156
+ if (!rdata.data) return
157
+
158
+ abort.abort() // 触发complete函数,执行complete回调
159
+ assert(rdata.data)
160
+ assert.equal(rdata.type, 'message')
161
+ },
162
+ })
163
+
164
+ wss.next({ type: 'event', data: false })
165
+ wss.next({ type: 'message', data: true })
166
+
167
+ await promise
168
+ ```
169
+ */
170
+ static ws(conf) {
171
+ return ws_reqwest(conf);
172
+ }
173
+ }
@@ -0,0 +1,11 @@
1
+ import { ExtendWebSocketSubject, ReqwestSSEConfig, ReqwestWsConfig } from './interface.js';
2
+ /** 正常的dispatch */
3
+ export declare function normal_dispatch(this: any): Promise<any>;
4
+ /** 请求拦截器运行核心 */
5
+ export declare function request_interceptor_run(conf: any, arrs: any[]): Promise<void>;
6
+ /** 响应拦截器运行核心 */
7
+ export declare function response_interceptor_run(res: any, arrs: any[], wrap?: boolean): 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>;
11
+ //# sourceMappingURL=core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/lib/core/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,EAAE,IAAI,UAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAQhG;AAiBD,sBAAsB;AACtB,eAAO,MAAM,WAAW,SAAU,gBAAgB,gDAwCjD,CAAA;AAMD,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,QAAQ,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,KAWzC,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAC7C,CAAA"}
@@ -0,0 +1,157 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
11
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
14
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
15
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
16
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
17
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
18
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
19
+ function fulfill(value) { resume("next", value); }
20
+ function reject(value) { resume("throw", value); }
21
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
22
+ };
23
+ /** biome-ignore-all lint/suspicious/noExplicitAny: < core file should ignore any type > */
24
+ import { AbortedError, AnyError, AnyHowError, CheckingError, Err, is_err, is_ok, None, Ok, rjx_ws, throwing, UnexpectedError, } from '@chzky/core';
25
+ import { AxiosError, CanceledError } from 'axios';
26
+ /** 正常的dispatch */
27
+ export function normal_dispatch() {
28
+ const { resolve, promise } = Promise.withResolvers();
29
+ /** request_interceptor */
30
+ this.internal_axios.interceptors.request.use((conf) => __awaiter(this, void 0, void 0, function* () {
31
+ yield request_interceptor_run(conf, this.interceptors_req);
32
+ return conf;
33
+ }));
34
+ /** response_interceptor */
35
+ this.internal_axios.interceptors.response.use((r) => r /* 2xx 范围内的状态码都会触发该函数。 */, throwing /* 超出 2xx 范围的状态码都会触发该函数。 */);
36
+ this.internal_axios({})
37
+ .then((res) => __awaiter(this, void 0, void 0, function* () {
38
+ const result = yield response_interceptor_run(res, this.interceptors_res);
39
+ return Ok(result);
40
+ }))
41
+ .then(resolve, (e) => {
42
+ /* 异常的可能情况
43
+ 1. interceptors.request的异常
44
+ 1.1 CanceledError - 请求终止错误
45
+ 2. interceptors.response超出200~300的code
46
+ 3. interceptors.response执行过程中的异常:<返回Err类型,或是执行过程中直接throw>
47
+ */
48
+ resolve(e instanceof AnyError
49
+ ? Err(e)
50
+ : e instanceof CanceledError
51
+ ? AbortedError.err(e.message || 'Request has been canceled')
52
+ : e instanceof AxiosError
53
+ ? AnyHowError.err(e.message)
54
+ : UnexpectedError.err(e));
55
+ });
56
+ return promise;
57
+ }
58
+ /** 请求拦截器运行核心 */
59
+ export function request_interceptor_run(conf, arrs) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ for (const fn of arrs) {
62
+ const res = yield Promise.resolve(fn(conf));
63
+ if (is_err(res) || is_ok(res))
64
+ res.unwrap(); /* 解包 */
65
+ else
66
+ throw CheckingError.new('RequestInterceptor must return Err<E> or Ok<void>');
67
+ }
68
+ });
69
+ }
70
+ /** 响应拦截器运行核心 */
71
+ export function response_interceptor_run(res_1, arrs_1) {
72
+ return __awaiter(this, arguments, void 0, function* (res, arrs, wrap = false) {
73
+ let result = res;
74
+ for (const fn of arrs) {
75
+ result = yield Promise.resolve(fn(result));
76
+ if (is_err(result) || is_ok(result))
77
+ result = result.unwrap(); /* 解包 */
78
+ else
79
+ throw CheckingError.new('ResponseInterceptor must return Err<E> or Ok<O>');
80
+ }
81
+ return wrap ? Ok(result) : result;
82
+ });
83
+ }
84
+ const is_aborteder = (err) => err instanceof DOMException && err.name === 'AbortError';
85
+ const cleanup_reader = (readerr) => __awaiter(void 0, void 0, void 0, function* () {
86
+ /**
87
+ 1. 数据读取完毕
88
+ 2. gen被关闭读取
89
+ 3. aborted
90
+ */
91
+ try {
92
+ yield (readerr === null || readerr === void 0 ? void 0 : readerr.cancel());
93
+ }
94
+ catch (_) {
95
+ // ignore exception
96
+ }
97
+ });
98
+ /** sse请求转Generator */
99
+ export const see_reqwest = (conf) => {
100
+ const { url, abort, method = 'GET' } = conf;
101
+ let response;
102
+ let reader;
103
+ const init_reader = () => __awaiter(void 0, void 0, void 0, function* () {
104
+ var _a;
105
+ if (reader !== undefined)
106
+ return;
107
+ response = yield fetch(url, {
108
+ method,
109
+ headers: { Accept: 'text/event-stream', 'Cache-Control': 'no-cache' },
110
+ signal: abort ? (abort instanceof AbortController ? abort.signal : abort) : undefined,
111
+ });
112
+ const lreader = (_a = response.body) === null || _a === void 0 ? void 0 : _a.getReader();
113
+ if (lreader === undefined)
114
+ throw None;
115
+ reader = lreader;
116
+ });
117
+ function start_sse() {
118
+ return __asyncGenerator(this, arguments, function* start_sse_1() {
119
+ try {
120
+ yield __await(init_reader());
121
+ }
122
+ catch (_) {
123
+ return yield __await(void 0);
124
+ }
125
+ try {
126
+ while (true) {
127
+ const { done, value } = yield __await(reader.read());
128
+ if (done)
129
+ break;
130
+ yield yield __await(value);
131
+ }
132
+ return yield __await(void 0);
133
+ }
134
+ catch (error) {
135
+ if (!is_aborteder(error))
136
+ throw error;
137
+ }
138
+ finally {
139
+ yield __await(cleanup_reader(reader));
140
+ }
141
+ });
142
+ }
143
+ return start_sse();
144
+ };
145
+ const default_deserialize = (data) => JSON.parse(data.data);
146
+ const default_serialize = (data) => JSON.stringify(data);
147
+ export const ws_reqwest = (conf) => {
148
+ const { url, abort, deserialize = default_deserialize, serialize = default_serialize } = conf;
149
+ const subject = rjx_ws.webSocket({ url, serializer: serialize, deserializer: deserialize });
150
+ if (abort instanceof AbortController) {
151
+ abort === null || abort === void 0 ? void 0 : abort.signal.addEventListener('abort', subject.complete.bind(subject));
152
+ }
153
+ else {
154
+ abort === null || abort === void 0 ? void 0 : abort.addEventListener('abort', subject.complete.bind(subject));
155
+ }
156
+ return subject;
157
+ };
@@ -1,11 +1,59 @@
1
1
  /** 工厂函数,省去类型声明的步骤 */
2
- import type { RequestInterceptor, MergeErrors, ResponseInterceptor, ReqwestInterceptor } from './interface.js';
3
- /** ### `merge_request` : 工厂函数 of `Reqwest-request`
4
- + 将多个request拦截器合并成一个拦截器
2
+ import type { MergeErrors, RequestInterceptor, ReqwestInterceptor, ResponseInterceptor } from './interface.js';
3
+ /** ### `merge_request` : `request`拦截器合并
4
+ @example Usage
5
+ ```ts
6
+ const reqs = merge_request(
7
+ get('/1/', { id: 1 }),
8
+ input_validate(zod.object({ id: zod.number() })), // 输出判断类型
9
+ response_type('arraybuffer') // 改变接受的响应类型
10
+ )
11
+
12
+ const res = await mock_reqwest.clone().request(reqs).dispatch()
13
+
14
+ const RR = res.unwrap().data as Uint8Array
15
+ const json_text = new TextDecoder('utf-8').decode(RR)
16
+ assert.equal(JSON.parse(json_text).id, 1)
17
+
18
+ mock_reqwest.conf({ timeout: 6000 })
19
+ ```
20
+ @Category Factory
5
21
  */
6
- export declare function merge_request<Interceptors extends RequestInterceptor<any>[]>(...interceptors: Interceptors): RequestInterceptor<MergeErrors<Interceptors>>;
7
- /** ### `merge_response` : 工厂函数 of `Reqwest-response`
8
- + 将多个response拦截器合并成一个拦截器
22
+ export declare function merge_request<Interceptors extends RequestInterceptor<unknown>[]>(...interceptors: Interceptors): RequestInterceptor<MergeErrors<Interceptors>>;
23
+ /** ### `merge_response` : `response`拦截器合并
24
+ @example
25
+ ```ts
26
+ const reqwest = Reqwest.new().conf({ baseURL: 'https://jsonplaceholder.typicode.com/posts/' })
27
+
28
+ const reps = merge_response(
29
+ // 返回值映射
30
+ response_map((data: AxiosResponse<unknown, unknown, object>) => {
31
+ if (data.status !== 200) return AnyHowError.err(data.statusText)
32
+ const rdata = data.data as { userId: number; id: number; title: string; body: string }
33
+ return Ok({ data: rdata, msg: 'success' })
34
+ }),
35
+ // 数据校验
36
+ output_validate(
37
+ zod.object({
38
+ data: zod
39
+ .object({
40
+ userId: zod.exchange.string(), // number --> string
41
+ title: zod.string(),
42
+ })
43
+ .passthrough(), //允许溢出数据
44
+ })
45
+ )
46
+ )
47
+
48
+ const result = await reqwest.request(append_url('/1')).response(reps).dispatch()
49
+
50
+ const {
51
+ data: { userId },
52
+ } = result.unwrap()
53
+
54
+ assert.strict_equal(userId, '1')
55
+ ```
56
+ @Category Factory
9
57
  */
10
58
  export declare function merge_response<I, O, E>(...interceptor: [ResponseInterceptor<I, O, E>]): ResponseInterceptor<I, O, E>;
11
59
  export declare function merge_response<I, O, E, O2, E2>(...interceptor: [ResponseInterceptor<I, O, E>, ResponseInterceptor<O, O2, E2>]): ResponseInterceptor<I, O2, E | E2>;
@@ -65,8 +113,8 @@ export declare function merge_response<I, O, E, O2, E2, O3, E3, O4, E4, O5, E5,
65
113
  ResponseInterceptor<O7, O8, E8>,
66
114
  ResponseInterceptor<O8, O9, E9>
67
115
  ]): ResponseInterceptor<I, O9, E | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9>;
68
- /** ### `merge_response` : 工厂函数 of `Reqwest-use`
69
- + 快捷生成`ReqwestInterceptor`供`use`使用
70
- */
116
+ /** ### `merge_response` : 合并生成`ReqwestInterceptor`
117
+ @Category Factory
118
+ */
71
119
  export declare function merge_reqwest<O, E, OO, EE>(request: RequestInterceptor<E>, response: ResponseInterceptor<O, OO, EE>): ReqwestInterceptor<O, E, OO, EE>;
72
120
  //# sourceMappingURL=factor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factor.d.ts","sourceRoot":"","sources":["../../../src/lib/core/factor.ts"],"names":[],"mappings":"AAAA,qBAAqB;AAErB,OAAO,KAAK,EACX,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,MAAM,gBAAgB,CAAA;AAMvB;;;;;;;;;;;;;;;;;;EAkBE;AACF,wBAAgB,aAAa,CAAC,YAAY,SAAS,kBAAkB,CAAC,OAAO,CAAC,EAAE,EAC/E,GAAG,YAAY,EAAE,YAAY,GAC3B,kBAAkB,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,CAK/C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAkCE;AACF,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EACrC,GAAG,WAAW,EAAE,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAC5C,mBAAmB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;AAC/B,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,EA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AAE,GAAG,EAAE,CAAC,CAAA;AASxE;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EACzC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAC9B,QAAQ,EAAE,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,GACtC,kBAAkB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,CAElC"}
@@ -0,0 +1,48 @@
1
+ /** 工厂函数,省去类型声明的步骤 */
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ import { Ok } from '@chzky/core';
12
+ import { request_interceptor_run, response_interceptor_run } from './core.js';
13
+ /** ### `merge_request` : `request`拦截器合并
14
+ @example Usage
15
+ ```ts
16
+ const reqs = merge_request(
17
+ get('/1/', { id: 1 }),
18
+ input_validate(zod.object({ id: zod.number() })), // 输出判断类型
19
+ response_type('arraybuffer') // 改变接受的响应类型
20
+ )
21
+
22
+ const res = await mock_reqwest.clone().request(reqs).dispatch()
23
+
24
+ const RR = res.unwrap().data as Uint8Array
25
+ const json_text = new TextDecoder('utf-8').decode(RR)
26
+ assert.equal(JSON.parse(json_text).id, 1)
27
+
28
+ mock_reqwest.conf({ timeout: 6000 })
29
+ ```
30
+ @Category Factory
31
+ */
32
+ export function merge_request(...interceptors) {
33
+ return (conf) => __awaiter(this, void 0, void 0, function* () {
34
+ yield request_interceptor_run(conf, interceptors);
35
+ return Ok();
36
+ });
37
+ }
38
+ export function merge_response(...interceptor) {
39
+ return (res) => __awaiter(this, void 0, void 0, function* () {
40
+ return yield response_interceptor_run(res, interceptor, true);
41
+ });
42
+ }
43
+ /** ### `merge_response` : 合并生成`ReqwestInterceptor`
44
+ @Category Factory
45
+ */
46
+ export function merge_reqwest(request, response) {
47
+ return { request, response };
48
+ }
@@ -0,0 +1,172 @@
1
+ import type { AsyncResult, Copy, Fn, PromiseOr, Result, rjx_ws, UnexpectedError, zod } from '@chzky/core';
2
+ import type { AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
3
+ type WebSocketMessage = string | ArrayBuffer | Blob | ArrayBufferView;
4
+ /** ### `ReqwestWsConfig` : WebSocket连接配置 */
5
+ export type ReqwestWsConfig<A, B> = {
6
+ /** WebSocket连接地址 */
7
+ url: string;
8
+ /** 终止控制器 */
9
+ abort?: AbortController | AbortSignal;
10
+ /** 反序列化函数 , 将获取的数据反序列化为`B`类型
11
+ @default `(data)=>JSON.parse(data.data)` */
12
+ deserialize?: Fn<MessageEvent, B>;
13
+ /** 序列化函数 , 将发送的数据序列化为`WebSocketMessage`类型
14
+ @default `(data)=>JSON.stringify(data)` */
15
+ serialize?: Fn<A, WebSocketMessage>;
16
+ };
17
+ export type ExtendWebSocketSubject<A, B> = {
18
+ next: (data: A) => void;
19
+ subscribe: Fn<Fn<B, void>, void>;
20
+ } & {
21
+ complete?: Fn<void, void>;
22
+ error?: Fn<Fn<unknown, void>, void>;
23
+ next?: Fn<Fn<B, void>, void>;
24
+ } & rjx_ws.WebSocketSubject<unknown>;
25
+ /** ### `ReqwestSSEConfig` : SSE连接配置 */
26
+ export type ReqwestSSEConfig = {
27
+ /** SSE连接地址 */
28
+ url: string;
29
+ /** 终止控制器 */
30
+ abort?: AbortController | AbortSignal;
31
+ /** SSE连接方法 : 默认`get` */
32
+ method?: 'GET' | 'POST';
33
+ /** 解码器 : 默认不使用,直接返回`Uint8Array` */
34
+ decode?: {
35
+ decode: (input: Uint8Array<ArrayBuffer>) => string;
36
+ };
37
+ /** 数据校验器,用于校验解码后的数据 : 默认不使用,直接返回解码后的数据 */
38
+ validator?: zod.ZodSchema<unknown>;
39
+ };
40
+ /** ### `RequestInterceptor` : 请求拦截器 | 返回`Ok()` or `Err(E)`
41
+ + `Ok()`表示通过拦截器 , 会运行下一个拦截器
42
+ + `Err(E)`表示拦截器拦截 , 会直接短路返回`Err(E)`
43
+ */
44
+ export type RequestInterceptor<E = never> = (conf: InternalAxiosRequestConfig<unknown>) => PromiseOr<Result<void, E>>;
45
+ /** ### `ResponseInterceptor` : 响应拦截器 | 返回`Ok(O)` or `Err(E)`
46
+ + `Ok(O)` 表示通过拦截器 , 会自动解包并将值传递给下一个拦截器
47
+ + `Err(E)`表示拦截器拦截 , 会直接短路返回`Err(E)`
48
+ */
49
+ export type ResponseInterceptor<O = AxiosResponse<unknown, unknown>, OO = AxiosResponse<unknown, unknown>, EE = never> = (response: O) => PromiseOr<Result<OO, EE>>;
50
+ /** ### `ReqwestInterceptor` : `请求-响应拦截器` | 分成响应拦截和请求拦截 */
51
+ export interface ReqwestInterceptor<O, E, OO = O, EE = never> {
52
+ request: RequestInterceptor<E>;
53
+ response: ResponseInterceptor<O, OO, EE>;
54
+ }
55
+ export type MergeErrors<T extends unknown[]> = T extends [
56
+ RequestInterceptor<infer E>,
57
+ ...infer Rest
58
+ ] ? E | MergeErrors<Rest> : never;
59
+ type FinalResult<Interceptors extends ResponseInterceptor<unknown, unknown, unknown>[], CurrentOutput = AxiosResponse<unknown>, CurrentError = UnexpectedError> = Interceptors extends [
60
+ infer First,
61
+ ...infer Rest extends ResponseInterceptor<unknown, unknown, unknown>[]
62
+ ] ? First extends ResponseInterceptor<CurrentOutput, infer NextOutput, infer NextError> ? FinalResult<Rest, NextOutput, NextError | CurrentError> : never : Result<CurrentOutput, CurrentError>;
63
+ export type ResponseResult<O, E, ER, Interceptors extends ResponseInterceptor<unknown, unknown, unknown>[]> = ReqwestInterface<FinalResult<Interceptors, O, ER> extends Result<infer O, unknown> ? O : never, E, FinalResult<Interceptors, O, ER> extends Result<unknown, infer E> ? E : never>;
64
+ export interface ReqwestInterface<O = AxiosResponse<unknown, unknown>, E = never, ER = UnexpectedError> extends Copy {
65
+ /** ### `conf` : 更新配置
66
+ @example Usage
67
+ ```ts
68
+ const conf = { baseURL: 'https://jsonplaceholder.typicode.com/todos/' }
69
+ const mock_reqwest = Reqwest.new().conf(conf)
70
+ mock_reqwest.dispatch() // curl https://jsonplaceholder.typicode.com/todos/
71
+ ```
72
+ */
73
+ conf(conf: CreateAxiosDefaults): ReqwestInterface<O, E, ER>;
74
+ /** ### `dispatch` : 发送请求,获取请求结果
75
+ @example Usage
76
+ ```ts
77
+ const conf = { baseURL: 'https://jsonplaceholder.typicode.com/todos/' }
78
+ const mock_reqwest = Reqwest.new().conf(conf)
79
+ mock_reqwest.dispatch() // dispatch to get https://jsonplaceholder.typicode.com/todos/
80
+ ```
81
+ */
82
+ dispatch(): AsyncResult<O, E | ER>;
83
+ /** ### `request` : 增加请求拦截器,拦截器会按顺序执行
84
+ + 该方法会返回一个新的`Reqwest`,并且与原`Reqwest`保持独立 */
85
+ request<I extends RequestInterceptor<unknown>[]>(...interceptors: I): ReqwestInterface<O, MergeErrors<I> | E, ER>;
86
+ /** ### `response` : 增加响应拦截器,拦截器会按顺序执行
87
+ + 该方法会返回一个新的`Reqwest`,并且与原`Reqwest`保持独立 */
88
+ response<OO, EE>(interceptor: ResponseInterceptor<O, OO, EE>): ReqwestInterface<OO, E, EE | ER>;
89
+ response<OO, EE, O2, E2>(...interceptor: [ResponseInterceptor<O, OO, EE>, ResponseInterceptor<OO, O2, E2>]): ReqwestInterface<O2, E, E2 | EE | ER>;
90
+ response<OO, EE, O2, E2, O3, E3>(...interceptor: [
91
+ ResponseInterceptor<O, OO, EE>,
92
+ ResponseInterceptor<OO, O2, E2>,
93
+ ResponseInterceptor<O2, O3, E3>
94
+ ]): ReqwestInterface<O3, E, E3 | E2 | EE | ER>;
95
+ response<OO, EE, O2, E2, O3, E3, O4, E4>(...interceptor: [
96
+ ResponseInterceptor<O, OO, EE>,
97
+ ResponseInterceptor<OO, O2, E2>,
98
+ ResponseInterceptor<O2, O3, E3>,
99
+ ResponseInterceptor<O3, O4, E4>
100
+ ]): ReqwestInterface<O4, E, E4 | E3 | E2 | EE | ER>;
101
+ response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5>(...interceptor: [
102
+ ResponseInterceptor<O, OO, EE>,
103
+ ResponseInterceptor<OO, O2, E2>,
104
+ ResponseInterceptor<O2, O3, E3>,
105
+ ResponseInterceptor<O3, O4, E4>
106
+ ]): ReqwestInterface<O5, E, E5 | E4 | E3 | E2 | EE | ER>;
107
+ response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6>(...interceptor: [
108
+ ResponseInterceptor<O, OO, EE>,
109
+ ResponseInterceptor<OO, O2, E2>,
110
+ ResponseInterceptor<O2, O3, E3>,
111
+ ResponseInterceptor<O3, O4, E4>,
112
+ ResponseInterceptor<O4, O5, E5>,
113
+ ResponseInterceptor<O5, O6, E6>
114
+ ]): ReqwestInterface<O6, E, E6 | E5 | E4 | E3 | E2 | EE | ER>;
115
+ response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7>(...interceptor: [
116
+ ResponseInterceptor<O, OO, EE>,
117
+ ResponseInterceptor<OO, O2, E2>,
118
+ ResponseInterceptor<O2, O3, E3>,
119
+ ResponseInterceptor<O3, O4, E4>,
120
+ ResponseInterceptor<O4, O5, E5>,
121
+ ResponseInterceptor<O5, O6, E6>,
122
+ ResponseInterceptor<O6, O7, E7>
123
+ ]): ReqwestInterface<O7, E, E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
124
+ response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7, O8, E8>(...interceptor: [
125
+ ResponseInterceptor<O, OO, EE>,
126
+ ResponseInterceptor<OO, O2, E2>,
127
+ ResponseInterceptor<O2, O3, E3>,
128
+ ResponseInterceptor<O3, O4, E4>,
129
+ ResponseInterceptor<O4, O5, E5>,
130
+ ResponseInterceptor<O5, O6, E6>,
131
+ ResponseInterceptor<O6, O7, E7>,
132
+ ResponseInterceptor<O7, O8, E8>
133
+ ]): ReqwestInterface<O8, E, E8 | E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
134
+ response<OO, EE, O2, E2, O3, E3, O4, E4, O5, E5, O6, E6, O7, E7, O8, E8, O9, E9>(...interceptor: [
135
+ ResponseInterceptor<O, OO, EE>,
136
+ ResponseInterceptor<OO, O2, E2>,
137
+ ResponseInterceptor<O2, O3, E3>,
138
+ ResponseInterceptor<O3, O4, E4>,
139
+ ResponseInterceptor<O4, O5, E5>,
140
+ ResponseInterceptor<O5, O6, E6>,
141
+ ResponseInterceptor<O6, O7, E7>,
142
+ ResponseInterceptor<O7, O8, E8>,
143
+ ResponseInterceptor<O8, O9, E9>
144
+ ]): ReqwestInterface<O9, E, E9 | E8 | E7 | E6 | E5 | E4 | E3 | E2 | EE | ER>;
145
+ /** ### `use` : 增加`请求-响应拦截器`,拦截器会按顺序执行
146
+ + 该方法会返回一个新的`Reqwest`,并且与原`Reqwest`保持独立
147
+ @example Usage - cancel
148
+ ```ts
149
+ const conf = { baseURL: 'https://jsonplaceholder.typicode.com/todos/' }
150
+ const mock_reqwest = Reqwest.new().conf(conf)
151
+
152
+ const [abort_controler, interceptor] = cancel() // 可取消的请求
153
+
154
+ const reqwest = mock_reqwest.clone().request(wait(2000), get('/1')).use(interceptor)
155
+
156
+ const mock_cause = 'mock abort'
157
+ abort_controler.abort(mock_cause)
158
+
159
+ const result = await reqwest.dispatch()
160
+
161
+ const error = result.unwrap_err()
162
+ assert(error.instance_of(AbortedError))
163
+ assert.equal(error.cause, mock_cause)
164
+ ```
165
+ */
166
+ use<OO, EC, EE>(interceptor: ReqwestInterceptor<O, EC, OO, EE>): ReqwestInterface<OO, EC | E, EE | ER>;
167
+ /** ### `clone`
168
+ + 实现{@link Copy}接口 : 生成一个全新的`Reqwest` */
169
+ clone(): ReqwestInterface<O, E, ER>;
170
+ }
171
+ export {};
172
+ //# sourceMappingURL=interface.d.ts.map