Skip to content

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

License

Notifications You must be signed in to change notification settings

IRCraziestTaxi/responsekit-express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@responsekit/express

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

Install

This lib relies on the @responsekit/core lib.

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published