mirror of
https://github.com/Steffo99/festa.git
synced 2024-12-22 22:54:22 +00:00
25 lines
777 B
TypeScript
25 lines
777 B
TypeScript
import { AxiosInstance, AxiosRequestConfig, default as axios } from "axios";
|
|
import { useContext, useMemo } from "react";
|
|
import { LoginContext } from "../contexts/login";
|
|
import { FestaLoginData } from "../types/user";
|
|
|
|
export function useAxios<D>(config: AxiosRequestConfig<D> = {}, data?: FestaLoginData | null): AxiosInstance {
|
|
const loginContext = useContext(LoginContext)
|
|
|
|
let login = data || loginContext?.[0]
|
|
|
|
return useMemo(
|
|
() => {
|
|
const ax = axios.create({
|
|
...config,
|
|
headers: {
|
|
...(config.headers ?? {}),
|
|
Authorization: login ? `Bearer ${login.token}` : false,
|
|
},
|
|
})
|
|
|
|
return ax
|
|
},
|
|
[config, login]
|
|
)
|
|
}
|