UNPKG

@shencom/request

Version:

封装 axios 请求,兼容了 uni-app 的 uni.request 和 uni.uploadFile 的适配器

233 lines (229 loc) 8.04 kB
import axios from 'axios'; import { pickBy } from 'lodash-es'; import settle from 'axios/unsafe/core/settle.js'; var __defProp$1 = Object.defineProperty; var __defProps$1 = Object.defineProperties; var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols; var __hasOwnProp$1 = Object.prototype.hasOwnProperty; var __propIsEnum$1 = Object.prototype.propertyIsEnumerable; var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues$1 = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp$1.call(b, prop)) __defNormalProp$1(a, prop, b[prop]); if (__getOwnPropSymbols$1) for (var prop of __getOwnPropSymbols$1(b)) { if (__propIsEnum$1.call(b, prop)) __defNormalProp$1(a, prop, b[prop]); } return a; }; var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)); const instance = axios.create(); instance.defaults.errcode = "0000"; instance.defaults.isFilterEmpty = true; instance.defaults.timeout = 2e4; const isObject$1 = (data) => Object.prototype.toString.call(data) === "[object Object]"; function handledBodyEmpty(data) { if (!data) return {}; if (Array.isArray(data)) { return data.map((item) => { if (isObject$1(item)) return pickBy(item, (v) => v !== null && v !== void 0 && v !== ""); return item; }); } if (isObject$1(data)) { return pickBy(data, (v) => v !== ""); } return data; } function checkFilterEmpty(config) { var _a, _b; if (["POST", "GET"].includes(((_a = config.method) == null ? void 0 : _a.toLocaleUpperCase()) || "")) { config.data = handledBodyEmpty(config.data); } if (((_b = config.method) == null ? void 0 : _b.toLocaleUpperCase()) === "GET") { if (config.params) config.params = handledBodyEmpty(config.params); } return config; } const __base = (url, config) => { var _a; let { isFilterEmpty = true } = instance.defaults; if (typeof (config == null ? void 0 : config.isFilterEmpty) === "boolean") { isFilterEmpty = config.isFilterEmpty; } if (isFilterEmpty) config = checkFilterEmpty(config); const errcode = (_a = config == null ? void 0 : config.errcode) != null ? _a : instance.defaults.errcode; return new Promise((resolve, reject) => { instance.request(__spreadValues$1({ url }, config)).then((res) => { var _a2; if (res.data.errcode) { if (((_a2 = res.data) == null ? void 0 : _a2.errcode) === errcode) { resolve(res.data); return; } reject(res); } else { resolve(res.data); } }).catch(reject); }); }; const __post = (url, data, config) => { return __base(url, __spreadProps$1(__spreadValues$1({ data }, config), { method: "post" })); }; const __get = (url, data, config) => { return __base(url, __spreadProps$1(__spreadValues$1({ params: data }, config), { method: "get" })); }; const __upload = (url, data, config) => { return new Promise((resolve, reject) => { instance.request(__spreadProps$1(__spreadValues$1({ url, data }, config), { method: "post" })).then((res) => { var _a, _b; if (res.status === 200) { if (typeof res.data === "string" && res.data === "") { resolve(res); } else if (((_a = res.data) == null ? void 0 : _a.errcode) === ((_b = res.config) == null ? void 0 : _b.errcode)) { resolve(res.data); } else { reject(res); } } else reject(res); }).catch(reject); }); }; instance.post = __post; instance.get = __get; instance.upload = __upload; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); function isFileType(files) { return files.some((file) => file.name && file.file); } function isMultiUpload(config) { return Array.isArray(config.files) && config.files.length > 0; } const isObject = (data) => Object.prototype.toString.call(data) === "[object Object]"; const isAliFormData = (data) => isObject(data) && "appendData" in data && "realFormData" in data; const isFormData = (data) => Object.prototype.toString.call(data) === "[object FormData]" || isAliFormData(data); function isUploadFile(config) { var _a; if (isFormData(config.data)) return true; if (config.method === "post") { if (config.data) { const data = JSON.parse(config.data); if ((_a = data.file) == null ? void 0 : _a.path) return true; if (data.filePath) return true; if (isMultiUpload(data) && isFileType(data.files)) return true; } } return false; } function format(config, resolve, reject) { var _a, _b; const uniConfig = __spreadProps(__spreadValues({}, config), { url: axios.getUri(config), header: __spreadValues({}, config.headers) }); delete uniConfig.headers; if (isUploadFile(uniConfig)) { (_a = uniConfig.header) == null ? true : delete _a["Content-Type"]; if (typeof uniConfig.data === "string") { const data = JSON.parse(uniConfig.data); uniConfig.formData = {}; Object.keys(data).forEach((name) => { const value = data[name]; if (name === "file") { uniConfig.filePath = value == null ? void 0 : value.path; return; } if (name === "filePath") uniConfig.filePath = value; uniConfig.formData[name] = value; }); uniConfig.name = "file"; delete uniConfig.data; } } else if (((_b = uniConfig.method) == null ? void 0 : _b.toLocaleUpperCase()) === "GET") { uniConfig.data = uniConfig.data ? uniConfig.data : uniConfig.params; } uniConfig.complete = function(response) { const result = { data: response.data, status: response.statusCode, statusText: response.errMsg, headers: response.header, config: uniConfig }; settle(resolve, reject, result); }; return uniConfig; } function uniappAdapter(config = {}) { return new Promise((resolve, reject) => { const uniConfig = format(config, resolve, reject); let requestTask; if (config.cancelToken) { config.cancelToken.promise.then((cancel) => { if (!requestTask) return; requestTask.abort(); reject(cancel); requestTask = null; }); } if (isUploadFile(config)) { if (typeof FormData === "undefined") { requestTask = uni.uploadFile(uniConfig); } else { const controller = new AbortController(); delete uniConfig.header; axios.request(__spreadProps(__spreadValues({}, uniConfig), { headers: config.headers, signal: controller.signal, // @ts-expect-error uniapp 的 H5 环境下需要移除适配器 adapter: null })).then((res) => { settle(resolve, reject, res); }).catch((err) => { settle(resolve, reject, err); }); requestTask = controller; } } else { requestTask = uni.request( uniConfig ); } }); } const adapter = uniappAdapter; export { adapter as a, instance as i }; //# sourceMappingURL=adapter-8c56e3fd.js.map