UNPKG

@uni-helper/uni-network

Version:

为 uni-app 打造的基于 Promise 的 HTTP 客户端

108 lines (107 loc) 4.3 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_src = require("./src-bcNVAi8T.cjs"); let _vueuse_core = require("@vueuse/core"); let vue_demi = require("vue-demi"); //#region src/composables.ts const isUnInstance = (val) => !!(val === null || val === void 0 ? void 0 : val.request) && !!(val === null || val === void 0 ? void 0 : val.download) && !!(val === null || val === void 0 ? void 0 : val.upload); function useUn(...args) { const url = typeof args[0] === "string" ? args[0] : void 0; const argsPlaceholder = typeof url === "string" ? 1 : 0; let defaultConfig = {}; let instance = require_src.un; let options = { immediate: !!argsPlaceholder, shallow: true, abortPrevious: true }; if (args.length > 0 + argsPlaceholder) /** 在这里不能使用 `instanceof`,原因请参考 https://github.com/axios/axios/issues/737 */ if (isUnInstance(args[0 + argsPlaceholder])) instance = args[0 + argsPlaceholder]; else defaultConfig = args[0 + argsPlaceholder]; if (args.length > 1 + argsPlaceholder && isUnInstance(args[1 + argsPlaceholder])) instance = args[1 + argsPlaceholder]; if (args.length === 2 + argsPlaceholder && !isUnInstance(args[1 + argsPlaceholder]) || args.length === 3 + argsPlaceholder) options = args[args.length - 1] || options; const { shallow, onSuccess = _vueuse_core.noop, onError = _vueuse_core.noop, immediate, resetOnExecute = false } = options; const initialData = options.initialData; const response = (0, vue_demi.shallowRef)(); const data = (shallow ? vue_demi.shallowRef : vue_demi.ref)(initialData); const isFinished = (0, vue_demi.ref)(false); const isLoading = (0, vue_demi.ref)(false); const isAborted = (0, vue_demi.ref)(false); const error = (0, vue_demi.shallowRef)(); const cancelTokenSource = require_src.un.CancelToken.source; let cancelToken = cancelTokenSource(); const abort = (message) => { if (isFinished.value || !isLoading.value) return; cancelToken.cancel(message); cancelToken = cancelTokenSource(); isAborted.value = true; isLoading.value = false; isFinished.value = false; }; const loading = (loading) => { isLoading.value = loading; isFinished.value = !loading; }; /** 重置 data 为 initialData */ const resetData = () => { if (resetOnExecute) data.value = initialData; }; const waitUntilFinished = () => new Promise((resolve, reject) => { (0, _vueuse_core.until)(isFinished).toBe(true).then(() => error.value ? reject(error.value) : resolve(result)); }); const promise = { then: (...args) => waitUntilFinished().then(...args), catch: (...args) => waitUntilFinished().catch(...args) }; let executeCounter = 0; const execute = (executeUrl = url, config = {}) => { error.value = void 0; const _url = typeof executeUrl === "string" ? executeUrl : url !== null && url !== void 0 ? url : config.url; if (_url === void 0) { error.value = new require_src.UnError(require_src.UnError.ERR_INVALID_URL); isFinished.value = true; return promise; } resetData(); if (options.abortPrevious) abort(); loading(true); executeCounter += 1; const currentExecuteCounter = executeCounter; isAborted.value = false; instance(_url, require_src._objectSpread2(require_src._objectSpread2(require_src._objectSpread2({}, defaultConfig), typeof executeUrl === "object" ? executeUrl : config), {}, { cancelToken: cancelToken.token })).then((r) => { if (isAborted.value) return; response.value = r; const result = r === null || r === void 0 ? void 0 : r.data; data.value = result; onSuccess(result); }).catch((error_) => { error.value = error_; onError(error_); }).finally(() => { var _options$onFinish; (_options$onFinish = options.onFinish) === null || _options$onFinish === void 0 || _options$onFinish.call(options); if (currentExecuteCounter === executeCounter) loading(false); }); return promise; }; if (immediate && url) execute(); const result = { response, data, error, finished: isFinished, loading: isLoading, isFinished, isLoading, cancel: abort, isAborted, canceled: isAborted, aborted: isAborted, isCanceled: isAborted, abort, execute }; return require_src._objectSpread2(require_src._objectSpread2({}, result), promise); } //#endregion exports.useUn = useUn;