|
| 1 | +import axios, { AxiosResponse } from "axios"; |
| 2 | +import { createContext, useContext } from "react"; |
| 3 | +import { useNavigate } from "react-router-dom"; |
| 4 | + |
| 5 | +// Define the API Context type |
| 6 | +interface ApiContextType { |
| 7 | + get: <T>(path: string) => Promise<T | null>; |
| 8 | + post: <T>(path: string, data: unknown) => Promise<T | null>; |
| 9 | +} |
| 10 | + |
| 11 | +// Create and export the context |
| 12 | +export const ApiContext = createContext<ApiContextType | undefined>(undefined); |
| 13 | + |
| 14 | +// Custom hook to use the API context |
| 15 | +export const useApi = () => { |
| 16 | + const context = useContext(ApiContext); |
| 17 | + if (context === undefined) { |
| 18 | + throw new Error("useApi must be used within an ApiProvider"); |
| 19 | + } |
| 20 | + return context; |
| 21 | +}; |
| 22 | + |
| 23 | +// Hook to provide API context values |
| 24 | +export const useApiProvider = () => { |
| 25 | + const navigate = useNavigate(); |
| 26 | + |
| 27 | + const get = async <T,>(path: string): Promise<T | null> => { |
| 28 | + try { |
| 29 | + const response: AxiosResponse<T> = await axios.get("/api/" + path, { |
| 30 | + timeout: 10000, // 10 seconds timeout |
| 31 | + }); |
| 32 | + return response.data; |
| 33 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 34 | + } catch (error: any) { |
| 35 | + if (error.response?.status === 401) { |
| 36 | + console.log("Unauthorized"); |
| 37 | + // Only redirect if we're not already on the login page |
| 38 | + if (!window.location.pathname.includes("/login")) { |
| 39 | + navigate("/login"); |
| 40 | + } |
| 41 | + return null; |
| 42 | + } |
| 43 | + if ( |
| 44 | + (error.response?.status >= 500 && error.response?.status < 600) || |
| 45 | + error.code === "ERR_CONNECTION_REFUSED" || |
| 46 | + error.code === "ERR_CONNECTION_TIMED_OUT" || |
| 47 | + error.code === "ERR_CONNECTION_RESET" || |
| 48 | + error.code === "ECONNABORTED" || |
| 49 | + error.code === "ERR_NETWORK" || |
| 50 | + error.message?.includes("timeout") || |
| 51 | + error.message === "Network Error" || |
| 52 | + (error.request && error.request.status === 0) |
| 53 | + ) { |
| 54 | + console.log("Unable to connect to server", { |
| 55 | + code: error.code, |
| 56 | + message: error.message, |
| 57 | + status: error.request?.status, |
| 58 | + }); |
| 59 | + navigate("/system-unavailable"); |
| 60 | + return null; |
| 61 | + } |
| 62 | + console.error("Error getting api " + path + ":", error); |
| 63 | + return null; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const post = async <T,>(path: string, data: unknown): Promise<T | null> => { |
| 68 | + try { |
| 69 | + const response: AxiosResponse<T> = await axios.post("/api/" + path, data, { |
| 70 | + timeout: 10000, // 10 seconds timeout |
| 71 | + }); |
| 72 | + return response.data; |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 74 | + } catch (error: any) { |
| 75 | + if (error.response?.status === 401) { |
| 76 | + console.log("Unauthorized"); |
| 77 | + // Only redirect if we're not already on the login page |
| 78 | + if (!window.location.pathname.includes("/login")) { |
| 79 | + navigate("/login"); |
| 80 | + } |
| 81 | + return null; |
| 82 | + } |
| 83 | + if ( |
| 84 | + (error.response?.status >= 500 && error.response?.status < 600) || |
| 85 | + error.code === "ERR_CONNECTION_REFUSED" || |
| 86 | + error.code === "ERR_CONNECTION_TIMED_OUT" || |
| 87 | + error.code === "ERR_CONNECTION_RESET" || |
| 88 | + error.code === "ECONNABORTED" || |
| 89 | + error.code === "ERR_NETWORK" || |
| 90 | + error.message?.includes("timeout") || |
| 91 | + error.message === "Network Error" || |
| 92 | + (error.request && error.request.status === 0) |
| 93 | + ) { |
| 94 | + console.log("Unable to connect to server", { |
| 95 | + code: error.code, |
| 96 | + message: error.message, |
| 97 | + status: error.request?.status, |
| 98 | + }); |
| 99 | + navigate("/system-unavailable"); |
| 100 | + return null; |
| 101 | + } |
| 102 | + console.error("Error posting to api " + path + ":", error); |
| 103 | + return null; |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + return { get, post }; |
| 108 | +}; |
0 commit comments