etherreq 1.2.2 → 1.2.5
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 +9 -2
- package/dist/index.js +7 -1
- package/dist/request.js +12 -2
- package/package.json +1 -1
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
|
|
22
|
+
method,
|
|
17
23
|
headers: {
|
|
18
24
|
...(processedConfig.headers || {}),
|
|
19
25
|
},
|
|
20
|
-
|
|
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
|
-
|
|
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/dist/request.js
CHANGED
|
@@ -81,8 +81,18 @@ function getCacheTTL(config) {
|
|
|
81
81
|
* @param options - 请求配置
|
|
82
82
|
*/
|
|
83
83
|
export const request = (url, options = {}) => {
|
|
84
|
-
|
|
85
|
-
const
|
|
84
|
+
// 如果 options.baseURL 存在则使用它,否则使用全局 _baseURL,如果都不存在则使用当前页面的 origin
|
|
85
|
+
const baseURL = options.baseURL || _baseURL || (typeof window !== 'undefined' ? window.location.origin : '');
|
|
86
|
+
// 构造最终的 URL,如果 baseURL 为空则直接使用传入的 url
|
|
87
|
+
let finalURL;
|
|
88
|
+
if (baseURL && !url.startsWith('http')) {
|
|
89
|
+
// 只有当 url 不是绝对 URL 时才拼接 baseURL
|
|
90
|
+
finalURL = new URL(url, baseURL).toString();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// 如果 url 是绝对 URL 或者 baseURL 为空,则直接使用 url
|
|
94
|
+
finalURL = url;
|
|
95
|
+
}
|
|
86
96
|
return instance({
|
|
87
97
|
...options,
|
|
88
98
|
url: finalURL,
|