From 7b29e39e9fb1bff382245eaaeaecdecaa1b4872a Mon Sep 17 00:00:00 2001 From: belisards Date: Sun, 3 May 2026 16:26:09 -0300 Subject: [PATCH] feat(client): swap auth client to session API; add guestsApi --- lib/api.ts | 74 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/lib/api.ts b/lib/api.ts index d43f63f..1b34030 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -23,14 +23,23 @@ async function handleResponse(response: Response): Promise { // Auth API export const authApi = { - async login(username: string, password: string) { - const response = await fetch(`${API_BASE_URL}/auth/login`, { + async session(payload: { adm?: string; usr?: string }) { + const response = await fetch(`${API_BASE_URL}/auth/session`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', - body: JSON.stringify({ username, password }), + body: JSON.stringify(payload), }); - return handleResponse<{ accessToken: string; refreshToken: string }>(response); + return handleResponse<{ success: true; role: 'admin' | 'guest'; guestId: string | null }>(response); + }, + + async whoami() { + const response = await fetch(`${API_BASE_URL}/auth/session`, { credentials: 'include' }); + return handleResponse< + | { role: 'admin'; guest: { id: string; name: string } | null } + | { role: 'guest'; guest: { id: string; name: string } } + | { role: 'none' } + >(response); }, async logout() { @@ -38,22 +47,52 @@ export const authApi = { method: 'POST', credentials: 'include', }); - return handleResponse(response); + return handleResponse<{ success: true }>(response); + }, +}; + +export interface Guest { + id: string; + name: string; + createdAt: string; + updatedAt: string; +} + +export const guestsApi = { + async list() { + const response = await fetch(`${API_BASE_URL}/admin/guests`, { credentials: 'include' }); + const data = await handleResponse<{ success: true; guests: Guest[] }>(response); + return data.guests; }, - async refresh() { - const response = await fetch(`${API_BASE_URL}/auth/refresh`, { + async create(name: string) { + const response = await fetch(`${API_BASE_URL}/admin/guests`, { method: 'POST', + headers: { 'Content-Type': 'application/json' }, credentials: 'include', + body: JSON.stringify({ name }), }); - return handleResponse<{ accessToken: string }>(response); + const data = await handleResponse<{ success: true; guest: Guest }>(response); + return data.guest; }, - async me() { - const response = await fetch(`${API_BASE_URL}/auth/me`, { + async rename(id: string, name: string) { + const response = await fetch(`${API_BASE_URL}/admin/guests/${id}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ name }), + }); + const data = await handleResponse<{ success: true; guest: Guest }>(response); + return data.guest; + }, + + async delete(id: string) { + const response = await fetch(`${API_BASE_URL}/admin/guests/${id}`, { + method: 'DELETE', credentials: 'include', }); - return handleResponse<{ username: string }>(response); + return handleResponse<{ success: true }>(response); }, }; @@ -246,23 +285,24 @@ export const itemsApi = { // Claiming API (public) export const claimingApi = { - async claim(itemId: string, name?: string, note?: string) { + async claim(itemId: string, opts: { quantity?: number; note?: string } = {}) { const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/claim`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', - body: JSON.stringify({ name, note }), + body: JSON.stringify({ quantity: opts.quantity ?? 1, note: opts.note }), }); - return handleResponse<{ claimToken: string; message: string }>(response); + return handleResponse<{ success: true; item: Item }>(response); }, - async unclaim(itemId: string) { + async unclaim(itemId: string, opts: { guestId?: string } = {}) { const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/unclaim`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', + body: JSON.stringify(opts.guestId ? { guestId: opts.guestId } : {}), }); - return handleResponse<{ success: boolean; message: string }>(response); + return handleResponse<{ success: true }>(response); }, }; @@ -293,8 +333,6 @@ export const scrapingApi = { export interface Settings { siteTitle: string; homepageSubtext: string; - passwordLockEnabled?: boolean; - passwordLock?: string; } export const settingsApi = {