@aidc/utils
Advanced tools
Comparing version 0.3.22 to 0.3.23
@@ -7,1 +7,2 @@ export declare function getClassName(object: any): string; | ||
export declare function getSearchParams(key: string): string | null | undefined; | ||
export declare function appendSearchParams(url: string, params: any): string; |
@@ -0,1 +1,3 @@ | ||
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; | ||
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper"; | ||
import { debug } from "../debug"; | ||
@@ -36,2 +38,22 @@ import { isWindow } from "../env"; | ||
} | ||
} | ||
// 给 url 添加 search 参数 | ||
export function appendSearchParams(url, params) { | ||
var u = new URL(url); | ||
var _iterator = _createForOfIteratorHelper(new URLSearchParams(params)), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var _step$value = _slicedToArray(_step.value, 2), | ||
key = _step$value[0], | ||
value = _step$value[1]; | ||
u.searchParams.set(key, value); | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
} | ||
return u.toString(); | ||
} |
@@ -12,2 +12,3 @@ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; | ||
import axios from 'axios'; | ||
import { appendSearchParams } from "../general"; | ||
export var RequestError = /*#__PURE__*/function (_Error) { | ||
@@ -47,4 +48,6 @@ _inherits(RequestError, _Error); | ||
timeout, | ||
response, | ||
res, | ||
_ref, | ||
_res, | ||
_response, | ||
success, | ||
@@ -60,3 +63,32 @@ message, | ||
_options$host = options.host, host = _options$host === void 0 ? '' : _options$host, _options$method = options.method, method = _options$method === void 0 ? 'GET' : _options$method, params = options.params, data = options.data, _options$headers = options.headers, headers = _options$headers === void 0 ? {} : _options$headers, _options$timeout = options.timeout, timeout = _options$timeout === void 0 ? 10000 : _options$timeout; | ||
_context.next = 4; | ||
if (!(typeof fetch === 'function')) { | ||
_context.next = 14; | ||
break; | ||
} | ||
_context.next = 5; | ||
return fetch(method === 'GET' ? appendSearchParams(host + url, params) : host + url, { | ||
method: method, | ||
body: method === 'POST' ? data : undefined, | ||
credentials: 'include', | ||
headers: _objectSpread({ | ||
'content-type': 'application/json' | ||
}, headers) | ||
}); | ||
case 5: | ||
res = _context.sent; | ||
_context.next = 8; | ||
return res.json(); | ||
case 8: | ||
_context.t0 = _context.sent; | ||
if (_context.t0) { | ||
_context.next = 11; | ||
break; | ||
} | ||
_context.t0 = {}; | ||
case 11: | ||
response = _context.t0; | ||
_context.next = 18; | ||
break; | ||
case 14: | ||
_context.next = 16; | ||
return axios({ | ||
@@ -73,14 +105,16 @@ url: host + url, | ||
}); | ||
case 4: | ||
res = _context.sent; | ||
case 16: | ||
_res = _context.sent; | ||
response = _res.data || {}; | ||
case 18: | ||
// 统一接口请求返回规范,需要让服务端遵循 | ||
_ref = res.data || {}, success = _ref.success, message = _ref.message, code = _ref.code, responseData = _ref.data; | ||
_response = response, success = _response.success, message = _response.message, code = _response.code, responseData = _response.data; | ||
if (success) { | ||
_context.next = 8; | ||
_context.next = 21; | ||
break; | ||
} | ||
throw new RequestError(message, code); | ||
case 8: | ||
case 21: | ||
return _context.abrupt("return", responseData); | ||
case 9: | ||
case 22: | ||
case "end": | ||
@@ -87,0 +121,0 @@ return _context.stop(); |
{ | ||
"name": "@aidc/utils", | ||
"version": "0.3.22", | ||
"version": "0.3.23", | ||
"module": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
22290
556
2