# nest-typed-event-emitter

> Simple wrapper around the NestJS event-emitter package to provide type hinting

Latest version **1.0.4** (published 2022-07-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install nest-typed-event-emitter
pnpm add nest-typed-event-emitter
yarn add nest-typed-event-emitter
bun add nest-typed-event-emitter
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-07-12 |
| First published | 2022-07-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Fraser McCallum |
| Maintainers | frasermcc |

## Links

- npm: https://www.npmjs.com/package/nest-typed-event-emitter
- Repository: https://github.com/frasermcc9/nest-typed-event-emitter
- Homepage: https://github.com/frasermcc9/nest-typed-event-emitter#readme
- Issues: https://github.com/frasermcc9/nest-typed-event-emitter/issues
- npm.io page: https://npm.io/package/nest-typed-event-emitter

## Recent versions

- 1.0.4 (latest) — 2022-07-12
- 1.0.3 — 2022-07-12
- 1.0.2 — 2022-07-12
- 1.0.1 — 2022-07-12
- 1.0.0 — 2022-07-12

## README

# Nest Typed Event Emitter

![npm](https://img.shields.io/npm/v/nest-typed-event-emitter)

## Why

The default NestJS event emitter system does not directly support strict hinting
for events. This package aims to add minimal support for having type-hinted
events, with strict typing if desired.

## How

First, define an interface for your events

```ts
// Your Event Map, with EventName => Types
export interface EventMap {
    purchase: number;
    paid: number;
}
```

Next, use the package to create listeners and emitters that wrap the normal
NestJS entities.

```ts
import { IEventEmitter, createTypedListener } from "nest-typed-event-emitter";

// API to define the typed emitters
export type MyEventEmitter = IEventEmitter<EventMap>;
// Pass true to the second type parameter to make the emitters strict
export type MyStrictEventEmitter = IEventEmitter<EventMap, true>;

// API to define the typed listeners
const MyListener = createTypedListener<EventMap>();
// Pass true to the second type parameter to make the listeners strict
const MyStrictListener = createTypedListener<EventMap, true>();
```


Next, use the listeners you defined in place of the usual `@OnEvent()`.

```ts
// Listener class
class Listeners {
    /**
     * This will type hint, but allow any value to be passed
     */
    @MyListener("paid") // allowed
    @MyListener("paidasdf") //allowed
    public onSleepAdded(event: EventMap["paid"]) {}

    /**
     * This will type hint and forbid values not in the event map.
     */
    @MyStrictListener("purchasef") // error
    @MyStrictListener("purchase") // allowed
    public onSleepAddedStrict(event: EventMap["purchase"]) {}
}
```

Finally, use the `@InjectEventEmitter()` decorator to get access to the emitter.
You can then annotate it with the custom event emitter type you made in the
second part.

```ts
import { Injectable } from "@nestjs/common";
import { InjectEventEmitter } from "nest-typed-event-emitter";

// Emitter class
@Injectable()
class Service {
    constructor(
        @InjectEventEmitter() private readonly emitter: MyEventEmitter,
        @InjectEventEmitter() private readonly strictEmitter: MyStrictEventEmitter
    ) {}

    onPurchase() {
        this.emitter.emit("purchase", 4); // allowed
        this.emitter.emit("something-else", 12); //allowed
        this.emitter.emit("purchase", "12"); //allowed

        this.strictEmitter.emit("paid", 5); //allowed
        this.strictEmitter.emit("ashjknfl", 34); //error
        this.strictEmitter.emit("paid", "34"); //error
    }
}
```

Remember to add the normal NestJS event emitter module into your app, i.e.

```ts
import { Module } from "@nestjs/common";
import { EventEmitterModule } from "@nestjs/event-emitter";

@Module({
  imports: [EventEmitterModule.forRoot()],
  controllers: [],
  providers: [],
})
export class AppModule {}
```

---
_Source: https://npm.io/package/nest-typed-event-emitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
