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