A mediator for UseCase and Command.


496251.2.32 years ago4 years agoMinified + gzip package size for @almin/usecase-bus in KB



A mediator for UseCase and Command. This library provide Command Bus.

What is Command and Command handler pattern?


  • A bus send Command to a single Command Handler
  • Command may be rejected by system
  • Command may be failed during executing in Handler
  • Command may be various effect in system state
  • Command does not be over the boundary
  • Command should have imperative named.

What is merits?

  • Flexible
  • Retry the UseCase
  • Logging
  • Adoptable
  • Additional Event

What is de-merits?

  • Add new Layer(CommandBus)


Install with npm:

npm install @almin/usecase-bus


import { UseCase, Context } from "almin";
import { UseCaseCommandBus } from "@almin/usecase-bus"
// async code
(async () => {
    const context = new Context({
        store: new NopeStore()

    class CommandA {
        type = "CommandA";

    class CommandB {
        type = "CommandB";

    const executed: (CommandA | CommandB)[] = [];

    class TestUseCaseA extends UseCase {
        execute(command: CommandA) {

    class TestUseCaseB extends UseCase {
        execute(command: CommandB) {

    const createTestUseCaseB = (_command: CommandB) => {
        return new TestUseCaseB();

    // create binding between Command Constructor and UseCase/UseCaseFactory.
    const bus = UseCaseCommandBus.create(context)
        .bind(CommandA, new TestUseCaseA())
        .bindFactory(CommandB, createTestUseCaseB);
    // send CommandA => execute TestUseCaseA
    await bus.send(new CommandA());
    assert.strictEqual(executed.length, 1);
    assert.ok(executed[0] instanceof CommandA);
    // send CommandB => execute createTestUseCaseB()
    await bus.send(new CommandB());
    assert.strictEqual(executed.length, 2);
    assert.ok(executed[1] instanceof CommandB);


See Releases page.



Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D



MIT © azu

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.