nomadro/src/modules/coffee/lib/api.ts
Eric 499f2d3e26 Ship coffeechat scheduling, notify PB access, and social/admin UI polish for .com.
Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 18:19:37 +08:00

86 lines
3.2 KiB
TypeScript

import type {
CoffeeBooking,
CoffeeEventType,
CoffeeHost,
CoffeeSchedule,
CoffeeSlot,
} from "./types";
import { requestJson } from "~/lib/client-request";
import { getGuestId } from '@social/lib/api';
import { coffeeDeviceKey } from './device-key';
const BASE = "/api/coffee/v1";
async function j<T>(path: string, init?: RequestInit): Promise<T> {
const headers = new Headers(init?.headers);
headers.set('X-Social-Guest-Id', getGuestId());
headers.set('X-Coffee-Key', coffeeDeviceKey(getGuestId()));
return requestJson<T>(`${BASE}${path}`, { ...init, headers });
}
export const coffeeApi = {
me: () => j<{ ok: true; host: CoffeeHost | null; schedule: CoffeeSchedule | null }>('/me'),
saveHost: (body: { name: string; bio: string; city: string; timezone: string }) => j<{ ok: true; host: CoffeeHost; schedule: CoffeeSchedule }>('/me', { method: 'PUT', body: JSON.stringify(body) }),
health: () => j<{ ok: true }>("/health"),
hosts: () => j<{ ok: true; hosts: CoffeeHost[] }>("/hosts"),
host: (username: string) =>
j<{ ok: true; host: CoffeeHost }>(`/hosts/${encodeURIComponent(username)}`),
eventType: (username: string, slug: string) =>
j<{ ok: true; event_type: CoffeeEventType }>(
`/hosts/${encodeURIComponent(username)}/event-types/${encodeURIComponent(slug)}`,
),
slots: (username: string, slug: string, day: string, signal?: AbortSignal) =>
j<{
ok: true;
day: string;
timezone: string;
slots: CoffeeSlot[];
event_type: { slug: string; title: string; length_minutes: number };
}>(
`/hosts/${encodeURIComponent(username)}/event-types/${encodeURIComponent(slug)}/slots?day=${encodeURIComponent(day)}`,
{ signal },
),
schedule: (username: string) =>
j<{ ok: true; schedule: CoffeeSchedule }>(
`/hosts/${encodeURIComponent(username)}/schedule`,
),
patchSchedule: (username: string, patch: Record<string, unknown>) =>
j<{ ok: true; schedule: CoffeeSchedule }>(
`/hosts/${encodeURIComponent(username)}/schedule`,
{ method: "PATCH", body: JSON.stringify(patch) },
),
upsertEventType: (username: string, body: Record<string, unknown>) =>
j<{ ok: true; event_type: CoffeeEventType }>(
`/hosts/${encodeURIComponent(username)}/event-types`,
{ method: "POST", body: JSON.stringify(body) },
),
book: (body: {
username: string;
event_slug: string;
start: string;
attendee_name: string;
attendee_email?: string;
attendee_device?: string;
location?: string;
notes?: string;
}) =>
j<{ ok: true; booking: CoffeeBooking }>("/bookings", {
method: "POST",
body: JSON.stringify(body),
}),
booking: (id: string, signal?: AbortSignal) =>
j<{ ok: true; booking: CoffeeBooking }>(`/bookings/${encodeURIComponent(id)}`, { signal }),
bookings: (host?: string, status?: string) => {
const q = new URLSearchParams();
if (host) q.set("host", host);
if (status) q.set("status", status);
const s = q.toString();
return j<{ ok: true; bookings: CoffeeBooking[] }>(`/bookings${s ? `?${s}` : ""}`);
},
cancel: (id: string) =>
j<{ ok: true; booking: CoffeeBooking }>(
`/bookings/${encodeURIComponent(id)}/cancel`,
{ method: "POST" },
),
};