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)}`;
|
||||
}
|
||||
|
||||
function resolveRequestConfig(requestConfig, data) {
|
||||
if (typeof requestConfig === "function") {
|
||||
return requestConfig(data);
|
||||
}
|
||||
|
||||
return requestConfig;
|
||||
}
|
||||
|
||||
export function createApi({
|
||||
key,
|
||||
url,
|
||||
@ -91,6 +99,7 @@ export function createApi({
|
||||
mutationFn: async (data = {}) => {
|
||||
const method = action.method || "post";
|
||||
const requestUrl = resolveUrl(action.url || url, data);
|
||||
const requestConfig = resolveRequestConfig(action.requestConfig, data);
|
||||
|
||||
const payload = action.body
|
||||
? action.body(data)
|
||||
@ -98,15 +107,37 @@ export function createApi({
|
||||
? data
|
||||
: omitId(data);
|
||||
|
||||
const response =
|
||||
method === "delete"
|
||||
? await axiosInstance.delete(
|
||||
requestUrl,
|
||||
payload && Object.keys(payload).length > 0
|
||||
? { data: payload }
|
||||
: undefined,
|
||||
)
|
||||
: await axiosInstance[method](requestUrl, payload);
|
||||
let response;
|
||||
|
||||
if (method === "get") {
|
||||
response = await axiosInstance.get(
|
||||
requestUrl,
|
||||
requestConfig
|
||||
? {
|
||||
...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;
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user