etherreq 1.1.10 → 1.1.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/package.json +1 -1
- package/src/request.js +11 -14
package/package.json
CHANGED
package/src/request.js
CHANGED
|
@@ -7,7 +7,7 @@ const instance = create({
|
|
|
7
7
|
baseURL: 'https://api.example.com', // 默认基础 URL
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
//
|
|
10
|
+
// 请求拦截器
|
|
11
11
|
instance.interceptors.request.use((config) => {
|
|
12
12
|
const token = localStorage.getItem('token');
|
|
13
13
|
|
|
@@ -20,15 +20,16 @@ instance.interceptors.request.use((config) => {
|
|
|
20
20
|
if (config.method === 'GET' && !config.disableCache) {
|
|
21
21
|
const cacheKey = getCacheKey(config.url, config);
|
|
22
22
|
const cached = requestCache.get(cacheKey);
|
|
23
|
+
|
|
23
24
|
if (cached) {
|
|
24
|
-
|
|
25
|
+
console.log('命中缓存:', cacheKey);
|
|
26
|
+
return Promise.resolve({
|
|
25
27
|
data: cached,
|
|
26
28
|
status: 200,
|
|
27
29
|
statusText: 'OK',
|
|
28
30
|
config,
|
|
29
|
-
isFromCache: true,
|
|
30
|
-
};
|
|
31
|
-
return Promise.resolve(cached); // ✅ 返回响应对象,中断请求链
|
|
31
|
+
isFromCache: true,
|
|
32
|
+
});
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
|
|
@@ -38,23 +39,19 @@ instance.interceptors.request.use((config) => {
|
|
|
38
39
|
};
|
|
39
40
|
});
|
|
40
41
|
|
|
41
|
-
//
|
|
42
|
+
// 响应拦截器
|
|
42
43
|
instance.interceptors.response.use(
|
|
43
44
|
(response) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
console.log('使用缓存数据');
|
|
48
|
-
console.log('返回数据', response.data);
|
|
49
|
-
return response.data; // ✅ 返回 cached 数据
|
|
45
|
+
if (response.isFromCache) {
|
|
46
|
+
console.log('从缓存返回');
|
|
47
|
+
return response.data;
|
|
50
48
|
}
|
|
51
49
|
|
|
52
|
-
// 否则正常处理 response.data
|
|
53
50
|
const config = response.config;
|
|
54
|
-
|
|
55
51
|
if (config && config.method === 'GET' && !config.disableCache) {
|
|
56
52
|
const cacheKey = getCacheKey(config.url, config);
|
|
57
53
|
requestCache.set(cacheKey, response.data);
|
|
54
|
+
console.log('写入缓存:', cacheKey);
|
|
58
55
|
}
|
|
59
56
|
|
|
60
57
|
return response.data;
|