@seayoo-web/request 3.4.5 → 3.5.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.
- package/README.md +4 -4
- package/dist/index.js +29 -30
- package/dist/node.js +7 -7
- package/dist/request.fetch-BHmmkAon.js +48 -0
- package/dist/retry-DodVI8cA.js +627 -0
- package/dist/wx.js +7 -7
- package/package.json +2 -2
- package/types/inc/type.d.ts +16 -4
- package/types/inc/utils.d.ts +5 -2
- package/dist/request.fetch-6HUu6_l5.js +0 -49
- package/dist/retry-BjOxSyvC.js +0 -614
package/types/inc/type.d.ts
CHANGED
|
@@ -47,7 +47,13 @@ export interface IBaseRequestOptions {
|
|
|
47
47
|
export interface IRetryRequestOptions {
|
|
48
48
|
/** 错误时重试的次数,不能超过 10 次,默认不重试 */
|
|
49
49
|
maxRetry?: number;
|
|
50
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* 重试策略:默认 `default`
|
|
52
|
+
*
|
|
53
|
+
* `default` 在 `network` 基础之上,当状态码为 Tencent EO 专属错误码 552 / 522 时重试;
|
|
54
|
+
*
|
|
55
|
+
* `network` 仅仅网络错误时重试,但忽略 aborted / timeout 状态;
|
|
56
|
+
*/
|
|
51
57
|
retryResolve?: IRetryResolve;
|
|
52
58
|
/**
|
|
53
59
|
* 两次重试的间隔,如果设置时间,则单位 ms,或者设置函数返回等待的时间(单位ms),默认 100,最小 100,函数参数 retryIndex 从 1 开始
|
|
@@ -140,12 +146,18 @@ export type IRequestGlobalConfig = {
|
|
|
140
146
|
/** 全局日志打印函数 */
|
|
141
147
|
logHandler?: null | ((log: IRequestLog) => void);
|
|
142
148
|
} & Pick<IBaseRequestOptions, "credentials" | "timeout"> & IRetryRequestOptions & IOtherRequestOptions;
|
|
149
|
+
/**
|
|
150
|
+
* 自定义重试检测方法
|
|
151
|
+
*/
|
|
152
|
+
export type IRetryResolveFunc = (response: IRequestBaseResponse, count: number) => boolean;
|
|
143
153
|
/**
|
|
144
154
|
* 失败重试策略:
|
|
145
|
-
*
|
|
146
|
-
*
|
|
155
|
+
*
|
|
156
|
+
* `default` 在 `network` 基础之上,当状态码为 Tencent EO 专属错误码 521 / 522 时重试;
|
|
157
|
+
*
|
|
158
|
+
* `network` 仅仅网络错误时重试,但忽略 aborted / timeout 状态;
|
|
147
159
|
*/
|
|
148
|
-
export type IRetryResolve = "
|
|
160
|
+
export type IRetryResolve = "default" | "network" | number[] | IRetryResolveFunc;
|
|
149
161
|
/** 响应内容转化器 */
|
|
150
162
|
export type IResponseBodyConverter = "camelize" | "snakify" | ((body: unknown) => unknown);
|
|
151
163
|
/** 响应内容解析规则配置 */
|
package/types/inc/utils.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { IRetryResolve, IRetryResolveFunc } from "./type";
|
|
2
|
+
/** 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题 */
|
|
3
|
+
export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* 根据配置返回重试策略函数
|
|
3
6
|
*/
|
|
4
|
-
export declare function
|
|
7
|
+
export declare function getRetryCheckFn(retryResolve: IRetryResolve): IRetryResolveFunc;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { h as w, r as y, c as E, u as T, f as R, a as c } from "./retry-BjOxSyvC.js";
|
|
2
|
-
const x = async function(a, s, n) {
|
|
3
|
-
return w(await y(g, a, s, n), a, s, n);
|
|
4
|
-
}, g = async function(a, s, n) {
|
|
5
|
-
const t = await E(a, s, n), i = new URL(t.url), u = t.params;
|
|
6
|
-
u instanceof Object && Object.keys(u).forEach((e) => i.searchParams.set(e, u[e]));
|
|
7
|
-
const r = T() ? new AbortController() : null;
|
|
8
|
-
function d() {
|
|
9
|
-
r && !r.signal.aborted && r.abort();
|
|
10
|
-
}
|
|
11
|
-
t.abort && t.abort.addEventListener("abort", d);
|
|
12
|
-
let l = !1;
|
|
13
|
-
const b = t.timeout > 0 ? setTimeout(function() {
|
|
14
|
-
l = !0, d();
|
|
15
|
-
}, t.timeout) : null, f = t.method === "HEAD";
|
|
16
|
-
return await fetch(i, {
|
|
17
|
-
method: t.method,
|
|
18
|
-
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
19
|
-
body: t.body,
|
|
20
|
-
credentials: t.credentials,
|
|
21
|
-
signal: r?.signal,
|
|
22
|
-
redirect: "follow"
|
|
23
|
-
}).then(async (e) => {
|
|
24
|
-
const o = {
|
|
25
|
-
url: i.toString(),
|
|
26
|
-
method: t.method,
|
|
27
|
-
status: e.status,
|
|
28
|
-
statusText: e.statusText,
|
|
29
|
-
headers: R([...e.headers.entries()])
|
|
30
|
-
}, h = f || e.status === 204 ? "" : await e.text().catch((m) => m);
|
|
31
|
-
return h instanceof Error ? { ...o, body: "", statusText: c.Unknown, rawError: h } : { ...o, body: h };
|
|
32
|
-
}).catch((e) => {
|
|
33
|
-
const o = r?.signal.aborted;
|
|
34
|
-
return {
|
|
35
|
-
url: i.toString(),
|
|
36
|
-
method: t.method,
|
|
37
|
-
status: o && !l ? 0 : -1,
|
|
38
|
-
// 设置为 0 可以防止自动重试的默认策略
|
|
39
|
-
statusText: l ? c.Timeout : o ? c.Aborted : c.NetworkError,
|
|
40
|
-
body: "",
|
|
41
|
-
rawError: e
|
|
42
|
-
};
|
|
43
|
-
}).finally(() => {
|
|
44
|
-
b !== null && clearTimeout(b), t.abort && t.abort.removeEventListener("abort", d);
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
export {
|
|
48
|
-
x as f
|
|
49
|
-
};
|