@beltawnticket/common 1.0.7 → 1.0.9

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 nats, { 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
+ subscriptionOption(): nats.SubscriptionOptions;
15
+ listen(): void;
16
+ parseMessage(msg: Message): any;
17
+ }
18
+ export {};
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Listener = void 0;
4
+ class Listener {
5
+ // constructor of the object
6
+ // we provide a nat client which is connected to the nats server
7
+ constructor(client) {
8
+ this.ackwait = 5 * 1000;
9
+ this.client = client;
10
+ }
11
+ // concrete subsrciption options
12
+ subscriptionOption() {
13
+ return this.client
14
+ .subscriptionOptions()
15
+ .setDeliverAllAvailable()
16
+ .setManualAckMode(true)
17
+ .setAckWait(this.ackwait)
18
+ .setDurableName(this.queueGroupname);
19
+ }
20
+ // concrete function to listen to create a subscription and
21
+ // then on message setup a funcion
22
+ // we will call a onMessage function which should be defined in the children class
23
+ listen() {
24
+ const subscription = this.client.subscribe(this.subject, this.queueGroupname, this.subscriptionOption());
25
+ subscription.on('message', (msg) => {
26
+ console.log(`Message receive ${this.subject} / ${this.queueGroupname}`);
27
+ const parseData = this.parseMessage(msg);
28
+ // calling an abstract function
29
+ this.onMessage(parseData, msg);
30
+ });
31
+ }
32
+ // concrete function to parse message
33
+ parseMessage(msg) {
34
+ const data = msg.getData();
35
+ return typeof data === 'string'
36
+ ? JSON.parse(data)
37
+ : JSON.parse(data.toString('utf8'));
38
+ }
39
+ }
40
+ exports.Listener = Listener;
41
+ ;
@@ -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,21 @@
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
+ // function to publish
9
+ publish(data) {
10
+ return new Promise((resolve, reject) => {
11
+ this.client.publish(this.subject, JSON.stringify(data), (err) => {
12
+ if (err) {
13
+ return reject(err);
14
+ }
15
+ console.log('Event published to Subject');
16
+ resolve();
17
+ });
18
+ });
19
+ }
20
+ }
21
+ 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,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 });
package/build/index.d.ts CHANGED
@@ -3,7 +3,13 @@ export * from './errors/custom-error';
3
3
  export * from './errors/DatabaseConnectionError';
4
4
  export * from './errors/not-auth-error';
5
5
  export * from './errors/request-validation-error';
6
+ export * from './errors/not-found-error';
6
7
  export * from './middlewares/current-user';
7
8
  export * from './middlewares/error-handler';
8
9
  export * from './middlewares/require-auth';
9
10
  export * from './middlewares/validate-request';
11
+ export * from './events/base-listener';
12
+ export * from './events/basePublisher';
13
+ export * from './events/subjects';
14
+ export * from './events/ticket-created-event';
15
+ export * from './events/ticket-updated-events';
package/build/index.js CHANGED
@@ -19,7 +19,13 @@ __exportStar(require("./errors/custom-error"), exports);
19
19
  __exportStar(require("./errors/DatabaseConnectionError"), exports);
20
20
  __exportStar(require("./errors/not-auth-error"), exports);
21
21
  __exportStar(require("./errors/request-validation-error"), exports);
22
+ __exportStar(require("./errors/not-found-error"), exports);
22
23
  __exportStar(require("./middlewares/current-user"), exports);
23
24
  __exportStar(require("./middlewares/error-handler"), exports);
24
25
  __exportStar(require("./middlewares/require-auth"), exports);
25
26
  __exportStar(require("./middlewares/validate-request"), exports);
27
+ __exportStar(require("./events/base-listener"), exports);
28
+ __exportStar(require("./events/basePublisher"), exports);
29
+ __exportStar(require("./events/subjects"), exports);
30
+ __exportStar(require("./events/ticket-created-event"), exports);
31
+ __exportStar(require("./events/ticket-updated-events"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beltawnticket/common",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -17,10 +17,10 @@
17
17
  "license": "ISC",
18
18
  "type": "commonjs",
19
19
  "devDependencies": {
20
+ "@types/express": "^4.17.17",
20
21
  "del-cli": "^7.0.0",
21
- "rimraf": "^6.0.1",
22
22
  "express": "^4.17.1",
23
- "@types/express": "^4.17.17",
23
+ "rimraf": "^6.0.1",
24
24
  "typescript": "^5.9.3"
25
25
  },
26
26
  "dependencies": {
@@ -28,7 +28,8 @@
28
28
  "@types/jsonwebtoken": "^9.0.10",
29
29
  "cookie-session": "^2.1.1",
30
30
  "express-validator": "^7.2.1",
31
- "jsonwebtoken": "^9.0.2"
31
+ "jsonwebtoken": "^9.0.2",
32
+ "node-nats-streaming": "^0.3.2"
32
33
  },
33
34
  "peerDependencies": {
34
35
  "express": "^4.17.1"