mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
35 lines
939 B
TypeScript
35 lines
939 B
TypeScript
const API_BASE = "/api";
|
|||
|
|
|
||
|
|
export async function apiGet<T>(path: string): Promise<T> {
|
||
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
||
|
|
headers: { Authorization: `Bearer ${getToken()}` },
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`API error: ${res.status}`);
|
||
|
|
return res.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function apiPost<T>(path: string, body?: unknown): Promise<T> {
|
||
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
||
|
|
method: "POST",
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
Authorization: `Bearer ${getToken()}`,
|
||
|
|
},
|
||
|
|
body: body ? JSON.stringify(body) : undefined,
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`API error: ${res.status}`);
|
||
|
|
return res.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
function getToken(): string {
|
||
|
|
return localStorage.getItem("stirling-token") || "";
|
||
|
|
}
|
||
|
|
|
||
|
|
export function setToken(token: string) {
|
||
|
|
localStorage.setItem("stirling-token", token);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function clearToken() {
|
||
|
|
localStorage.removeItem("stirling-token");
|
||
|
|
}
|