feat: initial version

This commit is contained in:
Artemy 2023-07-19 15:38:42 +03:00
parent 4e9727aff7
commit aff35696b1
8 changed files with 310 additions and 0 deletions

11
files/src/app.ts Normal file
View file

@ -0,0 +1,11 @@
import { IConfigService } from "./config/config.interface";
import { ConfigService } from "./config/config.service";
class App {
config: IConfigService;
constructor() {
this.config = new ConfigService();
}
}
const app = new App();

View file

@ -0,0 +1,3 @@
export interface IConfigService {
get(key: string): string;
}

View file

@ -0,0 +1,27 @@
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;
}
}