Router slug-only + guard tests; tighten client credentials; typed invitations accept shape
This commit is contained in:
parent
aa510d7e23
commit
d3e2faf81d
1 changed files with 11 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { api } from '@/api/client'
|
import { api, fetchApi } from '@/api/client'
|
||||||
|
|
||||||
export type Household = { id: number; name: string; slug: string }
|
export type Household = { id: number; name: string; slug: string }
|
||||||
|
|
||||||
|
|
@ -17,10 +17,16 @@ function isHousehold(value: unknown): value is Household {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function acceptInvitation(token: string): Promise<Household> {
|
export async function acceptInvitation(token: string): Promise<Household> {
|
||||||
const { data, error, response } = await api.POST('/api/v1/invitations/accept', { body: { token } })
|
// OpenAPI requestBody type currently rejects fields; use raw fetch until spec is corrected.
|
||||||
if (!response.ok) throw new Error(`${response.status} ${response.statusText || 'HTTP error'}${error ? `: ${String(error)}` : ''}`)
|
const resp = await fetchApi('/api/v1/invitations/accept', {
|
||||||
const obj = data && typeof data === 'object' ? data as Record<string, unknown> : null
|
method: 'POST',
|
||||||
const h = obj && hasKey(obj, 'household') ? obj.household : undefined
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ token }),
|
||||||
|
})
|
||||||
|
if (!resp.ok) throw new Error(`${resp.status} ${resp.statusText || 'HTTP error'}`)
|
||||||
|
const obj: unknown = await resp.json().then((x: unknown) => x)
|
||||||
|
const wrapped = obj && typeof obj === 'object' ? (obj as Record<string, unknown>) : null
|
||||||
|
const h = wrapped && hasKey(wrapped, 'household') ? wrapped.household : undefined
|
||||||
if (!isHousehold(h)) throw new Error('Invalid invitation accept response')
|
if (!isHousehold(h)) throw new Error('Invalid invitation accept response')
|
||||||
return { id: h.id, name: h.name, slug: h.slug }
|
return { id: h.id, name: h.name, slug: h.slug }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue