Files
fast_api_template/frontend/src/main.tsx
T

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from "@tanstack/react-query"
2024-03-17 17:28:45 +01:00
import { RouterProvider, createRouter } from "@tanstack/react-router"
import React, { StrictMode } from "react"
2024-03-17 17:28:45 +01:00
import ReactDOM from "react-dom/client"
import { routeTree } from "./routeTree.gen"
2024-01-23 11:48:56 -05:00
import { ApiError, OpenAPI } from "./client"
2025-02-17 19:33:00 +00:00
import { CustomProvider } from "./components/ui/provider"
2024-01-23 11:48:56 -05:00
2024-03-08 14:58:36 +01:00
OpenAPI.BASE = import.meta.env.VITE_API_URL
2024-01-23 11:48:56 -05:00
OpenAPI.TOKEN = async () => {
2024-03-17 17:28:45 +01:00
return localStorage.getItem("access_token") || ""
2024-01-23 11:48:56 -05:00
}
const handleApiError = (error: Error) => {
if (error instanceof ApiError && [401, 403].includes(error.status)) {
localStorage.removeItem("access_token")
window.location.href = "/login"
}
}
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: handleApiError,
}),
mutationCache: new MutationCache({
onError: handleApiError,
}),
})
const router = createRouter({ routeTree })
2024-03-17 17:28:45 +01:00
declare module "@tanstack/react-router" {
interface Register {
router: typeof router
}
}
2024-03-17 17:28:45 +01:00
ReactDOM.createRoot(document.getElementById("root")!).render(
<StrictMode>
2025-02-17 19:33:00 +00:00
<CustomProvider>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
2025-02-17 19:33:00 +00:00
</CustomProvider>
2024-03-08 14:58:36 +01:00
</StrictMode>,
)