-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
136/sanitize returned api data #373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | ||
import * as bcrypt from 'bcryptjs'; | ||
import { Repository } from 'typeorm/repository/Repository'; | ||
import { ReturnUserDto } from './dto/auth.dto'; | ||
import { CreateUserInternal, UpdateUserInternal } from './dto/create-user.internal'; | ||
import { User } from './entities/user.entity'; | ||
|
||
|
@@ -12,12 +11,11 @@ const { BCRYPT_WORK_FACTOR = '10' } = process.env; | |
export class UsersService { | ||
constructor(@InjectRepository(User) private usersRepository: Repository<User>) {} | ||
|
||
async create(createUserDto: CreateUserInternal): Promise<ReturnUserDto> { | ||
async create(createUserDto: CreateUserInternal): Promise<User> { | ||
try { | ||
const hashedPw = await bcrypt.hash(createUserDto.password, parseInt(BCRYPT_WORK_FACTOR)); | ||
createUserDto.password = hashedPw; | ||
const user = await this.usersRepository.save(createUserDto); | ||
delete user.password; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've verified locally that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in a way. changing the return type actually has no bearing on the returned data (thanks to Javascript). Thankfully, return plainToInstance(this.dto, data, {
excludeExtraneousValues: true,
strategy: 'excludeAll',
}); The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with |
||
return user; | ||
} catch (err) { | ||
Logger.error(`${err.message}: \n${err.stack}`, UsersService.name); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CallHandler, ExecutionContext, NestInterceptor, UseInterceptors } from '@nestjs/common'; | ||
import { Observable, map } from 'rxjs'; | ||
import { plainToInstance } from 'class-transformer'; | ||
|
||
export const MapTo = (dto: any) => { | ||
return UseInterceptors(new SerializeInterceptor(dto)); | ||
}; | ||
|
||
export class SerializeInterceptor implements NestInterceptor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is an interceptor similar to a middleware? (I should go to the link and read up on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's satisfied by the nestjs/common peer dependencies, which uses it as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In NestJs an interceptor is middle-ware. In this specific case, we're chaining a |
||
constructor(private dto: any) {} | ||
|
||
intercept( | ||
context: ExecutionContext, | ||
next: CallHandler<any>, | ||
): Observable<any> | Promise<Observable<any>> { | ||
return next.handle().pipe( | ||
map((data: any) => { | ||
return plainToInstance(this.dto, data, { | ||
excludeExtraneousValues: true, | ||
strategy: 'excludeAll', | ||
}); | ||
}), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worth to add a migration to update field names to follow a naming convention with camel-case instead of snake-case, or vice-versa? I think my preference is camelCase as it aligns with the front-end, but I'm not sure what is more common with Node/Nest.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. This was done, I believe to make table column names conform to the Postgres standard, but there should be a way to tell Typeorm to use camel case in JS.