All Projects → javascript-dragons → Nest Event

javascript-dragons / Nest Event

Licence: mit
Event handling with decorators for NestJS Framework

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Nest Event

Nestjs Learning
nestjs 学习教程 📚,跟我一起学习 nest 框架~ 💪
Stars: ✭ 638 (+398.44%)
Mutual labels:  nestjs, nest
Nest
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) 🚀
Stars: ✭ 42,981 (+33478.91%)
Mutual labels:  nestjs, nest
Graphql
GraphQL (TypeScript) module for Nest framework (node.js) 🍷
Stars: ✭ 697 (+444.53%)
Mutual labels:  nestjs, nest
Docs.nestjs.com
The official documentation https://docs.nestjs.com 📕
Stars: ✭ 389 (+203.91%)
Mutual labels:  nestjs, nest
Nest Passport
Nest authentication example using passport strategies
Stars: ✭ 44 (-65.62%)
Mutual labels:  nestjs, nest
Api
🏁🛠️ SaaS backend & API framework based on @nestjs
Stars: ✭ 390 (+204.69%)
Mutual labels:  nestjs, nest
Typeorm
TypeORM module for Nest framework (node.js) 🍇
Stars: ✭ 807 (+530.47%)
Mutual labels:  nestjs, nest
Nest Angular
NestJS, Angular 6, Server Side Rendering (Angular Universal), GraphQL, JWT (JSON Web Tokens) and Facebook/Twitter/Google Authentication, Mongoose, MongoDB, Webpack, TypeScript
Stars: ✭ 307 (+139.84%)
Mutual labels:  nestjs, nest
Typescript Starter
Nest framework TypeScript starter ☕️
Stars: ✭ 853 (+566.41%)
Mutual labels:  nestjs, nest
Nest Cli
CLI tool for Nest applications 🍹
Stars: ✭ 889 (+594.53%)
Mutual labels:  nestjs, nest
Nest Schedule
A cron-like and not-cron-like job distributed scheduler for Nest.js by decorators.
Stars: ✭ 368 (+187.5%)
Mutual labels:  nestjs, nest
Simple Todos
A simple web application powered by Nuxt.js 💚 & Nest Framework 😻
Stars: ✭ 81 (-36.72%)
Mutual labels:  nestjs, nest
Bull
Bull module for Nest framework (node.js) 🐮
Stars: ✭ 356 (+178.13%)
Mutual labels:  nestjs, nest
Docs.nestjs.cn
nestjs 中文文档
Stars: ✭ 393 (+207.03%)
Mutual labels:  nestjs, nest
Ng Universal
Angular Universal module for Nest framework (node.js) 🌷
Stars: ✭ 336 (+162.5%)
Mutual labels:  nestjs, nest
Swagger
OpenAPI (Swagger) module for Nest framework (node.js) 🌎
Stars: ✭ 734 (+473.44%)
Mutual labels:  nestjs, nest
Terminus
Terminus module for Nest framework (node.js) 🤖
Stars: ✭ 277 (+116.41%)
Mutual labels:  nestjs, nest
Nestjs Pino
Platform agnostic logger for NestJS based on Pino with REQUEST CONTEXT IN EVERY LOG
Stars: ✭ 283 (+121.09%)
Mutual labels:  nestjs, nest
Nodepress
😎 RESTful API service for Blog/CMS, powered by @nestjs
Stars: ✭ 829 (+547.66%)
Mutual labels:  nestjs, nest
Nestjs Roles
Type safe roles guard and decorator made easy
Stars: ✭ 78 (-39.06%)
Mutual labels:  nestjs, nest

Nest Event

NPM Version Package License NPM Downloads Travis

Event handler for Nest.js framework with decorators

Features

  • Communicate between modules without import
  • Organize event handlers with decorators
  • Work with multiple Event Emitters

Installation

$ npm i --save nest-event

Usage

Import NestEventModule into your root module (AppModule)

// app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NestEventModule } from 'nest-event';
@Module({
  imports: [NestEventModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Nest Event is coming with an internal event emitter. If you provide one without a name, the module do not create the internal emitter. Also, you can use any instance with extended from EventEmitter

To provide an emitter use @Emitter decorator.

import { EventEmitter } from 'events';
import { Injectable } from '@nestjs/common';
import { Emitter } from './nest-event';

@Emitter()
export class MyEventEmitter extends EventEmitter {}

You can provide multiple emitters with passing a name.

@Emitter('ws-emitter')
export class WebsocketClient extends Websocket {}

Event Handler

To adding a listener for an event you can use @On decorator.

import { Injectable } from '@nestjs/common';
import { On } from './nest-event';
import { User } from './interfaces';

@Injectable()
export class EmailService {

  @On('user-created')
  onUserCreated(user: User){
    // send verification email
  }
}

If you have multiple emitters you can separate the handlers with @From decorator.

  @From('ws-emitter')
  @On('subscribe')
  onSubscribe(channel: string){
    // do something
  }

Event Emitter

To access your emitters in different modules, controllers etc. You can use NestEventEmitter

import { NestEventEmitter } from './nest-event';

@Controller('user')
export class UserController {
  constructor(
    private readonly nestEventEmitter: NestEventEmitter,
    ) {}

  @Post('signup')
  signup() {
    // ...
    this.nestEventEmitter.emit('user-created', user);
  }
}

If you provide multiple emitters you can select one with:

 this.nestEventEmitter.emitter('my-emitter').emit('user-created', user);

Also, you can get your emitters as StrictEventEmitter

// define your events
 interface Events {
   request: (request: Request, response: Response) => void;
   done: void;
 }
 this.nestEventEmitter.strictEmitter<Events>().emit('done');
 //or
  this.nestEventEmitter.strictEmitter<Events>('my-emitter').emit('done');

Future Goals

  • Add tests;

Contributing

You are welcome to contribute to this project, just open a PR.

License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].