@munlib/common 1.0.1 → 1.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.
@@ -1,18 +1,15 @@
1
- import { Message, Stan } from 'node-nats-streaming';
2
- import { Subjects } from './subjects';
1
+ import { Channel } from "amqplib";
2
+ import { Subjects } from "./subjects";
3
3
  interface Event {
4
4
  subject: Subjects;
5
5
  data: any;
6
6
  }
7
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
- protected client: Stan;
12
- protected ackWait: number;
13
- constructor(client: Stan);
14
- subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
15
- listen(): void;
16
- parseMessage(msg: Message): any;
8
+ abstract subject: T["subject"];
9
+ abstract onMessage(data: T["data"], msg: any): void;
10
+ private channel;
11
+ constructor(channel: Channel);
12
+ listen(): Promise<void>;
13
+ parseMessage(msg: any): any;
17
14
  }
18
15
  export {};
@@ -1,33 +1,71 @@
1
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
+ };
2
38
  Object.defineProperty(exports, "__esModule", { value: true });
3
39
  exports.Listener = void 0;
4
40
  var Listener = /** @class */ (function () {
5
- function Listener(client) {
6
- this.ackWait = 5 * 1000;
7
- this.client = client;
41
+ function Listener(channel) {
42
+ this.channel = channel;
8
43
  }
9
- Listener.prototype.subscriptionOptions = function () {
10
- return this.client
11
- .subscriptionOptions()
12
- .setDeliverAllAvailable()
13
- .setManualAckMode(true)
14
- .setAckWait(this.ackWait)
15
- .setDurableName(this.queueGroupName);
16
- };
17
44
  Listener.prototype.listen = function () {
18
- var _this = this;
19
- var subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
20
- subscription.on('message', function (msg) {
21
- console.log("Message received: " + _this.subject + " / " + _this.queueGroupName);
22
- var parsedData = _this.parseMessage(msg);
23
- _this.onMessage(parsedData, msg);
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var _this = this;
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0: return [4 /*yield*/, this.channel.assertQueue(this.subject, {
50
+ durable: false,
51
+ })];
52
+ case 1:
53
+ _a.sent();
54
+ this.channel.consume(this.subject, function (msg) {
55
+ if (msg) {
56
+ var parsedData = _this.parseMessage(msg);
57
+ _this.onMessage(parsedData, msg);
58
+ _this.channel.ack(msg);
59
+ }
60
+ });
61
+ return [2 /*return*/];
62
+ }
63
+ });
24
64
  });
25
65
  };
26
66
  Listener.prototype.parseMessage = function (msg) {
27
- var data = msg.getData();
28
- return typeof data === 'string'
29
- ? JSON.parse(data)
30
- : JSON.parse(data.toString('utf8'));
67
+ var data = msg.content.toString();
68
+ return JSON.parse(data);
31
69
  };
32
70
  return Listener;
33
71
  }());
@@ -1,13 +1,12 @@
1
- import { Stan } from 'node-nats-streaming';
2
- import { Subjects } from './subjects';
1
+ import { Channel } from "amqplib/callback_api";
2
+ import { Subjects } from "./subjects";
3
3
  interface Event {
4
4
  subject: Subjects;
5
5
  data: any;
6
6
  }
7
7
  export declare abstract class Publisher<T extends Event> {
8
- abstract subject: T['subject'];
9
- protected client: Stan;
10
- constructor(client: Stan);
11
- publish(data: T['data']): Promise<void>;
8
+ abstract subject: T["subject"];
9
+ protected channel: Channel;
10
+ constructor(channel: Channel);
12
11
  }
13
12
  export {};
@@ -2,21 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Publisher = void 0;
4
4
  var Publisher = /** @class */ (function () {
5
- function Publisher(client) {
6
- this.client = client;
5
+ function Publisher(channel) {
6
+ this.channel = channel;
7
7
  }
8
- Publisher.prototype.publish = function (data) {
9
- var _this = this;
10
- return new Promise(function (resolve, reject) {
11
- _this.client.publish(_this.subject, JSON.stringify(data), function (err) {
12
- if (err) {
13
- return reject(err);
14
- }
15
- console.log('Event published to subject', _this.subject);
16
- resolve();
17
- });
18
- });
19
- };
20
8
  return Publisher;
21
9
  }());
22
10
  exports.Publisher = Publisher;
@@ -7,4 +7,4 @@ var Subjects;
7
7
  Subjects["TicketUpdated"] = "ticket:updated";
8
8
  Subjects["OrderCreated"] = "order:created";
9
9
  Subjects["OrderCancelled"] = "order:cancelled";
10
- })(Subjects = exports.Subjects || (exports.Subjects = {}));
10
+ })(Subjects || (exports.Subjects = Subjects = {}));
@@ -15,4 +15,4 @@ var OrderStatus;
15
15
  // The order has reserved the ticket and the user has
16
16
  // provided payment successfully
17
17
  OrderStatus["Complete"] = "complete";
18
- })(OrderStatus = exports.OrderStatus || (exports.OrderStatus = {}));
18
+ })(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
package/build/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -4,7 +4,7 @@ exports.validateRequest = void 0;
4
4
  var express_validator_1 = require("express-validator");
5
5
  var request_validation_error_1 = require("../errors/request-validation-error");
6
6
  var validateRequest = function (req, res, next) {
7
- var errors = express_validator_1.validationResult(req);
7
+ var errors = (0, express_validator_1.validationResult)(req);
8
8
  if (!errors.isEmpty()) {
9
9
  throw new request_validation_error_1.RequestValidationError(errors.array());
10
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@munlib/common",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -8,8 +8,8 @@
8
8
  "build/**/*"
9
9
  ],
10
10
  "scripts": {
11
- "clean": "del ./build/*",
12
- "build": "npm run clean && tsc",
11
+ "clean": "del .\\build\\*",
12
+ "build": "tsc",
13
13
  "pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
14
14
  },
15
15
  "keywords": [],