Skip to content

Commit

Permalink
Добавит схему конфигурации с помощью пакета convict
Browse files Browse the repository at this point in the history
  • Loading branch information
AdonaiJehosua committed Aug 24, 2024
1 parent 00f20fb commit 6449897
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 10 deletions.
125 changes: 125 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"mock:server": "json-server mocks/mock-server-data.json --port 3123"
},
"devDependencies": {
"@types/convict": "6.1.4",
"@types/convict-format-with-validator": "6.0.3",
"@types/node": "18.17.17",
"@typescript-eslint/eslint-plugin": "6.7.0",
"@typescript-eslint/parser": "6.7.0",
Expand All @@ -39,6 +41,8 @@
"npm": ">=8"
},
"dependencies": {
"convict": "6.2.4",
"convict-format-with-validator": "6.2.0",
"dotenv": "16.3.1",
"got": "13.0.0",
"pino": "8.15.1"
Expand Down
4 changes: 2 additions & 2 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Logger } from '../shared/libs/logger/index.js';
import {Config} from '../shared/libs/config/index.js';
import {Config, RestSchema} from '../shared/libs/config/index.js';

export class RestApplication {
constructor(
private readonly logger: Logger,
private readonly config: Config,
private readonly config: Config<RestSchema>,
) {}

public async init() {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/libs/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface Config {
get(key: string): string | undefined;
export interface Config<U> {
get<T extends keyof U>(key: T): U[T];
}
1 change: 1 addition & 0 deletions src/shared/libs/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './rest.config.js';
export * from './config.interface.js';
export * from './rest.schema.js';
16 changes: 10 additions & 6 deletions src/shared/libs/config/rest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Config } from './config.interface.js';
import {Config} from './config.interface.js';
import {Logger} from '../logger/index.js';
import {config, DotenvParseOutput} from 'dotenv';
import {config} from 'dotenv';
import {configRestSchema, RestSchema} from './rest.schema.js';

export class RestConfig implements Config {
private readonly config: NodeJS.ProcessEnv;
export class RestConfig implements Config<RestSchema> {
private readonly config: RestSchema;

constructor(
private readonly logger: Logger
Expand All @@ -14,11 +15,14 @@ export class RestConfig implements Config {
throw new Error('Can\'t read .env file. Perhaps the file does not exists.');
}

this.config = <DotenvParseOutput>parsedOutput.parsed;
configRestSchema.load({});
configRestSchema.validate({allowed: 'strict', output: this.logger.info});

this.config = configRestSchema.getProperties();
this.logger.info('.env file found and successfully parsed!');
}

public get(key: string): string | undefined {
public get<T extends keyof RestSchema>(key: T): RestSchema[T] {
return this.config[key];
}
}
32 changes: 32 additions & 0 deletions src/shared/libs/config/rest.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import convict from 'convict';
import validator from 'convict-format-with-validator';

convict.addFormats(validator);


export type RestSchema = {
PORT: number;
SALT: string;
DB_HOST: string;
}

export const configRestSchema = convict<RestSchema>({
PORT: {
doc: 'Port for incoming connections',
format: 'port',
env: 'PORT',
default: 4000
},
SALT: {
doc: 'Salt for password hash',
format: String,
env: 'SALT',
default: null
},
DB_HOST: {
doc: 'IP address of the database server (MongoDB)',
format: 'ipaddress',
env: 'DB_HOST',
default: '127.0.0.1'
},
});

0 comments on commit 6449897

Please sign in to comment.