ManiCandles/src/components/useOptionalPrivateQuery.ts

22 lines
692 B
TypeScript

import { useQuery } from "@tanstack/react-query";
import { z } from "zod";
import { ApiError } from "@/lib/api";
import { usePlatform } from "@/lib/platform";
export function useOptionalPrivateQuery<T>(path: string, schema: z.ZodType<T>, missingCode: string) {
const { request, scope } = usePlatform();
return useQuery({
queryKey: ["private", scope, path],
enabled: Boolean(scope),
retry: false,
queryFn: async ({ signal }) => {
try {
return await request(path, schema, { signal });
} catch (error) {
if (error instanceof ApiError && error.status === 404 && error.code === missingCode) return null;
throw error;
}
},
});
}