etherreq 1.0.34 → 1.0.36

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/etherreq.js +10 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
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
@@ -21,6 +21,9 @@ const dispatchRequest = async (config) => {
21
21
  let response;
22
22
 
23
23
  const { url, method = 'GET', headers = {}, body } = config;
24
+
25
+ let text = ''; // ✅ 提前定义 text 变量
26
+
24
27
  try {
25
28
  const options = {
26
29
  method,
@@ -31,7 +34,7 @@ const dispatchRequest = async (config) => {
31
34
  const res = await fetch(url, options);
32
35
 
33
36
  // 先读取文本判断是否是 HTML
34
- const text = await res.text();
37
+ text = await res.text(); // ✅ 赋值给外部定义的 text
35
38
 
36
39
  if (!res.ok) {
37
40
  throw new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
@@ -52,8 +55,12 @@ const dispatchRequest = async (config) => {
52
55
  config,
53
56
  };
54
57
  } catch (error) {
55
- error.config = config;
56
- error.message += `\n请求地址: ${url}\n响应内容: ${text?.substring(0, 200)}...`;
58
+ error.message += `\n请求地址: ${url}`;
59
+
60
+ // ✅ 安全地添加 text 信息(如果存在)
61
+ if (text) {
62
+ error.message += `\n响应内容: ${text.substring(0, 200)}...`;
63
+ }
57
64
 
58
65
  for (const interceptor of interceptors.response) {
59
66
  if (interceptor.rejected) {
@@ -71,7 +78,6 @@ const dispatchRequest = async (config) => {
71
78
 
72
79
  return response;
73
80
  };
74
-
75
81
  const instance = (config) => {
76
82
  return dispatchRequest({
77
83
  ...defaultConfig,