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

@deltic/service-dispatcher

v0.2.3

Published

A contract-based service abstraction

Downloads

420

Readme

@deltic/service-dispatcher

A contract-based service dispatch abstraction with type-safe request/response mapping and middleware support.

Installation

npm install @deltic/service-dispatcher

Usage

Defining a Service

Define a service structure that maps command types to their payload and response:

import type {ServiceStructure} from '@deltic/service-dispatcher';

type UserService = ServiceStructure<{
    createUser: {
        payload: {name: string; email: string};
        response: {id: string};
    };
    deleteUser: {
        payload: {id: string};
        response: void;
    };
}>;

Dispatching Commands

import {ServiceDispatcher} from '@deltic/service-dispatcher';

const service = new ServiceDispatcher<UserService>({
    createUser: async (payload) => {
        const user = await userRepo.create(payload);
        return {id: user.id};
    },
    deleteUser: async (payload) => {
        await userRepo.delete(payload.id);
    },
});

const result = await service.handle({
    type: 'createUser',
    payload: {name: 'Alice', email: '[email protected]'},
});
// result: {id: '...'}

Middleware

Add cross-cutting concerns via middleware:

import type {ServiceMiddleware} from '@deltic/service-dispatcher';

const loggingMiddleware: ServiceMiddleware<UserService> = async (input, next) => {
    console.log(`Handling ${String(input.type)}`);
    const result = await next(input);
    console.log(`Completed ${String(input.type)}`);
    return result;
};

const service = new ServiceDispatcher<UserService>(handlers, [loggingMiddleware]);

Locking Middleware

Prevent concurrent execution of commands for the same resource:

import {createServiceLockingMiddleware} from '@deltic/service-dispatcher/locking-middleware';

const lockingMiddleware = createServiceLockingMiddleware<UserService, string>({
    mutex,
    lockResolver: (input) => input.payload.id,
    timeoutMs: 5000,
});

const service = new ServiceDispatcher<UserService>(handlers, [lockingMiddleware]);

Locking Decorator

Alternatively, wrap an entire service with locking:

import {ServiceLocking} from '@deltic/service-dispatcher/locking-decorator';

const lockedService = new ServiceLocking<UserService, string>(service, {
    mutex,
    lockResolver: (input) => input.payload.id,
    shouldSkip: (input) => input.type === 'listUsers', // optional
});

Aggregate Service

For event-sourced aggregates, use the aggregate service dispatcher that auto-persists aggregates with unreleased events:

import {AggregateServiceDispatcher} from '@deltic/service-dispatcher/aggregate-service-dispatcher';

const service = new AggregateServiceDispatcher<UserService, UserStream>(
    {
        createUser: async (payload, aggregate) => {
            aggregate.create(payload.name, payload.email);
        },
    },
    aggregateRepository,
    (input) => input.payload.id,
);

API Reference

Service<S> (interface)

interface Service<S> {
    handle<T extends keyof S>(input: {type: T; payload: S[T]['payload']}): Promise<S[T]['response']>;
}

ServiceDispatcher<S>

Dispatches inputs to type-specific handlers through an optional middleware chain.

new ServiceDispatcher(handlers: ServiceHandlers<S>, middlewares?: ServiceMiddleware<S>[])

ServiceMiddleware<S> (interface)

interface ServiceMiddleware<S> {
    (input, next): Promise<response>;
}

InputNotSupported

Thrown when no handler is registered for the input type.

License

ISC