0.1.0 • Published 4 months ago

@tsdiapi/events v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

TSDIAPI-Events: Event Handling Plugin for TSDIAPI-Server

The TSDIAPI-Events plugin adds robust, scalable event management to TSDIAPI-Server. Built with mitt and TypeDI, it allows event-driven communication through TypeScript decorators, ensuring clean and modular code.


Features

  • Event Decorators: Use @On(event) to register event listeners.
  • Dispatch Events: Trigger events with dispatchEvent().
  • Type Safety: Define events with strict TypeScript types for payloads.
  • Integration: Works with the TSDIAPI-Server lifecycle and dependency injection system.
  • File Autoloading: Supports automatic loading of event files with glob patterns.

Installation

Install via NPM or CLI:

npm install @tsdiapi/events

or using the CLI:

tsdiapi plugins add events

Code Generation

NameDescription
baseCreate a new event file with a basic event listener.

The TSDIAPI-Events plugin includes an event generator to streamline event creation. Use the tsdiapi CLI command to generate event files automatically:

tsdiapi generate events

Example Usage

Define and Handle Events

import { On, dispatchEvent } from "@tsdiapi/events";
import { Service } from "typedi";

export enum EventType {
  onNewUser = "onNewUser",
  onAccountCreated = "onAccountCreated",
}

export interface EventPayloads {
  [EventType.onNewUser]: { userId: string; name: string };
  [EventType.onAccountCreated]: { accountId: string };
}

@Service()
export class EventListener {
  @On(EventType.onNewUser)
  public handleNewUser(payload: EventPayloads[EventType.onNewUser]) {
    console.log("New user:", payload.name);
  }
}

// Dispatch an event
dispatchEvent(EventType.onNewUser, { userId: "123", name: "John Doe" });

Plugin Setup

Register the plugin in your createApp function:

import { createApp } from "@tsdiapi/server";
import TSDIAPIEventsPlugin from "@tsdiapi/events";

createApp({
  plugins: [TSDIAPIEventsPlugin()],
});

Configuration

The plugin accepts the following option:

OptionTypeDefault ValueDescription
autoloadGlobPathstring"*.event{.ts,.js}"Glob pattern for automatically loading event files.

Example:

import TSDIAPIEventsPlugin from "@tsdiapi/events";

createApp({
  plugins: [
    TSDIAPIEventsPlugin({
      autoloadGlobPath: "*.event.ts", // Custom glob pattern
    }),
  ],
});

Summary

With TSDIAPI-Events, you can build scalable, event-driven APIs with minimal boilerplate. It integrates seamlessly with the TSDIAPI ecosystem, enabling better modularity, maintainability, and type-safe communication between components.

For more information, visit the TSDIAPI-Server GitHub.

0.1.0

4 months ago

0.0.1

4 months ago

0.0.1-alpha.9

5 months ago

0.0.1-alpha.8

5 months ago

0.0.1-alpha.7

5 months ago

0.0.1-alpha.6

5 months ago

0.0.1-alpha.5

5 months ago

0.0.1-alpha.4

5 months ago

0.0.1-alpha.3

6 months ago

0.0.1-alpha.2

6 months ago

0.0.1-alpha.1

6 months ago