diff --git a/apps/dashboard/src/lib/api.test.ts b/apps/dashboard/src/lib/api.test.ts index 6d17886..cc972f2 100644 --- a/apps/dashboard/src/lib/api.test.ts +++ b/apps/dashboard/src/lib/api.test.ts @@ -15,6 +15,7 @@ afterEach(() => { vi.restoreAllMocks() Reflect.deleteProperty(globalThis, 'window') Reflect.deleteProperty(globalThis, 'fetch') + delete process.env.NEXT_PUBLIC_API_URL vi.resetModules() }) @@ -38,6 +39,21 @@ describe('dashboard apiFetch', () => { })) }) + it('normalizes a public API origin to the versioned API base', async () => { + installBrowser() + process.env.NEXT_PUBLIC_API_URL = 'https://api.rentaldrivego.ma' + const fetchMock = vi.fn(async () => ({ + ok: true, + json: async () => ({ data: { ok: true } }), + })) + Object.defineProperty(globalThis, 'fetch', { configurable: true, value: fetchMock }) + + const { apiFetch } = await import('./api') + await apiFetch('/auth/account/start', { method: 'POST', body: JSON.stringify({}) }) + + expect(fetchMock).toHaveBeenCalledWith('https://api.rentaldrivego.ma/api/v1/auth/account/start', expect.any(Object)) + }) + it('does not force JSON content type for FormData payloads', async () => { installBrowser() const fetchMock = vi.fn(async () => ({ ok: true, json: async () => ({ data: { uploaded: true } }) })) diff --git a/apps/dashboard/src/lib/api.ts b/apps/dashboard/src/lib/api.ts index 1aab9d9..e0aeda8 100644 --- a/apps/dashboard/src/lib/api.ts +++ b/apps/dashboard/src/lib/api.ts @@ -1,7 +1,13 @@ -export const API_BASE = +function normalizeApiBase(value: string): string { + const base = value.replace(/\/+$/, '') + return /\/api\/v1$/.test(base) ? base : `${base}/api/v1` +} + +export const API_BASE = normalizeApiBase( typeof window === 'undefined' ? (process.env.API_INTERNAL_URL || 'http://localhost:4000/api/v1') - : (process.env.NEXT_PUBLIC_API_URL || '/dashboard/api/v1') + : (process.env.NEXT_PUBLIC_API_URL || '/dashboard/api/v1'), +) export const EMPLOYEE_PROFILE_KEY = 'employee_profile'