26 lines
646 B
TypeScript
26 lines
646 B
TypeScript
import { useAuth } from "@/context/AuthContext";
|
|
|
|
export function useApi() {
|
|
const { token } = useAuth();
|
|
|
|
async function get(path: string) {
|
|
return fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}${path}`, {
|
|
headers: {
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
},
|
|
});
|
|
}
|
|
|
|
async function post(path: string, body: any) {
|
|
return fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}${path}`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
return { get, post };
|
|
} |