proper use of react-openapi for resource api calls

This commit is contained in:
2026-04-25 12:13:28 +05:30
parent 5acbb7ccdd
commit 71afc157ff
13 changed files with 191 additions and 222 deletions

View File

@@ -1,16 +1,21 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { api } from "../api/client";
import { ResourceConfig } from "../types/config";
import { ConfigContext } from "../providers/ConfigContext";
import * as React from "react";
export function useResource<T = any>(config: ResourceConfig) {
export function useResource<T = any>(config: ResourceConfig | undefined) {
const queryClient = useQueryClient();
const { name, endpoint, primaryKey } = config;
// Return empty/disabled hooks if config is missing
const { name = '', endpoint = '', primaryKey = 'id' } = config || {};
// --- READ ALL ---
const useList = (params?: any) =>
useQuery({
queryKey: [name, "list", params],
queryFn: async () => {
if (!endpoint) return { data: [], total: 0 };
// @ts-ignore
const res = await api.get<T[]>(endpoint, { params });
const total = res.headers ? parseInt(res.headers['x-total-count'] || res.headers['X-Total-Count']) : undefined;
@@ -18,7 +23,8 @@ export function useResource<T = any>(config: ResourceConfig) {
data: res.data,
total: isNaN(total as any) ? undefined : total
};
}
},
enabled: !!endpoint,
});
// --- READ ONE ---
@@ -26,18 +32,19 @@ export function useResource<T = any>(config: ResourceConfig) {
useQuery({
queryKey: [name, "detail", id],
queryFn: async () => {
if (!id) return null;
if (!id || !endpoint) return null;
// @ts-ignore
const res = await api.get<T>(`${endpoint}/${id}`);
return res.data;
},
enabled: !!id,
enabled: !!id && !!endpoint,
});
// --- CREATE ---
const useCreate = () =>
useMutation({
mutationFn: async (data: Partial<T>) => {
if (!endpoint) throw new Error("Endpoint not defined");
// @ts-ignore
const res = await api.post<T>(endpoint, data);
return res.data;
@@ -51,6 +58,7 @@ export function useResource<T = any>(config: ResourceConfig) {
const useUpdate = () =>
useMutation({
mutationFn: async ({ id, data }: { id: string; data: Partial<T> }) => {
if (!endpoint) throw new Error("Endpoint not defined");
// @ts-ignore
const res = await api.put<T>(`${endpoint}/${id}`, data);
return res.data;
@@ -67,6 +75,7 @@ export function useResource<T = any>(config: ResourceConfig) {
const useDelete = () =>
useMutation({
mutationFn: async (id: string) => {
if (!endpoint) throw new Error("Endpoint not defined");
await api.delete(`${endpoint}/${id}`);
return id;
},
@@ -79,6 +88,7 @@ export function useResource<T = any>(config: ResourceConfig) {
const getListQueryOptions = (params?: any) => ({
queryKey: [name, "list", params],
queryFn: async () => {
if (!endpoint) return { data: [], total: 0 };
// @ts-ignore
const res = await api.get<T[]>(endpoint, { params });
const total = res.headers ? parseInt(res.headers['x-total-count'] || res.headers['X-Total-Count']) : undefined;
@@ -87,6 +97,7 @@ export function useResource<T = any>(config: ResourceConfig) {
total: isNaN(total as any) ? undefined : total
};
},
enabled: !!endpoint,
});
// --- READ ME ---
@@ -94,16 +105,19 @@ export function useResource<T = any>(config: ResourceConfig) {
useQuery({
queryKey: [name, "me"],
queryFn: async () => {
if (!endpoint) return null;
// @ts-ignore
const res = await api.get<T>(`${endpoint}/me`);
return res.data;
},
enabled: !!endpoint,
});
// --- UPDATE ME ---
const useUpdateMe = () =>
useMutation({
mutationFn: async (data: Partial<T>) => {
if (!endpoint) throw new Error("Endpoint not defined");
// @ts-ignore
const res = await api.put<T>(`${endpoint}/me`, data);
return res.data;
@@ -125,3 +139,10 @@ export function useResource<T = any>(config: ResourceConfig) {
getListQueryOptions,
};
}
export function useResourceByName<T = any>(name: string) {
const config = React.useContext(ConfigContext);
const resourceConfig = config?.resources.find((r) => r.name === name);
return useResource<T>(resourceConfig);
}