update createApi
This commit is contained in:
parent
c75420a91c
commit
4db95b8f0e
340
dist/core.js
vendored
340
dist/core.js
vendored
File diff suppressed because one or more lines are too long
@ -50,6 +50,14 @@ function toHookName(actionName) {
|
|||||||
return `use${actionName.charAt(0).toUpperCase()}${actionName.slice(1)}`;
|
return `use${actionName.charAt(0).toUpperCase()}${actionName.slice(1)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveRequestConfig(requestConfig, data) {
|
||||||
|
if (typeof requestConfig === "function") {
|
||||||
|
return requestConfig(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestConfig;
|
||||||
|
}
|
||||||
|
|
||||||
export function createApi({
|
export function createApi({
|
||||||
key,
|
key,
|
||||||
url,
|
url,
|
||||||
@ -91,6 +99,7 @@ export function createApi({
|
|||||||
mutationFn: async (data = {}) => {
|
mutationFn: async (data = {}) => {
|
||||||
const method = action.method || "post";
|
const method = action.method || "post";
|
||||||
const requestUrl = resolveUrl(action.url || url, data);
|
const requestUrl = resolveUrl(action.url || url, data);
|
||||||
|
const requestConfig = resolveRequestConfig(action.requestConfig, data);
|
||||||
|
|
||||||
const payload = action.body
|
const payload = action.body
|
||||||
? action.body(data)
|
? action.body(data)
|
||||||
@ -98,15 +107,37 @@ export function createApi({
|
|||||||
? data
|
? data
|
||||||
: omitId(data);
|
: omitId(data);
|
||||||
|
|
||||||
const response =
|
let response;
|
||||||
method === "delete"
|
|
||||||
? await axiosInstance.delete(
|
if (method === "get") {
|
||||||
requestUrl,
|
response = await axiosInstance.get(
|
||||||
payload && Object.keys(payload).length > 0
|
requestUrl,
|
||||||
? { data: payload }
|
requestConfig
|
||||||
: undefined,
|
? {
|
||||||
)
|
...payload,
|
||||||
: await axiosInstance[method](requestUrl, payload);
|
...requestConfig,
|
||||||
|
}
|
||||||
|
: payload,
|
||||||
|
);
|
||||||
|
} else if (method === "delete") {
|
||||||
|
const hasPayload = payload && Object.keys(payload).length > 0;
|
||||||
|
|
||||||
|
response = await axiosInstance.delete(
|
||||||
|
requestUrl,
|
||||||
|
requestConfig || hasPayload
|
||||||
|
? {
|
||||||
|
...(requestConfig || {}),
|
||||||
|
...(hasPayload ? { data: payload } : {}),
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
response = await axiosInstance[method](
|
||||||
|
requestUrl,
|
||||||
|
payload,
|
||||||
|
requestConfig,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user