Skip to content

A project using my own custom decorators such as @​Controller, @​Get, @​Post, @​Body, @​Query, @​Param, @​Use, etc. Auto Integration with Class Validators. Built using Reflect-Metadata.

Notifications You must be signed in to change notification settings

michaeldouglasdev/express-decorator-metadata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UserController Example with Class-Validator

@Controller('/users')
@Injectable({ name: "UserController" })
export class UserController {

  constructor (
    private getUserService: GetUserService,
    private createUserService: CreateUserService,
    private loginService: LoginService,
    private updatePasswordService: UpdatePasswordService,
  ) {}

  @Use([Logger])
  @Post()
  create(
    @Body(CreateUserRequest) user: CreateUserRequest,
  ) {
    this.createUserService.execute(user);

    return {
      user
    }
  }

  @Post('/login')
  login(
    @Body(LoginRequest) login: LoginRequest
  ): string {
    return this.loginService.execute(login);
  }

  @Use([UserAuthenticated])
  @Put('/update-password')
  updatePassword(
    @Body(UpdatePasswordRequest) updatePasswordRequest: UpdatePasswordRequest,
    @Context() context: RequestContext,
  ) {
    return this.updatePasswordService.execute(context.user.id, updatePasswordRequest)
  }

  @Use([Logger])
  @Get('/:id')
  async getById(
    @Param('id') id: string,
  ): Promise<User> {
   const user = await this.getUserService.execute({
      type: GetUserType.ID,
      value: id,
    });

    return user;
  }
}

Class Validator Example for CreateUserRequest

import { IsEmail, IsString, MinLength } from "class-validator";

export class CreateUserRequest {

  @IsString()
  name: string;

  @IsString()
  @MinLength(6)
  username: string;

  @IsString()
  @MinLength(4)
  password: string;

  @IsEmail()
  email: string;
}

About

A project using my own custom decorators such as @​Controller, @​Get, @​Post, @​Body, @​Query, @​Param, @​Use, etc. Auto Integration with Class Validators. Built using Reflect-Metadata.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published