Skip to content

Instantly share code, notes, and snippets.

@mastilver
Created December 21, 2016 19:12
Show Gist options
  • Save mastilver/2783c5bf1b39f1bc5e65bf80c9128df1 to your computer and use it in GitHub Desktop.
Save mastilver/2783c5bf1b39f1bc5e65bf80c9128df1 to your computer and use it in GitHub Desktop.
Mediator pattern - Part 1
import handler from './handler'
type ICommand = {
username: string;
password: string;
email: string;
}
@handler('./createUserCommandHandler')
export default class CreateUserCommand {
constructor(o : ICommand) {
Object.assign(this, o);
}
username: string;
password: string;
email: string;
}
import Command from './CreateUserCommand';
export default async handler(command : Command) {
await somewhatCreateAUser(command.username, command.password, command.email);
}
import { resolve, dirname } from 'path';
import parentModule from 'parent-module';
export default function (handlerPath: string) {
return function (command) {
handlerPath = resolve(dirname(parentModule()), handlerPath);
// eslint-disable-next-line import/no-dynamic-require
const handler = require(handlerPath).default;
command.handler = handler;
};
};
import winston from 'winston';
export async function exec(command) {
const commandHandler = command.constructor.handler;
try {
return await commandHandler(command, context);
} catch (err) {
throw err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment