-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
111 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TasksModule } from "./tasks/tasks.module"; | ||
import {ConfigurationModule} from "./configuration/configuration.module"; | ||
import * as path from 'node:path'; | ||
|
||
const configFileName = path.join('./', 'config.json'); | ||
|
||
console.log(configFileName); | ||
console.log(__dirname); | ||
|
||
// const configFileName = path.resolve( | ||
// `${__dirname}`, | ||
// "./config.json" | ||
// ) | ||
|
||
@Module({ | ||
imports: [TasksModule], | ||
imports: [TasksModule, ConfigurationModule.forRoot(configFileName) ], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
echo ${pwd} | ||
ls | ||
echo "============" | ||
ls ./dist | ||
cp config.json ./dist/config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"emailGetway": "some email getway", | ||
"smsGetway": "some sms getway", | ||
"someValue": "someValue" | ||
} |
16 changes: 16 additions & 0 deletions
16
03-di/01-notification-service/configuration/configuration.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module, DynamicModule, Global } from "@nestjs/common"; | ||
import { ConfigurationService } from './configuration.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [ConfigurationService], | ||
exports: [ConfigurationService] | ||
}) | ||
export class ConfigurationModule { | ||
static forRoot(fileName: string): DynamicModule { | ||
return { | ||
module: ConfigurationModule, | ||
providers: [{ provide: 'CONFIG_FILE_NAME', useFactory: () => fileName }], | ||
}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
03-di/01-notification-service/configuration/configuration.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as fs from 'node:fs'; | ||
import { Injectable, Inject } from "@nestjs/common"; | ||
import { z } from "zod"; | ||
|
||
const ConfigSchema = z.object({ | ||
emailGetway: z.string({ | ||
required_error: "emailGetway not provided" | ||
}).nonempty(), | ||
smsGetway: z.string({ | ||
required_error: "smsGetway not provided" | ||
}), | ||
someValue: z.string({ | ||
required_error: "someValue not provided" | ||
}) | ||
}); | ||
|
||
type Config = z.infer<typeof ConfigSchema>; | ||
|
||
const defaultOptions: Config = { | ||
emailGetway: 'default', | ||
smsGetway: 'default', | ||
someValue: 'default', | ||
}; | ||
|
||
@Injectable() | ||
export class ConfigurationService { | ||
private config: Config = defaultOptions; | ||
|
||
constructor(@Inject('CONFIG_FILE_NAME') private fileName: string) { | ||
try { | ||
const config = JSON.parse(fs.readFileSync(this.fileName, 'utf8')); | ||
|
||
ConfigSchema.parse(config); | ||
|
||
this.config = config; | ||
|
||
console.log(this.config); | ||
} catch (error) { | ||
// console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
get options() { | ||
return this.config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"scripts": { | ||
"start": "nest start --watch", | ||
"test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --no-warnings --experimental-vm-modules\" NODE_ENV=test jest --config jest.config.js --runInBand" | ||
"test": "./before-test.sh && cross-env NODE_OPTIONS=\"$NODE_OPTIONS --no-warnings --experimental-vm-modules\" NODE_ENV=test jest --config jest.config.js --runInBand" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
03-di/01-notification-service/providers/NotificationService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const LoggerService = 'LoggerService'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export class User { | ||
id: number; | ||
name: string; | ||
email: string; | ||
phone: string; | ||
id: number = 0; | ||
name: string = ''; | ||
email: string = ''; | ||
phone: string = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters