etherreq 1.0.26 → 1.0.28
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 +16 -16
- package/src/index.js +0 -5
- package/etherreq-1.0.0.tgz +0 -0
package/package.json
CHANGED
package/src/etherreq.js
CHANGED
|
@@ -10,27 +10,25 @@ export const create = (defaultConfig = {}) => {
|
|
|
10
10
|
interceptors[type].push({ fulfilled, rejected });
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// etherreq.js
|
|
14
|
+
dispatchRequest = async (config) => {
|
|
14
15
|
// 请求拦截器执行
|
|
15
|
-
|
|
16
|
+
for (const interceptor of interceptors.request) {
|
|
16
17
|
config = await interceptor.fulfilled(config);
|
|
17
18
|
}
|
|
18
|
-
let response;
|
|
19
19
|
|
|
20
|
+
let response;
|
|
20
21
|
|
|
21
|
-
// 否则才发起网络请求
|
|
22
22
|
const { url, method = 'GET', headers = {}, body } = config;
|
|
23
23
|
try {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const res = await fetch(url, options);
|
|
31
|
-
const data = await res.json();
|
|
32
|
-
|
|
24
|
+
const options = {
|
|
25
|
+
method,
|
|
26
|
+
headers,
|
|
27
|
+
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
28
|
+
};
|
|
33
29
|
|
|
30
|
+
const res = await fetch(url, options);
|
|
31
|
+
const data = await res.json();
|
|
34
32
|
|
|
35
33
|
if (!res.ok) {
|
|
36
34
|
const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
|
|
@@ -39,6 +37,7 @@ export const create = (defaultConfig = {}) => {
|
|
|
39
37
|
status: res.status,
|
|
40
38
|
statusText: res.statusText,
|
|
41
39
|
headers: res.headers,
|
|
40
|
+
config,
|
|
42
41
|
};
|
|
43
42
|
throw error;
|
|
44
43
|
}
|
|
@@ -51,12 +50,16 @@ export const create = (defaultConfig = {}) => {
|
|
|
51
50
|
config,
|
|
52
51
|
};
|
|
53
52
|
} catch (error) {
|
|
53
|
+
// 确保 error 对象始终有 config 字段
|
|
54
|
+
error.config = config;
|
|
55
|
+
|
|
54
56
|
// 响应拦截器 - 错误处理
|
|
55
57
|
for (const interceptor of interceptors.response) {
|
|
56
58
|
if (interceptor.rejected) {
|
|
57
59
|
return interceptor.rejected(error);
|
|
58
60
|
}
|
|
59
61
|
}
|
|
62
|
+
|
|
60
63
|
throw error;
|
|
61
64
|
}
|
|
62
65
|
|
|
@@ -67,7 +70,6 @@ export const create = (defaultConfig = {}) => {
|
|
|
67
70
|
|
|
68
71
|
return response;
|
|
69
72
|
};
|
|
70
|
-
|
|
71
73
|
const instance = (config) => {
|
|
72
74
|
return dispatchRequest({
|
|
73
75
|
...defaultConfig,
|
|
@@ -84,7 +86,5 @@ export const create = (defaultConfig = {}) => {
|
|
|
84
86
|
use: (fulfilled, rejected) => use(fulfilled, rejected, 'response'),
|
|
85
87
|
},
|
|
86
88
|
};
|
|
87
|
-
|
|
88
|
-
|
|
89
89
|
return instance;
|
|
90
90
|
};
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
// src/index.js
|
|
2
2
|
import { request, baseURL as _baseURL, setBaseURL } from './request';
|
|
3
|
-
|
|
4
|
-
// 设置全局 base URL
|
|
5
|
-
// setBaseURL('http://localhost:8081/api');
|
|
6
|
-
|
|
7
3
|
// 示例封装方法
|
|
8
4
|
const createMethod = (method) => (url, data, callback) => {
|
|
9
5
|
let options;
|
|
10
|
-
|
|
11
6
|
if (
|
|
12
7
|
data &&
|
|
13
8
|
typeof data === 'object' &&
|
package/etherreq-1.0.0.tgz
DELETED
|
Binary file
|