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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tsmediator

v0.1.5

Published

Tiny flexible mediator implemented in TypeScript

Readme

TS Mediator

Tiny flexible mediator implemented in TypeScript

Installation

Production

$ npm install --save tsmediator

Development

$ git clone https://github.com/alveflo/tsmediator.git
$ cd tsmediator
$ npm install && npm test

Usage

Setup

No setup is required per se, other than registering handlers for specific messages (commands/queries). However, this implemented relies on ES7 decorators and therefore experimentalDecorators needs to be set to true in your tsconfig.json file.

API

Mediator

  • Mediator.Send(type: string, payload: T) where type is the registered message type and payload is the data sent, where T is the datatype that the handler is expected to process.

Registering handlers

Registering a handler is easy, simple add the decorator Handler(message) onto your class, and implement the ICommandHandler interface and you're ready to roll. Note that this automatic registration only works if you, somewhere in your code, is referring to the handlers type (e.g. CmdHandler.Type as shown below).

If you're not explicitly referring to the handlers type, the decorator will not be run automatically and the handler will not be registered. In this case you might want to register the handler manually. In that case, simply use

import { Container } from 'tsmediator';

Container.RegisterHandler(YourHandler);

Command handler example

import { ICommandHandler, Handler } from 'tsmediator';

@Handler(CmdHandler.Type)
class CmdHandler implements ICommandHandler<string, number> {
    public static get Type():string { return "foo"; }

    Log() {}

    Validate(payload: string) {
        if (payload !== 'foo bar') {
            throw Error('Some error');
        }
    }

    Handle(payload: string) {
        return 5 * 5;
    }
}    

Send example

The following example shows how you would send a command into the registered handlers above.

import { Mediator } from 'tsmediator';
let mediator: Mediator = new Mediator();

mediator.Send(CmdHandler.Type, 5);

Middlewares

The mediator also supports adding middleware to the mediator. This is used if you have any generic actions that you would like to be triggered every time a the mediator is used. What you need to do is create a class that implements IMediatorMiddleware and register the middleware with the mediator.

Execution order

The order that you register the middlewares will also be the order that they will be executed. If you have registered two middlewares they will be executed in this order: Middleware1 -> Middleware2 -> Handler -> Middleware2 -> Middleware1

Example

As an example we can easily create a logging middleware:

class LoggingMiddleware implements IMediatorMiddleware {
    private logger: ILogger;
    constructor() {
        this.logger = new Logger();
    }

    public PreProcess(payload: any): void {
        logger.Log(`Processing object ${JSON.stringify(payload)}`)
    }

    public PostProcess(payload: any, response: any): void {
        logger.Log(`Was: ${JSON.stringify(payload)}, now is: ${JSON.stringify(response)}`);
    }
}

To register the middleware, you do this:

let loggingMiddleware = new LoggingMiddleware();
mediator.Use(loggingMiddleware);

Overriding mediator behaviour

You can easily override the default mediator behaviour by extending the BaseMediator. The only thing you need to do is to resolve the registered handler class by calling BaseMediator.Resolve(message). The message to handler connections are handled under the hood, so you don't need to worry about that at all.

import { BaseMediator } from "./baseMediator";

export class MyCustomMediator extends BaseMediator {
    public Send(command: string, payload: any) {
        return this.Process(command, payload);
    }

    public Request(query: string, payload: any) {
        return this.Process(query, payload);
    }

    private Process(msg: string, payload: any) {
        let handler: any = super.Resolve(msg);

        try {
            handler.Validate(payload);
        } catch (ex) {
            throw ex;
        }

        handler.Log();
        return handler.Handle(payload);
    }
}

Licence

MIT License

Copyright (c) 2017- Victor Alveflo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.