directus
This commit is contained in:
@@ -57,6 +57,13 @@ function createConfig() {
|
||||
from: env.MAIL_FROM,
|
||||
recipients: env.MAIL_RECIPIENTS,
|
||||
},
|
||||
directus: {
|
||||
url: env.DIRECTUS_URL,
|
||||
adminEmail: env.DIRECTUS_ADMIN_EMAIL,
|
||||
password: env.DIRECTUS_ADMIN_PASSWORD,
|
||||
token: env.DIRECTUS_API_TOKEN,
|
||||
proxyPath: '/cms',
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
|
||||
@@ -86,6 +93,7 @@ export const config = {
|
||||
get cache() { return getConfig().cache; },
|
||||
get logging() { return getConfig().logging; },
|
||||
get mail() { return getConfig().mail; },
|
||||
get directus() { return getConfig().directus; },
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -124,5 +132,11 @@ export function getMaskedConfig() {
|
||||
from: c.mail.from,
|
||||
recipients: c.mail.recipients,
|
||||
},
|
||||
directus: {
|
||||
url: c.directus.url,
|
||||
adminEmail: mask(c.directus.adminEmail),
|
||||
password: mask(c.directus.password),
|
||||
token: mask(c.directus.token),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
import { createDirectus, rest, authentication, readItems, readItem } from '@directus/sdk';
|
||||
import { createDirectus, rest, authentication, readItems } from '@directus/sdk';
|
||||
import { config } from './config';
|
||||
|
||||
const DIRECTUS_URL = process.env.DIRECTUS_URL || 'http://localhost:8055';
|
||||
const DIRECTUS_EMAIL = process.env.DIRECTUS_ADMIN_EMAIL;
|
||||
const DIRECTUS_PASSWORD = process.env.DIRECTUS_ADMIN_PASSWORD;
|
||||
const DIRECTUS_TOKEN = process.env.DIRECTUS_API_TOKEN;
|
||||
const { url, adminEmail, password, token, proxyPath } = config.directus;
|
||||
|
||||
const client = createDirectus(DIRECTUS_URL)
|
||||
const client = createDirectus(url)
|
||||
.with(rest())
|
||||
.with(authentication());
|
||||
|
||||
export async function ensureAuthenticated() {
|
||||
if (DIRECTUS_TOKEN) {
|
||||
client.setToken(DIRECTUS_TOKEN);
|
||||
if (token) {
|
||||
client.setToken(token);
|
||||
return;
|
||||
}
|
||||
if (DIRECTUS_EMAIL && DIRECTUS_PASSWORD) {
|
||||
if (adminEmail && password) {
|
||||
try {
|
||||
await client.login(DIRECTUS_EMAIL, DIRECTUS_PASSWORD);
|
||||
await client.login(adminEmail, password);
|
||||
} catch (e) {
|
||||
console.error("Failed to authenticate with Directus:", e);
|
||||
}
|
||||
@@ -41,7 +39,8 @@ function mapDirectusProduct(item: any, locale: string): any {
|
||||
voltageTables: translation.voltage_tables || []
|
||||
},
|
||||
locale: locale,
|
||||
data_sheet_url: item.data_sheet ? `${DIRECTUS_URL}/assets/${item.data_sheet}` : null,
|
||||
// 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,
|
||||
categories: (item.categories_link || []).map((c: any) => c.categories_id?.translations?.[0]?.name).filter(Boolean)
|
||||
};
|
||||
}
|
||||
|
||||
18
lib/env.ts
18
lib/env.ts
@@ -11,17 +11,17 @@ const preprocessEmptyString = (val: unknown) => (val === '' ? undefined : val);
|
||||
export const envSchema = z.object({
|
||||
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
|
||||
NEXT_PUBLIC_BASE_URL: z.preprocess(preprocessEmptyString, z.string().url()),
|
||||
|
||||
|
||||
// Analytics
|
||||
NEXT_PUBLIC_UMAMI_WEBSITE_ID: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
NEXT_PUBLIC_UMAMI_SCRIPT_URL: z.preprocess(preprocessEmptyString, z.string().url().default('https://analytics.infra.mintel.me/script.js')),
|
||||
|
||||
|
||||
// Error Tracking
|
||||
SENTRY_DSN: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
|
||||
|
||||
// Logging
|
||||
LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
|
||||
|
||||
|
||||
// Mail
|
||||
MAIL_HOST: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
MAIL_PORT: z.preprocess(preprocessEmptyString, z.coerce.number().default(587)),
|
||||
@@ -32,6 +32,12 @@ export const envSchema = z.object({
|
||||
(val) => (typeof val === 'string' ? val.split(',').filter(Boolean) : val),
|
||||
z.array(z.string()).default([])
|
||||
),
|
||||
|
||||
// Directus
|
||||
DIRECTUS_URL: z.preprocess(preprocessEmptyString, z.string().url().default('http://localhost:8055')),
|
||||
DIRECTUS_ADMIN_EMAIL: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
DIRECTUS_ADMIN_PASSWORD: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
DIRECTUS_API_TOKEN: z.preprocess(preprocessEmptyString, z.string().optional()),
|
||||
});
|
||||
|
||||
export type Env = z.infer<typeof envSchema>;
|
||||
@@ -54,5 +60,9 @@ export function getRawEnv() {
|
||||
MAIL_PASSWORD: process.env.MAIL_PASSWORD,
|
||||
MAIL_FROM: process.env.MAIL_FROM,
|
||||
MAIL_RECIPIENTS: process.env.MAIL_RECIPIENTS,
|
||||
DIRECTUS_URL: process.env.DIRECTUS_URL,
|
||||
DIRECTUS_ADMIN_EMAIL: process.env.DIRECTUS_ADMIN_EMAIL,
|
||||
DIRECTUS_ADMIN_PASSWORD: process.env.DIRECTUS_ADMIN_PASSWORD,
|
||||
DIRECTUS_API_TOKEN: process.env.DIRECTUS_API_TOKEN,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user