Nuclear Powered Marshmallows

    @responsekit/tsmediator
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    @responsekit/tsmediator

    Install

    This lib relies on the https://github.com/IRCraziestTaxi/responsekit 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 https://github.com/IRCraziestTaxi/responsekit-express package.

    Keywords

    none

    Install

    npm i @responsekit/tsmediator

    DownloadsWeekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    6.74 kB

    Total Files

    7

    Last publish

    Collaborators

    • ircraziesttaxi