@aidc/utils
Advanced tools
Comparing version 0.5.19-beta.3 to 0.5.19-beta.5
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
@@ -36,2 +37,28 @@ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
/** | ||
* @name encodeFormData | ||
* @description 将数据转换为 form-data | ||
*/ | ||
function encodeFormData(data) { | ||
var params = new URLSearchParams(); | ||
var addToParams = function addToParams(key, value) { | ||
// 过滤 undefined, null | ||
if ([undefined, null].includes(value)) return; | ||
// 如果值是对象或数组,将其转换为 JSON 字符串 | ||
if (_typeof(value) === 'object') { | ||
params.append(key, JSON.stringify(value)); | ||
} else { | ||
// 对于其他数据类型,直接添加到参数中 | ||
params.append(key, value); | ||
} | ||
}; | ||
for (var _key in data) { | ||
if (data.hasOwnProperty(_key)) { | ||
addToParams(_key, data[_key]); | ||
} | ||
} | ||
return params.toString(); | ||
} | ||
// 合并默认配置和请求特定配置 | ||
@@ -79,13 +106,14 @@ export function mergeRequestOptions(defaultOptions, requestOptions) { | ||
_sendRequestWithFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(options) { | ||
var url, _options$baseURL, baseURL, params, data, headers, timeout, withCredentials, _options$method, method, requstUrl, requestOptions, controller, timeoutId, response, result; | ||
var url, _options$baseURL, baseURL, params, data, _options$headers, headers, timeout, withCredentials, _options$method, method, contentType, requstUrl, requestOptions, controller, timeoutId, response, result; | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
url = options.url, _options$baseURL = options.baseURL, baseURL = _options$baseURL === void 0 ? '' : _options$baseURL, params = options.params, data = options.data, headers = options.headers, timeout = options.timeout, withCredentials = options.withCredentials, _options$method = options.method, method = _options$method === void 0 ? 'GET' : _options$method; | ||
url = options.url, _options$baseURL = options.baseURL, baseURL = _options$baseURL === void 0 ? '' : _options$baseURL, params = options.params, data = options.data, _options$headers = options.headers, headers = _options$headers === void 0 ? {} : _options$headers, timeout = options.timeout, withCredentials = options.withCredentials, _options$method = options.method, method = _options$method === void 0 ? 'GET' : _options$method; | ||
contentType = headers['Content-Type']; | ||
if (url) { | ||
_context3.next = 3; | ||
_context3.next = 4; | ||
break; | ||
} | ||
throw new RequestError('url is required', 'url_required'); | ||
case 3: | ||
case 4: | ||
requstUrl = appendSearchParams("".concat(baseURL || '').concat(url), params); | ||
@@ -100,3 +128,8 @@ requestOptions = { | ||
if (data && ['POST', 'PUT', 'PATCH'].includes(method.toUpperCase())) { | ||
requestOptions.body = JSON.stringify(data); | ||
// 针对 Content-Type 为 `application/x-www-form-urlencoded` 的请求,将数据转换为 form-data | ||
if (contentType === 'application/x-www-form-urlencoded') { | ||
requestOptions.body = encodeFormData(data); | ||
} else { | ||
requestOptions.body = JSON.stringify(data); | ||
} | ||
} | ||
@@ -110,35 +143,35 @@ controller = new AbortController(); | ||
} | ||
_context3.prev = 10; | ||
_context3.next = 13; | ||
_context3.prev = 11; | ||
_context3.next = 14; | ||
return fetch(requstUrl, requestOptions); | ||
case 13: | ||
case 14: | ||
response = _context3.sent; | ||
if (timeoutId) clearTimeout(timeoutId); | ||
if (response.ok) { | ||
_context3.next = 17; | ||
_context3.next = 18; | ||
break; | ||
} | ||
throw new RequestError("HTTP error: ".concat(response.statusText), response.status); | ||
case 17: | ||
_context3.next = 19; | ||
case 18: | ||
_context3.next = 20; | ||
return response.json(); | ||
case 19: | ||
case 20: | ||
result = _context3.sent; | ||
return _context3.abrupt("return", result || {}); | ||
case 23: | ||
_context3.prev = 23; | ||
_context3.t0 = _context3["catch"](10); | ||
case 24: | ||
_context3.prev = 24; | ||
_context3.t0 = _context3["catch"](11); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
if (!(_context3.t0.name === 'AbortError')) { | ||
_context3.next = 28; | ||
_context3.next = 29; | ||
break; | ||
} | ||
throw new RequestError('Request timed out', 'timeout'); | ||
case 28: | ||
case 29: | ||
throw _context3.t0; | ||
case 29: | ||
case 30: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}, _callee3, null, [[10, 23]]); | ||
}, _callee3, null, [[11, 24]]); | ||
})); | ||
@@ -145,0 +178,0 @@ return _sendRequestWithFetch.apply(this, arguments); |
{ | ||
"name": "@aidc/utils", | ||
"version": "0.5.19-beta.3", | ||
"version": "0.5.19-beta.5", | ||
"module": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
31702
715