@flyto/request 1.1.0 → 1.1.2
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/core/manager.d.ts +5 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +16 -4
- package/dist/index.esm.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/core/manager.d.ts
CHANGED
|
@@ -13,8 +13,12 @@ export declare function createRequestManager(): {
|
|
|
13
13
|
/**
|
|
14
14
|
* 获取一个请求实例客户端
|
|
15
15
|
* @param key 实例名
|
|
16
|
+
* @param type 获取类型 request: 仅获取请求实例 client: 获取请求实例客户端
|
|
16
17
|
*/
|
|
17
|
-
get(key?: string): {
|
|
18
|
+
get(key?: string, type?: 'request' | 'client'): {
|
|
19
|
+
<D = any, T = import("../types").HttpResponse<D>>(url: string, config?: import("../types").StandardRequestConfig<unknown>): Promise<T>;
|
|
20
|
+
<D_1 = any, T_1 = import("../types").HttpResponse<D_1>>(config: import("../types").StandardRequestConfig<unknown>): Promise<T_1>;
|
|
21
|
+
} | {
|
|
18
22
|
request: {
|
|
19
23
|
<D = any, T = import("../types").HttpResponse<D>>(url: string, config?: import("../types").StandardRequestConfig<unknown>): Promise<T>;
|
|
20
24
|
<D_1 = any, T_1 = import("../types").HttpResponse<D_1>>(config: import("../types").StandardRequestConfig<unknown>): Promise<T_1>;
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("qs"),t=require("axios");function r(r){let o;const n=()=>{o=setTimeout(()=>{r.toggleWarpLoading?.(!1)},10)},s=t.create({...r.axiosConfig,timeout:6e4});return s.interceptors.request.use(async t=>{if(Object.assign(t,{openLoading:t.openLoading??!0,method:t.method??"POST"}),r.beforeRequest)try{const e=await(r.beforeRequest?.(t));if(!1===e.success)return Promise.reject({message:e.message||"请求被拦截"})}catch(e){return Promise.reject(e)}t.openLoading&&(clearTimeout(o),r.toggleWarpLoading?.(!0,t.tip));const n=t.headers?.["Content-Type"]||"";return"get"===t.method||"GET"===t.method?t.params=t.data:n.includes("application/x-www-form-urlencoded")&&(t.notQsContentType||(t.data=e.stringify(t.data))),t}),s.interceptors.response.use(async e=>{if(n(),r.afterResponse)try{await(r.afterResponse?.(e))}catch(e){console.warn("[afterResponse 执行异常]",e)}if(!e.config.ignoreStandardHandler){const{config:t}=e,{ignoreErrorMsg:o=!1,ignoreSuccessMsg:n=!0}=t,s=e.data,i=s.Message?String(s.Message):"";if(0===s.Code&&s.Success)n||r.notify?.success?.(i||"成功");else if(!o)throw r.notify?.error?.(i||"错误,请联系相关人员!","normal"),s}return e},e=>{n();let t="";return t="string"==typeof e?e:"object"==typeof e?JSON.stringify(e):String(e),r.notify?.error?.(t,"network"),e}),s}
|
|
1
|
+
"use strict";var e=require("qs"),t=require("axios");function r(r){const o=function(r){let o;const n=()=>{o=setTimeout(()=>{r.toggleWarpLoading?.(!1)},10)},s=t.create({...r.axiosConfig,timeout:6e4});return s.interceptors.request.use(async t=>{if(Object.assign(t,{openLoading:t.openLoading??!0,method:t.method??"POST"}),r.beforeRequest)try{const e=await(r.beforeRequest?.(t));if(!1===e.success)return Promise.reject({message:e.message||"请求被拦截"})}catch(e){return Promise.reject(e)}t.openLoading&&(clearTimeout(o),r.toggleWarpLoading?.(!0,t.tip));const n=t.headers?.["Content-Type"]||"";return"get"===t.method||"GET"===t.method?t.params=t.data:n.includes("application/x-www-form-urlencoded")&&(t.notQsContentType||(t.data=e.stringify(t.data))),t}),s.interceptors.response.use(async e=>{if(n(),r.afterResponse)try{await(r.afterResponse?.(e))}catch(e){console.warn("[afterResponse 执行异常]",e)}if(!e.config.ignoreStandardHandler){const{config:t}=e,{ignoreErrorMsg:o=!1,ignoreSuccessMsg:n=!0}=t,s=e.data,i=s.Message?String(s.Message):"";if(0===s.Code&&s.Success)n||r.notify?.success?.(i||"成功");else if(!o)throw r.notify?.error?.(i||"错误,请联系相关人员!","normal"),s}return e},e=>{n();let t="";return t="string"==typeof e?e:"object"==typeof e?JSON.stringify(e):String(e),r.notify?.error?.(t,"network"),e}),s}(r);return{request:async function(e,t){if(!o)throw new Error("请先注册请求函数");const r="string"==typeof e?{method:"post",...t,url:e}:e,{data:n}=await o(r);return n},axiosIns:o}}function o(){const e="default",t=new Map;return{register(o=e,n={axiosConfig:{}}){if(t.has(o))throw new Error(`"${o}" 已经存在请求实例,请勿重复注册`);t.set(o,r(n))},get(r=e,o="request"){const n=t.get(r);if(!n)throw new Error(`"${r}" 不存在任何请求实例`);return"request"===o?n.request:n},remove(r=e){t.delete(r)},reset(){t.clear()}}}const n=o(),s=n.register,i=n.get;exports.createRequestClient=r,exports.createRequestManager=o,exports.defineConfig=function(e){return"function"==typeof e?e():e},exports.getRequest=i,exports.register=s;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
import { createRequestManager } from './core/manager';
|
|
2
|
+
export { defineConfig } from './core/config';
|
|
3
|
+
export { createRequestClient } from './core/axios';
|
|
4
|
+
export { createRequestManager };
|
|
5
|
+
export declare const register: (key?: string, options?: import("./types").RequestOptions) => void;
|
|
6
|
+
export declare const getRequest: (key?: string, type?: "request" | "client") => {
|
|
7
|
+
<D = any, T = import("./types").HttpResponse<D>>(url: string, config?: import("./types").StandardRequestConfig<unknown>): Promise<T>;
|
|
8
|
+
<D_1 = any, T_1 = import("./types").HttpResponse<D_1>>(config: import("./types").StandardRequestConfig<unknown>): Promise<T_1>;
|
|
9
|
+
} | {
|
|
10
|
+
request: {
|
|
11
|
+
<D = any, T = import("./types").HttpResponse<D>>(url: string, config?: import("./types").StandardRequestConfig<unknown>): Promise<T>;
|
|
12
|
+
<D_1 = any, T_1 = import("./types").HttpResponse<D_1>>(config: import("./types").StandardRequestConfig<unknown>): Promise<T_1>;
|
|
13
|
+
};
|
|
14
|
+
axiosIns: import("axios").AxiosInstance;
|
|
15
|
+
};
|
|
16
|
+
export { RequestOptions } from './types';
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"qs";import t from"axios";function r(r){let o;const n=()=>{o=setTimeout(()=>{r.toggleWarpLoading?.(!1)},10)},s=t.create({...r.axiosConfig,timeout:6e4});return s.interceptors.request.use(async t=>{if(Object.assign(t,{openLoading:t.openLoading??!0,method:t.method??"POST"}),r.beforeRequest)try{const e=await(r.beforeRequest?.(t));if(!1===e.success)return Promise.reject({message:e.message||"请求被拦截"})}catch(e){return Promise.reject(e)}t.openLoading&&(clearTimeout(o),r.toggleWarpLoading?.(!0,t.tip));const n=t.headers?.["Content-Type"]||"";return"get"===t.method||"GET"===t.method?t.params=t.data:n.includes("application/x-www-form-urlencoded")&&(t.notQsContentType||(t.data=e.stringify(t.data))),t}),s.interceptors.response.use(async e=>{if(n(),r.afterResponse)try{await(r.afterResponse?.(e))}catch(e){console.warn("[afterResponse 执行异常]",e)}if(!e.config.ignoreStandardHandler){const{config:t}=e,{ignoreErrorMsg:o=!1,ignoreSuccessMsg:n=!0}=t,s=e.data,i=s.Message?String(s.Message):"";if(0===s.Code&&s.Success)n||r.notify?.success?.(i||"成功");else if(!o)throw r.notify?.error?.(i||"错误,请联系相关人员!","normal"),s}return e},e=>{n();let t="";return t="string"==typeof e?e:"object"==typeof e?JSON.stringify(e):String(e),r.notify?.error?.(t,"network"),e}),s}
|
|
1
|
+
import e from"qs";import t from"axios";function r(r){const o=function(r){let o;const n=()=>{o=setTimeout(()=>{r.toggleWarpLoading?.(!1)},10)},s=t.create({...r.axiosConfig,timeout:6e4});return s.interceptors.request.use(async t=>{if(Object.assign(t,{openLoading:t.openLoading??!0,method:t.method??"POST"}),r.beforeRequest)try{const e=await(r.beforeRequest?.(t));if(!1===e.success)return Promise.reject({message:e.message||"请求被拦截"})}catch(e){return Promise.reject(e)}t.openLoading&&(clearTimeout(o),r.toggleWarpLoading?.(!0,t.tip));const n=t.headers?.["Content-Type"]||"";return"get"===t.method||"GET"===t.method?t.params=t.data:n.includes("application/x-www-form-urlencoded")&&(t.notQsContentType||(t.data=e.stringify(t.data))),t}),s.interceptors.response.use(async e=>{if(n(),r.afterResponse)try{await(r.afterResponse?.(e))}catch(e){console.warn("[afterResponse 执行异常]",e)}if(!e.config.ignoreStandardHandler){const{config:t}=e,{ignoreErrorMsg:o=!1,ignoreSuccessMsg:n=!0}=t,s=e.data,i=s.Message?String(s.Message):"";if(0===s.Code&&s.Success)n||r.notify?.success?.(i||"成功");else if(!o)throw r.notify?.error?.(i||"错误,请联系相关人员!","normal"),s}return e},e=>{n();let t="";return t="string"==typeof e?e:"object"==typeof e?JSON.stringify(e):String(e),r.notify?.error?.(t,"network"),e}),s}(r);return{request:async function(e,t){if(!o)throw new Error("请先注册请求函数");const r="string"==typeof e?{method:"post",...t,url:e}:e,{data:n}=await o(r);return n},axiosIns:o}}function o(){const e="default",t=new Map;return{register(o=e,n={axiosConfig:{}}){if(t.has(o))throw new Error(`"${o}" 已经存在请求实例,请勿重复注册`);t.set(o,r(n))},get(r=e,o="request"){const n=t.get(r);if(!n)throw new Error(`"${r}" 不存在任何请求实例`);return"request"===o?n.request:n},remove(r=e){t.delete(r)},reset(){t.clear()}}}function n(e){return"function"==typeof e?e():e}const s=o(),i=s.register,a=s.get;export{r as createRequestClient,o as createRequestManager,n as defineConfig,a as getRequest,i as register};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("qs"),require("axios")):"function"==typeof define&&define.amd?define(["exports","qs","axios"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).fRequest={},e.qs,e.axios)}(this,function(e,t,
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("qs"),require("axios")):"function"==typeof define&&define.amd?define(["exports","qs","axios"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).fRequest={},e.qs,e.axios)}(this,function(e,t,r){"use strict";function n(e){const n=function(e){let n;const o=()=>{n=setTimeout(()=>{e.toggleWarpLoading?.(!1)},10)},s=r.create({...e.axiosConfig,timeout:6e4});return s.interceptors.request.use(async r=>{if(Object.assign(r,{openLoading:r.openLoading??!0,method:r.method??"POST"}),e.beforeRequest)try{const t=await(e.beforeRequest?.(r));if(!1===t.success)return Promise.reject({message:t.message||"请求被拦截"})}catch(e){return Promise.reject(e)}r.openLoading&&(clearTimeout(n),e.toggleWarpLoading?.(!0,r.tip));const o=r.headers?.["Content-Type"]||"";return"get"===r.method||"GET"===r.method?r.params=r.data:o.includes("application/x-www-form-urlencoded")&&(r.notQsContentType||(r.data=t.stringify(r.data))),r}),s.interceptors.response.use(async t=>{if(o(),e.afterResponse)try{await(e.afterResponse?.(t))}catch(e){console.warn("[afterResponse 执行异常]",e)}if(!t.config.ignoreStandardHandler){const{config:r}=t,{ignoreErrorMsg:n=!1,ignoreSuccessMsg:o=!0}=r,s=t.data,i=s.Message?String(s.Message):"";if(0===s.Code&&s.Success)o||e.notify?.success?.(i||"成功");else if(!n)throw e.notify?.error?.(i||"错误,请联系相关人员!","normal"),s}return t},t=>{o();let r="";return r="string"==typeof t?t:"object"==typeof t?JSON.stringify(t):String(t),e.notify?.error?.(r,"network"),t}),s}(e);return{request:async function(e,t){if(!n)throw new Error("请先注册请求函数");const r="string"==typeof e?{method:"post",...t,url:e}:e,{data:o}=await n(r);return o},axiosIns:n}}function o(){const e="default",t=new Map;return{register(r=e,o={axiosConfig:{}}){if(t.has(r))throw new Error(`"${r}" 已经存在请求实例,请勿重复注册`);t.set(r,n(o))},get(r=e,n="request"){const o=t.get(r);if(!o)throw new Error(`"${r}" 不存在任何请求实例`);return"request"===n?o.request:o},remove(r=e){t.delete(r)},reset(){t.clear()}}}const s=o(),i=s.register,a=s.get;e.createRequestClient=n,e.createRequestManager=o,e.defineConfig=function(e){return"function"==typeof e?e():e},e.getRequest=a,e.register=i});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/core/axios.ts","../src/core/manager.ts","../src/core/config.ts"],"sourcesContent":["import qs from 'qs'\r\nimport axios, { type AxiosResponse } from 'axios'\r\nimport { HttpResponse, RequestOptions, StandardRequestConfig } from '../types'\r\n\r\n/** 默认 成功Code值 */\r\nconst SUCCESS_CODE = 0\r\n/** 需要通过qs转换的Content-Type */\r\nconst CONTENT_TYPE_TANSFORM = 'application/x-www-form-urlencoded'\r\n/** 请求超时时间 1分钟 */\r\nconst REQUEST_TIMEOUT = 1000 * 60 * 1\r\n\r\n/**\r\n * 创建一个已经对拦截器做过逻辑处理的Axios实例\r\n * @param options 配置项\r\n * @returns axios实例\r\n */\r\nexport function createAxiosIns(options: RequestOptions) {\r\n // #region loading遮罩层处理\r\n let tiemerId: NodeJS.Timeout\r\n /**\r\n * 关闭遮罩(会延时50ms)\r\n */\r\n const closeLoading = () => {\r\n // 遮罩延迟50毫秒关闭\r\n // 如果有两个连续的请求,使其动画不中断,体验感较好\r\n tiemerId = setTimeout(() => {\r\n options.toggleWarpLoading?.(false)\r\n }, 10)\r\n }\r\n // #endregion\r\n\r\n const axiosIns = axios.create({\r\n ...options.axiosConfig,\r\n timeout: REQUEST_TIMEOUT,\r\n })\r\n // 在发送请求之前做些什么\r\n axiosIns.interceptors.request.use(async (config: StandardRequestConfig) => {\r\n // 合并默认值\r\n Object.assign(config, {\r\n openLoading: config.openLoading ?? true,\r\n method: config.method ?? 'POST',\r\n })\r\n\r\n // 自定义在请求之前做些什么\r\n if (options.beforeRequest) {\r\n try {\r\n const result = await options.beforeRequest?.(config)\r\n if (result.success === false) {\r\n // 取消请求\r\n return Promise.reject({ message: result.message || '请求被拦截' })\r\n }\r\n } catch (err) {\r\n return Promise.reject(err)\r\n }\r\n }\r\n\r\n // 打开遮罩\r\n if (config.openLoading) {\r\n // 如果下一个请求马上进来了,则清除掉计时器\r\n clearTimeout(tiemerId)\r\n options.toggleWarpLoading?.(true, config.tip)\r\n }\r\n\r\n const contentType: string = config.headers?.['Content-Type'] || ''\r\n if (config.method === 'get' || config.method === 'GET') {\r\n config.params = config.data\r\n // 非默认的content-type,不进行qs的转换\r\n } else if (contentType.includes(CONTENT_TYPE_TANSFORM)) {\r\n // qs转格式\r\n if (!config.notQsContentType) {\r\n config.data = qs.stringify(config.data)\r\n }\r\n }\r\n\r\n return config\r\n })\r\n // 在请求成功后做些什么\r\n axiosIns.interceptors.response.use(\r\n async (\r\n response: AxiosResponse & {\r\n config: StandardRequestConfig\r\n },\r\n ) => {\r\n closeLoading()\r\n\r\n // 自定义在请求之后做些什么\r\n if (options.afterResponse) {\r\n try {\r\n await options.afterResponse?.(response)\r\n } catch (err) {\r\n console.warn('[afterResponse 执行异常]', err)\r\n }\r\n }\r\n\r\n // 使用标准返回结构体(HttpResponse)的通用错误处理\r\n // 非HttpResponse,请自行处理\r\n if (!response.config.ignoreStandardHandler) {\r\n const { config } = response\r\n const { ignoreErrorMsg = false, ignoreSuccessMsg = true } = config\r\n\r\n const data = response.data as HttpResponse\r\n const message = data.Message ? String(data.Message) : ''\r\n\r\n if (data.Code === SUCCESS_CODE && data.Success) {\r\n if (!ignoreSuccessMsg) {\r\n options.notify?.success?.(message || '成功')\r\n }\r\n } else if (!ignoreErrorMsg) {\r\n options.notify?.error?.(message || '错误,请联系相关人员!', 'normal')\r\n throw data\r\n }\r\n }\r\n\r\n return response\r\n },\r\n err => {\r\n // 只要服务器返回的code不是200,都会进入该函数,必须抛出实际错误\r\n closeLoading()\r\n\r\n let errStr = ''\r\n if (typeof err === 'string') {\r\n errStr = err\r\n } else if (typeof err === 'object') {\r\n errStr = JSON.stringify(err)\r\n } else {\r\n errStr = String(err)\r\n }\r\n\r\n options.notify?.error?.(errStr, 'network')\r\n return err\r\n },\r\n )\r\n\r\n return axiosIns\r\n}\r\n\r\n/**\r\n * 创建一个请求客户端\r\n * @param options 配置项\r\n */\r\nexport function createRequestClient(options: RequestOptions) {\r\n // axios实例 此实例针对拦截器进行了一些逻辑处理\r\n const axiosIns = createAxiosIns(options)\r\n\r\n /**\r\n * 标准请求方法\r\n * @template D 返回的值中,data的类型(例如HttpResponse中的Data)\r\n * @template T 返回值的类型,默认为HttpResponse\r\n * @param url 请求地址\r\n * @param config 配置项\r\n */\r\n async function request<D = any, T = HttpResponse<D>>(\r\n url: string,\r\n config?: StandardRequestConfig,\r\n ): Promise<T>\r\n /**\r\n * 标准请求方法\r\n * @template D 返回的值中,data的类型(例如HttpResponse中的Data)\r\n * @template T 返回值的类型,默认为HttpResponse\r\n * @param config 配置项\r\n */\r\n async function request<D = any, T = HttpResponse<D>>(\r\n config: StandardRequestConfig,\r\n ): Promise<T>\r\n async function request<D = any, T = HttpResponse<D>>(\r\n urlOrConfig: string | StandardRequestConfig,\r\n config?: StandardRequestConfig,\r\n ): Promise<T> {\r\n if (!axiosIns) {\r\n throw new Error('请先注册请求函数')\r\n }\r\n\r\n const params: StandardRequestConfig =\r\n typeof urlOrConfig === 'string'\r\n ? {\r\n method: 'post',\r\n ...config,\r\n url: urlOrConfig,\r\n }\r\n : urlOrConfig\r\n\r\n const { data }: { data: T } = await axiosIns(params)\r\n\r\n return data\r\n }\r\n\r\n return {\r\n request,\r\n axiosIns,\r\n }\r\n}\r\n\r\nexport default null\r\n","import { RequestOptions } from '../types'\r\nimport { createRequestClient } from './axios'\r\n\r\n/**\r\n * 创建请求管理器\r\n * @returns 请求管理器对象\r\n */\r\nexport function createRequestManager() {\r\n const KEY_DEFAULT_VALUE = 'default'\r\n\r\n const instances = new Map<string, ReturnType<typeof createRequestClient>>()\r\n\r\n return {\r\n /**\r\n * 注册一个请求实例客户端\r\n * @param key 实例名\r\n * @param options 配置\r\n */\r\n register(\r\n key = KEY_DEFAULT_VALUE,\r\n options: RequestOptions = {\r\n axiosConfig: {},\r\n },\r\n ) {\r\n if (instances.has(key)) {\r\n throw new Error(`\"${key}\" 已经存在请求实例,请勿重复注册`)\r\n }\r\n instances.set(key, createRequestClient(options))\r\n },\r\n\r\n /**\r\n * 获取一个请求实例客户端\r\n * @param key 实例名\r\n */\r\n get(key = KEY_DEFAULT_VALUE) {\r\n const ins = instances.get(key)\r\n if (!ins) {\r\n throw new Error(`\"${key}\" 不存在任何请求实例`)\r\n }\r\n\r\n return ins\r\n },\r\n\r\n /**\r\n * 移除一个请求实例客户端\r\n * @param key 实例名\r\n */\r\n remove(key = KEY_DEFAULT_VALUE) {\r\n instances.delete(key)\r\n },\r\n\r\n /**\r\n * 移除当前管理器中的所有请求实例客户端\r\n */\r\n reset() {\r\n instances.clear()\r\n },\r\n }\r\n}\r\n","import { RequestOptions } from '../types'\r\n\r\n/**\r\n * 定义一个标准的Request配置\r\n * @param config 配置\r\n * @returns\r\n */\r\nexport function defineConfig(config: RequestOptions | (() => RequestOptions)) {\r\n if (typeof config === 'function') {\r\n return config()\r\n }\r\n\r\n return config\r\n}\r\n\r\nexport default null\r\n"],"names":["createAxiosIns","options","tiemerId","closeLoading","setTimeout","toggleWarpLoading","axiosIns","axios","create","axiosConfig","timeout","interceptors","request","use","async","config","Object","assign","openLoading","method","beforeRequest","result","success","Promise","reject","message","err","clearTimeout","tip","contentType","headers","params","data","includes","notQsContentType","qs","stringify","response","afterResponse","console","warn","ignoreStandardHandler","ignoreErrorMsg","ignoreSuccessMsg","Message","String","Code","Success","notify","error","errStr","JSON","createRequestClient","urlOrConfig","Error","url","KEY_DEFAULT_VALUE","instances","Map","register","key","has","set","get","ins","remove","delete","reset","clear"],"mappings":"4SAgBM,SAAUA,EAAeC,GAE7B,IAAIC,EAIJ,MAAMC,EAAe,KAGnBD,EAAWE,WAAW,KACpBH,EAAQI,qBAAoB,IAC3B,KAICC,EAAWC,EAAMC,OAAO,IACzBP,EAAQQ,YACXC,QAxBoB,MA4HtB,OAjGAJ,EAASK,aAAaC,QAAQC,IAAIC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fmB,OAAOnB,GAGlBzB,EAAQ+C,QAAQC,QAAQC,EAAQ,WACzBxB,IAIJpB,CACT,CAMM,SAAU8C,EAAoBnD,GAElC,MAAMK,EAAWN,EAAeC,GA4ChC,MAAO,CACLW,QAvBFE,eACEuC,EACAtC,GAEA,IAAKT,EACH,MAAM,IAAIgD,MAAM,YAGlB,MAAMvB,EACmB,iBAAhBsB,EACH,CACElC,OAAQ,UACLJ,EACHwC,IAAKF,GAEPA,GAEArB,KAAEA,SAA4B1B,EAASyB,GAE7C,OAAOC,CACR,EAIC1B,WAEJ,8ECtLE,MAAMkD,EAAoB,UAEpBC,EAAY,IAAIC,IAEtB,MAAO,CAML,QAAAC,CACEC,EAAMJ,EACNvD,EAA0B,CACxBQ,YAAa,CAAE,IAGjB,GAAIgD,EAAUI,IAAID,GAChB,MAAM,IAAIN,MAAM,IAAIM,sBAEtBH,EAAUK,IAAIF,EAAKR,EAAoBnD,GACxC,EAMD,GAAA8D,CAAIH,EAAMJ,GACR,MAAMQ,EAAMP,EAAUM,IAAIH,GAC1B,IAAKI,EACH,MAAM,IAAIV,MAAM,IAAIM,gBAGtB,OAAOI,CACR,EAMD,MAAAC,CAAOL,EAAMJ,GACXC,EAAUS,OAAON,EAClB,EAKD,KAAAO,GACEV,EAAUW,OACX,EAEL,iBCnDM,SAAuBrD,GAC3B,MAAsB,mBAAXA,EACFA,IAGFA,CACT"}
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/core/axios.ts","../src/core/manager.ts","../src/index.ts","../src/core/config.ts"],"sourcesContent":["import qs from 'qs'\r\nimport axios, { type AxiosResponse } from 'axios'\r\nimport { HttpResponse, RequestOptions, StandardRequestConfig } from '../types'\r\n\r\n/** 默认 成功Code值 */\r\nconst SUCCESS_CODE = 0\r\n/** 需要通过qs转换的Content-Type */\r\nconst CONTENT_TYPE_TANSFORM = 'application/x-www-form-urlencoded'\r\n/** 请求超时时间 1分钟 */\r\nconst REQUEST_TIMEOUT = 1000 * 60 * 1\r\n\r\n/**\r\n * 创建一个已经对拦截器做过逻辑处理的Axios实例\r\n * @param options 配置项\r\n * @returns axios实例\r\n */\r\nexport function createAxiosIns(options: RequestOptions) {\r\n // #region loading遮罩层处理\r\n let tiemerId: NodeJS.Timeout\r\n /**\r\n * 关闭遮罩(会延时50ms)\r\n */\r\n const closeLoading = () => {\r\n // 遮罩延迟50毫秒关闭\r\n // 如果有两个连续的请求,使其动画不中断,体验感较好\r\n tiemerId = setTimeout(() => {\r\n options.toggleWarpLoading?.(false)\r\n }, 10)\r\n }\r\n // #endregion\r\n\r\n const axiosIns = axios.create({\r\n ...options.axiosConfig,\r\n timeout: REQUEST_TIMEOUT,\r\n })\r\n // 在发送请求之前做些什么\r\n axiosIns.interceptors.request.use(async (config: StandardRequestConfig) => {\r\n // 合并默认值\r\n Object.assign(config, {\r\n openLoading: config.openLoading ?? true,\r\n method: config.method ?? 'POST',\r\n })\r\n\r\n // 自定义在请求之前做些什么\r\n if (options.beforeRequest) {\r\n try {\r\n const result = await options.beforeRequest?.(config)\r\n if (result.success === false) {\r\n // 取消请求\r\n return Promise.reject({ message: result.message || '请求被拦截' })\r\n }\r\n } catch (err) {\r\n return Promise.reject(err)\r\n }\r\n }\r\n\r\n // 打开遮罩\r\n if (config.openLoading) {\r\n // 如果下一个请求马上进来了,则清除掉计时器\r\n clearTimeout(tiemerId)\r\n options.toggleWarpLoading?.(true, config.tip)\r\n }\r\n\r\n const contentType: string = config.headers?.['Content-Type'] || ''\r\n if (config.method === 'get' || config.method === 'GET') {\r\n config.params = config.data\r\n // 非默认的content-type,不进行qs的转换\r\n } else if (contentType.includes(CONTENT_TYPE_TANSFORM)) {\r\n // qs转格式\r\n if (!config.notQsContentType) {\r\n config.data = qs.stringify(config.data)\r\n }\r\n }\r\n\r\n return config\r\n })\r\n // 在请求成功后做些什么\r\n axiosIns.interceptors.response.use(\r\n async (\r\n response: AxiosResponse & {\r\n config: StandardRequestConfig\r\n },\r\n ) => {\r\n closeLoading()\r\n\r\n // 自定义在请求之后做些什么\r\n if (options.afterResponse) {\r\n try {\r\n await options.afterResponse?.(response)\r\n } catch (err) {\r\n console.warn('[afterResponse 执行异常]', err)\r\n }\r\n }\r\n\r\n // 使用标准返回结构体(HttpResponse)的通用错误处理\r\n // 非HttpResponse,请自行处理\r\n if (!response.config.ignoreStandardHandler) {\r\n const { config } = response\r\n const { ignoreErrorMsg = false, ignoreSuccessMsg = true } = config\r\n\r\n const data = response.data as HttpResponse\r\n const message = data.Message ? String(data.Message) : ''\r\n\r\n if (data.Code === SUCCESS_CODE && data.Success) {\r\n if (!ignoreSuccessMsg) {\r\n options.notify?.success?.(message || '成功')\r\n }\r\n } else if (!ignoreErrorMsg) {\r\n options.notify?.error?.(message || '错误,请联系相关人员!', 'normal')\r\n throw data\r\n }\r\n }\r\n\r\n return response\r\n },\r\n err => {\r\n // 只要服务器返回的code不是200,都会进入该函数,必须抛出实际错误\r\n closeLoading()\r\n\r\n let errStr = ''\r\n if (typeof err === 'string') {\r\n errStr = err\r\n } else if (typeof err === 'object') {\r\n errStr = JSON.stringify(err)\r\n } else {\r\n errStr = String(err)\r\n }\r\n\r\n options.notify?.error?.(errStr, 'network')\r\n return err\r\n },\r\n )\r\n\r\n return axiosIns\r\n}\r\n\r\n/**\r\n * 创建一个请求客户端\r\n * @param options 配置项\r\n */\r\nexport function createRequestClient(options: RequestOptions) {\r\n // axios实例 此实例针对拦截器进行了一些逻辑处理\r\n const axiosIns = createAxiosIns(options)\r\n\r\n /**\r\n * 标准请求方法\r\n * @template D 返回的值中,data的类型(例如HttpResponse中的Data)\r\n * @template T 返回值的类型,默认为HttpResponse\r\n * @param url 请求地址\r\n * @param config 配置项\r\n */\r\n async function request<D = any, T = HttpResponse<D>>(\r\n url: string,\r\n config?: StandardRequestConfig,\r\n ): Promise<T>\r\n /**\r\n * 标准请求方法\r\n * @template D 返回的值中,data的类型(例如HttpResponse中的Data)\r\n * @template T 返回值的类型,默认为HttpResponse\r\n * @param config 配置项\r\n */\r\n async function request<D = any, T = HttpResponse<D>>(\r\n config: StandardRequestConfig,\r\n ): Promise<T>\r\n async function request<D = any, T = HttpResponse<D>>(\r\n urlOrConfig: string | StandardRequestConfig,\r\n config?: StandardRequestConfig,\r\n ): Promise<T> {\r\n if (!axiosIns) {\r\n throw new Error('请先注册请求函数')\r\n }\r\n\r\n const params: StandardRequestConfig =\r\n typeof urlOrConfig === 'string'\r\n ? {\r\n method: 'post',\r\n ...config,\r\n url: urlOrConfig,\r\n }\r\n : urlOrConfig\r\n\r\n const { data }: { data: T } = await axiosIns(params)\r\n\r\n return data\r\n }\r\n\r\n return {\r\n request,\r\n axiosIns,\r\n }\r\n}\r\n\r\nexport default null\r\n","import { RequestOptions } from '../types'\r\nimport { createRequestClient } from './axios'\r\n\r\n/**\r\n * 创建请求管理器\r\n * @returns 请求管理器对象\r\n */\r\nexport function createRequestManager() {\r\n const KEY_DEFAULT_VALUE = 'default'\r\n\r\n const instances = new Map<string, ReturnType<typeof createRequestClient>>()\r\n\r\n return {\r\n /**\r\n * 注册一个请求实例客户端\r\n * @param key 实例名\r\n * @param options 配置\r\n */\r\n register(\r\n key = KEY_DEFAULT_VALUE,\r\n options: RequestOptions = {\r\n axiosConfig: {},\r\n },\r\n ) {\r\n if (instances.has(key)) {\r\n throw new Error(`\"${key}\" 已经存在请求实例,请勿重复注册`)\r\n }\r\n instances.set(key, createRequestClient(options))\r\n },\r\n\r\n /**\r\n * 获取一个请求实例客户端\r\n * @param key 实例名\r\n * @param type 获取类型 request: 仅获取请求实例 client: 获取请求实例客户端\r\n */\r\n get(key = KEY_DEFAULT_VALUE, type: 'request' | 'client' = 'request') {\r\n const ins = instances.get(key)\r\n if (!ins) {\r\n throw new Error(`\"${key}\" 不存在任何请求实例`)\r\n }\r\n\r\n return type === 'request' ? ins.request : ins\r\n },\r\n\r\n /**\r\n * 移除一个请求实例客户端\r\n * @param key 实例名\r\n */\r\n remove(key = KEY_DEFAULT_VALUE) {\r\n instances.delete(key)\r\n },\r\n\r\n /**\r\n * 移除当前管理器中的所有请求实例客户端\r\n */\r\n reset() {\r\n instances.clear()\r\n },\r\n }\r\n}\r\n","import { createRequestManager } from './core/manager'\r\n\r\nexport { defineConfig } from './core/config'\r\nexport { createRequestClient } from './core/axios'\r\nexport { createRequestManager }\r\n\r\n// 默认有一个单例的请求管理器,SPA/CSR模式下可以直接使用\r\nconst defaultRequestManager = createRequestManager()\r\nexport const register = defaultRequestManager.register\r\nexport const getRequest = defaultRequestManager.get\r\n\r\nexport { RequestOptions } from './types'\r\n","import { RequestOptions } from '../types'\r\n\r\n/**\r\n * 定义一个标准的Request配置\r\n * @param config 配置\r\n * @returns\r\n */\r\nexport function defineConfig(config: RequestOptions | (() => RequestOptions)) {\r\n if (typeof config === 'function') {\r\n return config()\r\n }\r\n\r\n return config\r\n}\r\n\r\nexport default null\r\n"],"names":["createRequestClient","options","axiosIns","tiemerId","closeLoading","setTimeout","toggleWarpLoading","axios","create","axiosConfig","timeout","interceptors","request","use","async","config","Object","assign","openLoading","method","beforeRequest","result","success","Promise","reject","message","err","clearTimeout","tip","contentType","headers","params","data","includes","notQsContentType","qs","stringify","response","afterResponse","console","warn","ignoreStandardHandler","ignoreErrorMsg","ignoreSuccessMsg","Message","String","Code","Success","notify","error","errStr","JSON","createAxiosIns","urlOrConfig","Error","url","createRequestManager","KEY_DEFAULT_VALUE","instances","Map","register","key","has","set","get","type","ins","remove","delete","reset","clear","defaultRequestManager","getRequest"],"mappings":"4SA4IM,SAAUA,EAAoBC,GAElC,MAAMC,EA9HF,SAAyBD,GAE7B,IAAIE,EAIJ,MAAMC,EAAe,KAGnBD,EAAWE,WAAW,KACpBJ,EAAQK,qBAAoB,IAC3B,KAICJ,EAAWK,EAAMC,OAAO,IACzBP,EAAQQ,YACXC,QAxBoB,MA4HtB,OAjGAR,EAASS,aAAaC,QAAQC,IAAIC,MAAOC,IAQvC,GANAC,OAAOC,OAAOF,EAAQ,CACpBG,YAAaH,EAAOG,cAAe,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