@vyron/uni-network
Version:
为 uni-app 打造的基于 Promise 的 HTTP 客户端
140 lines (137 loc) • 4.84 kB
JavaScript
import { noop, until } from '@vueuse/core';
import { shallowRef, ref } from 'vue-demi';
import { un, UnError } from './index.mjs';
import 'statuses-es';
import 'fast-querystring';
import 'lodash.merge';
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));
const isUnInstance = (val) => !!(val == null ? void 0 : val.request) && !!(val == null ? void 0 : val.download) && !!(val == null ? 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 = un;
let options = {
immediate: !!argsPlaceholder,
shallow: true,
abortPrevious: true
};
if (args.length > 0 + argsPlaceholder) {
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 = noop,
onError = noop,
immediate,
resetOnExecute = false
} = options;
const initialData = options.initialData;
const response = shallowRef();
const data = (shallow ? shallowRef : ref)(initialData);
const isFinished = ref(false);
const isLoading = ref(false);
const isAborted = ref(false);
const error = shallowRef();
const cancelTokenSource = 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 = (loading2) => {
isLoading.value = loading2;
isFinished.value = !loading2;
};
const resetData = () => {
if (resetOnExecute) data.value = initialData;
};
const waitUntilFinished = () => new Promise((resolve, reject) => {
until(isFinished).toBe(true).then(() => error.value ? reject(error.value) : resolve(result));
});
const promise = {
// biome-ignore lint/suspicious/noThenProperty: <explanation>
then: (...args2) => waitUntilFinished().then(...args2),
catch: (...args2) => waitUntilFinished().catch(...args2)
};
let executeCounter = 0;
const execute = (executeUrl = url, config = {}) => {
error.value = void 0;
const _url = typeof executeUrl === "string" ? executeUrl : url != null ? url : config.url;
if (_url === void 0) {
error.value = new UnError(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, __spreadProps(__spreadValues(__spreadValues({}, defaultConfig), typeof executeUrl === "object" ? executeUrl : config), {
cancelToken: cancelToken.token
})).then((r) => {
if (isAborted.value) return;
response.value = r;
const result2 = r.data;
data.value = result2;
onSuccess(result2);
}).catch((error_) => {
error.value = error_;
onError(error_);
}).finally(() => {
var _a;
(_a = options.onFinish) == null ? void 0 : _a.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 __spreadValues(__spreadValues({}, result), promise);
}
export { useUn };