Files
heygermany-e2e-mrg5ilhk/apps/website/pikku/rpc.ts
e2e 40352e4179
Some checks failed
Main / Setup and Test (push) Has been cancelled
Main / Build Website (push) Has been cancelled
chore: heygermany customer project
2026-07-11 11:17:38 +02:00

25 lines
634 B
TypeScript

export async function invokeRPC<TOutput = unknown>(
rpcName: string,
data?: Record<string, unknown>
): Promise<TOutput> {
const response = await fetch(`/remote/rpc/${encodeURIComponent(rpcName)}`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ data }),
})
if (!response.ok) {
throw new Error((await response.text()) || `RPC ${rpcName} failed`)
}
if (response.status === 204) {
return undefined as TOutput
}
const text = await response.text()
return text ? JSON.parse(text) as TOutput : undefined as TOutput
}