@williamkoller/structure-as-common
v0.0.14
Published
Structure As Commun For NestJS
Downloads
2
Maintainers
Readme
yarn add @williamkoller/structure-as-common
import { Module } from '@nestjs/common';
import { LoggerModule } from '@williamkoller/structure-as-common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [LoggerModule.register()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { LoggerService } from '@williamkoller/structure-as-common';
@Injectable()
export class AppService {
constructor(private readonly logger: LoggerService) {}
getHello(id: number): string {
this.logger.log('AppService', `getHello: ${id}`);
return `id: ${id}`;
}
}
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import {
HttpExceptionFilter,
LoggerService,
} from '@williamkoller/structure-as-common';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.useGlobalFilters(new HttpExceptionFilter(new LoggerService()));
await app.listen(3001);
}
bootstrap();
app.useGlobalInterceptors(new TimeoutInterceptor(), new LoggingInterceptor());
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import { ValidationParamsPipe } from '@williamkoller/structure-as-common';
import { AppService } from './app.service';
@Controller('app')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('find-by-id/:id')
getHello(
@Param('id', ValidationParamsPipe, ParseIntPipe)
id: number
): string {
return this.appService.getHello(id);
}
}
import { Module } from '@nestjs/common';
import {
ExceptionModule,
LoggerModule,
} from '@williamkoller/structure-as-common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [LoggerModule.register(), ExceptionModule.register()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import {
ExceptionService,
LoggerService,
} from '@williamkoller/structure-as-common';
@Injectable()
export class AppService {
constructor(
private readonly logger: LoggerService,
private readonly exception: ExceptionService
) {}
getHello(id: number): string {
this.logger.log('AppService', `getHello: ${id}`);
if (id === 1) {
this.exception.badRequestException({
message: 'id cannot be one',
});
}
return `id: ${id}`;
}
}
{
"statusCode": 400,
"timestamp": "2023-03-03T14:51:09.486Z",
"path": "/app/find-by-id/1",
"message": "id cannot be one"
}
Made with 🖤 by williamkoller :wave: