mirror of
https://github.com/SamyRai/tercul-frontend.git
synced 2025-12-27 04:51:34 +00:00
Sets up the project with initial files, components, routes, and UI elements. Replit-Commit-Author: Agent Replit-Commit-Session-Id: cbacfb18-842a-4116-a907-18c0105ad8ec Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/39b5c689-6e8a-4d5a-9792-69cc81a56534/affc56b0-365e-4ece-9cba-9e70bbbf0893.jpg
58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { QueryClient, QueryFunction } from "@tanstack/react-query";
|
|
|
|
async function throwIfResNotOk(res: Response) {
|
|
if (!res.ok) {
|
|
const text = (await res.text()) || res.statusText;
|
|
throw new Error(`${res.status}: ${text}`);
|
|
}
|
|
}
|
|
|
|
export async function apiRequest(
|
|
method: string,
|
|
url: string,
|
|
data?: unknown | undefined,
|
|
): Promise<Response> {
|
|
const res = await fetch(url, {
|
|
method,
|
|
headers: data ? { "Content-Type": "application/json" } : {},
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
credentials: "include",
|
|
});
|
|
|
|
await throwIfResNotOk(res);
|
|
return res;
|
|
}
|
|
|
|
type UnauthorizedBehavior = "returnNull" | "throw";
|
|
export const getQueryFn: <T>(options: {
|
|
on401: UnauthorizedBehavior;
|
|
}) => QueryFunction<T> =
|
|
({ on401: unauthorizedBehavior }) =>
|
|
async ({ queryKey }) => {
|
|
const res = await fetch(queryKey[0] as string, {
|
|
credentials: "include",
|
|
});
|
|
|
|
if (unauthorizedBehavior === "returnNull" && res.status === 401) {
|
|
return null;
|
|
}
|
|
|
|
await throwIfResNotOk(res);
|
|
return await res.json();
|
|
};
|
|
|
|
export const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
queryFn: getQueryFn({ on401: "throw" }),
|
|
refetchInterval: false,
|
|
refetchOnWindowFocus: false,
|
|
staleTime: Infinity,
|
|
retry: false,
|
|
},
|
|
mutations: {
|
|
retry: false,
|
|
},
|
|
},
|
|
});
|