etherreq 1.0.7 → 1.0.8

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 +6 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
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
@@ -13,23 +13,18 @@ instance.interceptors.request.use((config) => {
13
13
 
14
14
  // 构建最终 headers
15
15
  const headers = {
16
- ...(config.headers || {}), // 用户自定义 headers
16
+ ...(config.headers || {}),
17
17
  Authorization: token ? `Bearer ${token}` : undefined,
18
18
  };
19
19
 
20
- // 如果是 POST/PUT 等非 GET 请求,并且没有指定 Content-Type,则默认为 application/json
21
- if (config.method !== 'GET' && !headers['Content-Type']) {
22
- headers['Content-Type'] = 'application/json';
23
- }
24
-
25
20
  // GET 请求启用缓存(可选:添加 disableCache: true 配置跳过缓存)
26
21
  if (config.method === 'GET' && !config.disableCache) {
27
22
  const cacheKey = getCacheKey(config.url, config);
28
23
  const cached = requestCache.get(cacheKey);
29
24
 
30
25
  if (cached) {
31
- // 返回缓存数据,中断后续请求流程
32
- return Promise.resolve(cached);
26
+ // 返回缓存的 data 数据,中断后续请求流程
27
+ return Promise.resolve(cached); // ✅ 返回 data
33
28
  }
34
29
  }
35
30
 
@@ -47,10 +42,11 @@ instance.interceptors.response.use(
47
42
  // 如果是 GET 请求且未禁用缓存,则写入缓存
48
43
  if (config && config.method === 'GET' && !config.disableCache) {
49
44
  const cacheKey = getCacheKey(config.url, config);
50
- requestCache.set(cacheKey, response); // 缓存完整响应对象
45
+ // 只缓存 data 字段,避免缓存整个 response 对象(包含不可序列化内容)
46
+ requestCache.set(cacheKey, response.data); // ✅ 缓存 data 而不是 response
51
47
  }
52
48
 
53
- return response.data; // 返回 data 字段作为结果
49
+ return response.data;
54
50
  },
55
51
  (error) => {
56
52
  console.error('请求异常:', error);