@adarsh-tickets/common 1.0.6 → 1.0.11

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.
@@ -5,7 +5,7 @@ const custom_error_abstract_1 = require("./custom-error-abstract");
5
5
  class NotFoundError extends custom_error_abstract_1.AbstractCustomError {
6
6
  constructor() {
7
7
  super("Not found");
8
- this.statusCode = 400;
8
+ this.statusCode = 404;
9
9
  Object.setPrototypeOf(this, NotFoundError.prototype);
10
10
  }
11
11
  serializeErrors() {
@@ -0,0 +1,13 @@
1
+ import nats, { Message } from "node-nats-streaming";
2
+ import { SubjectAndMessageDataMap, SubjectEnum } from "../types";
3
+ export declare abstract class Listener<T extends SubjectEnum> {
4
+ private client;
5
+ abstract subject: T;
6
+ abstract queueGroupName: string;
7
+ abstract onMessage(data: SubjectAndMessageDataMap[T], msg: Message): void;
8
+ private ackWait;
9
+ constructor(client: nats.Stan);
10
+ setSubscriptionOptions(): nats.SubscriptionOptions;
11
+ listen(): void;
12
+ parseMessage(message: Message): any;
13
+ }
@@ -0,0 +1,31 @@
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
+ setSubscriptionOptions() {
10
+ return this.client
11
+ .subscriptionOptions()
12
+ .setManualAckMode(true)
13
+ .setAckWait(this.ackWait)
14
+ .setDeliverAllAvailable()
15
+ .setDurableName(this.queueGroupName);
16
+ }
17
+ listen() {
18
+ const subscriptions = this.client.subscribe(this.subject, this.queueGroupName, this.setSubscriptionOptions());
19
+ subscriptions.on("message", (msg) => {
20
+ const data = this.parseMessage(msg);
21
+ this.onMessage(data, msg);
22
+ });
23
+ }
24
+ parseMessage(message) {
25
+ const data = message.getData();
26
+ return typeof data === "string"
27
+ ? JSON.parse(data)
28
+ : JSON.parse(data.toString("utf8"));
29
+ }
30
+ }
31
+ exports.Listener = Listener;
@@ -0,0 +1,8 @@
1
+ import nats from "node-nats-streaming";
2
+ import { SubjectAndMessageDataMap, SubjectEnum } from "../types";
3
+ export declare abstract class Publisher<T extends SubjectEnum> {
4
+ private client;
5
+ abstract subject: T;
6
+ constructor(client: nats.Stan);
7
+ publish(data: SubjectAndMessageDataMap[T]): Promise<void>;
8
+ }
@@ -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
+ publish(data) {
9
+ const stringifyData = JSON.stringify(data);
10
+ return new Promise((res, rej) => {
11
+ this.client.publish(this.subject, stringifyData, (err) => {
12
+ if (err) {
13
+ return rej(err);
14
+ }
15
+ console.log(stringifyData, "published on channel", this.subject);
16
+ res();
17
+ });
18
+ });
19
+ }
20
+ }
21
+ exports.Publisher = Publisher;
@@ -0,0 +1,2 @@
1
+ export * from "./base-listener";
2
+ export * from "./base-publisher";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./base-listener"), exports);
18
+ __exportStar(require("./base-publisher"), exports);
package/build/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from "./errors";
2
2
  export * from "./middlewares";
3
+ export * from "./events";
4
+ export * from "./types";
package/build/index.js CHANGED
@@ -17,3 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  // export the common middlewares and errors
18
18
  __exportStar(require("./errors"), exports);
19
19
  __exportStar(require("./middlewares"), exports);
20
+ __exportStar(require("./events"), exports);
21
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,6 @@
1
+ import { IOrderCreateData, ITicketCreateData } from "./event.types";
2
+ import { SubjectEnum } from "./subject.type";
3
+ export interface SubjectAndMessageDataMap {
4
+ [SubjectEnum.TicketCreated]: ITicketCreateData;
5
+ [SubjectEnum.OrderCreated]: IOrderCreateData;
6
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const subject_type_1 = require("./subject.type");
@@ -0,0 +1,19 @@
1
+ import { SubjectEnum } from "./subject.type";
2
+ export interface ITicketCreateData {
3
+ id: string;
4
+ title: string;
5
+ price: number;
6
+ }
7
+ export interface ITicketCreateEvent {
8
+ subject: SubjectEnum.TicketCreated;
9
+ data: ITicketCreateData;
10
+ }
11
+ export interface IOrderCreateData {
12
+ id: string;
13
+ ticketId: string;
14
+ userId: string;
15
+ }
16
+ export interface IOrderCreateEvent {
17
+ subject: SubjectEnum.OrderCreated;
18
+ data: IOrderCreateData;
19
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from "./subject.type";
2
+ export * from "./event.types";
3
+ export * from "./event-mapping.type";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./subject.type"), exports);
18
+ __exportStar(require("./event.types"), exports);
19
+ __exportStar(require("./event-mapping.type"), exports);
@@ -0,0 +1,4 @@
1
+ export declare enum SubjectEnum {
2
+ TicketCreated = "ticket:created",
3
+ OrderCreated = "order:created"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SubjectEnum = void 0;
4
+ var SubjectEnum;
5
+ (function (SubjectEnum) {
6
+ SubjectEnum["TicketCreated"] = "ticket:created";
7
+ SubjectEnum["OrderCreated"] = "order:created";
8
+ })(SubjectEnum || (exports.SubjectEnum = SubjectEnum = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adarsh-tickets/common",
3
- "version": "1.0.6",
3
+ "version": "1.0.11",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -24,6 +24,7 @@
24
24
  "@types/jsonwebtoken": "^9.0.10",
25
25
  "express": "^5.2.1",
26
26
  "express-validator": "^7.3.2",
27
- "jsonwebtoken": "^9.0.3"
27
+ "jsonwebtoken": "^9.0.3",
28
+ "node-nats-streaming": "^0.3.2"
28
29
  }
29
30
  }