etherreq 1.0.10 → 1.0.12
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 +0 -1
- package/package.json +1 -1
- package/src/etherreq.js +50 -41
- package/src/request.js +6 -6
package/README.md
CHANGED
package/package.json
CHANGED
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
const { url, method = 'GET', headers = {}, body } = config;
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
const { url, method = 'GET', headers = {}, body } = config;
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
const options = {
|
|
24
|
+
method,
|
|
25
|
+
headers,
|
|
26
|
+
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
27
|
+
};
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
-
|
|
64
|
+
if (interceptor.rejected) {
|
|
65
|
+
return interceptor.rejected(error);
|
|
66
|
+
}
|
|
65
67
|
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
66
70
|
|
|
67
|
-
|
|
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({
|
package/src/request.js
CHANGED
|
@@ -18,14 +18,14 @@ instance.interceptors.request.use((config) => {
|
|
|
18
18
|
Authorization: token ? `Bearer ${token}` : undefined,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
// GET
|
|
21
|
+
// GET 请求启用缓存
|
|
22
22
|
if (config.method === 'GET' && !config.disableCache) {
|
|
23
23
|
const cacheKey = getCacheKey(config.url, config);
|
|
24
24
|
const cached = requestCache.get(cacheKey);
|
|
25
25
|
|
|
26
26
|
if (cached) {
|
|
27
|
-
//
|
|
28
|
-
return Promise.resolve(cached);
|
|
27
|
+
// ✅ 返回纯 data,不触发后续 fetch
|
|
28
|
+
return Promise.resolve(cached);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -40,11 +40,11 @@ instance.interceptors.response.use(
|
|
|
40
40
|
(response) => {
|
|
41
41
|
const config = response.config;
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
// 如果是 GET 请求且未禁用缓存,则写入缓存
|
|
44
44
|
if (config && config.method === 'GET' && !config.disableCache) {
|
|
45
45
|
const cacheKey = getCacheKey(config.url, config);
|
|
46
|
-
// 只缓存 data
|
|
47
|
-
requestCache.set(cacheKey, response.data);
|
|
46
|
+
// ✅ 只缓存 data 字段,确保是可序列化数据
|
|
47
|
+
requestCache.set(cacheKey, response.data);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
return response.data;
|