mirror of
https://github.com/artegoser/create-ts-prod.git
synced 2024-11-23 05:36:21 +03:00
28 lines
582 B
TypeScript
28 lines
582 B
TypeScript
|
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;
|
||
|
}
|
||
|
}
|