etherreq 1.0.12 → 1.0.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
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/etherreq.js CHANGED
@@ -28,17 +28,10 @@ export const create = (defaultConfig = {}) => {
28
28
 
29
29
  const res = await fetch(url, options);
30
30
 
31
- // 判断是否为 JSON 格式响应
32
- const contentType = res.headers.get('content-type');
33
- let data;
34
-
35
- if (contentType && contentType.includes('application/json')) {
36
- data = await res.json(); // 只有 JSON 内容才尝试解析
37
- } else {
38
- // 非 JSON 响应,可能是 HTML 页面(如登录重定向)
39
- data = await res.text(); // 保存原始文本避免报错
40
- throw new Error('非 JSON 响应,可能已跳转至登录页');
41
- }
31
+ const data = await res.json();
32
+ console.log(data);
33
+
34
+
42
35
 
43
36
  if (!res.ok) {
44
37
  const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
package/src/request.js CHANGED
@@ -8,14 +8,14 @@ const instance = create({
8
8
  });
9
9
 
10
10
  // 请求拦截器:自动注入 token、Content-Type 和缓存处理
11
+ // src/request.js - 请求拦截器部分
11
12
  instance.interceptors.request.use((config) => {
12
13
  const token = localStorage.getItem('token');
13
14
 
14
- // 构建最终 headers
15
15
  const headers = {
16
16
  ...(config.headers || {}),
17
- 'Content-Type': 'application/json',
18
17
  Authorization: token ? `Bearer ${token}` : undefined,
18
+ 'Content-Type': 'application/json',
19
19
  };
20
20
 
21
21
  // GET 请求启用缓存
@@ -24,8 +24,8 @@ instance.interceptors.request.use((config) => {
24
24
  const cached = requestCache.get(cacheKey);
25
25
 
26
26
  if (cached) {
27
- // ✅ 返回纯 data,不触发后续 fetch
28
- return Promise.resolve(cached);
27
+ // ✅ 返回缓存的 data 数据,中断后续请求流程
28
+ return Promise.resolve(cached); // 这里返回的就是 response.data
29
29
  }
30
30
  }
31
31
 
@@ -40,21 +40,19 @@ instance.interceptors.response.use(
40
40
  (response) => {
41
41
  const config = response.config;
42
42
 
43
- // 如果是 GET 请求且未禁用缓存,则写入缓存
44
43
  if (config && config.method === 'GET' && !config.disableCache) {
45
44
  const cacheKey = getCacheKey(config.url, config);
46
- // ✅ 只缓存 data 字段,确保是可序列化数据
45
+ // ✅ 只缓存 response.data,避免缓存整个 response 对象
47
46
  requestCache.set(cacheKey, response.data);
48
47
  }
49
48
 
50
- return response.data;
49
+ return response.data; // ✅ 正常请求也只返回 data 字段
51
50
  },
52
51
  (error) => {
53
52
  console.error('请求异常:', error);
54
53
  return Promise.reject(error);
55
54
  }
56
55
  );
57
-
58
56
  let _baseURL = 'https://api.example.com'; // 内部变量用于保存 base URL
59
57
 
60
58
  /**