@vmbbtickets/common 1.0.3 → 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/package.json +13 -2
- package/src/errors/bad-request-error.ts +13 -0
- package/src/errors/custom-error.ts +11 -0
- package/src/errors/database-connection-error.ts +20 -0
- package/src/errors/not-authorized-errors.ts +15 -0
- package/src/errors/not-found-error.ts +13 -0
- package/src/errors/request-validation-error.ts +20 -0
- package/src/index.ts +10 -16
- package/src/middleware/current-user.ts +35 -0
- package/src/middleware/error-handler.ts +17 -0
- package/src/middleware/require-auth.ts +12 -0
- package/src/middleware/validate-request.ts +17 -0
package/package.json
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vmbbtickets/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"main": "./build/index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "del ./build/*",
|
|
7
7
|
"build": "npm run clean && tsc",
|
|
8
|
-
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm publish"
|
|
8
|
+
"pub": "git add . && git commit -m \"Updates\" && npm run build && npm version patch && npm publish"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [],
|
|
11
11
|
"author": "",
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"description": "",
|
|
14
14
|
"devDependencies": {
|
|
15
|
+
"@types/express-session": "^1.19.0",
|
|
15
16
|
"del-cli": "^7.0.0",
|
|
16
17
|
"typescript": "^7.0.2"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/cookie-session": "^2.0.49",
|
|
21
|
+
"@types/express": "5.0.0",
|
|
22
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
23
|
+
"cookie-session": "^2.1.1",
|
|
24
|
+
"express": "4.16.2",
|
|
25
|
+
"express-async-errors": "^3.1.1",
|
|
26
|
+
"express-validator": "^7.3.2",
|
|
27
|
+
"jsonwebtoken": "^9.0.3"
|
|
17
28
|
}
|
|
18
29
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class BadRequestError extends CustomError {
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
|
|
6
|
+
constructor(public message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
serializeErrors() {
|
|
11
|
+
return [{ message: this.message }];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export abstract class CustomError extends Error {
|
|
2
|
+
abstract statusCode: number;
|
|
3
|
+
constructor(message: string) {
|
|
4
|
+
super(message);
|
|
5
|
+
|
|
6
|
+
// Ensure the prototype is set to the concrete subclass prototype so
|
|
7
|
+
// subclass methods (e.g. `serializeErrors`) remain available on `this`.
|
|
8
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
9
|
+
}
|
|
10
|
+
abstract serializeErrors(): { message: string; field?: string }[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class DatabaseConnectionError extends CustomError {
|
|
4
|
+
statusCode = 500;
|
|
5
|
+
reason = "Unable to connect to database";
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Database connection error");
|
|
9
|
+
|
|
10
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
serializeErrors() {
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
message: this.reason,
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class NotAuthorizedErrors extends CustomError {
|
|
4
|
+
statusCode: number = 401;
|
|
5
|
+
|
|
6
|
+
constructor(public message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
|
|
9
|
+
Object.setPrototypeOf(this, NotAuthorizedErrors.prototype);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
serializeErrors(): { message: string; field?: string }[] {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ValidationError } from "express-validator";
|
|
2
|
+
import { CustomError } from "./custom-error";
|
|
3
|
+
|
|
4
|
+
export class RequestValidationError extends CustomError {
|
|
5
|
+
statusCode = 400;
|
|
6
|
+
|
|
7
|
+
constructor(public errors: ValidationError[]) {
|
|
8
|
+
super("Validation errors");
|
|
9
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return this.errors.map((error) => {
|
|
14
|
+
if (error.type === "field") {
|
|
15
|
+
return { message: error.msg, field: error.path };
|
|
16
|
+
}
|
|
17
|
+
return { message: error.msg };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export * from "./errors/bad-request-error";
|
|
2
|
+
export * from "./errors/custom-error";
|
|
3
|
+
export * from "./errors/database-connection-error";
|
|
4
|
+
export * from "./errors/not-authorized-errors";
|
|
5
|
+
export * from "./errors/not-found-error";
|
|
6
|
+
export * from "./errors/request-validation-error";
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
yellow: 10,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
console.log(color);
|
|
16
|
-
|
|
17
|
-
export default color;
|
|
8
|
+
export * from "./middleware/current-user";
|
|
9
|
+
export * from "./middleware/error-handler";
|
|
10
|
+
export * from "./middleware/require-auth";
|
|
11
|
+
export * from "./middleware/validate-request";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
|
|
4
|
+
interface UserPayload {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
namespace Express {
|
|
11
|
+
interface Request {
|
|
12
|
+
currentUser?: UserPayload;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const currentUser = (
|
|
18
|
+
req: Request,
|
|
19
|
+
res: Response,
|
|
20
|
+
next: NextFunction,
|
|
21
|
+
) => {
|
|
22
|
+
if (!(req as any).session.jwt) {
|
|
23
|
+
next();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const decoded = jwt.verify(
|
|
28
|
+
(req as any).session.jwt,
|
|
29
|
+
process.env.JWT_KEY!,
|
|
30
|
+
) as UserPayload;
|
|
31
|
+
req.currentUser = decoded;
|
|
32
|
+
} catch (err) {}
|
|
33
|
+
|
|
34
|
+
next();
|
|
35
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Response, Request, NextFunction } from "express";
|
|
2
|
+
import { CustomError } from "../errors/custom-error";
|
|
3
|
+
|
|
4
|
+
export const errorHandler = (
|
|
5
|
+
err: Error,
|
|
6
|
+
req: Request,
|
|
7
|
+
res: Response,
|
|
8
|
+
next: NextFunction,
|
|
9
|
+
) => {
|
|
10
|
+
if (err instanceof CustomError) {
|
|
11
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
res.status(500).send({
|
|
15
|
+
errors: [{ message: "Internal Server Error" }],
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { NotAuthorizedErrors } from "../errors/not-authorized-errors";
|
|
3
|
+
|
|
4
|
+
export const requireAuth = (
|
|
5
|
+
req: Request,
|
|
6
|
+
res: Response,
|
|
7
|
+
next: NextFunction,
|
|
8
|
+
) => {
|
|
9
|
+
if (!req.currentUser) {
|
|
10
|
+
throw new NotAuthorizedErrors("User is not authorized");
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Response, Request, NextFunction } from "express";
|
|
2
|
+
import { CustomError } from "../errors/custom-error";
|
|
3
|
+
import { validationResult } from "express-validator";
|
|
4
|
+
import { RequestValidationError } from "../errors/request-validation-error";
|
|
5
|
+
|
|
6
|
+
export const validateRequest = (
|
|
7
|
+
req: Request,
|
|
8
|
+
res: Response,
|
|
9
|
+
next: NextFunction,
|
|
10
|
+
) => {
|
|
11
|
+
const errors = validationResult(req);
|
|
12
|
+
if (!errors.isEmpty()) {
|
|
13
|
+
throw new RequestValidationError(errors.array());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
next();
|
|
17
|
+
};
|