Initial commit: add .gitignore and README
This commit is contained in:
29
packages/shared-config/package.json
Normal file
29
packages/shared-config/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@workspace/shared-config",
|
||||
"version": "1.0.0",
|
||||
"description": "Shared configuration schemas and validation",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "vitest",
|
||||
"lint": "eslint src",
|
||||
"type-check": "tsc --noEmit",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"zod": "^3.23.8",
|
||||
"dotenv": "^16.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.4",
|
||||
"vitest": "^1.2.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"publishConfig": {
|
||||
"access": "restricted"
|
||||
}
|
||||
}
|
||||
|
||||
55
packages/shared-config/src/env.ts
Normal file
55
packages/shared-config/src/env.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Environment variable utilities
|
||||
*/
|
||||
|
||||
import { config } from 'dotenv';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Load environment variables
|
||||
*/
|
||||
export function loadEnv(envFile?: string): void {
|
||||
config({ path: envFile });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable with default
|
||||
*/
|
||||
export function getEnv(key: string, defaultValue?: string): string {
|
||||
const value = process.env[key];
|
||||
if (value === undefined) {
|
||||
if (defaultValue !== undefined) {
|
||||
return defaultValue;
|
||||
}
|
||||
throw new Error(`Environment variable ${key} is not set`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable as number
|
||||
*/
|
||||
export function getEnvNumber(key: string, defaultValue?: number): number {
|
||||
const value = getEnv(key, defaultValue?.toString());
|
||||
const num = Number(value);
|
||||
if (isNaN(num)) {
|
||||
throw new Error(`Environment variable ${key} is not a valid number`);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable as boolean
|
||||
*/
|
||||
export function getEnvBoolean(key: string, defaultValue?: boolean): boolean {
|
||||
const value = getEnv(key, defaultValue?.toString());
|
||||
return value.toLowerCase() === 'true' || value === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate environment variables against schema
|
||||
*/
|
||||
export function validateEnv<T>(schema: z.ZodSchema<T>): T {
|
||||
return schema.parse(process.env);
|
||||
}
|
||||
|
||||
9
packages/shared-config/src/index.ts
Normal file
9
packages/shared-config/src/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @workspace/shared-config
|
||||
* Shared configuration schemas and validation
|
||||
*/
|
||||
|
||||
export * from './env';
|
||||
export * from './schemas';
|
||||
export * from './validation';
|
||||
|
||||
37
packages/shared-config/src/schemas.ts
Normal file
37
packages/shared-config/src/schemas.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Configuration schemas
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Database configuration schema
|
||||
*/
|
||||
export const databaseConfigSchema = z.object({
|
||||
DB_HOST: z.string(),
|
||||
DB_PORT: z.string().transform(Number),
|
||||
DB_NAME: z.string(),
|
||||
DB_USER: z.string(),
|
||||
DB_PASSWORD: z.string(),
|
||||
DB_SSL: z.string().optional().transform((val) => val === 'true'),
|
||||
});
|
||||
|
||||
/**
|
||||
* Server configuration schema
|
||||
*/
|
||||
export const serverConfigSchema = z.object({
|
||||
PORT: z.string().transform(Number).default('3000'),
|
||||
NODE_ENV: z.enum(['development', 'staging', 'production']).default('development'),
|
||||
HOST: z.string().default('localhost'),
|
||||
});
|
||||
|
||||
/**
|
||||
* JWT configuration schema
|
||||
*/
|
||||
export const jwtConfigSchema = z.object({
|
||||
JWT_SECRET: z.string(),
|
||||
JWT_EXPIRES_IN: z.string().default('24h'),
|
||||
JWT_ISSUER: z.string().optional(),
|
||||
JWT_AUDIENCE: z.string().optional(),
|
||||
});
|
||||
|
||||
27
packages/shared-config/src/validation.ts
Normal file
27
packages/shared-config/src/validation.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Configuration validation utilities
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Validate configuration object
|
||||
*/
|
||||
export function validateConfig<T>(schema: z.ZodSchema<T>, config: unknown): T {
|
||||
return schema.parse(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate configuration with error handling
|
||||
*/
|
||||
export function validateConfigSafe<T>(
|
||||
schema: z.ZodSchema<T>,
|
||||
config: unknown
|
||||
): { success: true; data: T } | { success: false; error: z.ZodError } {
|
||||
const result = schema.safeParse(config);
|
||||
if (result.success) {
|
||||
return { success: true, data: result.data };
|
||||
}
|
||||
return { success: false, error: result.error };
|
||||
}
|
||||
|
||||
20
packages/shared-config/tsconfig.json
Normal file
20
packages/shared-config/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2022"],
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user