Added Swagger and Helmet

This commit is contained in:
Bucaille Thommy 2022-01-13 00:44:21 +01:00
parent db7e7bc5e8
commit b6d78f8a98
9 changed files with 713 additions and 44 deletions

658
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -27,17 +27,22 @@
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/sequelize": "^8.0.0",
"@nestjs/swagger": "^5.1.5",
"bcrypt": "^5.0.1",
"helmet": "^5.0.1",
"mysql2": "^2.3.3",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"sequelize": "^6.13.0",
"sequelize-typescript": "^2.1.2"
"sequelize-typescript": "^2.1.2",
"swagger-ui-express": "^4.3.0"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13",
"@types/jest": "27.0.2",
"@types/node": "^16.0.0",

View File

@ -1,12 +1,34 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { SwaggerModule, DocumentBuilder, SwaggerCustomOptions } from '@nestjs/swagger';
import helmet from 'helmet';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
app.use(helmet());
app.enableCors({
origin: configService.get("APP_URL", true)
});
const config = new DocumentBuilder()
.setTitle('Business Earn')
.setDescription('The Business Earn API documentation.')
.setVersion('1.0')
.addTag('users')
.build();
const customOptions: SwaggerCustomOptions = {
swaggerOptions: {
persistAuthorization: true,
}
};
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document, customOptions);
await app.listen(configService.get("APP_PORT", 3000));
}
bootstrap();

View File

@ -1 +1,5 @@
export class CreateUserDto {}
export class CreateUserDto {
firstName: string;
lastName: string;
email: string;
}

View File

@ -0,0 +1,4 @@
export class LoginUserDto {
email: string;
password: string;
}

View File

@ -1,4 +1,8 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateUserDto } from './create-user.dto';
export class UpdateUserDto extends PartialType(CreateUserDto) {}
export class UpdateUserDto extends PartialType(CreateUserDto) {
firstName: string;
lastName: string;
email: string;
}

View File

@ -1,6 +1,7 @@
import sequelize from 'sequelize';
import { AllowNull, Column, CreatedAt, HasMany, IsDate, IsEmail, Length, Model, NotEmpty, Table, Unique, UpdatedAt } from 'sequelize-typescript';
import { Company } from '../../companies/models/company.model';
import * as bcrypt from 'bcrypt';
@Table
export class User extends Model {
@ -23,6 +24,23 @@ export class User extends Model {
@Column
email: string;
@AllowNull(false)
@NotEmpty
@Length({msg: "The password must be 8 characters min and 32 characters max", min: 8, max: 32})
@Column
get password(): string {
return this.getDataValue('password_hash');
}
set password(value: string) {
const hash = bcrypt.hashSync(value, 10);
this.setDataValue("password_hash", value);
}
@AllowNull(false)
@NotEmpty
@Column
password_hash: string;
@IsDate
@AllowNull(false)
@Column({ defaultValue: sequelize.NOW })

View File

@ -1,26 +1,35 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './models/user.model';
@Injectable()
export class UsersService {
create(createUserDto: CreateUserDto) {
return 'This action adds a new user';
constructor(
@InjectModel(User)
private userModel: typeof User,
) {}
async create(createUserDto: CreateUserDto) : Promise<User> {
return this.userModel.create(createUserDto);
}
async findAll() : Promise<User[]> {
return this.userModel.findAll();
}
findAll() {
return `This action returns all users`;
async findOne(id: number) : Promise<User> {
return this.userModel.findByPk(id);
}
findOne(id: number) {
return `This action returns a #${id} user`;
async update(id: number, updateUserDto: UpdateUserDto) : Promise<User> {
const user = await this.findOne(id);
return user.update({ updateUserDto });
}
update(id: number, updateUserDto: UpdateUserDto) {
return `This action updates a #${id} user`;
}
remove(id: number) {
return `This action removes a #${id} user`;
async remove(id: number) : Promise<void> {
const user = await this.findOne(id);
return user.destroy();
}
}

View File

@ -6,6 +6,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",