Skip to content

Commit

Permalink
feat: setup config module
Browse files Browse the repository at this point in the history
add path alias of config dir
  • Loading branch information
leosuncin committed Sep 5, 2022
1 parent 7b6a7e3 commit 3c2e617
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"release": "semantic-release",
"start": "nest start",
"start:debug": "nest start --debug --watch",
"start:dev": "NODE_OPTIONS='-r dotenv/config' nest start --watch",
"start:dev": "nest start --watch",
"start:prod": "node dist/main",
"start:repl": "NODE_OPTIONS='-r @suncin/dotenv' nest start --entryFile repl",
"start:repl": "nest start --entryFile repl",
"test": "jest --selectProjects 'Unit test'",
"test:cov": "jest --selectProjects 'Unit test' --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand All @@ -34,6 +34,7 @@
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^9.0.0",
"@nestjs/mapped-types": "^1.1.0",
Expand Down
24 changes: 23 additions & 1 deletion pnpm-lock.yaml

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

7 changes: 7 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AppController } from '~app/app.controller';
import { AppService } from '~app/app.service';
import { AuthModule } from '~auth/auth.module';
import { BlogModule } from '~blog/blog.module';
import { configuration } from '~config/configuration';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
expandVariables: true,
load: [configuration],
}),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
Expand Down
12 changes: 12 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import invariant from 'tiny-invariant';

export type ConfigObject = ReturnType<typeof configuration>;

export function configuration() {
invariant(process.env.SECRET, 'SECRET is missing');

return {
port: Number.parseInt(process.env.PORT, 10) || 3000,
secret: process.env.SECRET,
};
}
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { HttpStatus, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { useContainer } from 'class-validator';
import cookieParser from 'cookie-parser';

import { AppModule } from '~app/app.module';
import type { ConfigObject } from '~config/configuration';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = app.get<ConfigService<ConfigObject, true>>(ConfigService);

app.useGlobalPipes(
new ValidationPipe({
Expand All @@ -15,10 +18,10 @@ async function bootstrap() {
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
);
app.use(cookieParser(process.env.SECRET));
app.use(cookieParser(config.getOrThrow('secret')));
useContainer(app.select(AppModule), { fallbackOnErrors: true });

await app.listen(process.env.PORT ?? 3000);
await app.listen(config.get('port'));
}

// eslint-disable-next-line unicorn/prefer-top-level-await
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"~auth/*": ["src/auth/*"],
"~blog/*": ["src/blog/*"],
"~common/*": ["src/common/*"],
"~config/*": ["src/config/*"],
"~migrations/*": ["src/migrations/*"]
}
}
Expand Down

0 comments on commit 3c2e617

Please sign in to comment.