etherreq 1.2.2 → 1.2.4

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/dist/etherreq.js CHANGED
@@ -11,13 +11,20 @@ export const create = (defaultConfig = {}) => {
11
11
  processedConfig = await interceptor(processedConfig);
12
12
  }
13
13
  try {
14
+ // 确定是否需要发送请求体
15
+ const method = processedConfig.method || 'GET';
16
+ const hasBody = processedConfig.body &&
17
+ method !== 'GET' &&
18
+ method !== 'HEAD' &&
19
+ method !== 'OPTIONS';
14
20
  // 发送请求
15
21
  const response = await fetch(processedConfig.url, {
16
- method: processedConfig.method || 'GET',
22
+ method,
17
23
  headers: {
18
24
  ...(processedConfig.headers || {}),
19
25
  },
20
- body: processedConfig.body ? JSON.stringify(processedConfig.body) : undefined,
26
+ // 只有非GET/HEAD/OPTIONS请求才发送body
27
+ body: hasBody ? JSON.stringify(processedConfig.body) : undefined,
21
28
  });
22
29
  // 检查响应状态
23
30
  if (!response.ok) {
package/dist/index.js CHANGED
@@ -11,7 +11,13 @@ const createMethod = (method) => (url, data, callback) => {
11
11
  !data.headers &&
12
12
  !data.params &&
13
13
  !data.baseURL) {
14
- options = { body: data, method: method };
14
+ // 对于GET请求,不应发送body,而是将数据作为params处理
15
+ if (method.toUpperCase() === 'GET') {
16
+ options = { params: data, method: method };
17
+ }
18
+ else {
19
+ options = { body: data, method: method };
20
+ }
15
21
  }
16
22
  else {
17
23
  options = { ...data, method: method };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A lightweight custom HTTP request library with TypeScript support.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",