bemoore-be-ticketing-common 1.0.0

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 './custom-error';
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,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BadRequestError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class BadRequestError extends custom_error_1.CustomError {
6
+ constructor(message) {
7
+ super(message);
8
+ this.message = message;
9
+ this.statusCode = 500;
10
+ Object.setPrototypeOf(this, BadRequestError.prototype);
11
+ }
12
+ serializeErrors() {
13
+ return [{ message: this.message }];
14
+ }
15
+ }
16
+ exports.BadRequestError = BadRequestError;
@@ -0,0 +1,8 @@
1
+ export declare abstract class CustomError extends Error {
2
+ abstract statusCode: number;
3
+ constructor(message: string);
4
+ abstract serializeErrors(): Array<{
5
+ message: string;
6
+ field?: string;
7
+ }>;
8
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomError = void 0;
4
+ class CustomError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ Object.setPrototypeOf(this, CustomError.prototype);
8
+ }
9
+ }
10
+ exports.CustomError = CustomError;
@@ -0,0 +1,9 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class DatabaseConnextionError extends CustomError {
3
+ reason: string;
4
+ statusCode: number;
5
+ constructor();
6
+ serializeErrors: () => {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseConnextionError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class DatabaseConnextionError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super('Error connecting to db!');
8
+ this.reason = 'Unable to connect to database';
9
+ this.statusCode = 500;
10
+ this.serializeErrors = () => {
11
+ return [{ message: this.reason }];
12
+ };
13
+ Object.setPrototypeOf(this, DatabaseConnextionError.prototype);
14
+ }
15
+ }
16
+ exports.DatabaseConnextionError = DatabaseConnextionError;
@@ -0,0 +1,7 @@
1
+ import { BadRequestError } from './bad-request-error';
2
+ import { CustomError } from './custom-error';
3
+ import { DatabaseConnextionError } from './database-connection-error';
4
+ import { NotAuthorizedError } from './not-authorized';
5
+ import { NotFoundError } from './not-found-error';
6
+ import { RequestValidationError } from './request-validation-error';
7
+ export { BadRequestError, CustomError, DatabaseConnextionError, NotAuthorizedError, NotFoundError, RequestValidationError, };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestValidationError = exports.NotFoundError = exports.NotAuthorizedError = exports.DatabaseConnextionError = exports.CustomError = exports.BadRequestError = void 0;
4
+ const bad_request_error_1 = require("./bad-request-error");
5
+ Object.defineProperty(exports, "BadRequestError", { enumerable: true, get: function () { return bad_request_error_1.BadRequestError; } });
6
+ const custom_error_1 = require("./custom-error");
7
+ Object.defineProperty(exports, "CustomError", { enumerable: true, get: function () { return custom_error_1.CustomError; } });
8
+ const database_connection_error_1 = require("./database-connection-error");
9
+ Object.defineProperty(exports, "DatabaseConnextionError", { enumerable: true, get: function () { return database_connection_error_1.DatabaseConnextionError; } });
10
+ const not_authorized_1 = require("./not-authorized");
11
+ Object.defineProperty(exports, "NotAuthorizedError", { enumerable: true, get: function () { return not_authorized_1.NotAuthorizedError; } });
12
+ const not_found_error_1 = require("./not-found-error");
13
+ Object.defineProperty(exports, "NotFoundError", { enumerable: true, get: function () { return not_found_error_1.NotFoundError; } });
14
+ const request_validation_error_1 = require("./request-validation-error");
15
+ Object.defineProperty(exports, "RequestValidationError", { enumerable: true, get: function () { return request_validation_error_1.RequestValidationError; } });
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class NotAuthorizedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotAuthorizedError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class NotAuthorizedError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super('Not authorized');
8
+ this.statusCode = 401;
9
+ Object.setPrototypeOf(this, NotAuthorizedError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: 'Not authorized' }];
13
+ }
14
+ }
15
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotFoundError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class NotFoundError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super('Route not found');
8
+ this.statusCode = 404;
9
+ Object.setPrototypeOf(this, NotFoundError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: 'Not Found' }];
13
+ }
14
+ }
15
+ exports.NotFoundError = NotFoundError;
@@ -0,0 +1,14 @@
1
+ import { ValidationError } from 'express-validator';
2
+ import { CustomError } from './custom-error';
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
+ message: any;
12
+ field?: undefined;
13
+ })[];
14
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestValidationError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class RequestValidationError extends custom_error_1.CustomError {
6
+ constructor(errors) {
7
+ super('Validation Error occured!');
8
+ this.errors = errors;
9
+ this.statusCode = 400;
10
+ this.serializeErrors = () => {
11
+ return this.errors.map((error) => {
12
+ if (error.type === 'field') {
13
+ return { message: error.msg, field: error.path };
14
+ }
15
+ return { message: error.msg };
16
+ });
17
+ };
18
+ }
19
+ }
20
+ exports.RequestValidationError = RequestValidationError;
@@ -0,0 +1,4 @@
1
+ export interface IUserPayload {
2
+ id: string;
3
+ email: string;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ import { IUserPayload } from '../interfaces';
3
+ declare global {
4
+ namespace Express {
5
+ interface Request {
6
+ currentUser?: IUserPayload;
7
+ }
8
+ }
9
+ }
10
+ export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.currentUser = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const currentUser = (req, res, next) => {
9
+ var _a;
10
+ if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt))
11
+ return next();
12
+ try {
13
+ const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
14
+ req.currentUser = payload;
15
+ }
16
+ catch (error) { }
17
+ next();
18
+ };
19
+ exports.currentUser = currentUser;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ const errors_1 = require("../errors");
5
+ const errorHandler = (err, req, res, next) => {
6
+ if (err instanceof errors_1.CustomError) {
7
+ return res.status(err.statusCode).send({
8
+ errors: err.serializeErrors(),
9
+ });
10
+ }
11
+ res.status(400).send({
12
+ errors: [{ message: 'Something went wrong' }],
13
+ });
14
+ };
15
+ exports.errorHandler = errorHandler;
@@ -0,0 +1,4 @@
1
+ import { currentUser } from './current-user';
2
+ import { errorHandler } from './error-handler';
3
+ import { requireAuth } from './require-auth';
4
+ export { currentUser, errorHandler, requireAuth };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAuth = exports.errorHandler = exports.currentUser = void 0;
4
+ const current_user_1 = require("./current-user");
5
+ Object.defineProperty(exports, "currentUser", { enumerable: true, get: function () { return current_user_1.currentUser; } });
6
+ const error_handler_1 = require("./error-handler");
7
+ Object.defineProperty(exports, "errorHandler", { enumerable: true, get: function () { return error_handler_1.errorHandler; } });
8
+ const require_auth_1 = require("./require-auth");
9
+ Object.defineProperty(exports, "requireAuth", { enumerable: true, get: function () { return require_auth_1.requireAuth; } });
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAuth = void 0;
4
+ const not_authorized_1 = require("../errors/not-authorized");
5
+ const requireAuth = (req, res, next) => {
6
+ if (!req.currentUser) {
7
+ throw new not_authorized_1.NotAuthorizedError();
8
+ }
9
+ next();
10
+ };
11
+ exports.requireAuth = requireAuth;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "bemoore-be-ticketing-common",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc"
12
+ },
13
+ "keywords": [
14
+ "typescript",
15
+ "npm",
16
+ "module"
17
+ ],
18
+ "author": "",
19
+ "license": "ISC",
20
+ "dependencies": {
21
+ "@types/cookie-session": "^2.0.49",
22
+ "@types/express": "^5.0.0",
23
+ "@types/jsonwebtoken": "^9.0.7",
24
+ "express": "^4.21.2",
25
+ "express-validator": "^7.2.1",
26
+ "jsonwebtoken": "^9.0.2",
27
+ "typescript": "^5.7.2"
28
+ }
29
+ }