@amktickets/common 1.0.4 → 1.0.8

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.
@@ -1,5 +1,8 @@
1
- import { CustomError } from "./custom-error.js";
2
- export class BadRequestError extends CustomError {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BadRequestError = void 0;
4
+ const custom_error_js_1 = require("./custom-error.js");
5
+ class BadRequestError extends custom_error_js_1.CustomError {
3
6
  constructor(message) {
4
7
  super(message);
5
8
  this.message = message;
@@ -10,3 +13,4 @@ export class BadRequestError extends CustomError {
10
13
  return [{ message: this.message }];
11
14
  }
12
15
  }
16
+ exports.BadRequestError = BadRequestError;
@@ -1,6 +1,10 @@
1
- export class CustomError extends Error {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomError = void 0;
4
+ class CustomError extends Error {
2
5
  constructor(message) {
3
6
  super(message);
4
7
  Object.setPrototypeOf(this, CustomError.prototype);
5
8
  }
6
9
  }
10
+ exports.CustomError = CustomError;
@@ -1,5 +1,8 @@
1
- import { CustomError } from "./custom-error.js";
2
- export class DatabaseConnectionError extends CustomError {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseConnectionError = void 0;
4
+ const custom_error_js_1 = require("./custom-error.js");
5
+ class DatabaseConnectionError extends custom_error_js_1.CustomError {
3
6
  constructor() {
4
7
  super('Error connecting to database');
5
8
  this.statusCode = 500;
@@ -10,3 +13,4 @@ export class DatabaseConnectionError extends CustomError {
10
13
  return [{ message: this.reason }];
11
14
  }
12
15
  }
16
+ exports.DatabaseConnectionError = DatabaseConnectionError;
@@ -1,5 +1,8 @@
1
- import { CustomError } from './custom-error.js';
2
- export class NotAuthorizedError extends CustomError {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotAuthorizedError = void 0;
4
+ const custom_error_js_1 = require("./custom-error.js");
5
+ class NotAuthorizedError extends custom_error_js_1.CustomError {
3
6
  constructor() {
4
7
  super('Not authorized');
5
8
  this.statusCode = 401;
@@ -9,3 +12,4 @@ export class NotAuthorizedError extends CustomError {
9
12
  return [{ message: 'Not authorized' }];
10
13
  }
11
14
  }
15
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -1,5 +1,8 @@
1
- import { CustomError } from "./custom-error.js";
2
- export class NotFoundError extends CustomError {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotFoundError = void 0;
4
+ const custom_error_js_1 = require("./custom-error.js");
5
+ class NotFoundError extends custom_error_js_1.CustomError {
3
6
  constructor() {
4
7
  super('Route not found');
5
8
  this.statusCode = 404;
@@ -9,3 +12,4 @@ export class NotFoundError extends CustomError {
9
12
  return [{ message: 'Not Found' }];
10
13
  }
11
14
  }
15
+ exports.NotFoundError = NotFoundError;
@@ -1,10 +1,13 @@
1
- import { CustomError } from "./custom-error.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestValidationError = void 0;
4
+ const custom_error_js_1 = require("./custom-error.js");
2
5
  // instead of creating an interface we will create abstract class
3
6
  // interface customError{
4
7
  // statusCode: number;
5
8
  // serializeErrors(): { message: string; field?: string }[];
6
9
  // }
7
- export class RequestValidationError extends CustomError {
10
+ class RequestValidationError extends custom_error_js_1.CustomError {
8
11
  constructor(errors) {
9
12
  super('Invalid request parameters');
10
13
  this.errors = errors;
@@ -21,3 +24,4 @@ export class RequestValidationError extends CustomError {
21
24
  });
22
25
  }
23
26
  }
27
+ exports.RequestValidationError = RequestValidationError;
@@ -0,0 +1,19 @@
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(): void;
16
+ parseMessage(msg: Message): any;
17
+ }
18
+ export {};
19
+ //# sourceMappingURL=base-listener.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-listener.d.ts","sourceRoot":"","sources":["../../src/events/base-listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,UAAU,KAAK;IACb,OAAO,EAAE,QAAQ,CAAC;IAClB,IAAI,EAAE,GAAG,CAAC;CACX;AAED,8BAAsB,QAAQ,CAAC,CAAC,SAAS,KAAK;IAC5C,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC;IAC/B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,OAAO,GAAG,IAAI;IACvD,OAAO,CAAC,MAAM,CAAO;IACrB,SAAS,CAAC,OAAO,SAAY;gBAEjB,MAAM,EAAE,IAAI;IAIxB,mBAAmB;IASnB,MAAM;IAcN,YAAY,CAAC,GAAG,EAAE,OAAO;CAM1B"}
@@ -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; // 5 seconds
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,14 @@
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 {};
14
+ //# sourceMappingURL=base-publisher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-publisher.d.ts","sourceRoot":"","sources":["../../src/events/base-publisher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,UAAU,KAAK;IACb,OAAO,EAAE,QAAQ,CAAC;IAClB,IAAI,EAAE,GAAG,CAAC;CACX;AAED,8BAAsB,SAAS,CAAC,CAAC,SAAS,KAAK;IAC3C,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC;IAC/B,OAAO,CAAC,MAAM,CAAO;gBAET,MAAM,EAAE,IAAI;IAIxB,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAW1C"}
@@ -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
+ return this.client.publish(this.subject, JSON.stringify(data), (err) => {
11
+ if (err) {
12
+ return reject(err);
13
+ }
14
+ console.log("Event published to subject", this.subject);
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
+ }
5
+ //# sourceMappingURL=subjects.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subjects.d.ts","sourceRoot":"","sources":["../../src/events/subjects.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ;IAChB,aAAa,mBAAmB;IAChC,aAAa,mBAAmB;CACnC"}
@@ -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,11 @@
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
+ }
11
+ //# sourceMappingURL=ticket-created-event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ticket-created-event.d.ts","sourceRoot":"","sources":["../../src/events/ticket-created-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC;IAChC,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAC;QACX,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
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
+ }
11
+ //# sourceMappingURL=ticket-updated-event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ticket-updated-event.d.ts","sourceRoot":"","sources":["../../src/events/ticket-updated-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC;IAChC,IAAI,EAAE;QACF,EAAE,EAAE,MAAM,CAAC;QACX,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;KAClB,CAAC;CACL"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/build/index.d.ts CHANGED
@@ -8,4 +8,9 @@ export * from './middlewares/current-user';
8
8
  export * from './middlewares/require-auth';
9
9
  export * from './middlewares/validate-request';
10
10
  export * from './middlewares/error-handler';
11
+ export * from './events/subjects';
12
+ export * from './events/ticket-created-event';
13
+ export * from './events/ticket-updated-event';
14
+ export * from './events/base-listener';
15
+ export * from './events/base-publisher';
11
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uBAAuB,CAAC;AACtC,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,0BAA0B,CAAC;AACzC,cAAc,mCAAmC,CAAC;AAGlD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uBAAuB,CAAC;AACtC,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,0BAA0B,CAAC;AACzC,cAAc,mCAAmC,CAAC;AAGlD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAG5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC"}
package/build/index.js CHANGED
@@ -1,10 +1,31 @@
1
- export * from './errors/bad-request-error';
2
- export * from './errors/custom-error';
3
- export * from './errors/database-connection-error';
4
- export * from './errors/not-authorized-error';
5
- export * from './errors/not-found-error';
6
- export * from './errors/request-validation-error';
7
- export * from './middlewares/current-user';
8
- export * from './middlewares/require-auth';
9
- export * from './middlewares/validate-request';
10
- export * from './middlewares/error-handler';
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("./errors/bad-request-error"), exports);
18
+ __exportStar(require("./errors/custom-error"), exports);
19
+ __exportStar(require("./errors/database-connection-error"), exports);
20
+ __exportStar(require("./errors/not-authorized-error"), exports);
21
+ __exportStar(require("./errors/not-found-error"), exports);
22
+ __exportStar(require("./errors/request-validation-error"), exports);
23
+ __exportStar(require("./middlewares/current-user"), exports);
24
+ __exportStar(require("./middlewares/require-auth"), exports);
25
+ __exportStar(require("./middlewares/validate-request"), exports);
26
+ __exportStar(require("./middlewares/error-handler"), exports);
27
+ __exportStar(require("./events/subjects"), exports);
28
+ __exportStar(require("./events/ticket-created-event"), exports);
29
+ __exportStar(require("./events/ticket-updated-event"), exports);
30
+ __exportStar(require("./events/base-listener"), exports);
31
+ __exportStar(require("./events/base-publisher"), exports);
@@ -1,10 +1,17 @@
1
- import jwt from "jsonwebtoken";
2
- export const currentUser = (req, res, next) => {
3
- if (!req.session?.jwt) {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.currentUser = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const currentUser = (req, res, next) => {
9
+ var _a;
10
+ if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
4
11
  return next();
5
12
  }
6
13
  try {
7
- const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
14
+ const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
8
15
  req.currentUser = payload;
9
16
  }
10
17
  catch (err) {
@@ -12,3 +19,4 @@ export const currentUser = (req, res, next) => {
12
19
  }
13
20
  next();
14
21
  };
22
+ exports.currentUser = currentUser;
@@ -1,9 +1,13 @@
1
- import { CustomError } from "../errors/custom-error.js";
2
- export const errorHandler = (err, req, res, next) => {
3
- if (err instanceof CustomError) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ const custom_error_js_1 = require("../errors/custom-error.js");
5
+ const errorHandler = (err, req, res, next) => {
6
+ if (err instanceof custom_error_js_1.CustomError) {
4
7
  return res.status(err.statusCode).send({ errors: err.serializeErrors() });
5
8
  }
6
9
  res.status(400).send({
7
10
  errors: [{ message: "Something went wrong" }],
8
11
  });
9
12
  };
13
+ exports.errorHandler = errorHandler;
@@ -1,7 +1,11 @@
1
- import { NotAuthorizedError } from "../errors/not-authorized-error.js";
2
- export const requireAuth = (req, res, next) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAuth = void 0;
4
+ const not_authorized_error_js_1 = require("../errors/not-authorized-error.js");
5
+ const requireAuth = (req, res, next) => {
3
6
  if (!req.currentUser) {
4
- throw new NotAuthorizedError();
7
+ throw new not_authorized_error_js_1.NotAuthorizedError();
5
8
  }
6
9
  next();
7
10
  };
11
+ exports.requireAuth = requireAuth;
@@ -1,9 +1,13 @@
1
- import { validationResult } from 'express-validator';
2
- import { RequestValidationError } from '../errors/request-validation-error.js';
3
- export const validateRequest = (req, res, next) => {
4
- const errors = validationResult(req);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateRequest = void 0;
4
+ const express_validator_1 = require("express-validator");
5
+ const request_validation_error_js_1 = require("../errors/request-validation-error.js");
6
+ const validateRequest = (req, res, next) => {
7
+ const errors = (0, express_validator_1.validationResult)(req);
5
8
  if (!errors.isEmpty()) {
6
- throw new RequestValidationError(errors.array());
9
+ throw new request_validation_error_js_1.RequestValidationError(errors.array());
7
10
  }
8
11
  next();
9
12
  };
13
+ exports.validateRequest = validateRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amktickets/common",
3
- "version": "1.0.4",
3
+ "version": "1.0.8",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.1.1",
27
27
  "express": "^5.2.1",
28
28
  "express-validator": "^7.3.1",
29
- "jsonwebtoken": "^9.0.3"
29
+ "jsonwebtoken": "^9.0.3",
30
+ "node-nats-streaming": "^0.3.2"
30
31
  }
31
32
  }