Skip to content

Extends tsmediator's Mediator to catch and return thrown responsekit Rejections.

License

Notifications You must be signed in to change notification settings

IRCraziestTaxi/responsekit-tsmediator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@responsekit/tsmediator

Install

This lib relies on the @responsekit/core lib.

To use @responsekit/tsmediator:

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

CommandResultMediator

Process your tsmediator commands with @responsekit/tsmediator's CommandResultMediator to gracefully handle Rejections thrown in the Validate method.

Your command handler would return Rejections in the Handle method and throw Rejections in the Validate method, like so:

import { CommandResult, GenericResponse, Rejection } from "@responsekit/core";
import { Handler, ICommandHandler } from "tsmediator";
import { Thing } from "../entities/Thing";
import { AddThingCommand } from "./AddThingCommand";

@Handler("AddThingHandler")
export class AddThingHandler implements ICommandHandler<
AddThingCommand,
Response<CommandResult<Thing>>
> {
    public async Handle(command: AddThingCommand): Promise<CommandResult<Thing>> {
        try {
            // Create the Thing.

            return new GenericResponse({
                value: createdThing
            });
        }
        catch (error) {
            return new Rejection(error);
        }
    }

    public Validate(command: AddThingCommand): void {
        if (!command) {
            return Rejection.BadRequest("Invalid request.");
        }
    }
}

Then, in your controller, etc.:

const result: CommandResult<Thing> = await new Mediator().Send("CommandHandler", command);

if (result instanceof Rejection) {
    // Return a response whose status is result.reason and whose payload contains result.message.
}

// Return a response with status 200 (or similar) whose payload is the GenericResponse that is the result.

For a base controller that handles returning the appropriate express Response for the resulting CommandResult, check out the @responsekit/express package.

About

Extends tsmediator's Mediator to catch and return thrown responsekit Rejections.

Resources

License

Stars

Watchers

Forks

Packages

No packages published