@aidc/utils
Advanced tools
Comparing version 0.5.20 to 0.5.21-beta.0
@@ -21,9 +21,6 @@ import { Method, ResponseType } from 'axios'; | ||
responseType?: ResponseType | any; | ||
/** | ||
* 超时时间 | ||
* @default 10000 | ||
*/ | ||
responseHasSuccess?: boolean; | ||
timeout?: number; | ||
} | ||
type DefaultRequestOptions = Pick<RequestOptions, 'baseURL' | 'host' | 'withCredentials' | 'headers' | 'method' | 'responseType' | 'timeout'>; | ||
export type DefaultRequestOptions = Pick<RequestOptions, 'baseURL' | 'host' | 'withCredentials' | 'headers' | 'method' | 'responseType' | 'responseHasSuccess' | 'timeout'>; | ||
export interface RequestResponse<Data = any> { | ||
@@ -44,10 +41,10 @@ success?: boolean; | ||
* @name createRequest | ||
* @description 默认的请求参数配置`defaultOptions`,将优先使用`baseURL`,向后兼容`host` | ||
* @description 默认的请求参数配置 defaultOptions,将优先使用 baseURL,向后兼容 host | ||
*/ | ||
export declare function createRequest(defaultOptions?: DefaultRequestOptions): <R extends RequestResponse<any>, P = any>(url: string | RequestOptions<P>, options?: Omit<RequestOptions<P>, "url"> | undefined) => Promise<R["data"]>; | ||
export declare function createRequest(defaultOptions?: DefaultRequestOptions): <R extends RequestResponse<any> = any, P = any>(url: string | RequestOptions<P>, options?: Omit<RequestOptions<P>, "url"> | undefined) => Promise<R["data"]>; | ||
/** | ||
* @name request | ||
* @description 请求入参`options`配置,将优先使用`baseURL`,向后兼容`host` | ||
* @description 请求入参 options 配置,将优先使用 baseURL,向后兼容 host | ||
*/ | ||
export declare const request: <R extends RequestResponse<any>, P = any>(url: string | RequestOptions<P>, options?: Omit<RequestOptions<P>, "url"> | undefined) => Promise<R["data"]>; | ||
export declare const request: <R extends RequestResponse<any> = any, P = any>(url: string | RequestOptions<P>, options?: Omit<RequestOptions<P>, "url"> | undefined) => Promise<R["data"]>; | ||
export {}; |
@@ -67,11 +67,11 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
headers: _objectSpread(_objectSpread({}, defaultOptions.headers), requestOptions.headers || {}), | ||
// TIP: 在 mergeOptions 时,向后兼容 `host` props,但优先使用 `baseURL` | ||
// TIP: 在合并配置项时,向后兼容 host 属性,但优先使用 baseURL | ||
baseURL: defaultOptions.baseURL || requestOptions.baseURL || defaultOptions.host || requestOptions.host | ||
}); | ||
} | ||
function sendRequestWithAxios(_x) { | ||
return _sendRequestWithAxios.apply(this, arguments); | ||
function requestWithAxios(_x) { | ||
return _requestWithAxios.apply(this, arguments); | ||
} | ||
function _sendRequestWithAxios() { | ||
_sendRequestWithAxios = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(options) { | ||
function _requestWithAxios() { | ||
_requestWithAxios = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(options) { | ||
var res; | ||
@@ -83,3 +83,3 @@ return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
return axios(_objectSpread(_objectSpread({}, options), {}, { | ||
// FIXME: 需要反推 `easyapi` 开发者遵循 `HTTP` 规范设置 `responseType` | ||
// FIXME: 需要反推 easyapi 开发者遵循 HTTP 规范设置 responseType | ||
responseType: options.responseType === '*/*' ? 'json' : options.responseType | ||
@@ -96,13 +96,13 @@ })); | ||
})); | ||
return _sendRequestWithAxios.apply(this, arguments); | ||
return _requestWithAxios.apply(this, arguments); | ||
} | ||
function sendRequestWithFetch(_x2) { | ||
return _sendRequestWithFetch.apply(this, arguments); | ||
function requestWithFetch(_x2) { | ||
return _requestWithFetch.apply(this, arguments); | ||
} | ||
/** | ||
* @name createRequest | ||
* @description 默认的请求参数配置`defaultOptions`,将优先使用`baseURL`,向后兼容`host` | ||
* @description 默认的请求参数配置 defaultOptions,将优先使用 baseURL,向后兼容 host | ||
*/ | ||
function _sendRequestWithFetch() { | ||
_sendRequestWithFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(options) { | ||
function _requestWithFetch() { | ||
_requestWithFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(options) { | ||
var url, _options$baseURL, baseURL, params, data, _options$headers, headers, timeout, withCredentials, _options$method, method, contentType, requstUrl, requestOptions, controller, timeoutId, response, result; | ||
@@ -129,3 +129,3 @@ return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
if (data && ['POST', 'PUT', 'PATCH'].includes(method.toUpperCase())) { | ||
// 针对 Content-Type 为 `application/x-www-form-urlencoded` 的请求,将数据转换为 form-data | ||
// 针对 Content-Type 为 application/x-www-form-urlencoded 的请求,将数据转换为 form-data | ||
if (contentType === 'application/x-www-form-urlencoded') { | ||
@@ -178,3 +178,3 @@ requestOptions.body = encodeFormData(data); | ||
})); | ||
return _sendRequestWithFetch.apply(this, arguments); | ||
return _requestWithFetch.apply(this, arguments); | ||
} | ||
@@ -187,2 +187,3 @@ export function createRequest(defaultOptions) { | ||
withCredentials: true, | ||
responseHasSuccess: true, | ||
timeout: 10000, | ||
@@ -193,5 +194,5 @@ headers: { | ||
}, defaultOptions || {}); | ||
var requestSender = typeof fetch === 'function' ? sendRequestWithFetch : sendRequestWithAxios; | ||
var req = typeof fetch === 'function' ? requestWithFetch : requestWithAxios; | ||
return /*#__PURE__*/function () { | ||
var _sendRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url, options) { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url, options) { | ||
var fullOptions, res; | ||
@@ -209,6 +210,6 @@ return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
_context.next = 3; | ||
return requestSender(fullOptions); | ||
return req(fullOptions); | ||
case 3: | ||
res = _context.sent; | ||
if (res.success) { | ||
if (!(fullOptions.responseHasSuccess && !res.success)) { | ||
_context.next = 6; | ||
@@ -226,6 +227,5 @@ break; | ||
})); | ||
function sendRequest(_x3, _x4) { | ||
return _sendRequest.apply(this, arguments); | ||
} | ||
return sendRequest; | ||
return function (_x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
@@ -236,4 +236,4 @@ } | ||
* @name request | ||
* @description 请求入参`options`配置,将优先使用`baseURL`,向后兼容`host` | ||
* @description 请求入参 options 配置,将优先使用 baseURL,向后兼容 host | ||
*/ | ||
export var request = createRequest(); |
{ | ||
"name": "@aidc/utils", | ||
"version": "0.5.20", | ||
"version": "0.5.21-beta.0", | ||
"module": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
31657
712