@a.m.ticketing/common 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ import { Message, Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects.js';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Listener<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ abstract queueGroupName: string;
10
+ abstract onMessage(data: T['data'], msg: Message): void;
11
+ private client;
12
+ protected ackWait: number;
13
+ constructor(client: Stan);
14
+ subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
15
+ listen(): void;
16
+ parseMessage(msg: Message): any;
17
+ }
18
+ export {};
@@ -0,0 +1,31 @@
1
+ import { Message, Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects.js';
3
+ export class Listener {
4
+ client;
5
+ ackWait = 5 * 1000;
6
+ constructor(client) {
7
+ this.client = client;
8
+ }
9
+ subscriptionOptions() {
10
+ return this.client
11
+ .subscriptionOptions()
12
+ .setDeliverAllAvailable()
13
+ .setManualAckMode(true)
14
+ .setAckWait(this.ackWait)
15
+ .setDurableName(this.queueGroupName);
16
+ }
17
+ listen() {
18
+ const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
19
+ subscription.on('message', (msg) => {
20
+ console.log(`Message received: ${this.subject} / ${this.queueGroupName}`);
21
+ const parsedData = this.parseMessage(msg);
22
+ this.onMessage(parsedData, msg);
23
+ });
24
+ }
25
+ parseMessage(msg) {
26
+ const data = msg.getData();
27
+ return typeof data === 'string'
28
+ ? JSON.parse(data)
29
+ : JSON.parse(data.toString('utf8'));
30
+ }
31
+ }
@@ -0,0 +1,13 @@
1
+ import { Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects.js';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Publisher<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ private client;
10
+ constructor(client: Stan);
11
+ publish(data: T['data']): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,19 @@
1
+ import { Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects.js';
3
+ export class Publisher {
4
+ client;
5
+ constructor(client) {
6
+ this.client = client;
7
+ }
8
+ publish(data) {
9
+ return new Promise((resolve, reject) => {
10
+ this.client.publish(this.subject, JSON.stringify(data), (err) => {
11
+ if (err) {
12
+ return reject(err);
13
+ }
14
+ console.log('Event published to subject', this.subject);
15
+ resolve();
16
+ });
17
+ });
18
+ }
19
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum Subjects {
2
+ TicketCreated = "ticket:created",
3
+ TicketUpdated = "ticket:updated"
4
+ }
@@ -0,0 +1,5 @@
1
+ export var Subjects;
2
+ (function (Subjects) {
3
+ Subjects["TicketCreated"] = "ticket:created";
4
+ Subjects["TicketUpdated"] = "ticket:updated";
5
+ })(Subjects || (Subjects = {}));
@@ -0,0 +1,10 @@
1
+ import { Subjects } from './subjects.js';
2
+ export interface TicketCreatedEvent {
3
+ subject: Subjects.TicketCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1 @@
1
+ import { Subjects } from './subjects.js';
@@ -0,0 +1,10 @@
1
+ import { Subjects } from './subjects.js';
2
+ export interface TicketUpdatedEvent {
3
+ subject: Subjects.TicketUpdated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1 @@
1
+ import { Subjects } from './subjects.js';
package/build/index.d.ts CHANGED
@@ -8,3 +8,8 @@ export * from "./middlewares/current-user.js";
8
8
  export * from "./middlewares/error-handler.js";
9
9
  export * from "./middlewares/validate-request.js";
10
10
  export * from "./middlewares/require-auth.js";
11
+ export * from "./events/base-listener.js";
12
+ export * from "./events/base-publisher.js";
13
+ export * from "./events/subjects.js";
14
+ export * from "./events/ticket-created-event.js";
15
+ export * from "./events/ticket-updated-event.js";
package/build/index.js CHANGED
@@ -8,3 +8,8 @@ export * from "./middlewares/current-user.js";
8
8
  export * from "./middlewares/error-handler.js";
9
9
  export * from "./middlewares/validate-request.js";
10
10
  export * from "./middlewares/require-auth.js";
11
+ export * from "./events/base-listener.js";
12
+ export * from "./events/base-publisher.js";
13
+ export * from "./events/subjects.js";
14
+ export * from "./events/ticket-created-event.js";
15
+ export * from "./events/ticket-updated-event.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a.m.ticketing/common",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -27,6 +27,7 @@
27
27
  "cookie-session": "^2.1.1",
28
28
  "express": "^5.3.0",
29
29
  "express-validator": "^7.3.2",
30
- "jsonwebtoken": "^9.0.3"
30
+ "jsonwebtoken": "^9.0.3",
31
+ "node-nats-streaming": "^0.3.2"
31
32
  }
32
33
  }