25 lines
1.4 KiB
TypeScript
25 lines
1.4 KiB
TypeScript
// Endpoints come from env, never hardcoded.
|
|
//
|
|
// In local dev VITE_API_URL is set at build time and Vite replaces the
|
|
// import.meta.env reference inline. In deployed/sandbox bundles VITE_API_URL is
|
|
// a runtime Worker binding — invisible to Vite at build time — so it's undefined
|
|
// in the bundle. We fall back to the current origin + /api, which is correct
|
|
// because the dispatcher routes every /api/* path to the API on the same host.
|
|
export function apiUrl(): string {
|
|
if (typeof window !== 'undefined' && (window as any).__E2E_API_URL) {
|
|
// E2E talks directly to the backend (no vite proxy). Better Auth is mounted
|
|
// at /api/auth there, while pikku routes live at the root — so the auth
|
|
// client must add the /api prefix that __E2E_API_URL (bare host) lacks.
|
|
return `${(window as any).__E2E_API_URL as string}/api`
|
|
}
|
|
if (import.meta.env.SSR) {
|
|
// SSR context: the auth client is only consumed in the browser. Return the
|
|
// build-time var when available or a placeholder so SSR never throws.
|
|
return import.meta.env.VITE_API_URL ?? '/__api'
|
|
}
|
|
// Client: build-time var (local dev) or derive from the current origin.
|
|
// Better Auth's client requires an absolute base URL, so never return a bare
|
|
// relative '/api' here — '/api/auth' throws "Invalid base URL".
|
|
return import.meta.env.VITE_API_URL ?? `${window.location.origin}/api`
|
|
}
|