update createApi
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 48s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s

This commit is contained in:
Mohamadzadeh 2026-07-22 18:58:58 +03:30
parent c75420a91c
commit 4db95b8f0e
2 changed files with 222 additions and 167 deletions

340
dist/core.js vendored

File diff suppressed because one or more lines are too long

View File

@ -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;
},