mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 20:36:27 +00:00
Migrate client config and error handling to client-axios
This commit is contained in:
+10
-6
@@ -5,21 +5,25 @@ import {
|
|||||||
QueryClientProvider,
|
QueryClientProvider,
|
||||||
} from "@tanstack/react-query"
|
} from "@tanstack/react-query"
|
||||||
import { createRouter, RouterProvider } from "@tanstack/react-router"
|
import { createRouter, RouterProvider } from "@tanstack/react-router"
|
||||||
|
import { AxiosError } from "axios"
|
||||||
import { StrictMode } from "react"
|
import { StrictMode } from "react"
|
||||||
import ReactDOM from "react-dom/client"
|
import ReactDOM from "react-dom/client"
|
||||||
import { ApiError, OpenAPI } from "./client"
|
import { client } from "./client/client.gen"
|
||||||
import { ThemeProvider } from "./components/theme-provider"
|
import { ThemeProvider } from "./components/theme-provider"
|
||||||
import { Toaster } from "./components/ui/sonner"
|
import { Toaster } from "./components/ui/sonner"
|
||||||
import "./index.css"
|
import "./index.css"
|
||||||
import { routeTree } from "./routeTree.gen"
|
import { routeTree } from "./routeTree.gen"
|
||||||
|
|
||||||
OpenAPI.BASE = import.meta.env.VITE_API_URL
|
client.setConfig({
|
||||||
OpenAPI.TOKEN = async () => {
|
baseURL: import.meta.env.VITE_API_URL,
|
||||||
return localStorage.getItem("access_token") || ""
|
auth: () => localStorage.getItem("access_token") || "",
|
||||||
}
|
})
|
||||||
|
|
||||||
const handleApiError = (error: Error) => {
|
const handleApiError = (error: Error) => {
|
||||||
if (error instanceof ApiError && [401, 403].includes(error.status)) {
|
if (
|
||||||
|
error instanceof AxiosError &&
|
||||||
|
[401, 403].includes(error.response?.status ?? 0)
|
||||||
|
) {
|
||||||
localStorage.removeItem("access_token")
|
localStorage.removeItem("access_token")
|
||||||
window.location.href = "/login"
|
window.location.href = "/login"
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-11
@@ -1,22 +1,20 @@
|
|||||||
import { AxiosError } from "axios"
|
import { AxiosError } from "axios"
|
||||||
import type { ApiError } from "./client"
|
|
||||||
|
|
||||||
function extractErrorMessage(err: ApiError): string {
|
function extractErrorMessage(err: Error): string {
|
||||||
if (err instanceof AxiosError) {
|
if (err instanceof AxiosError) {
|
||||||
return err.message
|
const errDetail = (err.response?.data as any)?.detail
|
||||||
}
|
|
||||||
|
|
||||||
const errDetail = (err.body as any)?.detail
|
|
||||||
if (Array.isArray(errDetail) && errDetail.length > 0) {
|
if (Array.isArray(errDetail) && errDetail.length > 0) {
|
||||||
return errDetail[0].msg
|
return errDetail[0].msg
|
||||||
}
|
}
|
||||||
return errDetail || "Something went wrong."
|
if (typeof errDetail === "string") {
|
||||||
|
return errDetail
|
||||||
|
}
|
||||||
|
return err.message
|
||||||
|
}
|
||||||
|
return "Something went wrong."
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handleError = function (
|
export const handleError = function (this: (msg: string) => void, err: Error) {
|
||||||
this: (msg: string) => void,
|
|
||||||
err: ApiError,
|
|
||||||
) {
|
|
||||||
const errorMessage = extractErrorMessage(err)
|
const errorMessage = extractErrorMessage(err)
|
||||||
this(errorMessage)
|
this(errorMessage)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user