@a.m.ticketing/common 1.0.2 → 1.0.3

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.
@@ -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
+ }
package/build/index.d.ts CHANGED
@@ -1 +1,10 @@
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";
package/build/index.js CHANGED
@@ -1,6 +1,10 @@
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";
@@ -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.3",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -15,9 +15,18 @@
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"
22
31
  }
23
32
  }