{"_id":"@alejandrodk/nestjs-event-bus","_rev":"4-de98e1fd859dcf8587360af4988aa8f9","name":"@alejandrodk/nestjs-event-bus","dist-tags":{"latest":"1.0.3"},"versions":{"1.0.1":{"name":"@alejandrodk/nestjs-event-bus","version":"1.0.1","description":"A simple and lightweight event bus module for Nest Framework","author":{"name":"Alejandro Barrios","email":"ottoabarriosp@hotmail.com"},"license":"MIT","url":"https://github.com/alejandrodk/nestjs-events-driven-bus","scripts":{"build":"tsc -p tsconfig.json","format":"prettier --write \"**/*.ts\"","lint":"eslint \"src/**/*.ts\" --fix","start:debug":"nest start --debug --watch"},"dependencies":{"@nestjs/common":"7.6.15","@nestjs/config":"0.6.3","@nestjs/core":"7.6.15","@nestjs/platform-express":"7.6.15","class-transformer":"0.4.0","class-validator":"0.13.1","reflect-metadata":"0.1.13","rimraf":"3.0.2","rxjs":"6.6.7","tslib":"2.2.0"},"devDependencies":{"@nestjs/cli":"7.6.0","@nestjs/schematics":"7.3.1","@nestjs/testing":"7.6.15","@types/express":"4.17.11","@types/jest":"26.0.22","@types/node":"14.14.41","@types/supertest":"2.0.11","@types/uuid":"8.3.0","@typescript-eslint/eslint-plugin":"4.22.0","@typescript-eslint/parser":"4.22.0","eslint":"7.24.0","eslint-config-prettier":"8.2.0","eslint-plugin-prettier":"3.3.1","jest":"26.6.3","prettier":"2.2.1","supertest":"6.1.3","ts-jest":"26.5.5","ts-loader":"9.0.2","ts-node":"9.1.1","tsconfig-paths":"3.9.0","typescript":"4.2.4"},"repository":{"type":"git","url":"git+https://github.com/alejandrodk/nestjs-events-driven-bus.git"},"bugs":{"url":"https://github.com/alejandrodk/nestjs-events-driven-bus/issues"},"homepage":"https://github.com/alejandrodk/nestjs-events-driven-bus#readme","main":"index.js","keywords":["event-bus","events","observer","nodejs","nestjs","javascript","typescript","rxjs"],"gitHead":"321329de3c1fd5e38930935ce0d6cf2df2b1380e","_id":"@alejandrodk/nestjs-event-bus@1.0.1","_nodeVersion":"14.16.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-glRPAI3HhfSDY6G77HHM9gwivdTXxvquH0PqaXJ6m7H5H+G1dVuwax2TSNx8iqK7nJx8QUAY56pqPrtwQ+KRdw==","shasum":"b1484955495c9260e90eff8646656c893048b221","tarball":"https://registry.npmjs.org/@alejandrodk/nestjs-event-bus/-/nestjs-event-bus-1.0.1.tgz","fileCount":65,"unpackedSize":58072,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDVlV3Z7tI5UZYqx3seWA2d0Y+zghdhPIzNVdFmVa0iLgIhAKZ4/daIwtuyVjXEo1MzJ54JBnIJTQajX9G6mo+ybpos"}]},"_npmUser":{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"},"directories":{},"maintainers":[{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-event-bus_1.0.1_1635288992466_0.43375048283730133"},"_hasShrinkwrap":false},"1.0.2":{"name":"@alejandrodk/nestjs-event-bus","version":"1.0.2","description":"A simple and lightweight event bus module for Nest Framework","author":{"name":"Alejandro Barrios","email":"ottoabarriosp@hotmail.com"},"license":"MIT","url":"https://www.npmjs.com/package/@alejandrodk/nestjs-event-bus","scripts":{"build":"tsc -p tsconfig.json","format":"prettier --write \"**/*.ts\"","lint":"eslint \"src/**/*.ts\" --fix","start:debug":"nest start --debug --watch"},"dependencies":{"@nestjs/common":"7.6.15","@nestjs/config":"0.6.3","@nestjs/core":"7.6.15","@nestjs/platform-express":"7.6.15","class-transformer":"0.4.0","class-validator":"0.13.1","reflect-metadata":"0.1.13","rimraf":"3.0.2","rxjs":"6.6.7","tslib":"2.2.0"},"devDependencies":{"@nestjs/cli":"7.6.0","@nestjs/schematics":"7.3.1","@nestjs/testing":"7.6.15","@types/express":"4.17.11","@types/jest":"26.0.22","@types/node":"14.14.41","@types/supertest":"2.0.11","@types/uuid":"8.3.0","@typescript-eslint/eslint-plugin":"4.22.0","@typescript-eslint/parser":"4.22.0","eslint":"7.24.0","eslint-config-prettier":"8.2.0","eslint-plugin-prettier":"3.3.1","jest":"26.6.3","prettier":"2.2.1","supertest":"6.1.3","ts-jest":"26.5.5","ts-loader":"9.0.2","ts-node":"9.1.1","tsconfig-paths":"3.9.0","typescript":"4.2.4"},"repository":{"type":"git","url":"git+https://github.com/alejandrodk/nestjs-events-driven-bus.git"},"bugs":{"url":"https://github.com/alejandrodk/nestjs-events-driven-bus/issues"},"homepage":"https://github.com/alejandrodk/nestjs-events-driven-bus#readme","main":"index.js","keywords":["event-bus","events","observer","nodejs","nestjs","javascript","typescript","rxjs"],"gitHead":"7fc8b7ed0887d6faebe3337f33de8742db41e8bd","_id":"@alejandrodk/nestjs-event-bus@1.0.2","_nodeVersion":"14.16.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-MTjSqlhieBSReeCkqrx6bPAyvv+8C40ahko2MlOSjRY4grAaSxv++FitkhDBeLdp+cb5uO2/MZ0FqNH3Kk6WBg==","shasum":"72d54b8155a6effb4a338d3a489b76d6422303d6","tarball":"https://registry.npmjs.org/@alejandrodk/nestjs-event-bus/-/nestjs-event-bus-1.0.2.tgz","fileCount":62,"unpackedSize":49542,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhxI2RCRA9TVsSAnZWagAAqDUP/3p4pDan+aTHNoJYfL3R\n+mLz4Na5+BBl42nvG/2gNK5dTW9eoyR4MxxtcYTZoNGLbPGvjEM2SsCsjKte\nrNdZpICnBQIQUMZdquYbNYNMZHwSFQzyfqRyCoZR74+jg+hDrxnjbLKgUvp8\nUThsJbLup1h4yK3MarUrYAsy5FQ8YfuZRLtBTm2CZLpe37NP/P0EQOq1vmMs\nVJ2a7KO3R7EslNiXtqtc7Rhq+0NwRsouvGgaa+zmuks5YE08VaRO2aW6O5Es\nU7lDEEgVCd078QL2vykEEFAXs3IYimVNORFztgVTC3mtmzKonK9QC8SsbsL4\nSKM5Yri6X0iplaBShEhkS2RciPfYnAbIWQURfLucYrWY72VTEfzDK4bJYYvR\nvJ4tF6WkzaGt+8CR6a94fwbrJlcAQ+2jXh8BQTpCxM5bgsxiZtgia3oMLhTY\nT23NJOW/P+gdhVK4drrMUVhVD2Yw9puAS06DCVMIZl0RYTjWr3HvDWTlunzT\n5mE5n1y5EN8BlSxMdVze0prKvYcqAvOc2Baad91I6VJObZrly7LqtA5sYS9I\ntWsJbEWEBlLDCZicVQUNPckEWdVPMq6QYJdoMSbdtbQXAwCYqk8iCC0tJgO3\nF6S+QDYoQQsy8WhClUBONSCvb/GxowYPtc+BCf/ijqLDVBRVRdW4vURT4PG8\nVS4r\r\n=PcoG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDfflLizRzbJKWs0u2CxmGfGrHoTCNgaM62v2K7x2cT2AiArJud5bDNuivbFu96TqwCwg7+lzh83EHlsXBGwSj2zRg=="}]},"_npmUser":{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"},"directories":{},"maintainers":[{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-event-bus_1.0.2_1635292055327_0.7091247275405395"},"_hasShrinkwrap":false},"1.0.3":{"name":"@alejandrodk/nestjs-event-bus","version":"1.0.3","description":"A simple and lightweight event bus module for Nest Framework","author":{"name":"Alejandro Barrios","email":"ottoabarriosp@hotmail.com"},"license":"MIT","url":"https://www.npmjs.com/package/@alejandrodk/nestjs-event-bus","scripts":{"build":"tsc -p tsconfig.json","format":"prettier --write \"**/*.ts\"","lint":"eslint \"src/**/*.ts\" --fix","start:debug":"nest start --debug --watch"},"dependencies":{"@nestjs/common":"7.6.15","@nestjs/config":"0.6.3","@nestjs/core":"7.6.15","@nestjs/platform-express":"7.6.15","class-transformer":"0.4.0","class-validator":"0.13.1","reflect-metadata":"0.1.13","rimraf":"3.0.2","rxjs":"6.6.7","tslib":"2.2.0"},"devDependencies":{"@nestjs/cli":"7.6.0","@nestjs/schematics":"7.3.1","@nestjs/testing":"7.6.15","@types/express":"4.17.11","@types/jest":"26.0.22","@types/node":"14.14.41","@types/supertest":"2.0.11","@types/uuid":"8.3.0","@typescript-eslint/eslint-plugin":"4.22.0","@typescript-eslint/parser":"4.22.0","eslint":"7.24.0","eslint-config-prettier":"8.2.0","eslint-plugin-prettier":"3.3.1","jest":"26.6.3","prettier":"2.2.1","supertest":"6.1.3","ts-jest":"26.5.5","ts-loader":"9.0.2","ts-node":"9.1.1","tsconfig-paths":"3.9.0","typescript":"4.2.4"},"repository":{"type":"git","url":"git+https://github.com/alejandrodk/nestjs-events-driven-bus.git"},"bugs":{"url":"https://github.com/alejandrodk/nestjs-events-driven-bus/issues"},"homepage":"https://github.com/alejandrodk/nestjs-events-driven-bus#readme","main":"index.js","keywords":["event-bus","events","observer","nodejs","nestjs","javascript","typescript","rxjs"],"gitHead":"82606eae57fb28a5914ff62b333dddc9928ee521","_id":"@alejandrodk/nestjs-event-bus@1.0.3","_nodeVersion":"14.16.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-G2/r6yBcBLjcI4xCF/Zz6LjL/cFSi7fbRCL5M/r+6AkG/Afq9GurokeC3WWjxHXIkQdy8rrXTWntUgYSOBgCrA==","shasum":"511b457f4f8f1fdce86730899cace72c82750b37","tarball":"https://registry.npmjs.org/@alejandrodk/nestjs-event-bus/-/nestjs-event-bus-1.0.3.tgz","fileCount":62,"unpackedSize":49548,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAgVRTMm3USdwoYRwj0+gz1gHBtnh9qcRo9FzaBLlgDOAiEAstLq3t9xdQ5c4D7/56O7wSHlhF4GcBZmHW1fSJ3a+XQ="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiR39KACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqPTg/6AvlX1YYJAW66uFdKgl2EoC5GI4y4XZAt9OxZ8nM3qgtgWmb1\r\nolEXz9Bmvf+EvNxY1tYyn4DJk/IW3echVZQXsmZkOsVX6BGoYSljX8bG2IWd\r\nCyW1BCLAQdKp8OowHIma8Gvh1gnZb+jC8oti3a6LPnxZYuNnHzmzYnHH2ZgW\r\ngg3zTEn3Sk+VN4XCzwHBHjHE9P2JPega9J/7Gfr5fsr95tVGO8z/IB013sA2\r\n+lpdVb6eelW5IcJ4ClT8OcQqR4/sBi6qunHqCBh4UW2lcprsSeRIV+PqC5q4\r\nK7hHiUWCX5J4Dl4pR/YsbTMjDikdhVSyxlweaLF4oCLCBiSX/sLrB+EPkd5K\r\nrABxtZQTur1cKXVZawUmHm3eNrLatK8rNP+ftiP2fs2DlfdD0+2W+eZ2Nw09\r\nXKSG3lzb/SZJkoJeKNKZigLULHymUX+gQcib4Ai57XH1tTbn6eqCaTwMjfsl\r\nnWeQGOKqXH/r7HTjg65rda20Fz1UsBrGTmUCg3NmYL9yDm53+4tCBgJfmvQY\r\nKVFTGdmUHSZ0G9sebHsD18GI0z9Ycgsyc5Ssnv+NfZT4JauKvTKGFEHiKGg5\r\niFFo9zDSL6qb+1Za5f3Rf/gxpOvjmKLvkOn73Nmd2VqbkcxLXCSme+oL5R5z\r\np2xIwLY3z7trUz/h4gTIlxIjNjalRXAYplA=\r\n=D9/n\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"},"directories":{},"maintainers":[{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-event-bus_1.0.3_1648852809852_0.8757623883359547"},"_hasShrinkwrap":false}},"time":{"created":"2021-10-26T22:56:32.424Z","1.0.1":"2021-10-26T22:56:32.599Z","modified":"2022-04-04T12:37:13.291Z","1.0.2":"2021-10-26T23:47:35.471Z","1.0.3":"2022-04-01T22:40:10.206Z"},"maintainers":[{"name":"alejandrodk","email":"ottoabarriosp@hotmail.com"}],"description":"A simple and lightweight event bus module for Nest Framework","homepage":"https://github.com/alejandrodk/nestjs-events-driven-bus#readme","keywords":["event-bus","events","observer","nodejs","nestjs","javascript","typescript","rxjs"],"repository":{"type":"git","url":"git+https://github.com/alejandrodk/nestjs-events-driven-bus.git"},"author":{"name":"Alejandro Barrios","email":"ottoabarriosp@hotmail.com"},"bugs":{"url":"https://github.com/alejandrodk/nestjs-events-driven-bus/issues"},"license":"MIT","readme":"# Nestjs Event-Driven Bus module\r\n\r\nSimple, easy and lightweight package for implement Event-Driven bus in a [NestJs Project](https://github.com/nestjs/nest) using reactive programming (RxJs) 🚀\r\n\r\n[npm package](https://www.npmjs.com/package/@alejandrodk/nestjs-event-bus)\r\n\r\n## Usage\r\n\r\n### 1- Install package\r\n\r\n```sh\r\nnpm install nestjs-event-driven-bus\r\n# or\r\nyarn add nestjs-event-driven-bus\r\n```\r\n\r\n### 2- Import EventBusModule\r\n\r\nImport the **EventBusModule** in the module where you want to dispatch events.\r\n\r\n```sh\r\n@Module({\r\n  imports: [EventBusModule],\r\n  ...\r\n})\r\n```\r\n\r\nFor example, assuming you are making a products application.\r\n\r\n```sh\r\n@Module({\r\n  imports: [EventBusModule],\r\n  controllers: [ProductsController],\r\n  providers: [ProductsService],\r\n  exports: [ProductsService]\r\n})\r\nexport class ProductsModule {}\r\n```\r\n\r\n### 3- Define Events\r\n\r\nFollowing the previous example, we create an event to update the stock of a product with each sale.\r\n\r\n```sh\r\n# src/events/products.events.ts\r\n\r\nexport class UpdateStockEvent {\r\n  constructor(public product: string, public quantity: number) {}\r\n}\r\n```\r\n\r\n### 4- Define Event handler\r\n\r\nThe event handler is a class in charge of reacting to events of a specific type, it must implement the methods defined in the IEventHandler interface.\r\n**The event handler class must extends the **IEventHandler** interface*\r\n\r\n```sh\r\nclass UpdateStockEventHandler implements IEventHandler\r\n```\r\n\r\nAn event handler must have two required methods (handle, error) and one optional method (complete).\r\nThe **handle** method will be in charge of receiving the event, as you should imagine, the **error** method will receive events if an error occurs, this will allow you to apply certain logic to handle certain cases.\r\nThe **complete** method is optional, as it is only executed once the event transmission is complete.\r\n\r\n```sh\r\ninterface IEventHandler {\r\n  handle: (event: any) => void;\r\n  error: (event: any) => void;\r\n  complete?: () => void;\r\n}\r\n```\r\n\r\nTo link an event handler with an event, we apply the **HandleEvent** decorator on the class, it receives the class of the event it wants to react to.\r\n\r\n```sh\r\n@HandleEvent(UpdateStockEvent)\r\n```\r\n\r\nThe event handlers be Dependency Injection friendly, so you can import injected dependencies into the class constructor as always.\r\n\r\n```sh\r\nconstructor(private productsService: ProductsService){}\r\n# or\r\nconstructor(@Inject() someDependency: SomeDependency) {}\r\n```\r\n\r\nFinally, our Event handler class must looks like\r\n\r\n```sh\r\n# src/handlers/products.handlers.ts\r\n\r\n@HandleEvent(UpdateStockEvent)\r\nexport class UpdateStockEventHandler implements IEventHandler {\r\n  constructor(private productsService: ProductsService){}\r\n  handle(event: UpdateStockEvent) {\r\n    console.log('Hello from handler!!!', UpdateStockEventHandler.name)\r\n    this.productsService.someMethod()...\r\n    #do something business logic...\r\n  }\r\n  error(error: any) {\r\n     #do something...\r\n  }\r\n}\r\n```\r\n\r\n### 5- Import Event Handlers in your module\r\n\r\nIn your events handlers file, you must to export and array with all handlers, or if you preferred, import all of then in the target module, but the first option is the cleanest and easiest way.\r\n\r\n```sh\r\n# src/handlers/products.handlers.ts\r\n\r\nexport const ProductsEventsHandlers = [\r\n  UpdateStockEventHandler, \r\n  someEventHandler, \r\n  AnotherEventHandler\r\n]\r\n```\r\n\r\nthen, simply import the array of Event Handlers in providers array.\r\n\r\n```sh\r\n@Module({\r\n  imports: [EventBusModule],\r\n  controllers: [ProductsController],\r\n  providers: [ProductsRepository, ProductsService, ...ProductsEventsHandlers],\r\n  exports: [ProductsService]\r\n})\r\nexport class ProductsModule {}\r\n```\r\n\r\n### 6- Dispatch events 🚀\r\n\r\nIn our controller (or wherever you need to produce events), you only need to import the EventBus class, the EventBus class has the **Publish**, **PublishError** and **Complete** methods, which we will use to produce new events.\r\n\r\n```sh\r\n# src/controllers/products.controller.ts\r\n\r\n@Controller('products')\r\nexport class ProductsController {\r\n  constructor(private service: ProductsService, private eventBus: EventBus) {}\r\n}\r\n```\r\n\r\nTo produce a new event, all we have to do is publish a new event using the **Publish** method.\r\n**The publish method receives an event instance and its name.*\r\n\r\n```sh\r\n @Get('sell/:id')\r\n  sellProduct(@Param('id') id: string, @Query() queryParams: any): void {\r\n    const { client, quantity } = queryParams;\r\n    this.eventBus.publish(new UpdateStockEvent(id, client, +quantity))\r\n  }\r\n```\r\n\r\nTo inform our event handler of an error, we can do so using the PublishError method.\r\n\r\n```sh\r\n @Get('sell/:id')\r\n  sellProduct(@Param('id') id: string, @Query() queryParams: any): void {\r\n    const { client, quantity } = queryParams;\r\n    try {\r\n        # some logic here....\r\n        this.eventBus.publish(new UpdateStockEvent(id, client, +quantity))\r\n        # more logic....\r\n    } catch (err) {\r\n        this.eventBus.publishError(error, UpdateStockEvent.name)\r\n    }\r\n  }\r\n```\r\n\r\n___\r\n\r\n### One event, many handlers\r\n\r\nBy implementing an event architecture, your application will be able to react to different events, keeping the components of your application completely decoupled.\r\n\r\nGoing back to the example of a product store, you could have an event called SellProductEvent, and for the same event have different event handlers, for example:\r\n\r\nstarting from the event:\r\n\r\n```sh\r\n# src/events/products.events.ts\r\n\r\nexport class SellProductEvent {\r\n  constructor(public productID: string, public quantity: number, public client: string) {}\r\n}\r\n```\r\n\r\nwe can have these different event handlers:\r\n\r\n```sh\r\n# src/handlers/products.handlers.ts\r\n\r\n@HandleEvent(SellProductEvent)\r\nexport class SellProductEventHandler implements IEventHandler {\r\n  constructor(private productsService: ProductsService){}\r\n  handle(event: SellProductEvent) {\r\n    #process sell...\r\n  }\r\n}\r\n\r\n@HandleEvent(SellProductEvent)\r\nexport class SendEmailToClient implements IEventHandler {\r\n  constructor(private emailTransport: EmailTransport){}\r\n  handle({ client }: SellProductEvent) {\r\n    #send email to client...\r\n    this.emailTransport.newSellEmail(client)...\r\n  }\r\n}\r\n\r\n@HandleEvent(SellProductEvent)\r\nexport class UpdateStock implements IEventHandler {\r\n  constructor(private productsService: ProductsService){}\r\n  handle({ productID, quantity }: SellProductEvent) {\r\n    #update product stock...\r\n    this.productsService.updateProductStock(productID, quantity)...\r\n  }\r\n}\r\n```\r\n\r\nLook how each event is independent of the others.\r\n\r\n### TEST\r\n\r\nDisable the event bus in test environments with *DISABLE_EVENT_HANDLING* env variable.\r\n","readmeFilename":"README.md"}