@psctickets/common 1.0.15 → 1.0.17
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/BaseListener.d.ts +13 -0
- package/build/events/BaseListener.js +29 -0
- package/build/events/BasePublisher.d.ts +8 -0
- package/build/events/BasePublisher.js +33 -0
- package/build/events/Event.d.ts +5 -0
- package/build/events/Event.js +2 -0
- package/build/events/Subjects.d.ts +4 -0
- package/build/events/Subjects.js +8 -0
- package/build/events/TicketCreatedEvent.d.ts +9 -0
- package/build/events/TicketCreatedEvent.js +2 -0
- package/build/events/index.d.ts +5 -0
- package/build/events/index.js +21 -0
- package/build/types/express/index.d.ts +12 -0
- package/package.json +13 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Stan, SubscriptionOptions, Message } from "node-nats-streaming";
|
|
2
|
+
import { Event } from "./Event";
|
|
3
|
+
export declare abstract class BaseListener<T extends Event> {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: Stan);
|
|
6
|
+
abstract subject: T["subject"];
|
|
7
|
+
abstract queueGroupName: string;
|
|
8
|
+
abstract onMsgCallback: (data: T["data"], msg: Message) => void;
|
|
9
|
+
protected ackWait: number;
|
|
10
|
+
protected subscriptionOptions: () => SubscriptionOptions;
|
|
11
|
+
private parseMsg;
|
|
12
|
+
listen: () => void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseListener = void 0;
|
|
4
|
+
class BaseListener {
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.ackWait = 5 * 1000;
|
|
7
|
+
this.subscriptionOptions = () => this.client
|
|
8
|
+
.subscriptionOptions()
|
|
9
|
+
.setManualAckMode(true)
|
|
10
|
+
.setDeliverAllAvailable()
|
|
11
|
+
.setDurableName(this.queueGroupName)
|
|
12
|
+
.setAckWait(this.ackWait);
|
|
13
|
+
this.parseMsg = (msg) => {
|
|
14
|
+
const data = msg.getData();
|
|
15
|
+
return typeof data === "string"
|
|
16
|
+
? JSON.parse(data)
|
|
17
|
+
: JSON.parse(data.toString("utf-8"));
|
|
18
|
+
};
|
|
19
|
+
this.listen = () => {
|
|
20
|
+
const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
|
|
21
|
+
subscription.on("message", (msg) => {
|
|
22
|
+
console.log(`Message received | Subject: ${msg.getSubject()} | QueueGroup: ${this.queueGroupName} | Sequence: ${msg.getSequence()} `);
|
|
23
|
+
this.onMsgCallback(this.parseMsg(msg), msg);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
this.client = client;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.BaseListener = BaseListener;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Stan } from "node-nats-streaming";
|
|
2
|
+
import { Event } from "./Event";
|
|
3
|
+
export declare abstract class BasePublisher<T extends Event> {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: Stan);
|
|
6
|
+
abstract subject: T["subject"];
|
|
7
|
+
publish(data: T["data"]): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BasePublisher = void 0;
|
|
13
|
+
class BasePublisher {
|
|
14
|
+
constructor(client) {
|
|
15
|
+
this.client = client;
|
|
16
|
+
}
|
|
17
|
+
publish(data) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
//in case of publish failure - nats client will send that error in the err parameter in the callback
|
|
21
|
+
this.client.publish(this.subject, JSON.stringify(data), (err) => {
|
|
22
|
+
//in case of err - reject
|
|
23
|
+
if (err)
|
|
24
|
+
reject(err);
|
|
25
|
+
//else console and resolve
|
|
26
|
+
console.log("Event published: ", JSON.stringify(data));
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.BasePublisher = BasePublisher;
|
|
@@ -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,21 @@
|
|
|
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("./BaseListener"), exports);
|
|
18
|
+
__exportStar(require("./BasePublisher"), exports);
|
|
19
|
+
__exportStar(require("./Event"), exports);
|
|
20
|
+
__exportStar(require("./Subjects"), exports);
|
|
21
|
+
__exportStar(require("./TicketCreatedEvent"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import SignInTokenPayload from "../SignInTokenPayload";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* There is a difference in how properties are added to global as opposed to the express' Request interface.
|
|
5
|
+
* This is because ts knows exactly the properties available in the interface, because it is something already declared
|
|
6
|
+
* using @types/express.
|
|
7
|
+
*/
|
|
8
|
+
declare module "express" {
|
|
9
|
+
interface Request {
|
|
10
|
+
currentUser?: SignInTokenPayload;
|
|
11
|
+
}
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psctickets/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "common package for tickets learning project",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"files": [
|
|
@@ -22,11 +22,20 @@
|
|
|
22
22
|
"import": "./build/middlewares/index.js",
|
|
23
23
|
"require": "./build/middlewares/index.js",
|
|
24
24
|
"types": "./build/middlewares/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./events": {
|
|
27
|
+
"import": "./build/events/index.js",
|
|
28
|
+
"require": "./build/events/index.js",
|
|
29
|
+
"types": "./build/events/index.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"./types/express": {
|
|
32
|
+
"types": "./build/types/express/index.d.ts"
|
|
25
33
|
}
|
|
26
34
|
},
|
|
27
35
|
"scripts": {
|
|
28
|
-
"build": "npm run clean && tsc",
|
|
36
|
+
"build": "npm run clean && tsc && npm run copy-types",
|
|
29
37
|
"clean": "del-cli ./build/*",
|
|
38
|
+
"copy-types": "cp -r src/types/express build/types/",
|
|
30
39
|
"pub": "npm version patch && npm publish --access=public"
|
|
31
40
|
},
|
|
32
41
|
"keywords": [],
|
|
@@ -43,6 +52,7 @@
|
|
|
43
52
|
"express": "^5.1.0",
|
|
44
53
|
"express-validator": "^7.2.1",
|
|
45
54
|
"jsonwebtoken": "^9.0.2",
|
|
46
|
-
"mongoose": "^8.16.1"
|
|
55
|
+
"mongoose": "^8.16.1",
|
|
56
|
+
"node-nats-streaming": "^0.3.2"
|
|
47
57
|
}
|
|
48
58
|
}
|