1.0.0 • Published 5 years ago

@responsekit/express v1.0.0

Weekly downloads
27
License
MIT
Repository
github
Last release
5 years ago

@responsekit/express

Wraps a base controller that returns express Responses with responsekit payloads.

Install

To use @responsekit/express:

npm install --save @responsekit/core @responsekit/express

CommandResultController

Extend your controller from CommandResultController to send a response based on a CommandResult and an express Response.

For example, using routing-controllers:

import { CommandResult } from "@responsekit/core";
import { CommandResultController } from "@responsekit/express";
import { JsonController, Post, Res } from "routing-controllers";
import { Mediator } from "tsmediator";
import { AddThingCommand } from "../commands/addThing/AddThingCommand";
import { Thing } from "../entities/Thing";

@JsonController("/things")
export class ThingController extends CommandResultController {
    @Post()
    public async addThing(
        @Body()
        command: AddThingCommand,
        @Res()
        response: Response
    ): Promise<Response> {
        const result: CommandResult<Thing> = await new Mediator().Send("AddThingHandler", command);

        // Return a response with the default HTTP status code of 200 if result was successful
        // or the appropriate HTTP status code based on the returned Rejection.
        return this.sendResponse(result, response);

        // Return a response with a custom HTTP status code of 201 if result was successful
        // or the appropriate HTTP status code based on the returned Rejection.
        return this.sendResponse(result, response, 201);
    }
}

For an extension of tsmediator's Mediator that handles returning thrown Rejections in a Validate method, for instance, check out the @responsekit/tsmediator package.