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

@nestwhats/webhook

v1.0.0

Published

Dynamic webhook event binding with persistence for NestWhats

Readme

About

@nestwhats/webhook extends NestWhats with outbound dispatch — mark regular @On/@Once listeners with @Webhook() to make them bindable/unbindable at runtime per client (including virtual clients), with per-handler granularity. Handler results can be forwarded to external systems (HTTP webhooks, queues, CRM integrations, chat platforms).

Its storage holds bindings only — which handler is attached to which client. Virtual clients themselves belong to the core: configure storage on NestWhatsModule.forRoot to persist those. For programmatic sending, use NestWhatsMessagingService, also from the core.

[!NOTE] Requires nestwhats ^4.0.0 and a platform adapter package.

Installation

npm i nestwhats @nestwhats/webhook
yarn add nestwhats @nestwhats/webhook
pnpm add nestwhats @nestwhats/webhook

Usage

Basic setup

Import NestWhatsWebhookModule alongside your NestWhatsModule. Use forRoot to enable persistent storage (saved to .nestwhats/webhook-bindings.json by default):

import { Module } from '@nestjs/common';
import { NestWhatsModule } from 'nestwhats';
import { NestWhatsWebhookModule } from '@nestwhats/webhook';
import { WhatsAppWebJsAdapterFactory } from '@nestwhats/platform-whatsapp-web.js';
import { LocalAuth } from 'whatsapp-web.js';

@Module({
  imports: [
    NestWhatsModule.forRoot({
      adapter: new WhatsAppWebJsAdapterFactory({ authStrategy: new LocalAuth() }),
    }),
    NestWhatsWebhookModule.forRoot(),
  ],
})
export class AppModule {}

Without forRoot, the module still works but bindings are not persisted across restarts.


Binding webhook events

Mark regular @On/@Once listeners with @Webhook() to register handlers that can be bound/unbound at runtime. They use the standard NestWhats context (the NestWhatsClient comes first) and full event typing — including platform-specific events added by adapter packages:

import { Injectable } from '@nestjs/common';
import { Context, ContextOf, On, Once } from 'nestwhats';
import { Webhook } from '@nestwhats/webhook';

@Injectable()
export class CrmDispatcher {
  @Webhook()
  @On('message')
  async onDirectMessage(@Context() [client, msg]: ContextOf<'message'>) {
    if (!msg.chatId.endsWith('@user')) return;
    // forward to your CRM, queue, or HTTP endpoint — client.name tells you which client fired
  }

  @Webhook()
  @On('message')
  async onGroupMessage(@Context() [, msg]: ContextOf<'message'>) {
    if (!msg.chatId.endsWith('@g.us')) return;
    // different handler, same event
  }

  @Webhook()
  @Once('ready')
  async onReady() {
    // fires once when the client connects
  }
}

[!NOTE] @Webhook() handlers are excluded from the automatic binding — they only fire after being bound via NestWhatsWebhookService (or the dashboard). If the target client's adapter does not support the event (see supportedEvents), the bind is skipped with a warning.

Multiple handlers for the same event are tracked independently — each one can be bound or unbound individually.

Per-client binding

@Webhook()
@On('message', { client: 'PERSONAL' })
async onPersonalMessage(@Context() [, msg]: ContextOf<'message'>) {}

An empty client: [] makes the handler bindable to no client — useful to park a handler without deleting it.

Programmatic bind/unbind

import { NestWhatsWebhookService } from '@nestwhats/webhook';

@Injectable()
export class MyService {
  constructor(private readonly webhook: NestWhatsWebhookService) {}

  bindAll() {
    this.webhook.register(); // bind all handlers to the default client
  }

  bindSelective() {
    this.webhook.register({
      client: 'PERSONAL',
      handlers: ['CrmDispatcher.onDirectMessage'],
    });
  }

  unbind() {
    this.webhook.unregister({ handlers: ['CrmDispatcher.onGroupMessage'] });
  }
}

Handler keys follow the format ClassName.methodName.


Storage

By default, forRoot() persists bindings to .nestwhats/webhook-bindings.json and restores them on startup. The file is also watched for external changes — edits made outside the application are synced automatically.

Custom storage path

NestWhatsWebhookModule.forRoot({
  storage: new JsonFileWebhookStorage('./data/bindings.json'),
})

Custom storage adapter

Implement WebhookStorageAdapter to use any backend (Redis, database, etc.):

import { WebhookStorageAdapter, WebhookStorageState } from '@nestwhats/webhook';

class RedisWebhookStorage implements WebhookStorageAdapter {
  async load(): Promise<WebhookStorageState> { /* ... */ }
  async save(state: WebhookStorageState): Promise<void> { /* ... */ }
}

NestWhatsWebhookModule.forRoot({ storage: new RedisWebhookStorage() })

This storage holds bindings only. Virtual clients belong to the core — configure storage on NestWhatsModule.forRoot to persist them (see nestwhats).


Logger options

Control which lifecycle events are logged. Pass false to silence all logs, true (default) to enable all, or a granular object:

NestWhatsWebhookModule.forRoot({
  logger: {
    bind: true,          // handler bound to a client
    unbind: true,        // handler unbound from a client
    restore: true,       // bindings restored from storage on startup
    fileChanged: true,   // storage file changed externally
    stale: true,         // stored key no longer matches any discovered handler
    syntaxError: true,   // storage file has a JSON parse error
    maxWarningCount: 5,  // suppress stale/syntaxError warnings after N occurrences
  },
})

Async configuration

NestWhatsWebhookModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    storage: new JsonFileWebhookStorage(config.get('WEBHOOK_STORAGE_PATH')),
    logger: config.get('DEBUG') ? true : { bind: false, unbind: false },
  }),
})

Dashboard integration

When using @nestwhats/dashboard, enable webhook controls to bind/unbind handlers directly from the UI:

NestWhatsDashboardModule.forRoot({ webhook: true })

Binding changes made in the dashboard are persisted and trigger live SSE updates.

License

GPL-3.0