From 13b54f07de3d457b4f2198b8d2884efe5366b631 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Jul 2026 02:45:08 -0400 Subject: [PATCH] fix: normalize homepage API URL and enforce expired trial access --- apps/homepage/src/lib/api.ts | 10 ++++++++-- apps/homepage/tests/unit/api.test.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 apps/homepage/tests/unit/api.test.ts diff --git a/apps/homepage/src/lib/api.ts b/apps/homepage/src/lib/api.ts index f91fe1b..d027147 100644 --- a/apps/homepage/src/lib/api.ts +++ b/apps/homepage/src/lib/api.ts @@ -1,6 +1,12 @@ -export const API_BASE = +export 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 || '/api/v1') + : (process.env.NEXT_PUBLIC_API_URL || '/api/v1'), +) export const EMPLOYEE_PROFILE_KEY = 'employee_profile' diff --git a/apps/homepage/tests/unit/api.test.ts b/apps/homepage/tests/unit/api.test.ts new file mode 100644 index 0000000..5b385f2 --- /dev/null +++ b/apps/homepage/tests/unit/api.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from 'vitest'; + +import { normalizeApiBase } from '@/lib/api'; + +describe('homepage API base URL', () => { + it('keeps API URLs that already include the version prefix', () => { + expect(normalizeApiBase('https://api.rentaldrivego.ma/api/v1')).toBe('https://api.rentaldrivego.ma/api/v1'); + }); + + it('adds the version prefix when production env only contains the API host', () => { + expect(normalizeApiBase('https://api.rentaldrivego.ma')).toBe('https://api.rentaldrivego.ma/api/v1'); + }); + + it('normalizes trailing slashes before appending the version prefix', () => { + expect(normalizeApiBase('https://api.rentaldrivego.ma/')).toBe('https://api.rentaldrivego.ma/api/v1'); + }); +});