etherreq 1.1.11 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/request.js +15 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
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/request.js CHANGED
@@ -7,7 +7,7 @@ const instance = create({
7
7
  baseURL: 'https://api.example.com', // 默认基础 URL
8
8
  });
9
9
 
10
- // 请求拦截器:自动注入 token、Content-Type 和缓存处理
10
+ // 请求拦截器
11
11
  instance.interceptors.request.use((config) => {
12
12
  const token = localStorage.getItem('token');
13
13
 
@@ -20,8 +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
- return Promise.resolve(cached); // ✅ 返回响应对象,中断请求链
25
+ console.log('命中缓存:', cacheKey);
26
+ return Promise.resolve({
27
+ data: cached,
28
+ status: 200,
29
+ statusText: 'OK',
30
+ config,
31
+ isFromCache: true,
32
+ });
25
33
  }
26
34
  }
27
35
 
@@ -31,21 +39,19 @@ instance.interceptors.request.use((config) => {
31
39
  };
32
40
  });
33
41
 
34
- // 响应拦截器:自动提取 response.data 并写入缓存
42
+ // 响应拦截器
35
43
  instance.interceptors.response.use(
36
44
  (response) => {
37
- // 如果是缓存命中,直接返回 cached 数据
38
-
39
- if (response ) {
40
- return response.data; // ✅ 返回 cached 数据
45
+ if (response.isFromCache) {
46
+ console.log('从缓存返回');
47
+ return response.data;
41
48
  }
42
49
 
43
- // 否则正常处理 response.data
44
50
  const config = response.config;
45
-
46
51
  if (config && config.method === 'GET' && !config.disableCache) {
47
52
  const cacheKey = getCacheKey(config.url, config);
48
53
  requestCache.set(cacheKey, response.data);
54
+ console.log('写入缓存:', cacheKey);
49
55
  }
50
56
 
51
57
  return response.data;