@munlib/common 1.0.1 → 1.0.6
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/build/events/base-listener.d.ts +8 -11
- package/build/events/base-listener.js +59 -21
- package/build/events/base-publisher.d.ts +5 -6
- package/build/events/base-publisher.js +2 -14
- package/build/events/subjects.js +1 -1
- package/build/events/types/order-status.js +1 -1
- package/build/index.js +5 -1
- package/build/middlewares/validate-request.js +1 -1
- package/package.json +2 -2
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Subjects } from
|
|
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[
|
|
9
|
-
abstract
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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(
|
|
6
|
-
this.
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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: true,
|
|
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.
|
|
28
|
-
return
|
|
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 {
|
|
2
|
-
import { Subjects } from
|
|
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[
|
|
9
|
-
protected
|
|
10
|
-
constructor(
|
|
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(
|
|
6
|
-
this.
|
|
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;
|
package/build/events/subjects.js
CHANGED
|
@@ -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
|
|
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
|
|
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.
|
|
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.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"build/**/*"
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"clean": "del
|
|
11
|
+
"clean": "del .\\build\\*",
|
|
12
12
|
"build": "npm run clean && tsc",
|
|
13
13
|
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
14
14
|
},
|