etherreq 1.0.32 → 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.
- package/package.json +1 -1
- package/src/etherreq.js +51 -55
- package/src/request.js +17 -22
package/package.json
CHANGED
package/src/etherreq.js
CHANGED
|
@@ -11,70 +11,66 @@ export const create = (defaultConfig = {}) => {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// ✅ 使用 const 显式声明 dispatchRequest
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
} catch (err) {
|
|
71
|
-
// 捕获响应拦截器中的异常并抛出
|
|
72
|
-
throw err;
|
|
59
|
+
if (interceptor.rejected) {
|
|
60
|
+
return interceptor.rejected(error);
|
|
73
61
|
}
|
|
74
62
|
}
|
|
75
63
|
|
|
76
|
-
|
|
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({
|
package/src/request.js
CHANGED
|
@@ -23,25 +23,16 @@ instance.interceptors.request.use((config) => {
|
|
|
23
23
|
const cached = requestCache.get(cacheKey);
|
|
24
24
|
console.log("缓存数据:",cached);
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"status": 200,
|
|
36
|
-
"statusText": "OK",
|
|
37
|
-
"headers": {
|
|
38
|
-
"content-type": "application/json"
|
|
39
|
-
}
|
|
26
|
+
if (cached) {
|
|
27
|
+
console.log("缓存命中");
|
|
28
|
+
const fakeResponse = {
|
|
29
|
+
data: cached, // 确保和 response.data 一致
|
|
30
|
+
status: 200,
|
|
31
|
+
statusText: 'OK',
|
|
32
|
+
config,
|
|
33
|
+
};
|
|
34
|
+
return Promise.resolve(fakeResponse);
|
|
40
35
|
}
|
|
41
|
-
console.log("返回数据:");
|
|
42
|
-
console.log("返回数据:",a);
|
|
43
|
-
return a;
|
|
44
|
-
}
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
return {
|
|
@@ -52,16 +43,20 @@ console.log("返回数据:",a);
|
|
|
52
43
|
|
|
53
44
|
// 响应拦截器:自动提取 response.data 并写入缓存
|
|
54
45
|
instance.interceptors.response.use(
|
|
55
|
-
|
|
46
|
+
(response) => {
|
|
47
|
+
// 判断是否是缓存返回的数据(没有 .config)
|
|
48
|
+
if (!response.config && response.data !== undefined) {
|
|
49
|
+
// 已经是 data,直接返回
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
52
|
+
|
|
56
53
|
const config = response.config;
|
|
57
54
|
|
|
58
55
|
if (config && config.method === 'GET' && !config.disableCache) {
|
|
59
56
|
const cacheKey = getCacheKey(config.url, config);
|
|
60
|
-
// ✅ 只缓存 response.data,避免缓存整个 response 对象
|
|
61
57
|
requestCache.set(cacheKey, response.data);
|
|
62
58
|
}
|
|
63
|
-
|
|
64
|
-
return response.data; // ✅ 正常请求也只返回 data 字段
|
|
59
|
+
return response.data;
|
|
65
60
|
},
|
|
66
61
|
(error) => {
|
|
67
62
|
console.error('请求异常:', error);
|