vue-admin-core
Version:
A Component Library for Vue 3
54 lines (49 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var Fetch = require('./Fetch.js');
var lodashEs = require('lodash-es');
function useRequestImplement(service, options = {}, plugins = []) {
const { manual = false, ...rest } = options;
const fetchOptions = {
manual,
...rest
};
const initState = plugins.map((p) => {
var _a;
return (_a = p == null ? void 0 : p.onInit) == null ? void 0 : _a.call(p, fetchOptions);
}).filter(Boolean);
const fetchInstance = new Fetch.default(
service,
fetchOptions,
Object.assign({}, ...initState)
);
fetchInstance.options = fetchOptions;
fetchInstance.pluginImpls = plugins.map((p) => p(fetchInstance, fetchOptions));
vue.onMounted(() => {
if (!manual) {
let params = vue.toValue(options.defaultParams || []);
if (!lodashEs.isEmpty(vue.toValue(fetchInstance.params))) {
params = vue.toValue(fetchInstance.params);
}
fetchInstance.run(...params);
}
});
vue.onUnmounted(() => {
fetchInstance.cancel();
});
return {
loading: fetchInstance.loading,
data: fetchInstance.data,
error: fetchInstance.error,
params: fetchInstance.params || [],
cancel: fetchInstance.cancel.bind(fetchInstance),
refresh: fetchInstance.refresh.bind(fetchInstance),
refreshAsync: fetchInstance.refreshAsync.bind(fetchInstance),
run: fetchInstance.run.bind(fetchInstance),
runAsync: fetchInstance.runAsync.bind(fetchInstance),
mutate: fetchInstance.mutate.bind(fetchInstance)
};
}
exports.default = useRequestImplement;
//# sourceMappingURL=useRequestImplement.js.map