UNPKG

@spoolcms/nextjs

Version:

The beautiful headless CMS for Next.js developers

113 lines (112 loc) 3.71 kB
"use strict"; /** * Configuration resolution utilities for Spool CMS * Handles environment variable resolution across server and client contexts */ Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveConfig = resolveConfig; exports.createTestConfig = createTestConfig; const environment_1 = require("./environment"); /** * Resolve Spool configuration from multiple sources * Automatically detects environment and uses appropriate variable sources */ function resolveConfig(config) { const environment = (0, environment_1.detectEnvironment)(); // Resolve API key from multiple sources const apiKey = resolveApiKey(config?.apiKey); // Resolve site ID from multiple sources const siteId = resolveSiteId(config?.siteId); // Resolve base URL with smart defaults const baseUrl = resolveBaseUrl(config?.baseUrl, environment); // Validate required configuration validateConfig({ apiKey, siteId, baseUrl }); return { apiKey, siteId, baseUrl, environment, }; } /** * Resolve API key from environment variables or config */ function resolveApiKey(configApiKey) { // Try config first if (configApiKey) { return configApiKey; } // Use NEXT_PUBLIC_ prefixed variables (works in both server and client) const apiKey = process.env.NEXT_PUBLIC_SPOOL_API_KEY; if (apiKey) { return apiKey; } throw new Error(`Spool API key not found. Please set NEXT_PUBLIC_SPOOL_API_KEY environment variable or pass apiKey in config.`); } /** * Resolve site ID from environment variables or config */ function resolveSiteId(configSiteId) { // Try config first if (configSiteId) { return configSiteId; } // Use NEXT_PUBLIC_ prefixed variables (works in both server and client) const siteId = process.env.NEXT_PUBLIC_SPOOL_SITE_ID; if (siteId) { return siteId; } throw new Error(`Spool site ID not found. Please set NEXT_PUBLIC_SPOOL_SITE_ID environment variable or pass siteId in config.`); } /** * Resolve base URL with smart defaults */ function resolveBaseUrl(configBaseUrl, environment) { const env = environment || (0, environment_1.detectEnvironment)(); // Try config first if (configBaseUrl) { return configBaseUrl; } // Try environment variables (for backward compatibility) const envBaseUrl = process.env.SPOOL_BASE_URL || process.env.SPOOL_API_BASE; if (envBaseUrl) { return envBaseUrl; } // Default to production Spool CMS (canonical host) return 'https://www.spoolcms.com'; } /** * Validate that all required configuration is present */ function validateConfig(config) { if (!config.apiKey) { throw new Error('Spool API key is required'); } if (!config.siteId) { throw new Error('Spool site ID is required'); } if (!config.baseUrl) { throw new Error('Spool base URL is required'); } // Validate API key format if (!config.apiKey.startsWith('spool_')) { console.warn('Spool API key should start with "spool_" prefix'); } // Validate site ID format (UUID) const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; if (!uuidRegex.test(config.siteId)) { console.warn('Spool site ID should be a valid UUID'); } } /** * Create a configuration object for testing purposes */ function createTestConfig(overrides) { return { apiKey: 'spool_test_key', siteId: '00000000-0000-0000-0000-000000000000', baseUrl: 'https://test.spoolcms.com', environment: (0, environment_1.detectEnvironment)(), ...overrides, }; }