diff --git a/apps/calendar/apps/web/src/lib/api/client.ts b/apps/calendar/apps/web/src/lib/api/client.ts index 5ca7b5a7b..5b12c0f6a 100644 --- a/apps/calendar/apps/web/src/lib/api/client.ts +++ b/apps/calendar/apps/web/src/lib/api/client.ts @@ -6,24 +6,40 @@ * refreshes expired tokens before making requests. */ +import { browser } from '$app/environment'; import { env } from '$env/dynamic/public'; -import { createApiClient, type ApiResult } from '@manacore/shared-api-client'; +import { createApiClient, type ApiResult, type ApiClient } from '@manacore/shared-api-client'; import { authStore } from '$lib/stores/auth.svelte'; -const API_BASE = env.PUBLIC_BACKEND_URL || 'http://localhost:3014'; +// Use client URL in browser (injected by hooks.server.ts), SSR URL on server +function getApiBase(): string { + if (browser && typeof window !== 'undefined') { + const injectedUrl = (window as unknown as { __PUBLIC_BACKEND_URL__?: string }) + .__PUBLIC_BACKEND_URL__; + if (injectedUrl) return injectedUrl; + } + return env.PUBLIC_BACKEND_URL || 'http://localhost:3014'; +} /** - * Calendar API client instance + * Calendar API client instance (lazy initialized) * - Auto token handling via authStore.getValidToken() * - Consistent ApiResult response format */ -const api = createApiClient({ - baseUrl: API_BASE, - apiPrefix: '/api/v1', - getAuthToken: () => authStore.getValidToken(), - timeout: 30000, - debug: import.meta.env.DEV, -}); +let _api: ApiClient | null = null; + +function getApi(): ApiClient { + if (!_api) { + _api = createApiClient({ + baseUrl: getApiBase(), + apiPrefix: '/api/v1', + getAuthToken: () => authStore.getValidToken(), + timeout: 30000, + debug: import.meta.env.DEV, + }); + } + return _api; +} /** * Legacy fetchApi interface for backwards compatibility @@ -45,6 +61,7 @@ export async function fetchApi( options: FetchOptions = {} ): Promise> { const { method = 'GET', body, isFormData = false } = options; + const api = getApi(); if (isFormData && body instanceof FormData) { return api.upload(endpoint, body);