create-ts-prod/files/src/config/config.service.ts

28 lines
582 B
TypeScript
Raw Normal View History

2023-07-19 15:38:42 +03:00
import { config, DotenvParseOutput } from "dotenv";
import { IConfigService } from "./config.interface";
export class ConfigService implements IConfigService {
private config: DotenvParseOutput;
constructor() {
const { error, parsed } = config();
if (error) {
throw new Error(".env file not found");
}
if (!parsed) {
throw new Error("Invalid .env file");
}
this.config = parsed;
}
get(key: string): string {
const res = this.config[key];
if (!res) {
throw new Error(`Key ${key} not found`);
}
return res;
}
}