@ajayjbtickets/common 1.0.28 → 1.0.33

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.
package/dist/index.cjs CHANGED
@@ -46,13 +46,16 @@ __export(index_exports, {
46
46
  InternalError: () => InternalError,
47
47
  InternalErrorResponse: () => InternalErrorResponse,
48
48
  JwtService: () => JwtService,
49
+ Listener: () => Listener,
49
50
  NoDataError: () => NoDataError,
50
51
  NoEntryError: () => NoEntryError,
51
52
  NoFoundResponse: () => NoFoundResponse,
52
53
  NotFoundError: () => NotFoundError,
53
54
  Password: () => Password,
55
+ Publisher: () => Publisher,
54
56
  ResponseStatusCode: () => ResponseStatusCode,
55
57
  StatusCode: () => StatusCode,
58
+ Subjects: () => Subjects,
56
59
  SuccessResponse: () => SuccessResponse,
57
60
  TokenExpiredError: () => TokenExpiredError,
58
61
  ValidationSource: () => ValidationSource,
@@ -508,6 +511,58 @@ var sanitizeObject = (value) => {
508
511
  }
509
512
  return newObject;
510
513
  };
514
+
515
+ // src/events/baseListener.ts
516
+ var Listener = class {
517
+ ackWait = 5 * 1e3;
518
+ client;
519
+ constructor(client) {
520
+ this.client = client;
521
+ }
522
+ subscriptionOptions() {
523
+ return this.client.subscriptionOptions().setManualAckMode(true).setDeliverAllAvailable().setAckWait(this.ackWait).setDurableName(this.queueGroupName);
524
+ }
525
+ listen() {
526
+ const subscription = this.client.subscribe(
527
+ this.subject,
528
+ this.queueGroupName,
529
+ this.subscriptionOptions()
530
+ );
531
+ subscription.on("message", (msg) => {
532
+ const parsedData = this.parseData(msg);
533
+ this.onMessage(parsedData, msg);
534
+ });
535
+ }
536
+ parseData(msg) {
537
+ const data = msg.getData();
538
+ return typeof data === "string" ? JSON.parse(data) : JSON.parse(data.toString("utf-8"));
539
+ }
540
+ };
541
+
542
+ // src/events/basePublisher.ts
543
+ var Publisher = class {
544
+ client;
545
+ constructor(client) {
546
+ this.client = client;
547
+ }
548
+ async publish(data) {
549
+ return new Promise((resolve, reject) => {
550
+ this.client.publish(this.subject, JSON.stringify(data), (error, guid) => {
551
+ if (error) {
552
+ reject(error);
553
+ }
554
+ resolve(guid);
555
+ });
556
+ });
557
+ }
558
+ };
559
+
560
+ // src/events/subjects.ts
561
+ var Subjects = /* @__PURE__ */ ((Subjects2) => {
562
+ Subjects2["TicketCreated"] = "ticket:created";
563
+ Subjects2["TicketUpdated"] = "ticket:updated";
564
+ return Subjects2;
565
+ })(Subjects || {});
511
566
  // Annotate the CommonJS export names for ESM import in node:
512
567
  0 && (module.exports = {
513
568
  AccessTokenError,
@@ -526,13 +581,16 @@ var sanitizeObject = (value) => {
526
581
  InternalError,
527
582
  InternalErrorResponse,
528
583
  JwtService,
584
+ Listener,
529
585
  NoDataError,
530
586
  NoEntryError,
531
587
  NoFoundResponse,
532
588
  NotFoundError,
533
589
  Password,
590
+ Publisher,
534
591
  ResponseStatusCode,
535
592
  StatusCode,
593
+ Subjects,
536
594
  SuccessResponse,
537
595
  TokenExpiredError,
538
596
  ValidationSource,
package/dist/index.d.cts CHANGED
@@ -2,6 +2,8 @@ import { Response, Request, NextFunction } from 'express';
2
2
  import winston from 'winston';
3
3
  import { ZodSchema } from 'zod';
4
4
  import jsonwebtoken, { SignOptions, VerifyOptions } from 'jsonwebtoken';
5
+ import * as node_nats_streaming from 'node-nats-streaming';
6
+ import { Message, Stan } from 'node-nats-streaming';
5
7
 
6
8
  declare const ENVIRONMENTS: {
7
9
  production: string;
@@ -184,4 +186,48 @@ declare const asyncHandler: (execution: AsyncFunction) => (req: Request, res: Re
184
186
 
185
187
  declare const sanitizeObject: <T>(value: T) => T;
186
188
 
187
- export { AccessTokenError, AccessTokenErrorResponse, ApiError, ApiResponse, AuthFailureError, BadRequestError, BadRequestResponse, BadTokenError, BadTokenResponse, ENVIRONMENTS, type ErrorDetailType, ErrorType, ForbiddenError, ForbiddenResponse, InternalError, InternalErrorResponse, type JwtPayload, JwtService, NoDataError, NoEntryError, NoFoundResponse, NotFoundError, type Pagination, Password, ResponseStatusCode, StatusCode, SuccessResponse, TokenExpiredError, ValidationSource, asyncHandler, logger, sanitizeObject, schemaValidator, verifyToken };
189
+ declare enum Subjects {
190
+ TicketCreated = "ticket:created",
191
+ TicketUpdated = "ticket:updated"
192
+ }
193
+
194
+ interface Event {
195
+ subject: Subjects;
196
+ data: any;
197
+ }
198
+
199
+ declare abstract class Listener<T extends Event> {
200
+ abstract subject: T["subject"];
201
+ abstract queueGroupName: string;
202
+ abstract onMessage(data: T["data"], msg: Message): void;
203
+ protected ackWait: number;
204
+ private client;
205
+ constructor(client: Stan);
206
+ subscriptionOptions(): node_nats_streaming.SubscriptionOptions;
207
+ listen(): void;
208
+ parseData(msg: Message): T["data"];
209
+ }
210
+
211
+ declare abstract class Publisher<T extends Event> {
212
+ abstract subject: T["subject"];
213
+ private client;
214
+ constructor(client: Stan);
215
+ publish(data: T["data"]): Promise<string>;
216
+ }
217
+
218
+ interface Ticket {
219
+ id: number;
220
+ name: string;
221
+ price: number;
222
+ userId: string;
223
+ }
224
+ interface TicketCreatedEvent {
225
+ subject: Subjects.TicketCreated;
226
+ data: Ticket;
227
+ }
228
+ interface TicketUpdatedEvent {
229
+ subject: Subjects.TicketUpdated;
230
+ data: Ticket;
231
+ }
232
+
233
+ export { AccessTokenError, AccessTokenErrorResponse, ApiError, ApiResponse, AuthFailureError, BadRequestError, BadRequestResponse, BadTokenError, BadTokenResponse, ENVIRONMENTS, type ErrorDetailType, ErrorType, ForbiddenError, ForbiddenResponse, InternalError, InternalErrorResponse, type JwtPayload, JwtService, Listener, NoDataError, NoEntryError, NoFoundResponse, NotFoundError, type Pagination, Password, Publisher, ResponseStatusCode, StatusCode, Subjects, SuccessResponse, type TicketCreatedEvent, type TicketUpdatedEvent, TokenExpiredError, ValidationSource, asyncHandler, logger, sanitizeObject, schemaValidator, verifyToken };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,8 @@ import { Response, Request, NextFunction } from 'express';
2
2
  import winston from 'winston';
3
3
  import { ZodSchema } from 'zod';
4
4
  import jsonwebtoken, { SignOptions, VerifyOptions } from 'jsonwebtoken';
5
+ import * as node_nats_streaming from 'node-nats-streaming';
6
+ import { Message, Stan } from 'node-nats-streaming';
5
7
 
6
8
  declare const ENVIRONMENTS: {
7
9
  production: string;
@@ -184,4 +186,48 @@ declare const asyncHandler: (execution: AsyncFunction) => (req: Request, res: Re
184
186
 
185
187
  declare const sanitizeObject: <T>(value: T) => T;
186
188
 
187
- export { AccessTokenError, AccessTokenErrorResponse, ApiError, ApiResponse, AuthFailureError, BadRequestError, BadRequestResponse, BadTokenError, BadTokenResponse, ENVIRONMENTS, type ErrorDetailType, ErrorType, ForbiddenError, ForbiddenResponse, InternalError, InternalErrorResponse, type JwtPayload, JwtService, NoDataError, NoEntryError, NoFoundResponse, NotFoundError, type Pagination, Password, ResponseStatusCode, StatusCode, SuccessResponse, TokenExpiredError, ValidationSource, asyncHandler, logger, sanitizeObject, schemaValidator, verifyToken };
189
+ declare enum Subjects {
190
+ TicketCreated = "ticket:created",
191
+ TicketUpdated = "ticket:updated"
192
+ }
193
+
194
+ interface Event {
195
+ subject: Subjects;
196
+ data: any;
197
+ }
198
+
199
+ declare abstract class Listener<T extends Event> {
200
+ abstract subject: T["subject"];
201
+ abstract queueGroupName: string;
202
+ abstract onMessage(data: T["data"], msg: Message): void;
203
+ protected ackWait: number;
204
+ private client;
205
+ constructor(client: Stan);
206
+ subscriptionOptions(): node_nats_streaming.SubscriptionOptions;
207
+ listen(): void;
208
+ parseData(msg: Message): T["data"];
209
+ }
210
+
211
+ declare abstract class Publisher<T extends Event> {
212
+ abstract subject: T["subject"];
213
+ private client;
214
+ constructor(client: Stan);
215
+ publish(data: T["data"]): Promise<string>;
216
+ }
217
+
218
+ interface Ticket {
219
+ id: number;
220
+ name: string;
221
+ price: number;
222
+ userId: string;
223
+ }
224
+ interface TicketCreatedEvent {
225
+ subject: Subjects.TicketCreated;
226
+ data: Ticket;
227
+ }
228
+ interface TicketUpdatedEvent {
229
+ subject: Subjects.TicketUpdated;
230
+ data: Ticket;
231
+ }
232
+
233
+ export { AccessTokenError, AccessTokenErrorResponse, ApiError, ApiResponse, AuthFailureError, BadRequestError, BadRequestResponse, BadTokenError, BadTokenResponse, ENVIRONMENTS, type ErrorDetailType, ErrorType, ForbiddenError, ForbiddenResponse, InternalError, InternalErrorResponse, type JwtPayload, JwtService, Listener, NoDataError, NoEntryError, NoFoundResponse, NotFoundError, type Pagination, Password, Publisher, ResponseStatusCode, StatusCode, Subjects, SuccessResponse, type TicketCreatedEvent, type TicketUpdatedEvent, TokenExpiredError, ValidationSource, asyncHandler, logger, sanitizeObject, schemaValidator, verifyToken };
package/dist/index.js CHANGED
@@ -442,6 +442,58 @@ var sanitizeObject = (value) => {
442
442
  }
443
443
  return newObject;
444
444
  };
445
+
446
+ // src/events/baseListener.ts
447
+ var Listener = class {
448
+ ackWait = 5 * 1e3;
449
+ client;
450
+ constructor(client) {
451
+ this.client = client;
452
+ }
453
+ subscriptionOptions() {
454
+ return this.client.subscriptionOptions().setManualAckMode(true).setDeliverAllAvailable().setAckWait(this.ackWait).setDurableName(this.queueGroupName);
455
+ }
456
+ listen() {
457
+ const subscription = this.client.subscribe(
458
+ this.subject,
459
+ this.queueGroupName,
460
+ this.subscriptionOptions()
461
+ );
462
+ subscription.on("message", (msg) => {
463
+ const parsedData = this.parseData(msg);
464
+ this.onMessage(parsedData, msg);
465
+ });
466
+ }
467
+ parseData(msg) {
468
+ const data = msg.getData();
469
+ return typeof data === "string" ? JSON.parse(data) : JSON.parse(data.toString("utf-8"));
470
+ }
471
+ };
472
+
473
+ // src/events/basePublisher.ts
474
+ var Publisher = class {
475
+ client;
476
+ constructor(client) {
477
+ this.client = client;
478
+ }
479
+ async publish(data) {
480
+ return new Promise((resolve, reject) => {
481
+ this.client.publish(this.subject, JSON.stringify(data), (error, guid) => {
482
+ if (error) {
483
+ reject(error);
484
+ }
485
+ resolve(guid);
486
+ });
487
+ });
488
+ }
489
+ };
490
+
491
+ // src/events/subjects.ts
492
+ var Subjects = /* @__PURE__ */ ((Subjects2) => {
493
+ Subjects2["TicketCreated"] = "ticket:created";
494
+ Subjects2["TicketUpdated"] = "ticket:updated";
495
+ return Subjects2;
496
+ })(Subjects || {});
445
497
  export {
446
498
  AccessTokenError,
447
499
  AccessTokenErrorResponse,
@@ -459,13 +511,16 @@ export {
459
511
  InternalError,
460
512
  InternalErrorResponse,
461
513
  JwtService,
514
+ Listener,
462
515
  NoDataError,
463
516
  NoEntryError,
464
517
  NoFoundResponse,
465
518
  NotFoundError,
466
519
  Password,
520
+ Publisher,
467
521
  ResponseStatusCode,
468
522
  StatusCode,
523
+ Subjects,
469
524
  SuccessResponse,
470
525
  TokenExpiredError,
471
526
  ValidationSource,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ajayjbtickets/common",
3
- "version": "1.0.28",
3
+ "version": "1.0.33",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -22,8 +22,8 @@
22
22
  }
23
23
  },
24
24
  "scripts": {
25
- "dev": "tsx ./src/index.ts",
26
- "clean": "del ./dist/*",
25
+ "dev": "tsx watch src/index.ts",
26
+ "clean": "del dist/*",
27
27
  "build": "npm run clean && tsup",
28
28
  "release": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish"
29
29
  },
@@ -37,6 +37,7 @@
37
37
  "express": "^5.1.0",
38
38
  "jsonwebtoken": "^9.0.2",
39
39
  "lodash": "^4.17.21",
40
+ "node-nats-streaming": "^0.3.2",
40
41
  "winston": "^3.17.0",
41
42
  "winston-daily-rotate-file": "^5.0.0",
42
43
  "zod": "^3.24.4"