refactor: standardize env and directus logic using enhanced @mintel/next-utils
This commit is contained in:
@@ -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),
|
||||
|
||||
21
lib/env.ts
21
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(),
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user