munch-ease-frontend/src/api/invitations.ts

28 lines
1.1 KiB
TypeScript
Raw Normal View History

import { fetchApi } from '@/api/client'
export type Household = { id: number; name: string; slug: string }
export async function acceptInvitation(token: string): Promise<Household> {
const resp = await fetchApi('/api/v1/invitations/accept', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token }),
})
if (!resp.ok) throw new Error(`${resp.status} ${resp.statusText || 'HTTP error'}`)
const data = await resp.json().catch(() => null)
const h = data?.household
if (!h || typeof h.id !== 'number' || typeof h.slug !== 'string' || typeof h.name !== 'string') {
throw new Error('Invalid invitation accept response')
}
return { id: h.id, name: h.name, slug: h.slug }
}
export async function sendInvitation(email: string): Promise<void> {
const resp = await fetchApi('/api/v1/invitations', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email }),
})
if (!resp.ok && resp.status !== 204) throw new Error(`${resp.status} ${resp.statusText || 'HTTP error'}`)
}