mirror of
https://github.com/artegoser/create-ts-prod.git
synced 2025-02-23 12:53:15 +03:00
feat: initial version
This commit is contained in:
parent
4e9727aff7
commit
aff35696b1
8 changed files with 310 additions and 0 deletions
11
files/src/app.ts
Normal file
11
files/src/app.ts
Normal 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();
|
3
files/src/config/config.interface.ts
Normal file
3
files/src/config/config.interface.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export interface IConfigService {
|
||||
get(key: string): string;
|
||||
}
|
27
files/src/config/config.service.ts
Normal file
27
files/src/config/config.service.ts
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue