fetch request steps

This commit is contained in:
2026-05-28 17:52:53 +05:30
parent d4a79c785d
commit c920276293
8 changed files with 807 additions and 68 deletions

View File

@@ -46,6 +46,10 @@ export const api = {
if (!_api) throw new Error("API client not initialized");
return _api.delete(...args);
},
patch: (...args: Parameters<AxiosInstance["patch"]>) => {
if (!_api) throw new Error("API client not initialized");
return _api.patch(...args);
},
};
export const auth = {

View File

@@ -73,6 +73,23 @@ export function useResource<T = any>(config: ResourceConfig | undefined) {
},
});
// --- PATCH ---
const usePatch = () =>
useMutation({
mutationFn: async ({ id, data }: { id: string; data: Partial<T> }) => {
if (!endpoint) throw new Error("Endpoint not defined");
// @ts-ignore
const res = await api.patch<T>(`${endpoint}/${id}`, data);
return res.data;
},
onSuccess: (updatedItem) => {
// @ts-ignore
const id = updatedItem[primaryKey];
queryClient.invalidateQueries({ queryKey: [name, "list"] });
queryClient.invalidateQueries({ queryKey: [name, "detail", id] });
},
});
// --- DELETE ---
const useDelete = () =>
useMutation({
@@ -136,6 +153,7 @@ export function useResource<T = any>(config: ResourceConfig | undefined) {
useMe,
useCreate,
useUpdate,
usePatch,
useUpdateMe,
useDelete,
getListQueryOptions,