fixing vault. allow users to delete their builds or update
This commit is contained in:
+55
-18
@@ -1,26 +1,63 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { useAuth } from "@/context/AuthContext";
|
||||
|
||||
const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:8080";
|
||||
|
||||
type JsonValue = any;
|
||||
|
||||
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}` : "",
|
||||
},
|
||||
});
|
||||
}
|
||||
return useMemo(() => {
|
||||
function authHeaders(extra?: HeadersInit): HeadersInit {
|
||||
const base: Record<string, string> = {};
|
||||
if (token) base.Authorization = `Bearer ${token}`;
|
||||
return { ...base, ...(extra as any) };
|
||||
}
|
||||
|
||||
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),
|
||||
});
|
||||
}
|
||||
async function get(path: string, init?: RequestInit) {
|
||||
return fetch(`${API_BASE_URL}${path}`, {
|
||||
...init,
|
||||
headers: authHeaders(init?.headers),
|
||||
});
|
||||
}
|
||||
|
||||
return { get, post };
|
||||
async function post(path: string, body: JsonValue, init?: RequestInit) {
|
||||
return fetch(`${API_BASE_URL}${path}`, {
|
||||
method: "POST",
|
||||
...init,
|
||||
headers: authHeaders({
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers as any),
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
async function put(path: string, body: JsonValue, init?: RequestInit) {
|
||||
return fetch(`${API_BASE_URL}${path}`, {
|
||||
method: "PUT",
|
||||
...init,
|
||||
headers: authHeaders({
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers as any),
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
async function del(path: string, init?: RequestInit) {
|
||||
return fetch(`${API_BASE_URL}${path}`, {
|
||||
method: "DELETE",
|
||||
...init,
|
||||
headers: authHeaders(init?.headers),
|
||||
});
|
||||
}
|
||||
|
||||
// ✅ stable reference unless token changes
|
||||
return { get, post, put, del };
|
||||
}, [token]);
|
||||
}
|
||||
Reference in New Issue
Block a user