@rovi-tickets/common 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ import { Message, Stan } from "node-nats-streaming";
2
+ import { Subjects } from "./subjects";
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<T>(): void;
16
+ parseMessage(msg: Message): T;
17
+ }
18
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Listener = void 0;
4
+ class Listener {
5
+ constructor(client) {
6
+ this.ackWait = 5 * 1000;
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
+ }
32
+ exports.Listener = Listener;
@@ -0,0 +1,13 @@
1
+ import { Stan } from "node-nats-streaming";
2
+ import { Subjects } from "./subjects";
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,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Publisher = void 0;
4
+ class Publisher {
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.');
15
+ resolve();
16
+ });
17
+ });
18
+ }
19
+ }
20
+ exports.Publisher = Publisher;
@@ -0,0 +1,5 @@
1
+ export declare enum Subjects {
2
+ TicketCreated = "ticket:created",
3
+ TicketUpdated = "ticket:updated",
4
+ OrderUpdated = "order:updated"
5
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subjects = void 0;
4
+ var Subjects;
5
+ (function (Subjects) {
6
+ Subjects["TicketCreated"] = "ticket:created";
7
+ Subjects["TicketUpdated"] = "ticket:updated";
8
+ Subjects["OrderUpdated"] = "order:updated";
9
+ })(Subjects || (exports.Subjects = Subjects = {}));
@@ -0,0 +1,9 @@
1
+ import { Subjects } from "./subjects";
2
+ export interface TicketCreatedEvent {
3
+ subject: Subjects.TicketCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { Subjects } from "../../../common/src/events/subjects";
2
+ export interface TicketUpdatedEvent {
3
+ subject: Subjects.TicketCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/build/index.d.ts CHANGED
@@ -8,3 +8,8 @@ export * from './middlewares/current-user';
8
8
  export * from './middlewares/error-handler';
9
9
  export * from './middlewares/require-auth';
10
10
  export * from './middlewares/validate-request';
11
+ export * from './events/base-listener';
12
+ export * from './events/base-publisher';
13
+ export * from './events/subjects';
14
+ export * from './events/ticket-created-event';
15
+ export * from './events/ticket-updated-event';
package/build/index.js CHANGED
@@ -24,3 +24,8 @@ __exportStar(require("./middlewares/current-user"), exports);
24
24
  __exportStar(require("./middlewares/error-handler"), exports);
25
25
  __exportStar(require("./middlewares/require-auth"), exports);
26
26
  __exportStar(require("./middlewares/validate-request"), exports);
27
+ __exportStar(require("./events/base-listener"), exports);
28
+ __exportStar(require("./events/base-publisher"), exports);
29
+ __exportStar(require("./events/subjects"), exports);
30
+ __exportStar(require("./events/ticket-created-event"), exports);
31
+ __exportStar(require("./events/ticket-updated-event"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rovi-tickets/common",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.1.0",
27
27
  "express": "^4.21.0",
28
28
  "express-validator": "^7.2.0",
29
- "jsonwebtoken": "^9.0.2"
29
+ "jsonwebtoken": "^9.0.2",
30
+ "node-nats-streaming": "^0.3.2"
30
31
  }
31
32
  }