-
Notifications
You must be signed in to change notification settings - Fork 0
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
mrkeksz
committed
Oct 19, 2023
1 parent
5065a8a
commit 54323fc
Showing
20 changed files
with
280 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Default environment variables for all environments. | ||
# ".env.local" and ".env.test.local" files has higher priority than ".env" and ".env.test" files. | ||
|
||
# Application supports variables in environment variables. | ||
# Example: | ||
# APP_URL=mywebsite.com | ||
# SUPPORT_EMAIL=support@${APP_URL} | ||
|
||
# Description: Node environment | ||
# Type: string | ||
# Options: production, development, test | ||
# Default: production | ||
NODE_ENV=production | ||
|
||
# Description: Port to run the server on | ||
# Type: number | ||
# Default: 3000 | ||
PORT=3000 |
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 @@ | ||
NODE_ENV=test |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 +1,33 @@ | ||
import {Test, TestingModule} from '@nestjs/testing' | ||
import {AppController} from './app.controller' | ||
import {AppService} from './app.service' | ||
import {configModule, configProviders} from './config' | ||
import {ServerConfigService} from './config/server/server.config.service' | ||
import {EnvironmentConfigService} from './config/environment/environment.config.service' | ||
|
||
describe('AppController', () => { | ||
let appController: AppController | ||
let serverConfigService: ServerConfigService | ||
let environmentConfigService: EnvironmentConfigService | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
imports: [configModule], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
providers: [AppService, ...configProviders], | ||
}).compile() | ||
|
||
appController = app.get<AppController>(AppController) | ||
appController = app.get(AppController) | ||
serverConfigService = app.get(ServerConfigService) | ||
environmentConfigService = app.get(EnvironmentConfigService) | ||
}) | ||
|
||
describe('root', () => { | ||
it('should return "Hello World!"', () => { | ||
expect(appController.getHello()).toBe('Hello World!') | ||
expect(appController.getHello()).toBe(`Hello World! | ||
Port: ${serverConfigService.port} | ||
Is development: ${environmentConfigService.isDevelopment} | ||
Test: ${environmentConfigService.test}`) | ||
}) | ||
}) | ||
}) |
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,8 +1,18 @@ | ||
import {Injectable} from '@nestjs/common' | ||
import {ServerConfigService} from './config/server/server.config.service' | ||
import {EnvironmentConfigService} from './config/environment/environment.config.service' | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor( | ||
private readonly serverConfigService: ServerConfigService, | ||
private readonly environmentConfigService: EnvironmentConfigService, | ||
) {} | ||
|
||
getHello(): string { | ||
return 'Hello World!' | ||
return `Hello World! | ||
Port: ${this.serverConfigService.port} | ||
Is development: ${this.environmentConfigService.isDevelopment} | ||
Test: ${this.environmentConfigService.test}` | ||
} | ||
} |
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 @@ | ||
export type NodeEnvType = 'development' | 'production' | 'test' | ||
export const NODE_ENV = { | ||
name: 'NODE_ENV', | ||
options: { | ||
DEVELOPMENT: 'development', | ||
PRODUCTION: 'production', | ||
TEST: 'test', | ||
}, | ||
defaultValue: 'production', | ||
} | ||
|
||
export type TestType = 1 | ||
export const TEST = { | ||
name: 'TEST', | ||
defaultValue: 1, | ||
} |
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,8 @@ | ||
import * as Joi from 'joi' | ||
import {NODE_ENV} from './environment.config.constants' | ||
|
||
export default { | ||
[NODE_ENV.name]: Joi.string() | ||
.valid(...Object.values(NODE_ENV.options)) | ||
.default(NODE_ENV.defaultValue), | ||
} |
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,24 @@ | ||
import {Injectable} from '@nestjs/common' | ||
import {ConfigService} from '@nestjs/config' | ||
import {NODE_ENV, TEST, TestType} from './environment.config.constants' | ||
|
||
@Injectable() | ||
export class EnvironmentConfigService { | ||
constructor(private configService: ConfigService) {} | ||
|
||
get isDevelopment(): boolean { | ||
return this.configService.get(NODE_ENV.name) === NODE_ENV.options.DEVELOPMENT | ||
} | ||
|
||
get isProduction(): boolean { | ||
return this.configService.get(NODE_ENV.name) === NODE_ENV.options.PRODUCTION | ||
} | ||
|
||
get isTest(): boolean { | ||
return this.configService.get(NODE_ENV.name) === NODE_ENV.options.TEST | ||
} | ||
|
||
get test(): number { | ||
return this.configService.get(TEST.name) as TestType | ||
} | ||
} |
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 @@ | ||
import {TEST} from './environment.config.constants' | ||
|
||
export default () => ({ | ||
[TEST.name]: TEST.defaultValue, | ||
}) |
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,38 @@ | ||
import {ServerConfigService} from './server/server.config.service' | ||
import {EnvironmentConfigService} from './environment/environment.config.service' | ||
import environmentConfig from './environment/environment.config' | ||
import * as Joi from 'joi' | ||
import {ConfigModule} from '@nestjs/config/dist/config.module' | ||
import serverConfigEnvValidation from './server/server.config.env-validation' | ||
import environmentConfigEnvValidation from './environment/environment.config.env-validation' | ||
import * as process from 'process' | ||
import {NODE_ENV} from './environment/environment.config.constants' | ||
|
||
// Add all config services here | ||
export const configProviders = [EnvironmentConfigService, ServerConfigService] | ||
|
||
// Add all custom config here | ||
const configsForLoad = [environmentConfig] | ||
|
||
// Add all config validation here | ||
const validationSchema = Joi.object({ | ||
...serverConfigEnvValidation, | ||
...environmentConfigEnvValidation, | ||
}) | ||
|
||
export const configModule = ConfigModule.forRoot({ | ||
envFilePath: | ||
process.env[NODE_ENV.name] === NODE_ENV.options.TEST | ||
? ['.env.test.local', '.env.test', '.env'] | ||
: ['.env.local', '.env'], | ||
cache: true, | ||
validationOptions: { | ||
allowUnknown: true, | ||
abortEarly: true, | ||
convert: true, | ||
dateFormat: 'iso', | ||
}, | ||
expandVariables: true, | ||
load: configsForLoad, | ||
validationSchema, | ||
}) |
Oops, something went wrong.