quivr/frontend/lib/hooks/useAxios.ts

34 lines
954 B
TypeScript
Raw Normal View History

import axios, { AxiosError, AxiosInstance } from "axios";
import { useBrainConfig } from "@/lib/context/BrainConfigProvider/hooks/useBrainConfig";
import { useSupabase } from "@/lib/context/SupabaseProvider";
const axiosInstance = axios.create({
baseURL: `${process.env.NEXT_PUBLIC_BACKEND_URL ?? ""}`,
});
export const useAxios = (): { axiosInstance: AxiosInstance } => {
const { session } = useSupabase();
const {
2023-06-09 19:49:47 +03:00
config: { backendUrl, openAiKey },
} = useBrainConfig();
axiosInstance.interceptors.request.clear();
axiosInstance.interceptors.request.use(
(config) => {
config.headers["Authorization"] = `Bearer ${session?.access_token ?? ""}`;
2023-06-09 19:49:47 +03:00
config.headers["Openai-Api-Key"] = openAiKey;
config.baseURL = backendUrl ?? config.baseURL;
return config;
},
(error: AxiosError) => {
console.error({ error });
void Promise.reject(error);
}
);
return {
axiosInstance,
};
};