etherreq 1.0.28 → 1.0.30

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.28",
3
+ "version": "1.0.30",
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,66 +10,72 @@ export const create = (defaultConfig = {}) => {
10
10
  interceptors[type].push({ fulfilled, rejected });
11
11
  };
12
12
 
13
- // etherreq.js
14
- dispatchRequest = async (config) => {
15
- // 请求拦截器执行
16
- for (const interceptor of interceptors.request) {
17
- config = await interceptor.fulfilled(config);
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
- let response;
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
- 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
- };
30
+ const res = await fetch(url, options);
31
+ const data = await res.json();
29
32
 
30
- const res = await fetch(url, options);
31
- const data = await res.json();
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
- if (!res.ok) {
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
- response = {
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
- if (interceptor.rejected) {
59
- return interceptor.rejected(error);
68
+ try {
69
+ response = await interceptor.fulfilled(response);
70
+ } catch (err) {
71
+ // 捕获响应拦截器中的异常并抛出
72
+ throw err;
60
73
  }
61
74
  }
62
75
 
63
- throw error;
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
  };
package/src/request.js CHANGED
@@ -25,7 +25,20 @@ instance.interceptors.request.use((config) => {
25
25
 
26
26
  if (cached) {
27
27
  console.log("缓存命中");
28
- return Promise.resolve(cached); // 这里返回的就是 response.data
28
+ // return Promise.resolve(cached); // 这里返回的就是 response.data
29
+ const a={
30
+ "data": {
31
+ "id": 123,
32
+ "name": "张三",
33
+ "email": "zhangsan@example.com"
34
+ },
35
+ "status": 200,
36
+ "statusText": "OK",
37
+ "headers": {
38
+ "content-type": "application/json"
39
+ }
40
+ }
41
+ return a;
29
42
  }
30
43
  }
31
44
 
@@ -26,7 +26,7 @@ interface EtherreqStatic extends EtherRequestMethod {
26
26
  put: EtherRequestMethod;
27
27
  delete: EtherRequestMethod;
28
28
  del: EtherRequestMethod;
29
- login: EtherRequestMethod; // 新增 login 类型
29
+ login: EtherRequestMethod;
30
30
  }
31
31
 
32
32
  // 导出对象