quivr/frontend/lib/hooks/useAxios.ts
Zineb El Bachiri 9c8e0aa0e4
Feat/multiple brains frontend (#344)
* 🗑️ remove date input from fetch_user_id_from_credentials

* ♻️ refactor backend utils by splitting it into files

* 💡 comments for next actions to update /upload

* 🚚 move SupabaseProvider tp libs

* 🚚 move useAxios to hooks

* ♻️ refacto brain routes

* 🚨 update lintermfor paths

*  new brain context provider

*  new brain component in navbar

* 🚨 fix linter and async

* 🇸🇪 add feature flag for multiple-brains
2023-06-20 16:17:13 +02:00

34 lines
948 B
TypeScript

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