From 7ad5b5696d93e125cac6d8a7c6a01e4e79f38f6e Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Tue, 10 Feb 2026 23:47:09 +0100 Subject: [PATCH] refactor: standardize env and directus logic using enhanced @mintel/next-utils --- lib/directus.ts | 19 ++++--------------- lib/env.ts | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/directus.ts b/lib/directus.ts index 2cbeaa81..23de415a 100644 --- a/lib/directus.ts +++ b/lib/directus.ts @@ -3,19 +3,8 @@ import { createMintelDirectusClient, ensureDirectusAuthenticated } from '@mintel import { config } from './config'; import { getServerAppServices } from './services/create-services.server'; -const { url, proxyPath, internalUrl } = config.directus; - -// Use internal URL if on server to bypass Gatekeeper/Auth -// Use proxy path in browser to stay on the same origin (CORS safe) -const effectiveUrl = - typeof window === 'undefined' - ? internalUrl || url - : typeof window !== 'undefined' - ? `${window.location.origin}${proxyPath}` - : proxyPath; - -// Initialize client using Mintel standards -const client = createMintelDirectusClient(effectiveUrl); +// Initialize client using Mintel standards (environment-aware) +const client = createMintelDirectusClient(); /** * Helper to determine if we should show detailed errors @@ -65,8 +54,8 @@ function mapDirectusProduct(item: any, locale: string): any { voltageTables: translation.voltage_tables || [], }, locale: locale, - // Use proxy URL for assets to avoid CORS and handle internal/external issues - data_sheet_url: item.data_sheet ? `${proxyPath}/assets/${item.data_sheet}` : null, + // Use standardized proxy path for assets to avoid CORS + data_sheet_url: item.data_sheet ? `/api/directus/assets/${item.data_sheet}` : null, categories: (item.categories_link || []) .map((c: any) => c.categories_id?.translations?.[0]?.name) .filter(Boolean), diff --git a/lib/env.ts b/lib/env.ts index b60e8cf9..ff9bc7e6 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -3,20 +3,20 @@ import { validateMintelEnv, mintelEnvSchema } from '@mintel/next-utils'; /** * Environment variable schema. - * Extends the default Mintel environment schema. + * Extends the default Mintel environment schema which already includes: + * - Directus (URL, TOKEN, INTERNAL_URL, etc.) + * - Mail (HOST, PORT, etc.) + * - Gotify + * - Logging + * - Analytics */ export const envSchema = z.object({ ...mintelEnvSchema, - // Project specific variables - NEXT_PUBLIC_TARGET: z.enum(['development', 'testing', 'staging', 'production']).optional(), - TARGET: z.enum(['development', 'testing', 'staging', 'production']).optional(), - // Directus (Extended from base) - INTERNAL_DIRECTUS_URL: z.string().url().optional(), - INFRA_DIRECTUS_URL: z.string().url().optional(), - INFRA_DIRECTUS_TOKEN: z.string().optional(), + // Project specific overrides or additions + AUTH_COOKIE_NAME: z.string().default('klz_gatekeeper_session'), - // Gatekeeper + // Gatekeeper specifics not in base GATEKEEPER_URL: z.string().url().default('http://gatekeeper:3000'), GATEKEEPER_BYPASS_ENABLED: z.preprocess( (val) => val === 'true' || val === true, @@ -26,6 +26,9 @@ export const envSchema = z.object({ (val) => val === 'true' || val === true, z.boolean().default(false), ), + + INFRA_DIRECTUS_URL: z.string().url().optional(), + INFRA_DIRECTUS_TOKEN: z.string().optional(), }); /**