| import { t } from "@lingui/macro"; |
| import type { ErrorMessage } from "@reactive-resume/utils"; |
| import { deepSearchAndParseDates } from "@reactive-resume/utils"; |
| import _axios from "axios"; |
| import createAuthRefreshInterceptor from "axios-auth-refresh"; |
| import { redirect } from "react-router"; |
|
|
| import { refreshToken } from "@/client/services/auth"; |
|
|
| import { USER_KEY } from "../constants/query-keys"; |
| import { toast } from "../hooks/use-toast"; |
| import { translateError } from "../services/errors/translate-error"; |
| import { queryClient } from "./query-client"; |
|
|
| export const axios = _axios.create({ baseURL: "/api", withCredentials: true }); |
|
|
| |
| axios.interceptors.response.use( |
| (response) => { |
| const transformedResponse = deepSearchAndParseDates(response.data, ["createdAt", "updatedAt"]); |
| return { ...response, data: transformedResponse }; |
| }, |
| (error) => { |
| const message = error.response?.data.message as ErrorMessage; |
| const description = translateError(message); |
|
|
| if (description) { |
| toast({ |
| variant: "error", |
| title: t`Oops, the server returned an error.`, |
| description, |
| }); |
| } |
|
|
| return Promise.reject(new Error(message)); |
| }, |
| ); |
|
|
| |
| |
| const axiosForRefresh = _axios.create({ baseURL: "/api", withCredentials: true }); |
|
|
| |
| const handleAuthError = () => refreshToken(axiosForRefresh); |
|
|
| |
| const handleRefreshError = async () => { |
| await queryClient.invalidateQueries({ queryKey: USER_KEY }); |
| redirect("/auth/login"); |
| }; |
|
|
| |
| createAuthRefreshInterceptor(axios, handleAuthError, { statusCodes: [401, 403] }); |
| createAuthRefreshInterceptor(axiosForRefresh, handleRefreshError); |
|
|