# nestjs-typed-events

> Typesafe NestJS event emitter module

Latest version **1.0.2** (published 2022-12-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-typed-events
pnpm add nestjs-typed-events
yarn add nestjs-typed-events
bun add nestjs-typed-events
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-12-26 |
| First published | 2022-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 31.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | tjaybroodryk |
| Keywords | nestjs, eventemitter, eventemitter2, events |

## Links

- npm: https://www.npmjs.com/package/nestjs-typed-events
- Repository: https://github.com/tjbroodryk/nestjs-typed-events
- Homepage: https://github.com/tjbroodryk/nestjs-typed-events#readme
- Issues: https://github.com/tjbroodryk/nestjs-typed-events
- npm.io page: https://npm.io/package/nestjs-typed-events

## Dependencies (3)

- [zod](https://npm.io/package/zod.md) ^3.20.2
- [@golevelup/nestjs-modules](https://npm.io/package/@golevelup/nestjs-modules.md) ^0.6.1
- [@golevelup/nestjs-discovery](https://npm.io/package/@golevelup/nestjs-discovery.md) ^3.0.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-12-26
- 1.0.1 — 2022-12-26
- 1.0.0 — 2022-12-26

## README

<h1 align="center"></h1>

<div align="center">
  <a href="http://nestjs.com/" target="_blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="150" alt="Nest Logo" />
  </a>
</div>

<h3 align="center">NestJS Type Safe Event Emitter Module</h3>

<div align="center">
  <a href="https://nestjs.com" target="_blank">
    <img src="https://img.shields.io/badge/built%20with-NestJs-red.svg" alt="Built with NestJS">
  </a>
</div>

Type Safe, contract-driven event emitter module for a NestJS application.

Uses [zod](https://github.com/colinhacks/zod) object defintions to create typesafe event listeners and emitter, with automatic validation of args.

## Features

- End-to-end type safe wrapper for `eventemitter2`
- Wild card events
- Enforce events have a listener via `contract.required(<event>, <schema>)`
- Optional event listeners via `contract.optional(<event>, <schema>)`

## Installation

```bash
npm i eventemitter2 nestjs-typed-events
```

## Limitations

- You can have multiple event listeners on one class **only** if the events have the same args signature.

## Example

1. Register in `app.module`

```typescript
import { EventContractBuilder, EventEmitterModule } from 'nestjs-typed-events';

const contract = EventContractBuilder.create()
  .required(
    'test.event',
    z.object({
      foo: z.string(),
    }),
  )
  .optional('optional.event', z.string())
  .build();

@Module({
  imports: [
    EventEmitterModule.forRoot(EventEmitterModule, {
      contract,
    }),
  ],
})
export class AppModule {}
```

2. Register your event listener

```typescript
import { Injectable } from '@nestjs/common';
import {
  EventListenerDecoratorFactory,
  ContractEvent,
} from 'nestjs-typed-events';

const ContractEvent = EventListenerDecoratorFactory(contract);

@Injectable()
@ContractEvent('test.event')
export class TestWatcher {
  async handle(args: ContractEvent<typeof contract['test.event']>) {}
}
```

3. Profit

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