@mjtickets981/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/build/errors/bad-request-error.d.ts +10 -0
- package/build/errors/bad-request-error.js +16 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +10 -0
- package/build/errors/database-connection-error.d.ts +9 -0
- package/build/errors/database-connection-error.js +37 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +16 -0
- package/build/errors/not-found-error.d.ts +9 -0
- package/build/errors/not-found-error.js +19 -0
- package/build/errors/request-validation-error.d.ts +11 -0
- package/build/errors/request-validation-error.js +50 -0
- package/build/index.d.ts +10 -7
- package/build/index.js +24 -7
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +31 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +50 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +20 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +17 -0
- package/package.json +10 -1
|
@@ -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 = 400;
|
|
10
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -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,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// export class DatabaseConnectionError extends Error {
|
|
3
|
+
// statusCode = 500;
|
|
4
|
+
// reason = "Error connecting to database";
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DatabaseConnectionError = void 0;
|
|
7
|
+
// constructor() {
|
|
8
|
+
// super();
|
|
9
|
+
// // Only because we are extending a built in class
|
|
10
|
+
// Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
|
+
// }
|
|
12
|
+
// serializeErrors() {
|
|
13
|
+
// return [
|
|
14
|
+
// {
|
|
15
|
+
// message: this.reason,
|
|
16
|
+
// },
|
|
17
|
+
// ];
|
|
18
|
+
// }
|
|
19
|
+
// }
|
|
20
|
+
const custom_error_1 = require("./custom-error");
|
|
21
|
+
class DatabaseConnectionError extends custom_error_1.CustomError {
|
|
22
|
+
constructor() {
|
|
23
|
+
super("Error connecting to database");
|
|
24
|
+
this.statusCode = 500;
|
|
25
|
+
this.reason = "Error connecting to database";
|
|
26
|
+
// Only because we are extending a built in class
|
|
27
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
28
|
+
}
|
|
29
|
+
serializeErrors() {
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
message: this.reason,
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
// Only because we are extending a built-in class
|
|
10
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: "Not authorized" }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,19 @@
|
|
|
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 [
|
|
13
|
+
{
|
|
14
|
+
message: "Route not found",
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
// ValidationError is a type that comes back whenever we do a validation attempt using express-validator
|
|
6
|
+
// The type refers has contains properties like msg, param, field etc....
|
|
7
|
+
// interface CustomError {
|
|
8
|
+
// statusCode: number;
|
|
9
|
+
// serializeErrors(): { message: string; field?: string }[]; // This is going to return an array of objects and each of these
|
|
10
|
+
// // objects should have a message which is of type string and a field which is optional. Our custom class will satisify the
|
|
11
|
+
// // above interface by writing the implements the CustomError interface as below.
|
|
12
|
+
// }
|
|
13
|
+
// export class RequestValidationError extends Error {
|
|
14
|
+
// // implements CustomError
|
|
15
|
+
// statusCode = 400;
|
|
16
|
+
// constructor(public errors: ValidationError[]) {
|
|
17
|
+
// super();
|
|
18
|
+
// // Only because we are extending a built in class
|
|
19
|
+
// Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
20
|
+
// }
|
|
21
|
+
// serializeErrors() {
|
|
22
|
+
// return this.errors
|
|
23
|
+
// .map((err) => {
|
|
24
|
+
// if (err.type === "field") {
|
|
25
|
+
// return { message: err.msg, field: err.path };
|
|
26
|
+
// }
|
|
27
|
+
// })
|
|
28
|
+
// .filter((error) => error !== undefined);
|
|
29
|
+
// }
|
|
30
|
+
// }
|
|
31
|
+
class RequestValidationError extends custom_error_1.CustomError {
|
|
32
|
+
constructor(errors) {
|
|
33
|
+
super("Invalid request parameters");
|
|
34
|
+
this.errors = errors;
|
|
35
|
+
// implements CustomError
|
|
36
|
+
this.statusCode = 400;
|
|
37
|
+
// Only because we are extending a built in class
|
|
38
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
39
|
+
}
|
|
40
|
+
serializeErrors() {
|
|
41
|
+
return this.errors
|
|
42
|
+
.map((err) => {
|
|
43
|
+
if (err.type === "field") {
|
|
44
|
+
return { message: err.msg, field: err.path };
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
.filter((error) => error !== undefined);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export
|
|
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-error";
|
|
5
|
+
export * from "./errors/not-found-error";
|
|
6
|
+
export * from "./errors/request-validation-error";
|
|
7
|
+
export * from "./middlewares/error-handler";
|
|
8
|
+
export * from "./middlewares/current-user";
|
|
9
|
+
export * from "./middlewares/require-auth";
|
|
10
|
+
export * from "./middlewares/validate-request";
|
package/build/index.js
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
7
15
|
};
|
|
8
|
-
|
|
9
|
-
exports
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./errors/bad-request-error"), exports); // This is going to import ev erything from the bad-request-error file
|
|
18
|
+
__exportStar(require("./errors/custom-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/database-connection-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
|
21
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
|
22
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
24
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/require-auth"), exports);
|
|
26
|
+
__exportStar(require("./middlewares/validate-request"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { 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
|
+
currentUser?: UserPayload;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
// If the jwt property is not set then it means that the user is not logged in
|
|
12
|
+
return next();
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY); // Whenever we call the verify() here then we are going to get back
|
|
16
|
+
// a payload either a string (or) an object. This is goping to be the information that we stored inside the jwt when we created it.
|
|
17
|
+
// If the jwt is valid, we can attach the payload to the request object. If you recall about the JWT, inside the payload we have an
|
|
18
|
+
// ID property which is a string and email which is a string. So we are going to first create an interface for the payload and then
|
|
19
|
+
// we are going to use that interface to type the payload variable.
|
|
20
|
+
req.currentUser = payload; // Attach the current user to the request object.
|
|
21
|
+
// When we are trying to assign the currentUser property to the request object and TypeScript is unhappy about that because typescript
|
|
22
|
+
// has a type definition file for express that defines exactly what a request is, and this type definition file says that a request
|
|
23
|
+
// doesnot have a property of currentUser. So we need to somehow augment the definition of what a request is. So we are going to
|
|
24
|
+
// add a additional property to the type definition of what a request is.
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
// If the jwt is invalid, we can just ignore it and continue
|
|
28
|
+
}
|
|
29
|
+
next(); // Whether the jwt is valid or not, we call next() to continue to the next middleware
|
|
30
|
+
};
|
|
31
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The only requirement we need when we write error handling middleware is , The function in this error handling middleware
|
|
3
|
+
// it has to recieve 4 arguments and the order of these arguments are the error, req, res, next
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.errorHandler = void 0;
|
|
6
|
+
const custom_error_1 = require("../errors/custom-error");
|
|
7
|
+
// import { ErrorRequestHandler } from "express";
|
|
8
|
+
// import { RequestValidationError } from "../errors/request-validation-error";
|
|
9
|
+
// import { DatabaseConnectionError } from "../errors/database-connection-error";
|
|
10
|
+
const errorHandler = (err, req, res, next) => {
|
|
11
|
+
// console.log("Something went wrong", err);
|
|
12
|
+
// if (err instanceof RequestValidationError) {
|
|
13
|
+
// // const formattedErrors = err.errors.map((error) => {
|
|
14
|
+
// // return {
|
|
15
|
+
// // message: error.msg,
|
|
16
|
+
// // field: error.param,
|
|
17
|
+
// // };
|
|
18
|
+
// // });
|
|
19
|
+
// // const formattedErrors = err.errors.map((error) => {
|
|
20
|
+
// // if (error.type === "field") {
|
|
21
|
+
// // return { message: error.msg, field: error.path };
|
|
22
|
+
// // }
|
|
23
|
+
// // });
|
|
24
|
+
// return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
25
|
+
// }
|
|
26
|
+
// if (err instanceof DatabaseConnectionError) {
|
|
27
|
+
// // console.log("handling this error as a DB connection error");
|
|
28
|
+
// return res.status(err.statusCode).send({
|
|
29
|
+
// errors:
|
|
30
|
+
// // [
|
|
31
|
+
// // {
|
|
32
|
+
// // message: err.reason,
|
|
33
|
+
// // },
|
|
34
|
+
// // ],
|
|
35
|
+
// err.serializeErrors(),
|
|
36
|
+
// });
|
|
37
|
+
// }
|
|
38
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
39
|
+
res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
res.status(400).send({
|
|
43
|
+
errors: [
|
|
44
|
+
{
|
|
45
|
+
message: "something went wrong",
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
5
|
+
const requireAuth = () => {
|
|
6
|
+
// We are going to really make a big assumption here where we are going to assume that we will never use the requiredAuth middleware
|
|
7
|
+
// in a situation where the user is not logged in. So we are going to assume that the currentUser property is always set on the request object.
|
|
8
|
+
// By the time this request shows up inside the requireAuth middleware, We should already checked to see if there is a JSON Web Token
|
|
9
|
+
// is present (or) not. We should have already attempted to decode it and set it on the req.currentUser property. Again if the
|
|
10
|
+
// req.currentUser property is not defined then it means that we have to reject this request and respond with an error.
|
|
11
|
+
return (req, res, next) => {
|
|
12
|
+
if (!req.currentUser) {
|
|
13
|
+
// If the currentUser property is not set then it means that the user is not logged in
|
|
14
|
+
// return res.status(401).send({ error: "Not authorized" });
|
|
15
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
|
16
|
+
}
|
|
17
|
+
next(); // If the user is logged in, we call next() to continue to the next middleware
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.requireAuth = requireAuth;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRequest = void 0;
|
|
4
|
+
const express_validator_1 = require("express-validator");
|
|
5
|
+
const request_validation_error_1 = require("../errors/request-validation-error");
|
|
6
|
+
const validateRequest = (req, res, next) => {
|
|
7
|
+
// Express distinguishes between a error handling middleware and a normal one on the no of arguments that the
|
|
8
|
+
// function accepts. Here we want to produce errors if the request is not valid.
|
|
9
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
10
|
+
if (!errors.isEmpty()) {
|
|
11
|
+
// If there are any errors then we want to throw a RequestValidationError
|
|
12
|
+
throw new request_validation_error_1.RequestValidationError(errors.array());
|
|
13
|
+
}
|
|
14
|
+
next(); // If there are no errors then we want to call the next middleware in the chain.
|
|
15
|
+
// This is how we can validate the request body using express-validator and throw a custom error if the validation fails.
|
|
16
|
+
};
|
|
17
|
+
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mjtickets981/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -18,5 +18,14 @@
|
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"del-cli": "^6.0.0",
|
|
20
20
|
"typescript": "^5.8.3"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@types/cookie-session": "^2.0.49",
|
|
24
|
+
"@types/express": "^5.0.3",
|
|
25
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
26
|
+
"cookie-session": "^2.1.1",
|
|
27
|
+
"express": "^5.1.0",
|
|
28
|
+
"express-validator": "^7.2.1",
|
|
29
|
+
"jsonwebtoken": "^9.0.2"
|
|
21
30
|
}
|
|
22
31
|
}
|