@psctickets/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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@psctickets/common",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "common package for tickets learning project",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,18 +0,0 @@
1
- import { CustomError } from "./CustomError";
2
-
3
- export class BadRequestError extends CustomError {
4
- statusCode = 400;
5
-
6
- constructor(public message: string) {
7
- super(message);
8
- Object.setPrototypeOf(this, BadRequestError.prototype);
9
- }
10
-
11
- serializeErrors(): { message: string; field?: string }[] {
12
- return [
13
- {
14
- message: this.message,
15
- },
16
- ];
17
- }
18
- }
@@ -1,11 +0,0 @@
1
- export abstract class CustomError extends Error {
2
- abstract statusCode: number;
3
- abstract serializeErrors(): { message: string; field?: string }[];
4
- constructor(message: string) {
5
- super(message); // calls the Error constructor with the message string
6
-
7
- Object.setPrototypeOf(this, CustomError.prototype); // Fix prototype chain
8
- // - if this is not done any class extending from this abstract class will not identify itself as an instance of this class
9
- // - basically instanceOf CustomError would return false
10
- }
11
- }
@@ -1,17 +0,0 @@
1
- import { CustomError } from "./CustomError";
2
-
3
- const defaultReason = "Error connecting to database";
4
-
5
- export class DatabaseConnectionError extends CustomError {
6
- statusCode = 500;
7
-
8
- constructor(public reason: string = "") {
9
- reason = reason || defaultReason;
10
- super(reason);
11
- Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
12
- }
13
-
14
- serializeErrors() {
15
- return [{ message: this.reason }];
16
- }
17
- }
@@ -1,14 +0,0 @@
1
- import { CustomError } from "./CustomError";
2
-
3
- export class NotFoundError extends CustomError {
4
- statusCode = 404;
5
- static defaultReason = "API Not found";
6
- constructor() {
7
- super(NotFoundError.defaultReason);
8
- Object.setPrototypeOf(this, NotFoundError);
9
- }
10
-
11
- serializeErrors() {
12
- return [{ message: NotFoundError.defaultReason }];
13
- }
14
- }
@@ -1,22 +0,0 @@
1
- import { ValidationError } from "express-validator";
2
- import { CustomError } from "./CustomError";
3
-
4
- export class RequestValidationError extends CustomError {
5
- statusCode = 400;
6
-
7
- constructor(public errors: ValidationError[]) {
8
- super("Invalid request parameters");
9
-
10
- Object.setPrototypeOf(this, RequestValidationError.prototype);
11
- }
12
-
13
- serializeErrors() {
14
- return this.errors.map((error) => {
15
- if (error.type === "field") {
16
- return { message: error.msg, field: error.path };
17
- }
18
-
19
- return { message: error.msg };
20
- });
21
- }
22
- }
@@ -1,18 +0,0 @@
1
- import { CustomError } from "./CustomError";
2
-
3
- export class UnauthorizedRequest extends CustomError {
4
- statusCode = 401;
5
-
6
- constructor(public message: string) {
7
- super(message);
8
- Object.setPrototypeOf(this, UnauthorizedRequest.prototype);
9
- }
10
-
11
- serializeErrors(): { message: string; field?: string }[] {
12
- return [
13
- {
14
- message: "Unauthorized Request",
15
- },
16
- ];
17
- }
18
- }
@@ -1,6 +0,0 @@
1
- export { CustomError } from "./CustomError";
2
- export { BadRequestError } from "./BadRequestError";
3
- export { UnauthorizedRequest } from "./UnAuthorizedRequest";
4
- export { NotFoundError } from "./NotFoundError";
5
- export { RequestValidationError } from "./RequestValidationError";
6
- export { DatabaseConnectionError } from "./DatabaseConnectionError";
package/src/index.ts DELETED
File without changes
@@ -1,21 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- import { CustomError } from "../errors/CustomError";
3
-
4
- const errorMiddleware = (
5
- err: Error,
6
- _req: Request,
7
- res: Response,
8
- _next: NextFunction
9
- ) => {
10
- if (err instanceof CustomError) {
11
- res.status(err.statusCode).send(err.serializeErrors());
12
- return;
13
- }
14
-
15
- res.status(500).send({
16
- status: "Failure",
17
- message: [{ message: err.message }],
18
- });
19
- };
20
-
21
- export default errorMiddleware;
@@ -1,3 +0,0 @@
1
- export { default as errorMiddleware } from "./errorMiddleware";
2
- export { default as verifyTokenMiddleware } from "./verifyTokenMiddleware";
3
- export { default as signUpValidator } from "./signUpValidator";
@@ -1,11 +0,0 @@
1
- import { body } from "express-validator";
2
-
3
- const signUpValidator = [
4
- body("email").isEmail().withMessage("Email must be valid"),
5
- body("password")
6
- .trim()
7
- .isLength({ min: 4, max: 20 })
8
- .withMessage("Password must be between 4 and 20 characters"),
9
- ];
10
-
11
- export default signUpValidator;
@@ -1,24 +0,0 @@
1
- import { NextFunction, Request, Response } from "express";
2
- import jwt from "jsonwebtoken";
3
- import { UnauthorizedRequest } from "../errors/UnAuthorizedRequest";
4
- import SignInTokenPayload from "../types/SignInTokenPayload";
5
-
6
- const verifyTokenMiddleware = async (
7
- req: Request,
8
- _res: Response,
9
- next: NextFunction
10
- ) => {
11
- let user: SignInTokenPayload;
12
-
13
- try {
14
- const signInToken = req.session?.jwt;
15
- user = jwt.verify(signInToken, process.env.JWT_KEY!) as SignInTokenPayload;
16
- } catch (error) {
17
- throw new UnauthorizedRequest("Invalid token");
18
- }
19
-
20
- req.currentUser = user;
21
- next();
22
- };
23
-
24
- export default verifyTokenMiddleware;
@@ -1,6 +0,0 @@
1
- type SignInTokenPayload = {
2
- userId: string;
3
- email: string;
4
- };
5
-
6
- export default SignInTokenPayload;
@@ -1,12 +0,0 @@
1
- import SignInTokenPayload from "../SignInTokenPayload";
2
-
3
- /**
4
- * There is a difference in how properties are added to global as opposed to the express' Request interface.
5
- * This is because ts knows exactly the properties available in the interface, because it is something already declared
6
- * using @types/express.
7
- */
8
- declare module "express" {
9
- interface Request {
10
- currentUser?: SignInTokenPayload;
11
- }
12
- }