1.0.8 ā€¢ Published 4 years ago

kimono-wrapper-js v1.0.8

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

kimono-wrapper

šŸ‘˜ Kimono is a typesafe, promise based wrapper for node-nats-streaming. Now develop with confidence without worrying about your cluster ID, client ID and message payload. Just an enum to rule 'em all.

How to use

Install using npm install kimono-wrapper service.ts

import { Message } from "node-nats-streaming";
import { Listener, Publisher, Event, natsWrapper } from "kimono-wrapper";

/**
 * Enum to map subjects
 */
enum Subjects {
  TransactionDebit = "transaction:debit",
  TransactionCredit = "transaction:credit"
}

/**
 * Interface defining the data structure and subject
 */
interface TransactionDebitEvent extends Event<Subjects> {
  subject: Subjects.TransactionDebit;
  data: {
    id: string;
    title: string;
    price: number;
  };
}

/**
 * Listener class implementation
 */
class TransactionDebitListener extends Listener<TransactionDebitEvent, Subjects> {
  subject: Subjects.TransactionDebit = Subjects.TransactionDebit;
  queueGroupName = "payments-service";
  debug = false;

  onMessage(data: TransactionDebitEvent["data"], msg: Message): void {
    this.debug ? console.log(`Event data: ${data}`) : null;
	  msg.ack();
  }
}

/**
 * Publisher class implementation
 */
class TransactionDebitPublisher extends Publisher<TransactionDebitEvent, Subjects> {
 subject: Subjects.TransactionDebit = Subjects.TransactionDebit;
}

/**
 * Publishing event
 */
new TransactionDebitPublisher(natsWrapper.client).publish({
  id: 'foo';
  title: 'bar';
  price: 10;
});