{"_id":"@byal-boilerplate/event-emitter","_rev":"1-a3e933ac54409d56026c2ad99a3becf7","name":"@byal-boilerplate/event-emitter","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@byal-boilerplate/event-emitter","version":"0.0.1","_id":"@byal-boilerplate/event-emitter@0.0.1","maintainers":[{"name":"arthurramaral","email":"ruhtraamaral@hotmail.com"}],"dist":{"shasum":"2a40182b5fbd459467aa065752cb52e18f5491a6","tarball":"https://registry.npmjs.org/@byal-boilerplate/event-emitter/-/event-emitter-0.0.1.tgz","fileCount":61,"integrity":"sha512-YBJf33BVdJw7k+1K/g6Wcw8O+AHAN7jjxtofECT0u/y4oT4rGPxoKaAroC2/tshUPaypJW2BuUI7NHsxGDmHWQ==","signatures":[{"sig":"MEUCIHlAXBUxiRijzhpwaSmGQENSsQiod6i79CGlYn5XVFtFAiEAofX/f7Vr2SrQ/TBTe+qa8Pq8MRWQzbPiQO6xT6C8oxA=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":36931},"type":"commonjs","gitHead":"9854925962dbf1bf136e9a039f385b24218c9878","_npmUser":{"name":"arthurramaral","email":"ruhtraamaral@hotmail.com"},"generators":"./generators.json","_npmVersion":"10.8.3","description":"This library can be used to send and handle events. It provides an abstraction of a message queue provider like RabbitMQ or Redis.","directories":{},"_nodeVersion":"22.2.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/event-emitter_0.0.1_1729443644988_0.7986821892064095","host":"s3://npm-registry-packages"}}},"time":{"created":"2024-10-20T17:00:44.889Z","modified":"2024-10-20T18:13:52.434Z","0.0.1":"2024-10-20T17:00:45.162Z"},"description":"This library can be used to send and handle events. It provides an abstraction of a message queue provider like RabbitMQ or Redis.","maintainers":[{"email":"periclespires@protonmail.com","name":"bitpickle"},{"email":"ruhtraamaral@hotmail.com","name":"arthurramaral"}],"readme":"# Event Emitter\n\nThis library can be used to send and handle events. It provides an abstraction of a message queue provider like RabbitMQ or Redis.\n\n## Usage\n\n### Import the module\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { EventEmitter } from '@byal-boilerplate/event-emitter';\nimport { env } from '../config/env';\n\n@Module({\n  imports: [\n    EventEmitterModule.forRoot({ queuePrefix: env.APPLICATION_SLUG_NAME }),\n  ],\n  controllers: [],\n  providers: [],\n})\nexport class AppModule {}\n```\n\nWhen the API import this library module, it will search for the handlers declared by the `@Subscriber()` decorator. The handlers will be registered in the event emitter and will be called when an event is received.\n\n### Sending events\n\nAfter importing the module, the event emitter can be injected into a service and used to send events.\n\n```typescript\nimport { EventEmitter, TestEvent } from '@byal-boilerplate/event-emitter';\nimport { Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class AppService {\n  constructor(private readonly eventEmitter: EventEmitter) {}\n\n  async sendEvent() {\n    await this.eventEmitter.send(TestEvent, {\n      email: 'tester@test.com',\n    });\n  }\n}\n```\n\n### Decorators\n\n#### @Subscriber(EventType)\n\nThis decorator can be used to register a handler for an event. It should be used only in a provider method.\n\nThis decorator has one parameter, the event type. The event type must be a class that extends the `BaseEvent` abstract class and decorated with `@RoutingKey()`. Next, an example of a subscriber:\n\n```typescript\nimport { Subscribe, TestEvent } from '@byal-boilerplate/event-emitter';\nimport { Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class AppService {\n  @Subscribe(TestEvent)\n  handleTestEvent(event: TestEvent) {\n    this.logger.debug(`Message received: ${event.payload}`);\n  }\n}\n```\n\nEach application can have only one handler for each event type. If more than one handler is registered for the same event type, an error will be thrown when the application starts, but the application will continue to run using only the first handler registered.\n\n#### @RoutingKey('exemple.event')\n\nThis decorator can be used to define the routing key of an event. It should be used only in a class that extends the `BaseEvent` abstract class and inside this library. All contracts events declared in this library are already decorated with this decorator.\n\n```typescript\nimport { RoutingKey } from '../../decorators/routing-key.decorator';\nimport { BaseEvent } from '../base-event';\n\ninterface UserCreatedPayload {\n  id: string;\n  name: string;\n}\n\n@RoutingKey('user.created')\nexport class UserCreatedEvent extends BaseEvent<UserCreatedPayload> {}\n```\n\n### Create a new event\n\nTo create a new event, you must run the generate command:\n\n```bash\nnx generate @byal-boilerplate/event-emitter:event\n```\n\nThis command will ask you for the following information:\n\n- Project: The project that is the owner of the event.\n- Event name: The name of the event. This name will be used to create the event file and class name.\n- Routing key: The routing key of the event. This key will be used to identify the event in the message queue.\n- Description: A description of the event.\n\nFor example, if you want to create an event called `UserUpdated`, you must run the following command:\n\n```bash\nnx generate @byal-boilerplate/event-emitter:event --dryRun                                                                     \n\n>  NX  Generating @byal-boilerplate/event-emitter:event\n\n✔ Which project is the owner of this event · auth-api\n✔ What is the name of the event in `kebab-case` · user-updated\n✔ What is the event routing key · user.updated\n✔ What is the event description · Emitted when an user has changed\n```\n\nThe command will return the following output with the created and updated files:\n\n```bash\nCREATE libs/event-emitter/src/lib/contracts/auth-api/user-updated.ts\nUPDATE libs/event-emitter/src/lib/contracts/index.ts\n```\n\nIt means that the event file was created in the `contracts` folder and the `index.ts` file was updated to export the new event. Now you can use the new event in your application.\n\n#### Send the event\n\n```typescript\nimport { EventEmitter, UserUpdatedEvent } from '@byal-boilerplate/event-emitter';\nimport { Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class AppService {\n  constructor(private readonly eventEmitter: EventEmitter) {}\n\n  async sendEvent() {\n    await this.eventEmitter.send(UserUpdatedEvent, {\n      id: 'user-id-123',\n    });\n  }\n}\n```\n\n#### Handle the event\n\n```typescript\nimport { Subscribe, UserUpdatedEvent } from '@byal-boilerplate/event-emitter';\nimport { Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class AppService {\n  @Subscribe(UserUpdatedEvent)\n  handleUserUpdatedEvent(event: UserUpdatedEvent) {\n    this.logger.debug(`Message received: ${event.payload}`);\n  }\n}\n```\n","readmeFilename":"README.md"}