1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 07:04:22 +00:00
festa/hooks/useAxios.ts

30 lines
862 B
TypeScript
Raw Normal View History

2022-06-02 02:26:52 +00:00
import { AxiosInstance, AxiosRequestConfig, default as axios } from "axios";
2022-06-01 23:29:37 +00:00
import { useContext, useMemo } from "react";
import { LoginContext } from "../contexts/login";
import { FestaLoginData } from "../types/user";
2022-06-02 02:26:52 +00:00
export function useAxios<D>(config: AxiosRequestConfig<D> = {}, data?: FestaLoginData | null): AxiosInstance {
2022-06-01 23:29:37 +00:00
const loginContext = useContext(LoginContext)
let login = data || loginContext?.[0]
2022-06-02 02:26:52 +00:00
return useMemo(
() => {
console.debug(config, login)
const ax = axios.create({
...config,
headers: {
...(config.headers ?? {}),
Authorization: login ? `Bearer ${login.token}` : false,
},
})
console.debug(ax)
return ax
},
[config, login]
)
2022-06-01 23:29:37 +00:00
}