import React, { createContext, useContext, ReactNode } from "react"; interface ApiContextType { baseUrl: string; wsBaseUrl: string; fetchWithHeaders: (url: string, options?: RequestInit) => Promise; } const ApiContext = createContext(undefined); const DEFAULT_LOCALHOST = "http://localhost:8000"; const DEFAULT_WS_LOCALHOST = "ws://localhost:8000"; interface ApiProviderProps { children: ReactNode; } export const ApiProvider: React.FC = ({ children }) => { const baseUrl = DEFAULT_LOCALHOST; const wsBaseUrl = DEFAULT_WS_LOCALHOST; // Enhanced fetch function that automatically includes necessary headers const fetchWithHeaders = async ( url: string, options: RequestInit = {} ): Promise => { const enhancedOptions: RequestInit = { ...options, headers: { "Content-Type": "application/json", ...options.headers, }, }; return fetch(url, enhancedOptions); }; return ( {children} ); }; export const useApi = (): ApiContextType => { const context = useContext(ApiContext); if (context === undefined) { throw new Error("useApi must be used within an ApiProvider"); } return context; };