@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.
Files changed (33) hide show
  1. package/build/errors/BadRequestError.d.ts +9 -0
  2. package/build/errors/BadRequestError.js +12 -0
  3. package/build/errors/DatabaseConnectionError.d.ts +9 -0
  4. package/build/errors/DatabaseConnectionError.js +11 -0
  5. package/build/errors/RequestValidationError.d.ts +14 -0
  6. package/build/errors/RequestValidationError.js +17 -0
  7. package/build/errors/customError.d.ts +8 -0
  8. package/build/errors/customError.js +5 -0
  9. package/build/errors/not-authorized-error.d.ts +8 -0
  10. package/build/errors/not-authorized-error.js +10 -0
  11. package/build/errors/not-found-error.d.ts +8 -0
  12. package/build/errors/not-found-error.js +10 -0
  13. package/build/events/base-listener.d.ts +18 -0
  14. package/build/events/base-listener.js +31 -0
  15. package/build/events/base-publisher.d.ts +13 -0
  16. package/build/events/base-publisher.js +19 -0
  17. package/build/events/subjects.d.ts +4 -0
  18. package/build/events/subjects.js +5 -0
  19. package/build/events/ticket-created-event.d.ts +10 -0
  20. package/build/events/ticket-created-event.js +1 -0
  21. package/build/events/ticket-updated-event.d.ts +10 -0
  22. package/build/events/ticket-updated-event.js +1 -0
  23. package/build/index.d.ts +15 -1
  24. package/build/index.js +15 -6
  25. package/build/middlewares/current-user.d.ts +17 -0
  26. package/build/middlewares/current-user.js +12 -0
  27. package/build/middlewares/error-handler.d.ts +2 -0
  28. package/build/middlewares/error-handler.js +8 -0
  29. package/build/middlewares/require-auth.d.ts +2 -0
  30. package/build/middlewares/require-auth.js +7 -0
  31. package/build/middlewares/validate-request.d.ts +2 -0
  32. package/build/middlewares/validate-request.js +9 -0
  33. package/package.json +12 -2
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./customError.js";
2
+ export declare class BadRequestError extends CustomError {
3
+ message: string;
4
+ statusCode: number;
5
+ constructor(message: string);
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -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,9 @@
1
+ import { CustomError } from "./customError.js";
2
+ export declare class DatabaseConnectionError extends CustomError {
3
+ statusCode: number;
4
+ reason: string;
5
+ constructor();
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -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,8 @@
1
+ export declare abstract class CustomError extends Error {
2
+ abstract statusCode: number;
3
+ constructor(message: string);
4
+ abstract serializeErrors(): {
5
+ message: string;
6
+ field?: string;
7
+ }[];
8
+ }
@@ -0,0 +1,5 @@
1
+ export class CustomError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ }
5
+ }
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './customError.js';
2
+ export declare class NotAuthorizedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,10 @@
1
+ import { CustomError } from './customError.js';
2
+ export class NotAuthorizedError extends CustomError {
3
+ statusCode = 401;
4
+ constructor() {
5
+ super('Not Authorized');
6
+ }
7
+ serializeErrors() {
8
+ return [{ message: 'Not authorized' }];
9
+ }
10
+ }
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './customError.js';
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,10 @@
1
+ import { CustomError } from './customError.js';
2
+ export class NotFoundError extends CustomError {
3
+ statusCode = 404;
4
+ constructor() {
5
+ super('Route not found');
6
+ }
7
+ serializeErrors() {
8
+ return [{ message: 'Not Found' }];
9
+ }
10
+ }
@@ -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,4 @@
1
+ export declare enum Subjects {
2
+ TicketCreated = "ticket:created",
3
+ TicketUpdated = "ticket:updated"
4
+ }
@@ -0,0 +1,5 @@
1
+ export var Subjects;
2
+ (function (Subjects) {
3
+ Subjects["TicketCreated"] = "ticket:created";
4
+ Subjects["TicketUpdated"] = "ticket:updated";
5
+ })(Subjects || (Subjects = {}));
@@ -0,0 +1,10 @@
1
+ import { Subjects } from './subjects.js';
2
+ export interface TicketCreatedEvent {
3
+ subject: Subjects.TicketCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1 @@
1
+ import { Subjects } from './subjects.js';
@@ -0,0 +1,10 @@
1
+ import { Subjects } from './subjects.js';
2
+ export interface TicketUpdatedEvent {
3
+ subject: Subjects.TicketUpdated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const client = (client) => {
4
- return client;
5
- };
6
- console.log(client({ name: "ahmed mokhtar", age: 20 }));
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,2 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "../errors/customError.js";
2
+ export const errorHandler = (err, req, res, next) => {
3
+ if (err instanceof CustomError) {
4
+ res.status(err.statusCode).send(err.serializeErrors());
5
+ return;
6
+ }
7
+ next(err);
8
+ };
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,7 @@
1
+ import { NotAuthorizedError } from '../errors/not-authorized-error.js';
2
+ export const requireAuth = (req, res, next) => {
3
+ if (!req.currentUser) {
4
+ throw new NotAuthorizedError();
5
+ }
6
+ next();
7
+ };
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;
@@ -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.2",
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": "commonjs",
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
  }