etherreq 1.0.37 → 1.0.39

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.37",
3
+ "version": "1.0.39",
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
@@ -15,14 +15,17 @@ export const create = (defaultConfig = {}) => {
15
15
  const dispatchRequest = async (config) => {
16
16
  // 请求拦截器执行
17
17
  for (const interceptor of interceptors.request) {
18
- config = await interceptor.fulfilled(config);
18
+ const nextConfig = await interceptor.fulfilled(config);
19
+ // ✅ 拦截器可以返回一个 response 对象,表示中断请求
20
+ if (nextConfig && nextConfig.isFromCache) {
21
+ return nextConfig; // ✅ 直接返回缓存结果,不再往下执行
22
+ }
23
+ config = nextConfig;
19
24
  }
20
25
 
21
26
  let response;
22
-
23
27
  const { url, method = 'GET', headers = {}, body } = config;
24
-
25
- let text = ''; // ✅ 提前定义 text 变量
28
+ let text = '';
26
29
 
27
30
  try {
28
31
  const options = {
@@ -33,21 +36,17 @@ const dispatchRequest = async (config) => {
33
36
 
34
37
  const res = await fetch(url, options);
35
38
 
36
- // 先读取文本判断是否是 HTML
37
- text = await res.text(); // ✅ 赋值给外部定义的 text
39
+ text = await res.text();
38
40
 
39
41
  if (!res.ok) {
40
42
  throw new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
41
43
  }
42
44
 
43
- // 判断是否是 HTML 内容
44
45
  if (text.trim().startsWith('<')) {
45
- console.log(text);
46
-
47
46
  throw new Error('服务器返回了 HTML 页面,预期为 JSON 格式');
48
47
  }
49
48
 
50
- const data = JSON.parse(text); // 安全解析
49
+ const data = JSON.parse(text);
51
50
 
52
51
  response = {
53
52
  data,
@@ -58,8 +57,6 @@ const dispatchRequest = async (config) => {
58
57
  };
59
58
  } catch (error) {
60
59
  error.message += `\n请求地址: ${url}`;
61
-
62
- // ✅ 安全地添加 text 信息(如果存在)
63
60
  if (text) {
64
61
  error.message += `\n响应内容: ${text.substring(0, 200)}...`;
65
62
  }
package/src/request.js CHANGED
@@ -17,22 +17,22 @@ instance.interceptors.request.use((config) => {
17
17
  'Content-Type': 'application/json',
18
18
  };
19
19
 
20
- // GET 请求启用缓存
21
20
  if (config.method === 'GET' && !config.disableCache) {
22
21
  const cacheKey = getCacheKey(config.url, config);
23
22
  const cached = requestCache.get(cacheKey);
24
- console.log("缓存数据:",cached);
23
+ console.log("缓存数据:", cached);
25
24
 
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);
35
- }
25
+ if (cached) {
26
+ console.log("缓存命中");
27
+ const fakeResponse = {
28
+ data: cached,
29
+ status: 200,
30
+ statusText: 'OK',
31
+ config,
32
+ isFromCache: true, // ✅ 添加标志位
33
+ };
34
+ return Promise.resolve(fakeResponse); // ✅ 返回响应对象,中断请求链
35
+ }
36
36
  }
37
37
 
38
38
  return {