etherreq 1.0.28 → 1.0.29
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 +49 -42
package/package.json
CHANGED
package/src/etherreq.js
CHANGED
|
@@ -10,66 +10,72 @@ export const create = (defaultConfig = {}) => {
|
|
|
10
10
|
interceptors[type].push({ fulfilled, rejected });
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
//
|
|
14
|
-
dispatchRequest = async (config) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
// ✅ 使用 const 显式声明 dispatchRequest
|
|
14
|
+
const dispatchRequest = async (config) => {
|
|
15
|
+
// 请求拦截器执行
|
|
16
|
+
for (const interceptor of interceptors.request) {
|
|
17
|
+
config = await interceptor.fulfilled(config);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let response;
|
|
19
21
|
|
|
20
|
-
|
|
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
|
+
};
|
|
21
29
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const options = {
|
|
25
|
-
method,
|
|
26
|
-
headers,
|
|
27
|
-
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
28
|
-
};
|
|
30
|
+
const res = await fetch(url, options);
|
|
31
|
+
const data = await res.json();
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
}
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
|
|
35
|
-
error.response = {
|
|
45
|
+
response = {
|
|
36
46
|
data,
|
|
37
47
|
status: res.status,
|
|
38
48
|
statusText: res.statusText,
|
|
39
49
|
headers: res.headers,
|
|
40
50
|
config,
|
|
41
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
|
+
}
|
|
62
|
+
|
|
42
63
|
throw error;
|
|
43
64
|
}
|
|
44
65
|
|
|
45
|
-
|
|
46
|
-
data,
|
|
47
|
-
status: res.status,
|
|
48
|
-
statusText: res.statusText,
|
|
49
|
-
headers: res.headers,
|
|
50
|
-
config,
|
|
51
|
-
};
|
|
52
|
-
} catch (error) {
|
|
53
|
-
// 确保 error 对象始终有 config 字段
|
|
54
|
-
error.config = config;
|
|
55
|
-
|
|
56
|
-
// 响应拦截器 - 错误处理
|
|
66
|
+
// 响应拦截器执行
|
|
57
67
|
for (const interceptor of interceptors.response) {
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
try {
|
|
69
|
+
response = await interceptor.fulfilled(response);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
// 捕获响应拦截器中的异常并抛出
|
|
72
|
+
throw err;
|
|
60
73
|
}
|
|
61
74
|
}
|
|
62
75
|
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 响应拦截器执行
|
|
67
|
-
for (const interceptor of interceptors.response) {
|
|
68
|
-
response = await interceptor.fulfilled(response);
|
|
69
|
-
}
|
|
76
|
+
return response;
|
|
77
|
+
};
|
|
70
78
|
|
|
71
|
-
return response;
|
|
72
|
-
};
|
|
73
79
|
const instance = (config) => {
|
|
74
80
|
return dispatchRequest({
|
|
75
81
|
...defaultConfig,
|
|
@@ -86,5 +92,6 @@ dispatchRequest = async (config) => {
|
|
|
86
92
|
use: (fulfilled, rejected) => use(fulfilled, rejected, 'response'),
|
|
87
93
|
},
|
|
88
94
|
};
|
|
95
|
+
|
|
89
96
|
return instance;
|
|
90
97
|
};
|