etherreq 1.0.10 → 1.0.11

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/README.md CHANGED
@@ -42,7 +42,6 @@ console.log(b1);
42
42
  };
43
43
 
44
44
  // 登录方法(自动保存 token)
45
- etherreq ;
46
45
  const login = async () => {
47
46
  const user={
48
47
  "id": 1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
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,62 +10,71 @@ export const create = (defaultConfig = {}) => {
10
10
  interceptors[type].push({ fulfilled, rejected });
11
11
  };
12
12
 
13
- const dispatchRequest = async (config) => {
14
- // 请求拦截器执行
15
- for (const interceptor of interceptors.request) {
16
- config = await interceptor.fulfilled(config);
17
- }
13
+ const dispatchRequest = async (config) => {
14
+ // 请求拦截器执行
15
+ for (const interceptor of interceptors.request) {
16
+ config = await interceptor.fulfilled(config);
17
+ }
18
18
 
19
- // 执行请求
20
- let response;
21
- try {
22
- const { url, method = 'GET', headers = {}, body } = config;
19
+ let response;
20
+ try {
21
+ const { url, method = 'GET', headers = {}, body } = config;
23
22
 
24
- const options = {
25
- method,
26
- headers,
27
- body: method !== 'GET' ? JSON.stringify(body) : undefined,
28
- };
23
+ const options = {
24
+ method,
25
+ headers,
26
+ body: method !== 'GET' ? JSON.stringify(body) : undefined,
27
+ };
29
28
 
30
- const res = await fetch(url, options);
31
- const data = await res.json();
32
-
33
- // 手动处理 HTTP 错误状态
34
- if (!res.ok) {
35
- const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
36
- error.response = {
37
- data,
38
- status: res.status,
39
- statusText: res.statusText,
40
- headers: res.headers,
41
- };
42
- throw error;
43
- }
29
+ const res = await fetch(url, options);
30
+
31
+ // 判断是否为 JSON 格式响应
32
+ const contentType = res.headers.get('content-type');
33
+ let data;
34
+
35
+ if (contentType && contentType.includes('application/json')) {
36
+ data = await res.json(); // 只有 JSON 内容才尝试解析
37
+ } else {
38
+ // 非 JSON 响应,可能是 HTML 页面(如登录重定向)
39
+ data = await res.text(); // 保存原始文本避免报错
40
+ throw new Error('非 JSON 响应,可能已跳转至登录页');
41
+ }
44
42
 
45
- response = {
43
+ if (!res.ok) {
44
+ const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
45
+ error.response = {
46
46
  data,
47
47
  status: res.status,
48
48
  statusText: res.statusText,
49
49
  headers: res.headers,
50
- config,
51
50
  };
52
- } catch (error) {
53
- // 响应拦截器 - 错误处理
54
- for (const interceptor of interceptors.response) {
55
- if (interceptor.rejected) {
56
- return interceptor.rejected(error);
57
- }
58
- }
59
51
  throw error;
60
52
  }
61
53
 
62
- // 响应拦截器执行
54
+ response = {
55
+ data,
56
+ status: res.status,
57
+ statusText: res.statusText,
58
+ headers: res.headers,
59
+ config,
60
+ };
61
+ } catch (error) {
62
+ // 响应拦截器 - 错误处理
63
63
  for (const interceptor of interceptors.response) {
64
- response = await interceptor.fulfilled(response);
64
+ if (interceptor.rejected) {
65
+ return interceptor.rejected(error);
66
+ }
65
67
  }
68
+ throw error;
69
+ }
66
70
 
67
- return response;
68
- };
71
+ // 响应拦截器执行
72
+ for (const interceptor of interceptors.response) {
73
+ response = await interceptor.fulfilled(response);
74
+ }
75
+
76
+ return response;
77
+ };
69
78
 
70
79
  const instance = (config) => {
71
80
  return dispatchRequest({