npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@qnn92/mediatorts

v1.0.8

Published

This project implements the Mediator pattern along with the CQRS pattern in TypeScript.

Downloads

6

Readme

TypeScript Mediator Pattern with Command and Query Responsibility Segregation (CQRS)

This project implements the Mediator pattern along with the CQRS pattern in TypeScript.

Files

interfaces.ts

This file contains the interfaces and types used throughout the application, which includes:

  • ICommand: The base interface for commands.
  • AuthorizeCommand: An example implementation of the ICommand interface.
  • ICommandHandler: Interface for command handlers.
  • ICommandValidator: Interface for command validators.
  • IContainer: Interface for the container, which holds handlers and validators.
  • IPipelineBehavior: Interface for defining the pipeline behavior.

mediator.ts

This file contains the Mediator class which manages and routes commands to their appropriate handlers. The mediator also executes any pipeline behaviors before processing the command.

container.ts

The container file is a simple Dependency Injection container which stores instances of handlers and validators. It also contains decorators to register handlers and validators to the container:

  • RegisterHandler: A decorator to register a handler class to the container.
  • RegisterValidator: A decorator to register a validator class to the container.
  • Authorize: A decorator to add an authorization mechanism to the handlers. This decorator can be used on classes to add a list of authorized roles.

Usage

In this example, we'll create a SumCommand and a SumCommandHandler to calculate the sum of two numbers.

First, define the SumCommand:

// This could be in your commands.ts file
export class SumCommand implements ICommand {
  constructor(public num1: number, public num2: number) {}
}

Next, define the SumCommandHandler:

import { ICommandHandler } from './interfaces';
import { RegisterHandler } from './container';
import { SumCommand } from './commands';

// This could be in your handlers.ts file
@RegisterHandler
export class SumCommandHandler implements ICommandHandler<SumCommand, number> {
  async handle(command: SumCommand): Promise<number> {
    return command.num1 + command.num2;
  }
}

Finally, you can use the SumCommand and SumCommandHandler like this:

// Import mediator from mediator.ts file
import { mediator } from './mediator';
import { SumCommand } from './commands';

async function calculateSum() {
  const command = new SumCommand(3, 5);
  const result = await mediator.send<number>(command);
  console.log(`The sum is ${result}`);
}

calculateSum();

Publish packages to npm

  • npm login
  • npm init --scope=@@qnn92: init organization-scoped package
  • npm publish --access public: publish package