@a.m.ticketing/common 1.0.2 → 1.0.5
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/errors/BadRequestError.d.ts +9 -0
- package/build/errors/BadRequestError.js +12 -0
- package/build/errors/DatabaseConnectionError.d.ts +9 -0
- package/build/errors/DatabaseConnectionError.js +11 -0
- package/build/errors/RequestValidationError.d.ts +14 -0
- package/build/errors/RequestValidationError.js +17 -0
- package/build/errors/customError.d.ts +8 -0
- package/build/errors/customError.js +5 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +10 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +10 -0
- package/build/events/base-listener.d.ts +18 -0
- package/build/events/base-listener.js +31 -0
- package/build/events/base-publisher.d.ts +13 -0
- package/build/events/base-publisher.js +19 -0
- package/build/events/subjects.d.ts +4 -0
- package/build/events/subjects.js +5 -0
- package/build/events/ticket-created-event.d.ts +10 -0
- package/build/events/ticket-created-event.js +1 -0
- package/build/events/ticket-updated-event.d.ts +10 -0
- package/build/events/ticket-updated-event.js +1 -0
- package/build/index.d.ts +15 -1
- package/build/index.js +15 -6
- package/build/middlewares/current-user.d.ts +17 -0
- package/build/middlewares/current-user.js +12 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +8 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +7 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +9 -0
- package/package.json +12 -2
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class BadRequestError extends CustomError {
|
|
3
|
+
message;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.message = message;
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [{ message: this.message }];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class DatabaseConnectionError extends CustomError {
|
|
3
|
+
statusCode = 500;
|
|
4
|
+
reason = "database connection error";
|
|
5
|
+
constructor() {
|
|
6
|
+
super('error connection with DB');
|
|
7
|
+
}
|
|
8
|
+
serializeErrors() {
|
|
9
|
+
return [{ message: this.reason }];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
import type { ValidationError } from "express-validator";
|
|
3
|
+
export declare class RequestValidationError extends CustomError {
|
|
4
|
+
errors: ValidationError[];
|
|
5
|
+
statusCode: number;
|
|
6
|
+
constructor(errors: ValidationError[]);
|
|
7
|
+
serializeErrors(): ({
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
} | {
|
|
11
|
+
field?: never;
|
|
12
|
+
message: any;
|
|
13
|
+
})[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class RequestValidationError extends CustomError {
|
|
3
|
+
errors;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(errors) {
|
|
6
|
+
super("invalid error");
|
|
7
|
+
this.errors = errors;
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return this.errors.map(error => {
|
|
11
|
+
if (error.type === "field") {
|
|
12
|
+
return { message: error.msg, field: error.path };
|
|
13
|
+
}
|
|
14
|
+
return { message: error.msg };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Message, Stan } from 'node-nats-streaming';
|
|
2
|
+
import { Subjects } from './subjects.js';
|
|
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
|
+
private client;
|
|
12
|
+
protected ackWait: number;
|
|
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,31 @@
|
|
|
1
|
+
import { Message, Stan } from 'node-nats-streaming';
|
|
2
|
+
import { Subjects } from './subjects.js';
|
|
3
|
+
export class Listener {
|
|
4
|
+
client;
|
|
5
|
+
ackWait = 5 * 1000;
|
|
6
|
+
constructor(client) {
|
|
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 parsedData = this.parseMessage(msg);
|
|
22
|
+
this.onMessage(parsedData, msg);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
parseMessage(msg) {
|
|
26
|
+
const data = msg.getData();
|
|
27
|
+
return typeof data === 'string'
|
|
28
|
+
? JSON.parse(data)
|
|
29
|
+
: JSON.parse(data.toString('utf8'));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Stan } from 'node-nats-streaming';
|
|
2
|
+
import { Subjects } from './subjects.js';
|
|
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
|
+
private client;
|
|
10
|
+
constructor(client: Stan);
|
|
11
|
+
publish(data: T['data']): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Stan } from 'node-nats-streaming';
|
|
2
|
+
import { Subjects } from './subjects.js';
|
|
3
|
+
export class Publisher {
|
|
4
|
+
client;
|
|
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 published to subject', this.subject);
|
|
15
|
+
resolve();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { Subjects } from './subjects.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { Subjects } from './subjects.js';
|
package/build/index.d.ts
CHANGED
|
@@ -1 +1,15 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./errors/BadRequestError.js";
|
|
2
|
+
export * from "./errors/DatabaseConnectionError.js";
|
|
3
|
+
export * from "./errors/RequestValidationError.js";
|
|
4
|
+
export * from "./errors/customError.js";
|
|
5
|
+
export * from "./errors/not-authorized-error.js";
|
|
6
|
+
export * from "./errors/not-found-error.js";
|
|
7
|
+
export * from "./middlewares/current-user.js";
|
|
8
|
+
export * from "./middlewares/error-handler.js";
|
|
9
|
+
export * from "./middlewares/validate-request.js";
|
|
10
|
+
export * from "./middlewares/require-auth.js";
|
|
11
|
+
export * from "./events/base-listener.js";
|
|
12
|
+
export * from "./events/base-publisher.js";
|
|
13
|
+
export * from "./events/subjects.js";
|
|
14
|
+
export * from "./events/ticket-created-event.js";
|
|
15
|
+
export * from "./events/ticket-updated-event.js";
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export * from "./errors/BadRequestError.js";
|
|
2
|
+
export * from "./errors/DatabaseConnectionError.js";
|
|
3
|
+
export * from "./errors/RequestValidationError.js";
|
|
4
|
+
export * from "./errors/customError.js";
|
|
5
|
+
export * from "./errors/not-authorized-error.js";
|
|
6
|
+
export * from "./errors/not-found-error.js";
|
|
7
|
+
export * from "./middlewares/current-user.js";
|
|
8
|
+
export * from "./middlewares/error-handler.js";
|
|
9
|
+
export * from "./middlewares/validate-request.js";
|
|
10
|
+
export * from "./middlewares/require-auth.js";
|
|
11
|
+
export * from "./events/base-listener.js";
|
|
12
|
+
export * from "./events/base-publisher.js";
|
|
13
|
+
export * from "./events/subjects.js";
|
|
14
|
+
export * from "./events/ticket-created-event.js";
|
|
15
|
+
export * from "./events/ticket-updated-event.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
interface UserPayload {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Express {
|
|
8
|
+
interface Request {
|
|
9
|
+
session?: {
|
|
10
|
+
jwt?: string;
|
|
11
|
+
};
|
|
12
|
+
currentUser?: UserPayload;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
export const currentUser = (req, res, next) => {
|
|
3
|
+
if (!req.session?.jwt) {
|
|
4
|
+
return next();
|
|
5
|
+
}
|
|
6
|
+
try {
|
|
7
|
+
const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
|
|
8
|
+
req.currentUser = payload;
|
|
9
|
+
}
|
|
10
|
+
catch (err) { }
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { validationResult } from 'express-validator';
|
|
2
|
+
import { RequestValidationError } from '../errors/RequestValidationError.js';
|
|
3
|
+
export const validateRequest = (req, res, next) => {
|
|
4
|
+
const errors = validationResult(req);
|
|
5
|
+
if (!errors.isEmpty()) {
|
|
6
|
+
throw new RequestValidationError(errors.array());
|
|
7
|
+
}
|
|
8
|
+
next();
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a.m.ticketing/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -15,9 +15,19 @@
|
|
|
15
15
|
"keywords": [],
|
|
16
16
|
"author": "",
|
|
17
17
|
"license": "ISC",
|
|
18
|
-
"type": "
|
|
18
|
+
"type": "module",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"del-cli": "^8.0.1",
|
|
21
21
|
"typescript": "^7.0.2"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/cookie-session": "^2.0.49",
|
|
25
|
+
"@types/express": "^5.0.6",
|
|
26
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
27
|
+
"cookie-session": "^2.1.1",
|
|
28
|
+
"express": "^5.3.0",
|
|
29
|
+
"express-validator": "^7.3.2",
|
|
30
|
+
"jsonwebtoken": "^9.0.3",
|
|
31
|
+
"node-nats-streaming": "^0.3.2"
|
|
22
32
|
}
|
|
23
33
|
}
|