etherreq 1.0.32 → 1.0.33

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 +17 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
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
@@ -23,25 +23,16 @@ instance.interceptors.request.use((config) => {
23
23
  const cached = requestCache.get(cacheKey);
24
24
  console.log("缓存数据:",cached);
25
25
 
26
- if (cached) {
27
- console.log("缓存命中");
28
- // return Promise.resolve(cached); // 这里返回的就是 response.data
29
- const a={
30
- "data": {
31
- "id": 123,
32
- "name": "张三",
33
- "email": "zhangsan@example.com"
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
- (response) => {
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);