@ecom-micro/common 2.0.2 → 2.0.4

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/README.md ADDED
@@ -0,0 +1,19 @@
1
+ ### ecom-micro/common helps developer to catch production level nodejs application error easy way.
2
+
3
+ `app.use(errorHandler)` function is to catch global level error. It's alternative to express error controller. You need to use it in your `app.js` or where you write your express app logic.
4
+
5
+ #### Example to use any error class
6
+
7
+ ```JavaScript
8
+ import { BadRequestError } from '@ecom-micro/common';
9
+
10
+ return next(new BadRequestError('Invalid email or password'));
11
+ // OR
12
+ throw new BadRequestError('Invalid email or password');
13
+ ```
14
+
15
+ ##### List of method
16
+
17
+ ```JavaScript
18
+ import { BadRequestError, NotFoundError, errorHandler } from '@ecom-micro/common';
19
+ ```
@@ -0,0 +1,15 @@
1
+ import { Message, Connection, Channel } from 'amqplib';
2
+ import { Subjects } from '../types/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 onMessage(data: T['data'], msg: Message, channel: Channel): void;
10
+ private client;
11
+ constructor(client: Connection);
12
+ listen(): Promise<void>;
13
+ parseMessage(msg: Message): any;
14
+ }
15
+ export {};
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Listener = void 0;
40
+ var Listener = /** @class */ (function () {
41
+ function Listener(client) {
42
+ this.client = client;
43
+ }
44
+ Listener.prototype.listen = function () {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var _this = this;
47
+ return __generator(this, function (_a) {
48
+ this.client.createChannel().then(function (channel) {
49
+ channel.assertQueue(_this.subject);
50
+ channel.consume(_this.subject, function (message) {
51
+ if (message !== null) {
52
+ console.log("Message Received to: ".concat(_this.subject));
53
+ var parseMsg = _this.parseMessage(message);
54
+ _this.onMessage(parseMsg, message, channel);
55
+ }
56
+ });
57
+ });
58
+ return [2 /*return*/];
59
+ });
60
+ });
61
+ };
62
+ Listener.prototype.parseMessage = function (msg) {
63
+ var data = msg.content;
64
+ return typeof data === 'string' ? JSON.parse(data) : JSON.parse(data.toString('utf8'));
65
+ };
66
+ return Listener;
67
+ }());
68
+ exports.Listener = Listener;
@@ -0,0 +1,13 @@
1
+ import { Connection } from 'amqplib';
2
+ import { Subjects } from '../types/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
+ protected client: Connection;
10
+ constructor(client: Connection);
11
+ publish(data: T['data']): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Publisher = void 0;
4
+ var Publisher = /** @class */ (function () {
5
+ function Publisher(client) {
6
+ this.client = client;
7
+ }
8
+ Publisher.prototype.publish = function (data) {
9
+ var _this = this;
10
+ return new Promise(function (resolve, reject) {
11
+ _this.client
12
+ .createChannel()
13
+ .then(function (channel) {
14
+ channel.assertQueue(_this.subject, { durable: true });
15
+ channel.sendToQueue(_this.subject, Buffer.from(JSON.stringify(data)));
16
+ console.log("Event published to subject: ".concat(_this.subject));
17
+ resolve();
18
+ })
19
+ .catch(function (error) { return reject(error); });
20
+ });
21
+ };
22
+ return Publisher;
23
+ }());
24
+ exports.Publisher = Publisher;
@@ -0,0 +1,11 @@
1
+ import { Subjects } from '../types/subjects';
2
+ export interface ProductCreatedEvent {
3
+ subject: Subjects.ProductCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ productUrl: string;
9
+ sellerId: string;
10
+ };
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Message, Channel } from 'amqplib';
2
+ import { Subjects } from '../types/subjects';
3
+ import { Listener } from './baseListener';
4
+ import { ProductCreatedEvent } from './productCreatedEvent';
5
+ export declare class ProductCreatedListener extends Listener<ProductCreatedEvent> {
6
+ subject: Subjects.ProductCreated;
7
+ onMessage(data: ProductCreatedEvent['data'], message: Message, channel: Channel): void;
8
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.ProductCreatedListener = void 0;
19
+ var subjects_1 = require("../types/subjects");
20
+ var baseListener_1 = require("./baseListener");
21
+ var ProductCreatedListener = /** @class */ (function (_super) {
22
+ __extends(ProductCreatedListener, _super);
23
+ function ProductCreatedListener() {
24
+ var _this = _super !== null && _super.apply(this, arguments) || this;
25
+ _this.subject = subjects_1.Subjects.ProductCreated;
26
+ return _this;
27
+ }
28
+ ProductCreatedListener.prototype.onMessage = function (data, message, channel) {
29
+ channel.ack(message);
30
+ };
31
+ return ProductCreatedListener;
32
+ }(baseListener_1.Listener));
33
+ exports.ProductCreatedListener = ProductCreatedListener;
@@ -0,0 +1,6 @@
1
+ import { Subjects } from '../types/subjects';
2
+ import { Publisher } from './basePublisher';
3
+ import { ProductCreatedEvent } from './productCreatedEvent';
4
+ export declare class ProductCreatedPublisher extends Publisher<ProductCreatedEvent> {
5
+ subject: Subjects.ProductCreated;
6
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.ProductCreatedPublisher = void 0;
19
+ var subjects_1 = require("../types/subjects");
20
+ var basePublisher_1 = require("./basePublisher");
21
+ var ProductCreatedPublisher = /** @class */ (function (_super) {
22
+ __extends(ProductCreatedPublisher, _super);
23
+ function ProductCreatedPublisher() {
24
+ var _this = _super !== null && _super.apply(this, arguments) || this;
25
+ _this.subject = subjects_1.Subjects.ProductCreated;
26
+ return _this;
27
+ }
28
+ return ProductCreatedPublisher;
29
+ }(basePublisher_1.Publisher));
30
+ exports.ProductCreatedPublisher = ProductCreatedPublisher;
package/build/index.d.ts CHANGED
@@ -6,3 +6,9 @@ export * from './errors/requestValidationError';
6
6
  export * from './middleware/errorHandler';
7
7
  export * from './middleware/protect';
8
8
  export * from './middleware/requestValidation';
9
+ export * from './events/baseListener';
10
+ export * from './events/basePublisher';
11
+ export * from './events/productCreatedEvent';
12
+ export * from './events/productCreatedListener';
13
+ export * from './events/productCreatedPublisher';
14
+ export * from './types/subjects';
package/build/index.js CHANGED
@@ -22,3 +22,9 @@ __exportStar(require("./errors/requestValidationError"), exports);
22
22
  __exportStar(require("./middleware/errorHandler"), exports);
23
23
  __exportStar(require("./middleware/protect"), exports);
24
24
  __exportStar(require("./middleware/requestValidation"), exports);
25
+ __exportStar(require("./events/baseListener"), exports);
26
+ __exportStar(require("./events/basePublisher"), exports);
27
+ __exportStar(require("./events/productCreatedEvent"), exports);
28
+ __exportStar(require("./events/productCreatedListener"), exports);
29
+ __exportStar(require("./events/productCreatedPublisher"), exports);
30
+ __exportStar(require("./types/subjects"), exports);
@@ -0,0 +1,10 @@
1
+ export declare enum Subjects {
2
+ ProductCreated = "product-created",
3
+ ProductUpdated = "product-updated",
4
+ ProductDeleted = "product-deleted",
5
+ OrderCreated = "order-created",
6
+ OrderUpdated = "order-updated",
7
+ OrderCanceled = "order-cancelled",
8
+ ProductAddedCart = "product-add-to-cart",
9
+ ProductRemoveCart = "product-remove-to-cart"
10
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subjects = void 0;
4
+ var Subjects;
5
+ (function (Subjects) {
6
+ Subjects["ProductCreated"] = "product-created";
7
+ Subjects["ProductUpdated"] = "product-updated";
8
+ Subjects["ProductDeleted"] = "product-deleted";
9
+ Subjects["OrderCreated"] = "order-created";
10
+ Subjects["OrderUpdated"] = "order-updated";
11
+ Subjects["OrderCanceled"] = "order-cancelled";
12
+ Subjects["ProductAddedCart"] = "product-add-to-cart";
13
+ Subjects["ProductRemoveCart"] = "product-remove-to-cart";
14
+ })(Subjects = exports.Subjects || (exports.Subjects = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecom-micro/common",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -16,14 +16,16 @@
16
16
  "author": "",
17
17
  "license": "ISC",
18
18
  "dependencies": {
19
- "express": "^4.18.2",
19
+ "@types/amqplib": "^0.10.0",
20
+ "@types/cookie-session": "^2.0.44",
20
21
  "@types/express": "^4.17.14",
21
- "express-validator": "^6.14.2",
22
22
  "@types/express-validator": "^3.0.0",
23
23
  "@types/jsonwebtoken": "^8.5.9",
24
- "jsonwebtoken": "^8.5.1",
25
- "@types/cookie-session": "^2.0.44",
24
+ "amqplib": "^0.10.3",
26
25
  "cookie-session": "^2.0.0",
26
+ "express": "^4.18.2",
27
+ "express-validator": "^6.14.2",
28
+ "jsonwebtoken": "^8.5.1",
27
29
  "typescript": "^4.8.4"
28
30
  }
29
31
  }