@greeksquard/common 1.0.5 → 1.0.7
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 +18 -0
- package/build/events/base-listener.js +30 -0
- package/build/events/base-publiser.d.ts +13 -0
- package/build/events/base-publiser.js +20 -0
- package/build/events/subjects.d.ts +4 -0
- package/build/events/subjects.js +8 -0
- package/build/events/ticket-created-event.d.ts +10 -0
- package/build/events/ticket-created-event.js +2 -0
- package/build/events/ticket-updated-event.d.ts +10 -0
- package/build/events/ticket-updated-event.js +2 -0
- package/build/middlewares/error-handler.d.ts +1 -1
- package/build/middlewares/error-handler.js +4 -1
- package/package.json +3 -2
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
protected ackWait: number;
|
|
12
|
+
private client;
|
|
13
|
+
constructor(client: Stan);
|
|
14
|
+
subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
|
|
15
|
+
listen(): void;
|
|
16
|
+
parseMessage(msg: Message): any;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
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;
|
|
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 parseData = this.parseMessage(msg);
|
|
22
|
+
this.onMessage(parseData, msg);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
parseMessage(msg) {
|
|
26
|
+
const data = msg.getData();
|
|
27
|
+
return typeof data === "string" ? data : JSON.parse(data.toString("utf8"));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Listener = Listener;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Subjects } from "./subjects";
|
|
2
|
+
import { Stan } from "node-nats-streaming";
|
|
3
|
+
interface Event {
|
|
4
|
+
subject: Subjects;
|
|
5
|
+
data: any;
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class BasePubliser<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 {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BasePubliser = void 0;
|
|
4
|
+
class BasePubliser {
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
publish(data) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
this.client.publish(this.subject, JSON.stringify(data), (err) => {
|
|
11
|
+
if (err) {
|
|
12
|
+
return reject(err);
|
|
13
|
+
}
|
|
14
|
+
console.log("Event Publish");
|
|
15
|
+
resolve();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.BasePubliser = BasePubliser;
|
|
@@ -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 = {}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) =>
|
|
2
|
+
export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
|
|
@@ -4,7 +4,10 @@ exports.errorHandler = void 0;
|
|
|
4
4
|
const custom_errors_1 = require("../errors/custom-errors");
|
|
5
5
|
const errorHandler = (err, req, res, next) => {
|
|
6
6
|
if (err instanceof custom_errors_1.CustomErrors) {
|
|
7
|
-
res.status(err.statusCode).send({ errors: err.serializedError() });
|
|
7
|
+
return res.status(err.statusCode).send({ errors: err.serializedError() });
|
|
8
8
|
}
|
|
9
|
+
res.status(400).send({
|
|
10
|
+
errors: [{ message: "Something went wrong" }]
|
|
11
|
+
});
|
|
9
12
|
};
|
|
10
13
|
exports.errorHandler = errorHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greeksquard/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"cookie-session": "^2.1.1",
|
|
28
28
|
"express": "^4.21.2",
|
|
29
29
|
"express-validator": "^7.3.0",
|
|
30
|
-
"jsonwebtoken": "^9.0.2"
|
|
30
|
+
"jsonwebtoken": "^9.0.2",
|
|
31
|
+
"node-nats-streaming": "^0.3.2"
|
|
31
32
|
}
|
|
32
33
|
}
|