UNPKG

@uni-helper/uni-network

Version:

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

109 lines (107 loc) 4.38 kB
const require_src = require('./src-DOo65kmA.cjs'); const __vueuse_core = require_src.__toESM(require("@vueuse/core")); const vue_demi = require_src.__toESM(require("vue-demi")); //#region src/composables.ts var import_objectSpread2 = require_src.__toESM(require_src.require_objectSpread2(), 1); 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$1) => { isLoading.value = loading$1; isFinished.value = !loading$1; }; /** 重置 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$1) => waitUntilFinished().then(...args$1), catch: (...args$1) => waitUntilFinished().catch(...args$1) }; 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, (0, import_objectSpread2.default)((0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, defaultConfig), typeof executeUrl === "object" ? executeUrl : config), {}, { cancelToken: cancelToken.token })).then((r) => { if (isAborted.value) return; response.value = r; const result$1 = r.data; data.value = result$1; onSuccess(result$1); }).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 (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, result), promise); } //#endregion exports.useUn = useUn;