fix login issue in production
Build & Push / Pipeline Tests (push) Successful in 1m53s
Test / Type Check (all packages) (push) Successful in 55s
Build & Push / Build & Push Docker Image (push) Successful in 4m32s
Test / API Unit Tests (push) Successful in 1m17s
Test / Homepage Unit Tests (push) Successful in 50s
Test / Carplace Unit Tests (push) Successful in 43s
Test / Admin Unit Tests (push) Successful in 44s
Test / Dashboard Unit Tests (push) Successful in 45s
Test / API Integration Tests (push) Successful in 1m12s
Build & Push / Pipeline Tests (push) Successful in 1m53s
Test / Type Check (all packages) (push) Successful in 55s
Build & Push / Build & Push Docker Image (push) Successful in 4m32s
Test / API Unit Tests (push) Successful in 1m17s
Test / Homepage Unit Tests (push) Successful in 50s
Test / Carplace Unit Tests (push) Successful in 43s
Test / Admin Unit Tests (push) Successful in 44s
Test / Dashboard Unit Tests (push) Successful in 45s
Test / API Integration Tests (push) Successful in 1m12s
This commit is contained in:
@@ -50,6 +50,8 @@ const nextConfig: NextConfig = {
|
||||
},
|
||||
|
||||
async rewrites() {
|
||||
const apiOrigin =
|
||||
(process.env.API_INTERNAL_URL ?? process.env.API_URL ?? 'http://api:4000').replace(/\/api\/v1\/?$/, '');
|
||||
const dashboardOrigin =
|
||||
process.env.DASHBOARD_INTERNAL_URL ?? 'http://dashboard:3001';
|
||||
const adminOrigin =
|
||||
@@ -58,6 +60,10 @@ const nextConfig: NextConfig = {
|
||||
process.env.CARPLACE_INTERNAL_URL ?? 'http://carplace:3004';
|
||||
|
||||
return [
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${apiOrigin}/api/:path*`,
|
||||
},
|
||||
{
|
||||
source: '/carplace',
|
||||
destination: `${carplaceOrigin}`,
|
||||
|
||||
@@ -3,10 +3,18 @@ export function normalizeApiBase(value: string): string {
|
||||
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'),
|
||||
)
|
||||
function resolveApiBase(): string {
|
||||
if (typeof window === 'undefined') {
|
||||
return normalizeApiBase(process.env.API_INTERNAL_URL || 'http://localhost:4000/api/v1')
|
||||
}
|
||||
|
||||
if (process.env.NEXT_PUBLIC_HOMEPAGE_DIRECT_API === 'true' && process.env.NEXT_PUBLIC_API_URL) {
|
||||
return normalizeApiBase(process.env.NEXT_PUBLIC_API_URL)
|
||||
}
|
||||
|
||||
return '/api/v1'
|
||||
}
|
||||
|
||||
export const API_BASE = resolveApiBase()
|
||||
|
||||
export const EMPLOYEE_PROFILE_KEY = 'employee_profile'
|
||||
|
||||
@@ -1,17 +1,68 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { normalizeApiBase } from '@/lib/api';
|
||||
function installBrowser() {
|
||||
Object.defineProperty(globalThis, 'window', {
|
||||
configurable: true,
|
||||
value: {
|
||||
location: {
|
||||
origin: 'https://rentaldrivego.ma',
|
||||
hostname: 'rentaldrivego.ma',
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(globalThis, 'window');
|
||||
delete process.env.API_INTERNAL_URL;
|
||||
delete process.env.NEXT_PUBLIC_API_URL;
|
||||
delete process.env.NEXT_PUBLIC_HOMEPAGE_DIRECT_API;
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('homepage API base URL', () => {
|
||||
it('keeps API URLs that already include the version prefix', () => {
|
||||
it('keeps API URLs that already include the version prefix', async () => {
|
||||
const { normalizeApiBase } = await import('@/lib/api');
|
||||
|
||||
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', () => {
|
||||
it('adds the version prefix when production env only contains the API host', async () => {
|
||||
const { normalizeApiBase } = await import('@/lib/api');
|
||||
|
||||
expect(normalizeApiBase('https://api.rentaldrivego.ma')).toBe('https://api.rentaldrivego.ma/api/v1');
|
||||
});
|
||||
|
||||
it('normalizes trailing slashes before appending the version prefix', () => {
|
||||
it('normalizes trailing slashes before appending the version prefix', async () => {
|
||||
const { normalizeApiBase } = await import('@/lib/api');
|
||||
|
||||
expect(normalizeApiBase('https://api.rentaldrivego.ma/')).toBe('https://api.rentaldrivego.ma/api/v1');
|
||||
});
|
||||
|
||||
it('uses the same-origin API proxy in browsers by default', async () => {
|
||||
installBrowser();
|
||||
process.env.NEXT_PUBLIC_API_URL = 'https://api.rentaldrivego.ma/api/v1';
|
||||
|
||||
const { API_BASE } = await import('@/lib/api');
|
||||
|
||||
expect(API_BASE).toBe('/api/v1');
|
||||
});
|
||||
|
||||
it('allows direct browser API calls only when explicitly enabled', async () => {
|
||||
installBrowser();
|
||||
process.env.NEXT_PUBLIC_API_URL = 'https://api.rentaldrivego.ma';
|
||||
process.env.NEXT_PUBLIC_HOMEPAGE_DIRECT_API = 'true';
|
||||
|
||||
const { API_BASE } = await import('@/lib/api');
|
||||
|
||||
expect(API_BASE).toBe('https://api.rentaldrivego.ma/api/v1');
|
||||
});
|
||||
|
||||
it('uses the internal API URL on the server', async () => {
|
||||
process.env.API_INTERNAL_URL = 'http://api:4000/api/v1';
|
||||
|
||||
const { API_BASE } = await import('@/lib/api');
|
||||
|
||||
expect(API_BASE).toBe('http://api:4000/api/v1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import nextConfig from '../../next.config';
|
||||
|
||||
describe('homepage next config', () => {
|
||||
it('proxies same-origin API requests to the API service before app rewrites', async () => {
|
||||
const rewrites = await nextConfig.rewrites?.();
|
||||
|
||||
expect(rewrites).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: 'http://api:4000/api/:path*',
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user