vendease-common
TypeScript icon, indicating that this package has built-in type declarations

2.1.20 • Public • Published

vendease backend common

contains a set of common utility functions, classes, and modules that are used across the Vendease platform's backend system

Code example Jetstream setup

const CONFIG = {
  connectionOptions: {
    servers: ["localhost"],
    name: process.env.APP_NAME ?? "e-procurement",
  },
  consumerOptions: {
    deliverGroup: process.env.JETSTREAM_DELIVER_GROUP ?? "core-service-group",
    durable: process.env.JETSTREAM_CONSUMER_DURABLE ?? "core-services-durable",
    deliverTo: process.env.JETSTREAM_CONSUMER_DELIVER_TO ?? "core-services-deliver-to",
    manualAck: Boolean(process.env.JETSTREAM_MANUAL_ACK ?? "true"),
  },
  streamConfig: {
    name: process.env.JETSTREAM_STREAM_NAME || "core-services",
    subjects: (process.env.JETSTREAM_STREAM_SUBJECTS || "core.*").split(","),
  },

  //   host: process.env.NATS_HOST || 'demo.nats.io',
  //   port: Number(process.env.NATS_PORT) || 4222,
};
interface ICreateCompany {
  subject: Subjects.CompanyCreated;
  data: { id: number; name: string };
}
interface IUserCreatedCompany {
  subject: Subjects.UserCreated;
  data: { id: number; first_name: string; last_name: string };
}

// Singleton
class SingleInstanceStream extends BaseStream {
  queueGroupName: string = "sample group";

  protected async onMessage(data: any, msg: JsMsg): Promise<void> {
    console.log(data);
    console.log(msg.subject);

    switch (msg.subject) {
      case "Subjects.UserCreated":
        return this.handleUserCreated(data);
      case Subjects.CompanyCreated:
        return this.handleCompanyCreated(data);
      default:
        return "i probably don't understand this message";
    }

    msg.ack();
  }
}

export async function testConnection() {
  const stream = new SingleInstanceStream(config);

  await stream.connect();

  await stream.publish<IUserCreatedCompany>(Subjects.UserCreated, { id: 20, first_name: "bola", last_name: "tinubu" });
  await stream.publish<ICreateCompany>(Subjects.CompanyCreated, { id: 20, name: "dayo's company company created" });

  await stream.listen();

  await stream.close();
}

Readme

Keywords

none

Package Sidebar

Install

npm i vendease-common

Weekly Downloads

7

Version

2.1.20

License

ISC

Unpacked Size

432 kB

Total Files

414

Last publish

Collaborators

  • victoroluwaleye