@thebester/common 1.0.12 → 1.0.13
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.js +3 -5
- package/build/errors/custom-error.js +1 -5
- package/build/errors/database-connection-error.js +2 -6
- package/build/errors/not-authorized-error.js +2 -6
- package/build/errors/not-found-error.js +2 -6
- package/build/errors/request-validation-error.js +2 -6
- package/build/index.js +10 -26
- package/build/middelwares/current-user.js +3 -10
- package/build/middelwares/error-handler.js +3 -7
- package/build/middelwares/require-auth.js +3 -7
- package/build/middelwares/validate-request.js +5 -9
- package/package.json +2 -1
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const custom_error_1 = require("./custom-error");
|
|
4
|
-
class BadRequestError extends custom_error_1.CustomError {
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
class BadRequestError extends CustomError {
|
|
5
3
|
constructor(message) {
|
|
6
4
|
super(message);
|
|
7
5
|
this.message = message;
|
|
@@ -13,4 +11,4 @@ class BadRequestError extends custom_error_1.CustomError {
|
|
|
13
11
|
return [{ message: this.message }];
|
|
14
12
|
}
|
|
15
13
|
}
|
|
16
|
-
|
|
14
|
+
export default BadRequestError;
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CustomError = void 0;
|
|
4
|
-
class CustomError extends Error {
|
|
1
|
+
export class CustomError extends Error {
|
|
5
2
|
constructor(message) {
|
|
6
3
|
super(message);
|
|
7
4
|
// Only because we are extending a built in class
|
|
8
5
|
Object.setPrototypeOf(this, CustomError.prototype);
|
|
9
6
|
}
|
|
10
7
|
}
|
|
11
|
-
exports.CustomError = CustomError;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.DatabaseConnectionError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class DatabaseConnectionError extends custom_error_1.CustomError {
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class DatabaseConnectionError extends CustomError {
|
|
6
3
|
constructor() {
|
|
7
4
|
super("Error connecting to database");
|
|
8
5
|
this.statusCode = 500;
|
|
@@ -18,4 +15,3 @@ class DatabaseConnectionError extends custom_error_1.CustomError {
|
|
|
18
15
|
];
|
|
19
16
|
}
|
|
20
17
|
}
|
|
21
|
-
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.NotAuthorizedError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class NotAuthorizedError extends custom_error_1.CustomError {
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class NotAuthorizedError extends CustomError {
|
|
6
3
|
constructor() {
|
|
7
4
|
super("Not authorized");
|
|
8
5
|
this.statusCode = 401;
|
|
@@ -13,4 +10,3 @@ class NotAuthorizedError extends custom_error_1.CustomError {
|
|
|
13
10
|
return [{ message: "Not authorized" }];
|
|
14
11
|
}
|
|
15
12
|
}
|
|
16
|
-
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.NotFoundError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class NotFoundError extends custom_error_1.CustomError {
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class NotFoundError extends CustomError {
|
|
6
3
|
constructor() {
|
|
7
4
|
super("Route not found");
|
|
8
5
|
this.statusCode = 404;
|
|
@@ -17,4 +14,3 @@ class NotFoundError extends custom_error_1.CustomError {
|
|
|
17
14
|
];
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
|
-
exports.NotFoundError = NotFoundError;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.RequestValidationError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class RequestValidationError extends custom_error_1.CustomError {
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class RequestValidationError extends CustomError {
|
|
6
3
|
constructor(errors) {
|
|
7
4
|
super("Invalid request parameters");
|
|
8
5
|
this.errors = errors;
|
|
@@ -20,4 +17,3 @@ class RequestValidationError extends custom_error_1.CustomError {
|
|
|
20
17
|
});
|
|
21
18
|
}
|
|
22
19
|
}
|
|
23
|
-
exports.RequestValidationError = RequestValidationError;
|
package/build/index.js
CHANGED
|
@@ -1,27 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
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);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
1
|
// Re-export stuff from errors and middlewares
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
export * from "./errors/bad-request-error";
|
|
3
|
+
export * from "./errors/custom-error";
|
|
4
|
+
export * from "./errors/database-connection-error";
|
|
5
|
+
export * from "./errors/not-authorized-error";
|
|
6
|
+
export * from "./errors/not-found-error";
|
|
7
|
+
export * from "./errors/request-validation-error";
|
|
8
|
+
export * from "./middelwares/current-user";
|
|
9
|
+
export * from "./middelwares/error-handler";
|
|
10
|
+
export * from "./middelwares/require-auth";
|
|
11
|
+
export * from "./middelwares/validate-request";
|
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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) => {
|
|
1
|
+
import jwt from "jsonwebtoken";
|
|
2
|
+
export const currentUser = (req, res, next) => {
|
|
9
3
|
var _a;
|
|
10
4
|
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
11
5
|
return next();
|
|
12
6
|
}
|
|
13
7
|
try {
|
|
14
|
-
const payload =
|
|
8
|
+
const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
|
|
15
9
|
req.currentUser = payload;
|
|
16
10
|
}
|
|
17
11
|
catch (err) {
|
|
18
12
|
}
|
|
19
13
|
next();
|
|
20
14
|
};
|
|
21
|
-
exports.currentUser = currentUser;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const custom_error_1 = require("../errors/custom-error");
|
|
5
|
-
const errorHandler = (err, req, res, next) => {
|
|
6
|
-
if (err instanceof custom_error_1.CustomError) {
|
|
1
|
+
import { CustomError } from "../errors/custom-error";
|
|
2
|
+
export const errorHandler = (err, req, res, next) => {
|
|
3
|
+
if (err instanceof CustomError) {
|
|
7
4
|
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
5
|
}
|
|
9
6
|
res.status(400).send({
|
|
10
7
|
errors: [{ message: "Something went wrong" }]
|
|
11
8
|
});
|
|
12
9
|
};
|
|
13
|
-
exports.errorHandler = errorHandler;
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.requireAuth = void 0;
|
|
4
|
-
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
5
|
-
const requireAuth = (req, res, next) => {
|
|
1
|
+
import { NotAuthorizedError } from "../errors/not-authorized-error";
|
|
2
|
+
export const requireAuth = (req, res, next) => {
|
|
6
3
|
if (!req.currentUser) {
|
|
7
|
-
throw new
|
|
4
|
+
throw new NotAuthorizedError();
|
|
8
5
|
}
|
|
9
6
|
next();
|
|
10
7
|
};
|
|
11
|
-
exports.requireAuth = requireAuth;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const request_validation_error_1 = require("../errors/request-validation-error");
|
|
6
|
-
const validateRequest = (req, res, next) => {
|
|
7
|
-
const errors = (0, express_validator_1.validationResult)(req);
|
|
1
|
+
import { validationResult } from "express-validator";
|
|
2
|
+
import { RequestValidationError } from "../errors/request-validation-error";
|
|
3
|
+
export const validateRequest = (req, res, next) => {
|
|
4
|
+
const errors = validationResult(req);
|
|
8
5
|
if (!errors.isEmpty()) {
|
|
9
|
-
throw new
|
|
6
|
+
throw new RequestValidationError(errors.array());
|
|
10
7
|
}
|
|
11
8
|
next();
|
|
12
9
|
};
|
|
13
|
-
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thebester/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"build": "npm run clean && tsc",
|
|
13
13
|
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
14
14
|
},
|
|
15
|
+
"type": "module",
|
|
15
16
|
"keywords": [],
|
|
16
17
|
"author": "",
|
|
17
18
|
"license": "ISC",
|