etherreq 1.0.27 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "A lightweight custom HTTP request library.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/types/etherreq.d.ts",
package/src/etherreq.js CHANGED
@@ -10,63 +10,71 @@ export const create = (defaultConfig = {}) => {
10
10
  interceptors[type].push({ fulfilled, rejected });
11
11
  };
12
12
 
13
- const dispatchRequest = async (config) => {
14
- // 请求拦截器执行
13
+ // 使用 const 显式声明 dispatchRequest
14
+ const dispatchRequest = async (config) => {
15
+ // 请求拦截器执行
15
16
  for (const interceptor of interceptors.request) {
16
- config = await interceptor.fulfilled(config);
17
- }
18
- let response;
17
+ config = await interceptor.fulfilled(config);
18
+ }
19
19
 
20
+ let response;
20
21
 
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
- };
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
29
 
30
- const res = await fetch(url, options);
31
- const data = await res.json();
32
- let response;
30
+ const res = await fetch(url, options);
31
+ const data = await res.json();
33
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
+ }
34
44
 
35
- if (!res.ok) {
36
- const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
37
- error.response = {
45
+ response = {
38
46
  data,
39
47
  status: res.status,
40
48
  statusText: res.statusText,
41
49
  headers: res.headers,
50
+ config,
42
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
+
43
63
  throw error;
44
64
  }
45
65
 
46
- response = {
47
- data,
48
- status: res.status,
49
- statusText: res.statusText,
50
- headers: res.headers,
51
- config,
52
- };
53
- } catch (error) {
54
- // 响应拦截器 - 错误处理
66
+ // 响应拦截器执行
55
67
  for (const interceptor of interceptors.response) {
56
- if (interceptor.rejected) {
57
- return interceptor.rejected(error);
68
+ try {
69
+ response = await interceptor.fulfilled(response);
70
+ } catch (err) {
71
+ // 捕获响应拦截器中的异常并抛出
72
+ throw err;
58
73
  }
59
74
  }
60
- throw error;
61
- }
62
-
63
- // 响应拦截器执行
64
- for (const interceptor of interceptors.response) {
65
- response = await interceptor.fulfilled(response);
66
- }
67
75
 
68
- return response;
69
- };
76
+ return response;
77
+ };
70
78
 
71
79
  const instance = (config) => {
72
80
  return dispatchRequest({
@@ -85,6 +93,5 @@ export const create = (defaultConfig = {}) => {
85
93
  },
86
94
  };
87
95
 
88
-
89
96
  return instance;
90
97
  };
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' &&
Binary file