mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
import { ApplyPluginsType, plugin } from '@fesjs/fes';
|
|
import { ref, shallowRef } from 'vue';
|
|
|
|
import { createRequest } from '@qlin/request';
|
|
|
|
function getRequestInstance() {
|
|
const defaultConfig = plugin.applyPlugins({
|
|
key: 'request',
|
|
type: ApplyPluginsType.modify,
|
|
initialValue: {
|
|
timeout: 10000,
|
|
},
|
|
});
|
|
|
|
return createRequest(defaultConfig);
|
|
}
|
|
|
|
let currentRequest;
|
|
|
|
export const rawRequest = (url, data, options = {}) => {
|
|
if (typeof options === 'string') {
|
|
options = {
|
|
method: options,
|
|
};
|
|
}
|
|
if (!currentRequest) {
|
|
currentRequest = getRequestInstance();
|
|
}
|
|
return currentRequest(url, data, options);
|
|
};
|
|
|
|
export const request = async (url, data, options = {}) => {
|
|
const response = await rawRequest(url, data, options);
|
|
return response.data;
|
|
};
|
|
|
|
function isPromiseLike(obj) {
|
|
return !!obj && typeof obj === 'object' && typeof obj.then === 'function';
|
|
}
|
|
|
|
export const useRequest = (url, data, options = {}) => {
|
|
const loadingRef = ref(true);
|
|
const errorRef = ref(null);
|
|
const dataRef = shallowRef(null);
|
|
let promise;
|
|
if (isPromiseLike(url)) {
|
|
promise = url;
|
|
} else {
|
|
promise = request(url, data, options);
|
|
}
|
|
promise
|
|
.then((res) => {
|
|
dataRef.value = res;
|
|
})
|
|
.catch((error) => {
|
|
errorRef.value = error;
|
|
})
|
|
.finally(() => {
|
|
loadingRef.value = false;
|
|
});
|
|
return {
|
|
loading: loadingRef,
|
|
error: errorRef,
|
|
data: dataRef,
|
|
};
|
|
};
|