{"id":13598791,"url":"https://github.com/yak0/nest-event","last_synced_at":"2025-10-28T11:31:42.494Z","repository":{"id":35836710,"uuid":"219616026","full_name":"yak0/nest-event","owner":"yak0","description":"Event handling with decorators for NestJS Framework","archived":false,"fork":false,"pushed_at":"2023-05-10T22:18:10.000Z","size":835,"stargazers_count":150,"open_issues_count":12,"forks_count":7,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-02-01T15:01:56.204Z","etag":null,"topics":["event-driven-architecture","event-driven-programming","eventemitter","nest","nestjs"],"latest_commit_sha":null,"homepage":"https://twitter.com/jsdragonsdev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yak0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-11-04T23:27:23.000Z","updated_at":"2024-11-28T08:22:39.000Z","dependencies_parsed_at":"2024-01-12T19:48:25.729Z","dependency_job_id":"80b23fe1-173b-4545-a6ca-afb80b5528fc","html_url":"https://github.com/yak0/nest-event","commit_stats":null,"previous_names":["javascript-dragons/nest-event"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak0%2Fnest-event","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak0%2Fnest-event/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak0%2Fnest-event/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak0%2Fnest-event/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yak0","download_url":"https://codeload.github.com/yak0/nest-event/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238645827,"owners_count":19506916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-driven-architecture","event-driven-programming","eventemitter","nest","nestjs"],"created_at":"2024-08-01T17:00:56.504Z","updated_at":"2025-10-28T11:31:42.113Z","avatar_url":"https://github.com/yak0.png","language":"TypeScript","funding_links":[],"categories":["Components \u0026 Libraries","资源"],"sub_categories":["组件和库"],"readme":"## Nest Event\r\n\u003cp\u003e\r\n  \u003ca href=\"https://www.npmjs.com/~nest-event\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/nest-event.svg\"\r\n      alt=\"NPM Version\" /\u003e\u003c/a\u003e\r\n  \u003ca href=\"https://www.npmjs.com/~nest-event\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/nest-event.svg\"\r\n      alt=\"Package License\" /\u003e\u003c/a\u003e\r\n  \u003ca href=\"https://www.npmjs.com/~nest-event\" target=\"_blank\"\u003e\u003cimg\r\n      src=\"https://img.shields.io/npm/dm/nest-event.svg\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\r\n  \u003ca href=\"https://travis-ci.org/javascript-dragons/nest-event\"\u003e\u003cimg\r\n      src=\"https://api.travis-ci.org/javascript-dragons/nest-event.svg?branch=master\" alt=\"Travis\" /\u003e\u003c/a\u003e\r\n\u003c/p\u003e\r\n  \u003cp \u003eEvent handler for \u003ca href=\"http://nestjs.com\" target=\"_blank\"\u003eNest.js\u003c/a\u003e framework with decorators \u003c/p\u003e\r\n\r\n### Features\r\n- Communicate between modules without import\r\n- Organize event handlers with decorators\r\n- Work with multiple Event Emitters\r\n\r\n### Installation\r\n\r\n```bash\r\n$ npm i --save nest-event\r\n```\r\n### Usage\r\nImport `NestEventModule` into your root module _(`AppModule`)_\r\n\r\n```ts\r\n// app.module.ts\r\n\r\nimport { Module } from '@nestjs/common';\r\nimport { AppController } from './app.controller';\r\nimport { AppService } from './app.service';\r\nimport { NestEventModule } from 'nest-event';\r\n@Module({\r\n  imports: [NestEventModule],\r\n  controllers: [AppController],\r\n  providers: [AppService],\r\n})\r\nexport class AppModule {}\r\n```\r\n\r\nNest 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`\r\n\r\nTo provide an emitter use `@Emitter` decorator.\r\n\r\n```ts\r\nimport { EventEmitter } from 'events';\r\nimport { Injectable } from '@nestjs/common';\r\nimport { Emitter } from './nest-event';\r\n\r\n@Emitter()\r\nexport class MyEventEmitter extends EventEmitter {}\r\n```\r\nYou can provide multiple emitters with passing a name.\r\n```ts\r\n@Emitter('ws-emitter')\r\nexport class WebsocketClient extends Websocket {}\r\n```\r\n\r\n#### Event Handler\r\n\r\nTo adding a listener for an event you can use `@On` decorator.\r\n\r\n```ts\r\nimport { Injectable } from '@nestjs/common';\r\nimport { On } from './nest-event';\r\nimport { User } from './interfaces';\r\n\r\n@Injectable()\r\nexport class EmailService {\r\n\r\n  @On('user-created')\r\n  onUserCreated(user: User){\r\n    // send verification email\r\n  }\r\n}\r\n```\r\nIf you have multiple emitters you can separate the handlers with `@From` decorator.\r\n\r\n```ts\r\n  @From('ws-emitter')\r\n  @On('subscribe')\r\n  onSubscribe(channel: string){\r\n    // do something\r\n  }\r\n```\r\n#### Event Emitter\r\n\r\nTo access your emitters in different modules, controllers etc. You can use  `NestEventEmitter`\r\n\r\n```ts\r\nimport { NestEventEmitter } from './nest-event';\r\n\r\n@Controller('user')\r\nexport class UserController {\r\n  constructor(\r\n    private readonly nestEventEmitter: NestEventEmitter,\r\n    ) {}\r\n\r\n  @Post('signup')\r\n  signup() {\r\n    // ...\r\n    this.nestEventEmitter.emit('user-created', user);\r\n  }\r\n}\r\n```\r\nIf you provide multiple emitters you can select one with:\r\n\r\n```ts\r\n this.nestEventEmitter.emitter('my-emitter').emit('user-created', user);\r\n```\r\n\r\nAlso, you can get your emitters as  \u003ca href=\"https://github.com/bterlson/strict-event-emitter-types\"\u003eStrictEventEmitter\u003c/a\u003e\r\n\r\n```ts\r\n// define your events\r\n interface Events {\r\n   request: (request: Request, response: Response) =\u003e void;\r\n   done: void;\r\n }\r\n this.nestEventEmitter.strictEmitter\u003cEvents\u003e().emit('done');\r\n //or\r\n  this.nestEventEmitter.strictEmitter\u003cEvents\u003e('my-emitter').emit('done');\r\n```\r\n### Future Goals\r\n\r\n* Add tests;\r\n\r\n\r\n### Contributing\r\n\r\nYou are welcome to contribute to this project, just open a PR.\r\n### License\r\n\r\n- NestEvent is [MIT licensed](LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyak0%2Fnest-event","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyak0%2Fnest-event","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyak0%2Fnest-event/lists"}