RouteIO

RouteIO acts as a mapper, tasked with converting data from the request into input for the controller, and the controller's result into the response. Each route, if it provides or returns data, is automatically assigned a RouteIO where this data parsing should be performed. RouteIO contains two optional methods (toResponse, fromRequest), depending on whether the route expects or/and returns data.

export class GetCustomerDetailsRouteIO implemets RouteIO {
  public toResponse(output: Result<Customer>): Response {
    if (result.isFailure) {
      // decide on the status type and content
      return response.status(500).send(result.failure.error.message);
    }
    const { ... } = result.content;
    // map result content to output if needed
    return response.status(200).send({...});
  }
  
  public fromRequest(request: Request): string {
    return request.params.id;
  }
}

Last updated