diff --git a/src/auth/client.ts b/src/auth/client.ts index 6447cd9..130a43a 100644 --- a/src/auth/client.ts +++ b/src/auth/client.ts @@ -25,8 +25,23 @@ export type Schema = {}; type DirectusClient = ReturnType; +/** + * Resolve a (possibly relative) URL against the current page origin. + * + * Runtime config supports relative paths like `/api` because the SPA always + * runs same-origin to its backends. The Directus SDK, however, calls + * `new URL(...)` internally and requires an absolute URL — so we resolve + * before handing it over. + * + * - `'/api'` → `'http://localhost:5173/api'` (in dev) + * - `'https://api.example.com'` → `'https://api.example.com/'` (passes through) + */ +function toAbsoluteUrl(maybeRelative: string): string { + return new URL(maybeRelative, window.location.origin).toString(); +} + function buildClient(directusUrl: string) { - return createDirectus(directusUrl) + return createDirectus(toAbsoluteUrl(directusUrl)) .with(authentication('cookie', { credentials: 'include', autoRefresh: true })) .with(rest({ credentials: 'include' })); }