chore: heygermany customer project
This commit is contained in:
27
apps/website/pikku/http.ts
Normal file
27
apps/website/pikku/http.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { PikkuFetch } from '@heygermany/sdk/pikku/pikku-fetch.gen.js'
|
||||
|
||||
// serverUrl resolution mirrors the green apps (germantax/perauset): the E2E
|
||||
// runtime override, then the build-time var, then a bare '/api' — the fabric
|
||||
// dispatcher routes every /api/* path to the API on the same host, so '/api' is
|
||||
// always correct on deploy without knowing the domain at build time. Never a
|
||||
// hardcoded localhost origin.
|
||||
const serverUrl = (): string => {
|
||||
if (typeof window !== 'undefined' && (window as Window & { __E2E_API_URL?: string }).__E2E_API_URL) {
|
||||
return (window as Window & { __E2E_API_URL?: string }).__E2E_API_URL as string
|
||||
}
|
||||
return import.meta.env.VITE_API_URL ?? import.meta.env.VITE_API_BASE_URL ?? '/api'
|
||||
}
|
||||
|
||||
let client: PikkuFetch | null = null
|
||||
|
||||
const pikku = (): PikkuFetch => {
|
||||
if (!client) {
|
||||
client = new PikkuFetch({
|
||||
serverUrl: serverUrl(),
|
||||
credentials: 'include',
|
||||
})
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
export { pikku }
|
||||
24
apps/website/pikku/rpc.ts
Normal file
24
apps/website/pikku/rpc.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user