Skip to content

Instantly share code, notes, and snippets.

@H4ad
Created August 24, 2020 02:37
Show Gist options
  • Save H4ad/336812f23a652dd92f81673084f791bb to your computer and use it in GitHub Desktop.
Save H4ad/336812f23a652dd92f81673084f791bb to your computer and use it in GitHub Desktop.
NestJS Configurations

NestJS Configurations

This is my configurations for startup of NestJS Applications.

import { INestApplication, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { CrudConfigService } from '@nestjsx/crud';
import { Request, Response } from 'express';
import * as helmet from 'helmet';
const bodyParser = require('body-parser');
/**
* Método que configura o Swagger para a aplicação
*
* @param app A instância da aplicação
*/
function setupSwagger(app: INestApplication): void {
if (!process.env.SWAGGER_ENABLED)
return;
const swaggerOptions = new DocumentBuilder()
.setTitle(process.env.SWAGGER_TITLE)
.setDescription(process.env.SWAGGER_DESCRIPTION)
.setVersion(process.env.SWAGGER_VERSION)
.addTag(process.env.SWAGGER_TAG)
.setBasePath(process.env.API_BASE_PATH)
.setSchemes('https', 'http')
.addBearerAuth('Authorization', 'header')
.build();
const document = SwaggerModule.createDocument(app, swaggerOptions);
SwaggerModule.setup(`${ env.API_BASE_PATH }/swagger`, app, document);
}
/**
* Método que configura os pipes globais
*
* @param app A instância da aplicação
*/
function setupPipes(app: INestApplication): void {
app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
}),
);
}
/**
* Método que configura os middleware da aplicação
*
* @param app A instância da aplicação
*/
function setupMiddleware(app: INestApplication): void {
app.use(helmet());
app.enableCors();
app.use(bodyParser.json({
limit: '50mb',
}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true,
}));
}
export async function setup(app: INestApplication): Promise<INestApplication> {
setupSwagger(app);
setupPipes(app);
setupMiddleware(app);
app.setGlobalPrefix(process.env.API_BASE_PATH);
return app;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment