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 🙏

© 2025 – Pkg Stats / Ryan Hefner

safe-event

v1.1.1

Published

TypeScript event code generator from JSON schemas

Readme

safe-event

A TypeScript event generator library that creates strongly-typed event definitions with runtime validation.

Features

  • Generate TypeScript types from JSON schema definitions
  • Runtime validation using ajv
  • Type-safe event payload access
  • Domain-scoped event naming (e.g. "user:profile.updated")
  • Support for dot notation in event names
  • Support for multiple schema files

Installation

npm install safe-event

Quick Start

1. Configuration

After installation, a safe-event.config.json file is created in your project root:

{
  "schemaDir": "./events/schema",
  "outputDir": "./events/generated"
}

2. Define Events

Create event schema files in your schema directory (e.g. events/schema/user-events.json). The schema format follows the Ajv JSON Schema specification:

{
  "domain": "user",
  "events": {
    "role.assigned": {
      "schema": {
        "type": "object",
        "properties": {
          "userId": { "type": "string" },
          "role": {
            "type": "string",
            "enum": ["admin", "editor", "viewer"]
          },
          "assignedBy": { "type": "string" },
          "expiresAt": { "type": "number" }
        },
        "required": ["userId", "role", "assignedBy"],
        "additionalProperties": false
      }
    }
  }
}

3. Generate Types

npx safe-event

Note: Run this command whenever you update your event schemas to regenerate the TypeScript types.

4. Use Generated Types

import {
  userEvents,
  RoleAssignedEventData,
  RoleAssignedEventPayload,
} from "./events/generated/user-events";

// Create type-safe event with validation
const event = RoleAssignedEventData.from({
  userId: "123",
  role: "editor", // Type-safe: only "admin" | "editor" | "viewer" allowed
  assignedBy: "456",
  expiresAt: Date.now() + 86400000,
});

// Get event type string
const eventType = userEvents["role.assigned"]; // "user:role.assigned"

// Type casting example
function handleEvent(type: string, data: any) {
  if (type === userEvents["role.assigned"]) {
    // data is now typed as RoleAssignedEventPayload
    const payload = data as RoleAssignedEventPayload;
    console.log(`${payload.role} role assigned by ${payload.assignedBy}`);
  }
}

EventBus

A simple way to emit and listen to events across your application.

Basic Example

import { EventBus } from "./EventBus";
import {
  UserEvents,
  RoleAssignedEventData,
} from "./events/generated/user-events";

// Initialize
const eventBus = EventBus.init();

// Listen for events
eventBus.onEvent({
  event: UserEvents["role.assigned"],
  callback: (data) => {
    console.log("New role assigned:", data.role);
  },
});

// Emit events
eventBus.emitEvent({
  event: UserEvents["role.assigned"],
  data: RoleAssignedEventData.from({
    userId: "123",
    role: "editor",
    assignedBy: "456",
  }),
});

Advanced Features

Correlation IDs

Use correlation IDs to track related events:

// Emit with correlation ID
eventBus.emitEvent({
  event: UserEvents["role.assigned"],
  data: RoleAssignedEventData.from({
    /* ... */
  }),
  correlationId: "request-123",
});

// Access correlation ID in listener
eventBus.onEvent({
  event: UserEvents["role.assigned"],
  callback: (data, correlationId) => {
    console.log(`Event ${correlationId}:`, data);
  },
});

Buffered Events

Wait for multiple related events before processing:

// Listen for multiple events
eventBus.onDependentEvents({
  events: [UserEvents["profile.created"], UserEvents["role.assigned"]],
  callback: (buffer) => {
    // Access events by their names
    const profile = buffer[UserEvents["profile.created"]];
    const role = buffer[UserEvents["role.assigned"]];
    console.log("User setup complete:", { profile, role });
  },
});

// Emit related events
eventBus.emitEvent({
  event: UserEvents["profile.created"],
  data: ProfileCreatedEventData.from({
    /* ... */
  }),
  correlationId: "request-123",
});

eventBus.emitEvent({
  event: UserEvents["role.assigned"],
  data: RoleAssignedEventData.from({
    /* ... */
  }),
  correlationId: "request-123",
});

One-time Events

Listen for a single occurrence of an event with a specific correlation ID:

eventBus.onceEvent({
  event: UserEvents["role.assigned"],
  correlationId: "request-123",
  callback: (data) => {
    console.log("Role assigned:", data);
    // Listener automatically detaches after this callback
  },
});

License

MIT