{"_id":"@alexy4744/nestjs-nats-jetstream-transporter","_rev":"4-ee39b06903f2e33ccb10f4bf6ebb6f05","name":"@alexy4744/nestjs-nats-jetstream-transporter","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.0":{"name":"@alexy4744/nestjs-nats-jetstream-transporter","version":"1.0.0","description":"NATS JetStream transporter for NestJS","keywords":["nestjs","nats","microservices","jetstream","nats-jetstream","messaging"],"license":"MIT","homepage":"https://github.com/alexy4744/packages#readme","bugs":{"url":"https://github.com/alexy4744/packages/issues"},"repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"engines":{"node":">=12.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","@nestjs/microservices":"^7.6.17","nats":"^2.0.8","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-nats-jetstream-transporter@1.0.0","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-6Vfst57qYDzCBa93/9Dub0zrJJRrXAPKUaSwTs5WFqbhGzWQVm/KO4I+7jKvynPRI3CoDDLtBu6x18m3rlZvzQ==","shasum":"c9e22cff8987b52c28341b09f789cbe81d62c6cb","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-nats-jetstream-transporter/-/nestjs-nats-jetstream-transporter-1.0.0.tgz","fileCount":27,"unpackedSize":39037,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5m27CRA9TVsSAnZWagAALL4P/A6YPx5CE5nYCBBeTwPf\ndGcQkk5KBxjZ1cCHP/tBak0SwUE4X1Ns7+iLRYamcF6ujFnryAAw+QN4jNMB\niMpdest7KRFjynNz6MTWFdA04UWgwyuyzIR2EgrnOoMgUasi2Q4nlf1nzTog\nAmbDjopImzMMy9SaxErQ9o43ikenRyQpEkZ8I7n4MCEkc0wDucs2yPWzvHp8\n0XBlORUK95x+JI3NCvIEAtKMim23VWdF8L6BnANj3CvQJk4dr0gnhDKXfmhf\n1bKzL/Ff1pvgL/7euQxlGBoKICJULtPERQerp8sTwpHRtYNOUqvseiRSi5Es\nW5PzdEwNukQVb0tT7jLDRbYNkvVi0V2I7dkgl/Gv83GZTmEcyIbev97cl8Bo\nBr/PFGQqP/3qmu8YscdxtLOXEbxpQvL1ykAEorZlcnm6NOB2BfFd5XKzpH8n\nCkNjr3Fif29KI56RzoeEgS1mrvhfk9Xirp+CtQETuPTvm7TbaJzTc7l5CUq7\nUBLPQb1cj1M+ZZ/hLNvJooGCkT2MzKVW3lR1imcpVdk9rclHqRh+XA00+KuA\nJjNxAoB7F2XO7tlU1YUx7RWzVkbgi3ULLbv5MrmsFoO7Y/FRWBv0HqLIIKjt\nnxXntZVUTd76MN2/WkBAsd2NtvbCIKGI3BgDbJ6ue6j0U6H5zByExsFRwgTl\nzjDE\r\n=E4Tf\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCvLAE+ozT71X+FP1CIrR5ZT8+xzi7W0jfrHL5ARvyQxQIgYij9sNocqlgPryKbhpH//z4Nd/T5B1c22aCiC1R7KyM="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-nats-jetstream-transporter_1.0.0_1625714106884_0.44182464317037207"},"_hasShrinkwrap":false},"1.0.1":{"name":"@alexy4744/nestjs-nats-jetstream-transporter","version":"1.0.1","description":"NATS JetStream transporter for NestJS","keywords":["nestjs","nats","microservices","jetstream","nats-jetstream","messaging"],"license":"MIT","homepage":"https://github.com/alexy4744/packages#readme","bugs":{"url":"https://github.com/alexy4744/packages/issues"},"repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"engines":{"node":">=12.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","@nestjs/microservices":"^7.6.17","nats":"2.2.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-nats-jetstream-transporter@1.0.1","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-/xZV5ykXwEioFYnp0L4X76/Jdsd/yszT4phtD1U/itsMkx9QYuKzRd68gxeV3/B5Z6EE3FPZFuLY5Q2UyvM0jA==","shasum":"3210ca48c70c17328b6251ddac8f2971f50e827c","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-nats-jetstream-transporter/-/nestjs-nats-jetstream-transporter-1.0.1.tgz","fileCount":27,"unpackedSize":40102,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCH038BalBKSkvtmPX42hPNmupdO0FH4VRb5TeA8nS4NgIgZnrTZ01M6TD8wgTiKd1gkGMdisvAXq9IVBDXAjJ9H1E="}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-nats-jetstream-transporter_1.0.1_1632705624113_0.9079441761434934"},"_hasShrinkwrap":false},"1.0.2":{"name":"@alexy4744/nestjs-nats-jetstream-transporter","version":"1.0.2","description":"NATS JetStream transporter for NestJS","keywords":["nestjs","nats","microservices","jetstream","nats-jetstream","messaging"],"license":"MIT","homepage":"https://github.com/alexy4744/packages#readme","bugs":{"url":"https://github.com/alexy4744/packages/issues"},"repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"engines":{"node":">=12.0.0"},"peerDependencies":{"@nestjs/common":"^7.0.0","@nestjs/core":"^7.0.0","@nestjs/microservices":"^7.6.17","nats":"2.2.0","reflect-metadata":"^0.1.12","rxjs":"^6.0.0"},"main":"./src/index.js","typings":"./src/index.d.ts","_id":"@alexy4744/nestjs-nats-jetstream-transporter@1.0.2","_nodeVersion":"16.0.0","_npmVersion":"7.10.0","dist":{"integrity":"sha512-D/PZdAsJ1cI5dZhQC+1QEm5+Hb2JYHgh0/gnA/P5QOw8HB6PStIvTbhJcvDBXV1FfRvCl/MumwTuSPr2NytOMg==","shasum":"0d3748d3a6c8ef3371851f11c3ec1f044c43d582","tarball":"https://registry.npmjs.org/@alexy4744/nestjs-nats-jetstream-transporter/-/nestjs-nats-jetstream-transporter-1.0.2.tgz","fileCount":27,"unpackedSize":40667,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhxJA8CRA9TVsSAnZWagAAGKoP/2jLjMHvBcPDHd0Of+FV\nYUalkuunhYuY49y+UOpEbuA1OV2oidmFrnfMRRgUGCo9n3HMh/xN146Tj8bl\nXCjdoiyAhzizBvn7Kz4SLyUrDKIMS81ICgiuBg/nBLNDN+mjTDyWZCuWCDT8\nRfpyEw2M7pTbHDNYIVQkgtLgygDi4rulgk3KnHMl5OtAqA6pW5VV9JJJskrA\nyZHAWBKtSM28XimkquQfZ+kLv/b3LskC9HYO4awOr11QQeRajw7/iaozGa34\nfAsFJ259SJLhMwAf1gkaPOfmH6tn0O8ax0fUAzqQGRDZO4oR7EKAV8QxLrKp\nJOaN+hZnn7VvcCmBQeeA+RplqCTYy3nsO7XN+NjHV7aFdh8Jy6dVc7bH0RzH\n+eRVO5+0INnMFXeJNNfmrKG9M02NJw63+WvFwvEZrsZNq788ECrYyvcFtZCm\npcwL5wCae4w+qYH+mOU/OUULBtlXOUnQe0tLRSIoOzuxk3tmsYqElBhhKZKz\nhIzypFzQoRljG5yZ8/51MpCO0XjB8IuwzQu9MmFmurQEt+xj5uix14ExTnlm\njLP7jZYK/gwVPzfdBnB5Nb+uJ/GrIvh/0MNKRgy7LgSSMa2Gg0F/8O4vU2Fi\najbjVZ2yUDR0p1XhL6tBZUggn94YdnFA/Fayg1SJsr16j7kNbF6qbdOAf8Wl\nUGRb\r\n=x5gB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCfGo4N4IE75hxIJVItJeJudO5T3wNva/oYP/jDN37cywIhAIJXQtw4G7P/nQZAIzDkYnc0MBEvLYEIIJxVvUAFN0IN"}]},"_npmUser":{"name":"~alexy4744","email":"alexy4744@gmail.com"},"directories":{},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-nats-jetstream-transporter_1.0.2_1632706148642_0.9476812748511063"},"_hasShrinkwrap":false}},"time":{"created":"2021-07-08T03:15:06.494Z","1.0.0":"2021-07-08T03:15:07.018Z","modified":"2022-04-04T12:42:01.081Z","1.0.1":"2021-09-27T01:20:24.308Z","1.0.2":"2021-09-27T01:29:08.806Z"},"maintainers":[{"name":"~alexy4744","email":"alexy4744@gmail.com"}],"description":"NATS JetStream transporter for NestJS","homepage":"https://github.com/alexy4744/packages#readme","keywords":["nestjs","nats","microservices","jetstream","nats-jetstream","messaging"],"repository":{"type":"git","url":"git+https://github.com/alexy4744/packages.git"},"bugs":{"url":"https://github.com/alexy4744/packages/issues"},"license":"MIT","readme":"# NestJS NATS JetStream Transporter\n\nA NATS transporter for NestJS that takes advantage of [JetStream](https://docs.nats.io/jetstream/jetstream) for event patterns.\n\n- [NestJS NATS JetStream Transporter](#nestjs-nats-jetstream-transporter)\n  - [Installation](#installation)\n  - [Publishing Messages](#publishing-messages)\n    - [Request-Reply](#request-reply)\n    - [Event-Based](#event-based)\n  - [Receiving Messages](#receiving-messages)\n    - [Customizing JetStream consumer options](#customizing-jetstream-consumer-options)\n    - [NACK and TERM JetStream messages](#nack-and-term-jetstream-messages)\n    - [Queue Groups](#queue-groups)\n  - [Tests](#tests)\n  \n## Installation\n\n```bash\n$ npm install @alexy4744/nestjs-nats-jetstream-transporter nats@2.2.0\n```\n\n⚠️ This library uses [NATS.js](https://github.com/nats-io/nats.js#jetstream), which means that the underlying JetStream API is subject to change and does not follow semver. It is recommended to use `nats@2.2.0` until the JetStream API is out of beta as it has been fully tested with this package.\n\n## Publishing Messages\n\nThe `NatsClient` works mostly the same with the built-in NATS transporter for NestJS. The only difference is that you must instantiate `NatsClient` yourself.\n\n### Request-Reply\n```ts\n// main.ts\nimport { NatsTransportStrategy } from \"nestjs-nats-jetstream-transporter\";\n\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy()\n});\n\nawait app.listenAsync();\n```\n\n```ts\n// math.controller.ts\nimport { NatsClient } from \"nestjs-nats-jetstream-transporter\";\n\n@Controller()\nexport class MathController {\n  private readonly client = new NatsClient();\n\n  accumulate(): Observable<number> {\n    return this.client.send<number>(\"sum\", [1, 2, 3]);\n  }\n}\n```\n\n### Event-Based\n```ts\n// main.ts\nimport { NatsTransportStrategy } from \"nestjs-nats-jetstream-transporter\";\n\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy({\n    // Create a stream with a subject called \"orders.created\"\n    // This is important later on when we publish an event with NatsClient\n    streams: [\n      {\n        name: \"orders-events\",\n        subjects: [\"orders.created\"]\n      }\n    ]\n  })\n});\n\nawait app.listenAsync();\n```\n\n```ts\n// orders.controller.ts\nimport { NatsClient } from \"nestjs-nats-jetstream-transporter\";\n\n@Controller()\nexport class OrdersController {\n  private readonly client = new NatsClient();\n\n  constructor(private readonly ordersService: OrdersService) {}\n\n  async create(): Promise<void> {\n    const order = await this.ordersService.create();\n\n    this.client.emit(\"orders.created\", order);\n  }\n}\n```\n\n## Receiving Messages\n\nThere are no special changes needed to receive messages. Just use the `@EventPattern()` and `@MessagePattern()` decorators provided by NestJS.\n\n`@Ctx()` works exactly the same, however you should use the `NatsContext` provided by this package as the parameter type. It exposes an additional `getMessage()` method that returns the original message object if needed.\n\n### Customizing JetStream consumer options\n\nYou can customize how the JetStream push consumer behaves. One example is making the consumer durable to survive application restarts. Another example is taking advantage of distributed queues for horozontal scaling.\n\nRead more about JetStream consumers [here](https://docs.nats.io/jetstream/concepts/consumers), and refer to the underlying API [here](https://github.com/nats-io/nats.deno/blob/main/nats-base-client/types.ts#L330).\n\n```ts\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy({\n    consumer: (options) => {\n      options.durable(\"my-durable-name\");\n\n      // When using a queue group with JetStream, it is necessary that deliverTo() and queue() uses the same name.\n      // This is a requirement for NATS.js v2.2.0, see this issue for more details:\n      // https://github.com/nats-io/nats.js/issues/446\n      options.deliverTo(\"my-queue-group\");\n      options.queue(\"my-queue-group\");\n    }\n  })\n});\n```\n\n### NACK and TERM JetStream messages\n\nBy default, all JetStream messages are automatically acknowledged. However, you can also NACK and TERM the message by returning the respective symbols from your application.\n\nReturning `NACK` will ask for the message to be redelivered, while `TERM` will terminate future deliveries of the message.\n\n```ts\n// shipping.controller.ts\nimport { NACK, TERM } from \"nestjs-nats-jetstream-transporter\";\n\n@Controller()\nexport class ShippingController {\n  constructor(private readonly shippingService: ShippingService) {}\n\n  @EventPattern(\"orders.created\")\n  handleCreatedOrder(order) {\n    // If a shipment cannot be scheduled at this time, then ask for the message to be redelivered\n    if (this.shippingService.isBusy()) {\n      return NACK;\n    }\n\n    // If a shipment already exists for this order, then terminate the redelivery of this message\n    if (this.shippingService.exists(order)) {\n      return TERM;\n    }\n\n    this.shippingService.scheduleShipment(order);\n    \n    // Otherwise, the message will be auto-acked\n  }\n}\n```\n\nIf the handler for your event pattern throws an error, the message will automatically be terminated. You can change this behavior by providing an `onError` function to the transport strategy options.\n\n```ts\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy({\n    // Messages that caused an exception will be acked instead\n    onError: (message) => message.ack()\n  })\n});\n```\n\n### Queue Groups\n\nYou can specify a queue group name to enable [distributed queues](https://docs.nats.io/nats-concepts/queue). This will load balance message delivery across all other application instances with the same queue group name. It makes horozontal scaling simple as you can scale up by running another instance of your application with no additional configuration.\n\n```ts\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy({\n    queue: \"my-queue-group\"\n  })\n});\n```\n\nIf you want to enable this functionality for event patterns, you must also specify the queue group name using the JetStream consumer options builder.\n\n```ts\nconst app = await NestFactory.createMicroservice(AppModule, {\n  strategy: new NatsTransportStrategy({\n    consumer: (options) => {\n      // When using a queue group with JetStream, it is necessary that deliverTo() and queue() uses the same name.\n      // This is a requirement for NATS.js v2.2.0, see this issue for more details:\n      // https://github.com/nats-io/nats.js/issues/446\n      options.deliverTo(\"my-queue-group\");\n      options.queue(\"my-queue-group\")\n    },\n    queue: \"my-queue-group\"\n  })\n});\n```\n\n## Development\n\n```bash\n# Run tests\n$ nx test nestjs-nats-jetstream-transporter\n```\n\n```bash\n# Update version\n$ nx version nestjs-nats-jetstream-transporter\n```\n\n```bash\n# Build the project\n$ nx build nestjs-nats-jetstream-transporter\n```\n\n```bash\n# Publish new version on GitHub\n$ git push --follow-tags origin master\n```\n\n```bash\n# Publish new version on NPM\n$ npm publish ./dist/packages/nestjs-nats-jetstream-transporter --access=public\n```\n\n","readmeFilename":"README.md"}