chore: heygermany customer project
This commit is contained in:
39
apps/website/pikku/http.ts
Normal file
39
apps/website/pikku/http.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { PikkuFetch } from '@heygermany/sdk/pikku/pikku-fetch.gen.js'
|
||||
|
||||
const viteEnv = (import.meta as ImportMeta & {
|
||||
env?: Record<string, string | undefined>
|
||||
}).env
|
||||
|
||||
const getBrowserApiBaseUrl = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return ''
|
||||
}
|
||||
|
||||
const { hostname, origin } = window.location
|
||||
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return 'http://localhost:3000'
|
||||
}
|
||||
|
||||
return origin
|
||||
}
|
||||
|
||||
const getApiBaseUrl = () =>
|
||||
viteEnv?.VITE_API_BASE_URL ||
|
||||
process.env.VITE_API_BASE_URL ||
|
||||
process.env.API_URL ||
|
||||
getBrowserApiBaseUrl()
|
||||
|
||||
let client: PikkuFetch | null = null
|
||||
|
||||
const pikku = (): PikkuFetch => {
|
||||
if (!client) {
|
||||
client = new PikkuFetch({
|
||||
serverUrl: getApiBaseUrl(),
|
||||
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