Skip to content

Matusal3m/damex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decorators

Used in Classes

    @Controller

Receives a string indicating the route of the controller.

    @GlobalMiddleware

Receives an array with all the middleware methods that will be applied to all the methods of the controller.

    @Inject

Used on classes that required DI. If the class Already has a decorator (as @Controller), the @Inject is not required.

    @Implementations

Receives an array with the concrete classes that will be injected into the constructor parameters. The implementations must follow the same order as the constructor arguments. If used, the @Inject decorator is not required.

See a @Implementations usage example:

@Controller("/users")
@Implementations([ConsoleLogger, FileLogger])
class UserController {
  constructor(
    private readonly logger1: Logger,
    private readonly logger2: Logger
  ) {}

  @Get("/")
  getUsers(req: Request, res: Response) {
    this.logger1.log("fetching...");
    this.logger2.log("done!");
  }
}

Used in Methods

    @Get - path: string
    @Post - path: string
    @Put - path: string
    @Patch - path: string
    @Delete - path: string
    @Middleware - Array with methods

Example

@Controller('/users')
@GlobalMiddleware([auth])
export class UsersController {
    constructor(private userService: UserService) {}

    @Get()
    @Middleware([logger])
    async getAll(req: Request, res: Response) {
        const users = await this.userService.all();

        res.status(200).send(users);
    }

    @Get('/:id')
    @Middleware([anotherLogger])
    async getById(req: Request, res: Response) {
        const users = await this.userService.findById(req.params.id!);

        res.status(200).send(users);
    }
}

About

Use decorators with express to simplify your life.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors