Yield generated for f09a72b1-e279-4458-be05-9161fd9753a6
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

61 lines
1.4 KiB

import { Injectable } from '@nestjs/common';
import * as z from 'zod';
import { config as parseConfig } from 'dotenv';
const schema = z.object({
NODE_ENV: z
.enum(['development', 'production', 'test'])
.default('development'),
LOG_LEVEL: z
.enum(['error', 'warning', 'info', 'debug', 'silly'])
.default('debug'),
DATABASE_URL: z.string().nonempty(),
LOG_SQL_REQUEST: z
.string()
.transform((value) => value === 'true')
.default('false'),
});
type EnvConfig = z.infer<typeof schema>;
@Injectable()
export class ConfigService {
private readonly envConfig: EnvConfig;
constructor() {
try {
parseConfig();
} catch (e) {}
this.envConfig = this.validateInput(process.env);
}
/**
* Ensures all needed variables are set, and returns the validated JavaScript object
* including the applied default values.
*/
private validateInput(envConfig: any): EnvConfig {
const result = schema.safeParse(envConfig);
if (result.success === false) {
throw new Error(`Config validation error: ${result.error.toString()}`);
}
return result.data;
}
get nodeEnv(): string {
return this.envConfig.NODE_ENV;
}
get databaseUrl(): string {
return this.envConfig.DATABASE_URL;
}
get isLoggingDb(): boolean {
return this.envConfig.LOG_SQL_REQUEST;
}
get loggerLevel(): string {
return this.envConfig.LOG_LEVEL;
}
}