@thantickets/common 1.0.9 → 1.0.11

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 actWait: 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,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Listener = void 0;
4
+ class Listener {
5
+ constructor(client) {
6
+ this.actWait = 5 * 1000;
7
+ this.client = client;
8
+ }
9
+ subscriptionOptions() {
10
+ return this.client
11
+ .subscriptionOptions()
12
+ .setDeliverAllAvailable()
13
+ .setManualAckMode(true)
14
+ .setAckWait(this.actWait)
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 recieved: ${this.subject} / ${this.queueGroupName}`);
21
+ const parseData = this.parseMessage(msg);
22
+ this.onMessage(parseData, 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 publish to subject', this.subject);
15
+ resolve();
16
+ });
17
+ });
18
+ }
19
+ }
20
+ exports.Publisher = Publisher;
@@ -0,0 +1,4 @@
1
+ export declare enum Subjects {
2
+ TicketCreated = "ticket:created",
3
+ TicketUpdated = "ticket:updated"
4
+ }
@@ -0,0 +1,8 @@
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 || (exports.Subjects = Subjects = {}));
@@ -0,0 +1,10 @@
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
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { Subjects } from "./subjects";
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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export declare enum OrderStatus {
2
+ Created = "created",
3
+ Cancelled = "cancelled",
4
+ AwaitingPayment = "awaiting:payment",
5
+ Complete = "complete"
6
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderStatus = void 0;
4
+ var OrderStatus;
5
+ (function (OrderStatus) {
6
+ //When the order has been created, but the
7
+ //ticket it is trying to order has not been reserved
8
+ OrderStatus["Created"] = "created";
9
+ //The ticket the order is trying to reserve has already
10
+ //been reserved, or when the user has cancelled the order
11
+ //The order expires before payment
12
+ OrderStatus["Cancelled"] = "cancelled";
13
+ //The order has successfully reserved the ticket
14
+ OrderStatus["AwaitingPayment"] = "awaiting:payment";
15
+ //The order has reserved the ticket and the user has
16
+ //provide payment successfully
17
+ OrderStatus["Complete"] = "complete";
18
+ })(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
package/build/index.d.ts CHANGED
@@ -8,3 +8,9 @@ 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-events';
16
+ export * from './events/types/order-status';
package/build/index.js CHANGED
@@ -24,3 +24,9 @@ __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-events"), exports);
32
+ __exportStar(require("./events/types/order-status"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thantickets/common",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
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
  }