{"version":3,"file":"fastAxios.mjs","sources":["../../../../src/axios/fastAxios.ts"],"sourcesContent":["import { AxiosError } from \"axios\";\nimport { isNil } from \"lodash-unified\";\nimport { CacheManage, CryptoManage, InterceptorsManage, LoadingManage, MessageBoxManage, MessageManage } from \"./types\";\nimport type { AxiosHeaderValue } from \"axios\";\n\ntype InitializeOptions = Partial<Pick<FastAxios, \"baseUrl\" | \"timeout\" | \"headers\" | \"requestCipher\">>;\n\ntype CodeKeyType = string | number;\n\nclass FastAxios {\n\tstatic instance: FastAxios;\n\n\tconstructor(options?: InitializeOptions) {\n\t\tthis.setOptions(options);\n\n\t\tthis.errorCode = {\n\t\t\tdefault: \"请求失败，请稍后再试！\",\n\t\t\tcancelDuplicate: \"重复请求，自动取消！\",\n\t\t\toffLine: \"您断网了！\",\n\t\t\tfileDownloadError: \"文件下载失败或此文件不存在！\",\n\t\t\t302: \"接口重定向了！\",\n\t\t\t400: \"参数不正确！\",\n\t\t\t401: \"您没有权限操作（令牌、用户名、密码错误）！\",\n\t\t\t403: \"您的访问是被禁止的！\",\n\t\t\t404: \"请求的资源不存在！\",\n\t\t\t405: \"请求的格式不正确！\",\n\t\t\t408: \"请求超时！\",\n\t\t\t409: \"系统已存在相同数据！\",\n\t\t\t410: \"请求的资源被永久删除，且不会再得到的！\",\n\t\t\t422: \"当创建一个对象时，发生一个验证错误！\",\n\t\t\t429: \"请求过于频繁，请稍后再试！\",\n\t\t\t500: \"服务器内部错误！\",\n\t\t\t501: \"服务未实现！\",\n\t\t\t502: \"网关错误！\",\n\t\t\t503: \"服务不可用，服务器暂时过载或维护！\",\n\t\t\t504: \"服务暂时无法访问，请稍后再试！\",\n\t\t\t505: \"HTTP版本不受支持！\",\n\t\t\t[AxiosError.ETIMEDOUT]: \"请求超时！\",\n\t\t\t[AxiosError.ERR_CANCELED]: \"连接已被取消！\",\n\t\t\t[AxiosError.ECONNABORTED]: \"连接中断，服务器暂时过载或维护！\",\n\t\t\t[AxiosError.ERR_NETWORK]: \"网关错误，服务不可用，服务器暂时过载或维护！\",\n\t\t\t// UniApp\n\t\t\t\"request:fail\": \"网关错误，服务不可用，服务器暂时过载或维护！\",\n\t\t};\n\n\t\tthis.loading = new LoadingManage();\n\t\tthis.message = new MessageManage();\n\t\tthis.messageBox = new MessageBoxManage();\n\t\tthis.cache = new CacheManage();\n\t\tthis.crypto = new CryptoManage();\n\t\tthis.interceptors = new InterceptorsManage();\n\t}\n\n\t/**\n\t * 设置选项\n\t * @param options 初始化选项\n\t */\n\tpublic setOptions(options: InitializeOptions): FastAxios {\n\t\tif (options?.baseUrl) {\n\t\t\tthis._baseUrl = options.baseUrl;\n\t\t}\n\n\t\tif (options?.timeout) {\n\t\t\tthis._timeout = options.timeout;\n\t\t} else {\n\t\t\tthis._timeout = this._timeout ?? 60000;\n\t\t}\n\n\t\tif (options?.headers) {\n\t\t\tthis._headers = { ...(this._headers ?? {}), ...options.headers };\n\t\t}\n\n\t\tif (!isNil(options?.requestCipher)) {\n\t\t\tthis._requestCipher = options.requestCipher;\n\t\t} else {\n\t\t\tthis._requestCipher = isNil(this._requestCipher) ? true : this._requestCipher;\n\t\t}\n\t\treturn this;\n\t}\n\n\tprivate _baseUrl: string;\n\t/** 请求域名或者Base路径 */\n\tpublic get baseUrl(): string {\n\t\treturn this._baseUrl;\n\t}\n\n\tprivate _timeout: number;\n\t/**\n\t * 超时时间，单位毫秒\n\t * @default 60000\n\t */\n\tpublic get timeout(): number {\n\t\treturn this._timeout;\n\t}\n\n\tprivate _headers: {\n\t\t[key: string]: AxiosHeaderValue;\n\t};\n\t/** 默认头部 */\n\tpublic get headers(): {\n\t\t[key: string]: AxiosHeaderValue;\n\t} {\n\t\treturn this._headers;\n\t}\n\n\tprivate _requestCipher: boolean;\n\t/**\n\t * 请求加密解密\n\t * @default true\n\t */\n\tpublic get requestCipher(): boolean {\n\t\treturn this._requestCipher;\n\t}\n\n\t/** 错误Code */\n\treadonly errorCode: Record<CodeKeyType, string>;\n\n\t/** 加载 @description 需要自行处理多次调用的问题 */\n\treadonly loading: LoadingManage;\n\n\t/** 消息提示 */\n\treadonly message: MessageManage;\n\n\t/** 消息提示框 */\n\treadonly messageBox: MessageBoxManage;\n\n\t/** 缓存 */\n\treadonly cache: CacheManage;\n\n\t/** 加密解密 */\n\treadonly crypto: CryptoManage;\n\n\t/** 拦截器 */\n\treadonly interceptors: InterceptorsManage;\n\n\t/** 添加错误Code */\n\taddErrorCode(key: CodeKeyType, message: string): FastAxios;\n\t/** 添加错误Code */\n\taddErrorCode(codes: Record<CodeKeyType, string>): FastAxios;\n\n\taddErrorCode(arg: CodeKeyType | Record<CodeKeyType, string>, message?: string): FastAxios {\n\t\tif (typeof arg === \"string\" || typeof arg === \"number\") {\n\t\t\tthis.errorCode[arg] = message;\n\t\t} else {\n\t\t\tfor (const key in arg) {\n\t\t\t\tthis.errorCode[key] = arg[key];\n\t\t\t}\n\t\t}\n\t\treturn this;\n\t}\n}\n\n/**\n * 初始化 fast-axios\n * @param options 基础选项\n * @param newInstance 是否强制创建新的实例\n * @returns\n */\nexport const createFastAxios = (options?: InitializeOptions, newInstance = false): FastAxios => {\n\tif (newInstance) {\n\t\treturn new FastAxios(options);\n\t} else {\n\t\tif (!FastAxios.instance) {\n\t\t\tconst fastAxios = new FastAxios(options);\n\t\t\tFastAxios.instance = fastAxios;\n\t\t}\n\t\treturn FastAxios.instance;\n\t}\n};\n\n/**\n * 获取 fast-axios 实例\n */\nexport const useFastAxios = (): FastAxios => {\n\tif (!FastAxios.instance) {\n\t\tthrow new Error(\"请先调用 'createFastAxios' 初始化 'fast-axios'。\");\n\t}\n\n\treturn FastAxios.instance;\n};\n"],"names":["FastAxios","static","constructor","options","this","setOptions","errorCode","default","cancelDuplicate","offLine","fileDownloadError","AxiosError","ETIMEDOUT","ERR_CANCELED","ECONNABORTED","ERR_NETWORK","loading","LoadingManage","message","MessageManage","messageBox","MessageBoxManage","cache","CacheManage","crypto","CryptoManage","interceptors","InterceptorsManage","baseUrl","_baseUrl","_timeout","timeout","headers","_headers","isNil","requestCipher","_requestCipher","addErrorCode","arg","key","createFastAxios","newInstance","instance","fastAxios","useFastAxios","Error"],"mappings":"saASA,MAAMA,EACLC,gBAEA,WAAAC,CAAYC,GACXC,KAAKC,WAAWF,GAEhBC,KAAKE,UAAY,CAChBC,QAAS,cACTC,gBAAiB,aACjBC,QAAS,QACTC,kBAAmB,iBACnB,IAAK,UACL,IAAK,SACL,IAAK,wBACL,IAAK,aACL,IAAK,YACL,IAAK,YACL,IAAK,QACL,IAAK,aACL,IAAK,sBACL,IAAK,qBACL,IAAK,gBACL,IAAK,WACL,IAAK,SACL,IAAK,QACL,IAAK,oBACL,IAAK,kBACL,IAAK,cACL,CAACC,EAAWC,WAAY,QACxB,CAACD,EAAWE,cAAe,UAC3B,CAACF,EAAWG,cAAe,mBAC3B,CAACH,EAAWI,aAAc,yBAE1B,eAAgB,0BAGjBX,KAAKY,QAAU,IAAIC,EACnBb,KAAKc,QAAU,IAAIC,EACnBf,KAAKgB,WAAa,IAAIC,EACtBjB,KAAKkB,MAAQ,IAAIC,EACjBnB,KAAKoB,OAAS,IAAIC,EAClBrB,KAAKsB,aAAe,IAAIC,CACzB,CAMO,UAAAtB,CAAWF,GAoBjB,OAnBIA,GAASyB,UACZxB,KAAKyB,SAAW1B,EAAQyB,SAIxBxB,KAAK0B,SADF3B,GAAS4B,QACI5B,EAAQ4B,QAER3B,KAAK0B,UAAY,IAG9B3B,GAAS6B,UACZ5B,KAAK6B,SAAW,IAAM7B,KAAK6B,UAAY,MAAQ9B,EAAQ6B,UAGnDE,EAAM/B,GAASgC,eAGnB/B,KAAKgC,iBAAiBF,EAAM9B,KAAKgC,iBAAyBhC,KAAKgC,eAF/DhC,KAAKgC,eAAiBjC,EAAQgC,cAIxB/B,IACR,CAEQyB,SAER,WAAWD,GACV,OAAOxB,KAAKyB,QACb,CAEQC,SAKR,WAAWC,GACV,OAAO3B,KAAK0B,QACb,CAEQG,SAIR,WAAWD,GAGV,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