vue-hooks-plus
Version:
Vue hooks library
63 lines (62 loc) • 1.93 kB
JavaScript
import { ref } from "vue";
import useRequest from "../useRequest";
const DEFAULT_KEY = "VUE_HOOKS_PLUS_USE_REQUEST_DEFAULT_KEY";
function useFetchs(service, options, self) {
const fetchKeyPersist = ref(self == null ? void 0 : self.fetchKey);
const fetchs = ref({});
const newFetchs = ref({});
const setFetchs = (fetchs_) => {
newFetchs.value = fetchs_;
};
const { run } = useRequest(service, {
...options,
manual: true,
concurrent: true,
onSuccess: (data, params) => {
var _a, _b, _c;
const cacheKey = (_b = (_a = fetchKeyPersist.value) == null ? void 0 : _a.call(fetchKeyPersist, ...params)) != null ? _b : DEFAULT_KEY;
fetchs.value[cacheKey] = {
key: cacheKey,
data,
params,
loading: false
};
setFetchs(fetchs.value);
(_c = options.onSuccess) == null ? void 0 : _c.call(options, data, params);
},
onError: (error, params) => {
var _a, _b, _c;
const cacheKey = (_b = (_a = fetchKeyPersist.value) == null ? void 0 : _a.call(fetchKeyPersist, ...params)) != null ? _b : DEFAULT_KEY;
fetchs.value[cacheKey] = {
key: cacheKey,
data: void 0,
params,
loading: false
};
setFetchs(fetchs.value);
(_c = options.onError) == null ? void 0 : _c.call(options, error, params);
},
onBefore: (params) => {
var _a, _b, _c;
const cacheKey = (_b = (_a = fetchKeyPersist.value) == null ? void 0 : _a.call(fetchKeyPersist, ...params)) != null ? _b : DEFAULT_KEY;
fetchs.value[cacheKey] = {
key: cacheKey,
data: void 0,
params,
loading: true
};
setFetchs(fetchs.value);
(_c = options.onBefore) == null ? void 0 : _c.call(options, params);
}
});
const fetchRun = (...args) => {
run(...args);
};
return {
fetchs: newFetchs,
fetchRun
};
}
export {
useFetchs as default
};