Skip to content

@PreProcessor Decorator

Thiago Bustamante edited this page Jan 20, 2019 · 1 revision

Pre Processors

It is possible to add a function to process the request before the handler on an endpoint by endpoint basis. This can be used to add a validator to your application without including it in the body of the handler.

function validator(req: express.Request): express.Request {
  if (!req.body.userId) {
    throw new Errors.BadRequestError("userId not present");
  } 
}

@Path('users')
export class UserHandler {
  
  @Path('email')
  @POST
  @PreProcessor(validator)
  setEmail(body: any) {
    // will have body.userId
  }
}

PreProcessors can also be added to a class, applying it to all endpoints on the class

@Path('users')
@PreProcessor(validator)
export class UserHandler {
  
  @Path('email')
  @POST
  setEmail(body: any) {
    // will have body.user
  }
}