vue-hooks-plus
Version:
Vue hooks library
64 lines (63 loc) • 2.15 kB
JavaScript
const vue = require("vue");
const useRequest = require("../useRequest");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const useRequest__default = /* @__PURE__ */ _interopDefaultLegacy(useRequest);
const DEFAULT_KEY = "VUE_HOOKS_PLUS_USE_REQUEST_DEFAULT_KEY";
function useFetchs(service, options, self) {
const fetchKeyPersist = vue.ref(self == null ? void 0 : self.fetchKey);
const fetchs = vue.ref({});
const newFetchs = vue.ref({});
const setFetchs = (fetchs_) => {
newFetchs.value = fetchs_;
};
const { run } = useRequest__default.default(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
};
}
module.exports = useFetchs;
;