Files
menulio/apps/mobile/src/lib/api.ts
T
2026-08-20 01:51:59 +03:00

37 lines
1.3 KiB
TypeScript

import { supabase } from "./supabase";
const API_URL = process.env.EXPO_PUBLIC_API_URL ?? "http://localhost:3001";
async function authHeaders(): Promise<Record<string, string>> {
const { data } = await supabase.auth.getSession();
const token = data.session?.access_token;
return {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
};
}
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
const headers = await authHeaders();
const res = await fetch(`${API_URL}${path}`, { ...options, headers });
if (!res.ok) {
const body = await res.json().catch(() => ({ message: res.statusText }));
throw new Error(body.message ?? `Request failed: ${res.status}`);
}
if (res.status === 204) return undefined as T;
return res.json() as Promise<T>;
}
export const api = {
get: <T>(path: string) => request<T>(path),
post: <T>(path: string, body?: unknown) =>
request<T>(path, { method: "POST", body: body ? JSON.stringify(body) : undefined }),
put: <T>(path: string, body?: unknown) =>
request<T>(path, { method: "PUT", body: body ? JSON.stringify(body) : undefined }),
patch: <T>(path: string, body: unknown) =>
request<T>(path, { method: "PATCH", body: JSON.stringify(body) }),
delete: <T>(path: string) => request<T>(path, { method: "DELETE" }),
};