etherreq 1.0.33 → 1.0.35
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/package.json +1 -1
- package/src/etherreq.js +48 -55
package/package.json
CHANGED
package/src/etherreq.js
CHANGED
|
@@ -11,70 +11,63 @@ export const create = (defaultConfig = {}) => {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// ✅ 使用 const 显式声明 dispatchRequest
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
// etherreq.js
|
|
15
|
+
const dispatchRequest = async (config) => {
|
|
16
|
+
// 请求拦截器执行
|
|
17
|
+
for (const interceptor of interceptors.request) {
|
|
18
|
+
config = await interceptor.fulfilled(config);
|
|
19
|
+
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const { url, method = 'GET', headers = {}, body } = config;
|
|
23
|
-
try {
|
|
24
|
-
const options = {
|
|
25
|
-
method,
|
|
26
|
-
headers,
|
|
27
|
-
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const res = await fetch(url, options);
|
|
31
|
-
const data = await res.json();
|
|
32
|
-
|
|
33
|
-
if (!res.ok) {
|
|
34
|
-
const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
|
|
35
|
-
error.response = {
|
|
36
|
-
data,
|
|
37
|
-
status: res.status,
|
|
38
|
-
statusText: res.statusText,
|
|
39
|
-
headers: res.headers,
|
|
40
|
-
config,
|
|
41
|
-
};
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
21
|
+
let response;
|
|
44
22
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
} catch (error) {
|
|
53
|
-
// 确保 error 对象始终有 config 字段
|
|
54
|
-
error.config = config;
|
|
55
|
-
|
|
56
|
-
// 响应拦截器 - 错误处理
|
|
57
|
-
for (const interceptor of interceptors.response) {
|
|
58
|
-
if (interceptor.rejected) {
|
|
59
|
-
return interceptor.rejected(error);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
23
|
+
const { url, method = 'GET', headers = {}, body } = config;
|
|
24
|
+
try {
|
|
25
|
+
const options = {
|
|
26
|
+
method,
|
|
27
|
+
headers,
|
|
28
|
+
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
29
|
+
};
|
|
62
30
|
|
|
63
|
-
|
|
31
|
+
const res = await fetch(url, options);
|
|
32
|
+
|
|
33
|
+
// 先读取文本判断是否是 HTML
|
|
34
|
+
const text = await res.text();
|
|
35
|
+
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
throw new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
|
|
64
38
|
}
|
|
65
39
|
|
|
66
|
-
//
|
|
40
|
+
// 判断是否是 HTML 内容
|
|
41
|
+
if (text.trim().startsWith('<')) {
|
|
42
|
+
throw new Error('服务器返回了 HTML 页面,预期为 JSON 格式');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const data = JSON.parse(text); // 安全解析
|
|
46
|
+
|
|
47
|
+
response = {
|
|
48
|
+
data,
|
|
49
|
+
status: res.status,
|
|
50
|
+
statusText: res.statusText,
|
|
51
|
+
headers: res.headers,
|
|
52
|
+
config,
|
|
53
|
+
};
|
|
54
|
+
} catch (error) {
|
|
67
55
|
for (const interceptor of interceptors.response) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} catch (err) {
|
|
71
|
-
// 捕获响应拦截器中的异常并抛出
|
|
72
|
-
throw err;
|
|
56
|
+
if (interceptor.rejected) {
|
|
57
|
+
return interceptor.rejected(error);
|
|
73
58
|
}
|
|
74
59
|
}
|
|
75
60
|
|
|
76
|
-
|
|
77
|
-
}
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 响应拦截器执行
|
|
65
|
+
for (const interceptor of interceptors.response) {
|
|
66
|
+
response = await interceptor.fulfilled(response);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return response;
|
|
70
|
+
};
|
|
78
71
|
|
|
79
72
|
const instance = (config) => {
|
|
80
73
|
return dispatchRequest({
|