refactor: add FRONTEND_API_KEY to runtime config and update API key retrieval logic

This commit is contained in:
Tudor Stanciu 2025-10-04 18:51:40 +03:00
parent 64b13b3df0
commit fcbea476a5
3 changed files with 4 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import { writeFileSync, existsSync } from 'fs'; import { writeFileSync, existsSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import logger from './logger.js'; import logger from './logger.js';
import config from './config';
/** /**
* Generates the runtime configuration file (env.js) for the frontend * Generates the runtime configuration file (env.js) for the frontend
@ -32,6 +33,7 @@ export function generateRuntimeConfig(
// Generate env.js content // Generate env.js content
const envConfig = { const envConfig = {
basePath: basePath || '/', basePath: basePath || '/',
FRONTEND_API_KEY: config.apiKeys.frontend,
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars

View File

@ -8,6 +8,7 @@ declare global {
interface Window { interface Window {
env?: { env?: {
basePath?: string; basePath?: string;
FRONTEND_API_KEY: string;
}; };
} }
} }

View File

@ -21,7 +21,7 @@ const pathCombine = (baseUrl: string, path: string): string => {
}; };
const isDevelopment = import.meta.env.MODE === 'development'; const isDevelopment = import.meta.env.MODE === 'development';
const API_KEY = import.meta.env.VITE_API_KEY || 'frontend-default-key'; const API_KEY = window.env?.FRONTEND_API_KEY || import.meta.env.VITE_API_KEY;
const BASE_URL = isDevelopment const BASE_URL = isDevelopment
? pathCombine(import.meta.env.VITE_API_URL, '/api') ? pathCombine(import.meta.env.VITE_API_URL, '/api')
: '/api'; : '/api';