Created
December 21, 2016 19:12
-
-
Save mastilver/2783c5bf1b39f1bc5e65bf80c9128df1 to your computer and use it in GitHub Desktop.
Mediator pattern - Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Command from './CreateUserCommand'; | |
export default async handler(command : Command) { | |
await somewhatCreateAUser(command.username, command.password, command.email); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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