etherreq 1.0.33 → 1.0.34

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/etherreq.js +51 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
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
@@ -11,70 +11,66 @@ export const create = (defaultConfig = {}) => {
11
11
  };
12
12
 
13
13
  // ✅ 使用 const 显式声明 dispatchRequest
14
- const dispatchRequest = async (config) => {
15
- // 请求拦截器执行
16
- for (const interceptor of interceptors.request) {
17
- config = await interceptor.fulfilled(config);
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
- let response;
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
- 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
- // 响应拦截器 - 错误处理
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
+ };
30
+
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}`);
38
+ }
62
39
 
63
- throw error;
40
+ // 判断是否是 HTML 内容
41
+ if (text.trim().startsWith('<')) {
42
+ throw new Error('服务器返回了 HTML 页面,预期为 JSON 格式');
64
43
  }
65
44
 
66
- // 响应拦截器执行
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) {
55
+ error.config = config;
56
+ error.message += `\n请求地址: ${url}\n响应内容: ${text?.substring(0, 200)}...`;
57
+
67
58
  for (const interceptor of interceptors.response) {
68
- try {
69
- response = await interceptor.fulfilled(response);
70
- } catch (err) {
71
- // 捕获响应拦截器中的异常并抛出
72
- throw err;
59
+ if (interceptor.rejected) {
60
+ return interceptor.rejected(error);
73
61
  }
74
62
  }
75
63
 
76
- return response;
77
- };
64
+ throw error;
65
+ }
66
+
67
+ // 响应拦截器执行
68
+ for (const interceptor of interceptors.response) {
69
+ response = await interceptor.fulfilled(response);
70
+ }
71
+
72
+ return response;
73
+ };
78
74
 
79
75
  const instance = (config) => {
80
76
  return dispatchRequest({