@open-kingdom/shared-poly-util-env-config
v0.0.2-7
Published
Centralized configuration service for accessing environment variables. Works in both Node.js and browser environments.
Readme
@open-kingdom/shared-poly-util-env-config
Centralized configuration service for accessing environment variables. Works in both Node.js and browser environments.
Usage
Backend (Node.js)
import { createConfigService, nodeEnvAdapter } from '@open-kingdom/shared-poly-util-env-config';
const envKeys = ['PORT', 'JWT_SECRET', 'JWT_EXPIRES_IN', 'BASE_URL'] as const;
const configService = createConfigService(envKeys, nodeEnvAdapter);
// Type-safe access to environment variables
const port = configService.get('PORT', '3000');
const secret = configService.getOrThrow('JWT_SECRET');Frontend (Vite)
import { createConfigService, createBrowserEnvAdapter } from '@open-kingdom/shared-poly-util-env-config';
const envKeys = ['VITE_API_URL', 'VITE_APP_NAME'] as const;
const configService = createConfigService(envKeys, createBrowserEnvAdapter(import.meta));
const apiUrl = configService.get('VITE_API_URL');API
createConfigService(keys, adapter)
Factory function to create a typed ConfigService instance.
keys: Array of environment variable keys (used for type inference)adapter: EithernodeEnvAdapteror result ofcreateBrowserEnvAdapter(import.meta)
createBrowserEnvAdapter(importMeta)
Factory function to create a browser environment adapter.
importMeta: Passimport.metafrom your Vite app
ConfigService<TKeys>
get(key): Returns the value or undefinedget(key, defaultValue): Returns the value or the defaultgetOrThrow(key): Returns the value or throws if not sethas(key): Returns true if the variable is set