1export type Note = {
2 id: number;
3 body: string;
4 createdAt: string;
5};
6
7const base = (import.meta.env.VITE_API_URL ?? "").replace(/\/+$/, "");
8
9const url = (path: string) => `${base}${path}`;
10
11export const listNotes = async (): Promise<Note[]> => {
12 const response = await fetch(url("/notes"));
13 if (!response.ok) {
14 throw new Error(`GET /notes failed: ${response.status}`);
15 }
16 const payload = (await response.json()) as { notes: Note[] };
17 return payload.notes;
18};
19
20export const createNote = async (body: string): Promise<Note> => {
21 const response = await fetch(url("/notes"), {
22 method: "POST",
23 headers: { "content-type": "application/json" },
24 body: JSON.stringify({ body }),
25 });
26 if (!response.ok) {
27 throw new Error(`POST /notes failed: ${response.status}`);
28 }
29 const payload = (await response.json()) as { note: Note };
30 return payload.note;
31};