Skip to content

Commit

Permalink
chore: add preload example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaple committed May 10, 2021
1 parent dc9003e commit ea2c483
Show file tree
Hide file tree
Showing 14 changed files with 1,943 additions and 2 deletions.
6 changes: 4 additions & 2 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "nest-typed-config-examples",
"name": "nest-typed-config-example-basic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"nest-typed-config": "latest"
},
"devDependencies": {},
"devDependencies": {
"@nestjs/cli": "^7.6.0"
},
"scripts": {
"start": "nest start --watch",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
7 changes: 7 additions & 0 deletions examples/preload/.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"host": "127.0.0.1",
"port": 26874,
"route": {
"app": "/app"
}
}
3 changes: 3 additions & 0 deletions examples/preload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Basic Example

This folder is an example project to demonstrate `Nest-Typed-Config`
4 changes: 4 additions & 0 deletions examples/preload/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
19 changes: 19 additions & 0 deletions examples/preload/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "nest-typed-config-example-preload",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"nest-typed-config": "latest"
},
"devDependencies": {
"@nestjs/cli": "^7.6.0"
},
"scripts": {
"start": "nest start --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
13 changes: 13 additions & 0 deletions examples/preload/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { rootConfig } from './config.module';

@Controller(rootConfig.route.app)
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
show(): void {
return this.appService.show();
}
}
11 changes: 11 additions & 0 deletions examples/preload/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './config.module';

@Module({
imports: [ConfigModule],
providers: [AppService],
controllers: [AppController],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions examples/preload/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { RootConfig } from './config';

@Injectable()
export class AppService {
// inject any config or sub-config you like
constructor(private config: RootConfig) {}

// enjoy type safety!
public show(): any {
return `Your configuration is: \n${JSON.stringify(this.config, null, 4)}\n`;
}
}
9 changes: 9 additions & 0 deletions examples/preload/src/config.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TypedConfigModule, fileLoader, selectConfig } from 'nest-typed-config';
import { RootConfig } from './config';

export const ConfigModule = TypedConfigModule.forRoot({
schema: RootConfig,
load: fileLoader(),
});

export const rootConfig = selectConfig(ConfigModule, RootConfig);
19 changes: 19 additions & 0 deletions examples/preload/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Type } from 'class-transformer';
import { IsDefined, IsNumber, IsString } from 'class-validator';

export class RouteConfig {
@IsString()
public readonly app!: string;
}

export class RootConfig {
@IsString()
public readonly host!: string;

@IsNumber()
public readonly port!: number;

@IsDefined()
@Type(() => RouteConfig)
public readonly route!: RouteConfig;
}
18 changes: 18 additions & 0 deletions examples/preload/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { RootConfig } from './config';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const { host, port, route } = app.get(RootConfig);

await app.listen(port, host);

console.log(
`\nApp successfully bootstrapped. You can try running:
curl http://${host}:${port}${route.app}`,
);
}

bootstrap().catch(console.error);
6 changes: 6 additions & 0 deletions examples/preload/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
}
}
13 changes: 13 additions & 0 deletions examples/preload/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
Loading

0 comments on commit ea2c483

Please sign in to comment.