@onkarnik-tickets/common 1.0.4 → 1.0.6
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 +7 -3
- package/build/errors/custom-error.js +5 -1
- package/build/errors/database-connection-error.js +8 -4
- package/build/errors/not-authorised-error.js +7 -3
- package/build/errors/not-found-error.js +7 -3
- package/build/errors/request-validation-error.js +7 -4
- package/build/index.js +26 -10
- package/build/middlewares/current-user.js +39 -2
- package/build/middlewares/error-handler.js +8 -3
- package/build/middlewares/require-auth.js +7 -3
- package/build/middlewares/validateRequest.js +9 -5
- package/package.json +1 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 {
|
|
4
6
|
constructor(message) {
|
|
5
7
|
super(message);
|
|
8
|
+
this.statusCode = 400;
|
|
6
9
|
// line for modifying in-built class
|
|
7
10
|
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
8
11
|
}
|
|
@@ -12,3 +15,4 @@ export class BadRequestError extends CustomError {
|
|
|
12
15
|
];
|
|
13
16
|
}
|
|
14
17
|
}
|
|
18
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomError = void 0;
|
|
4
|
+
class CustomError extends Error {
|
|
2
5
|
constructor(message) {
|
|
3
6
|
super(message);
|
|
4
7
|
Object.setPrototypeOf(this, CustomError.prototype);
|
|
5
8
|
}
|
|
6
9
|
}
|
|
10
|
+
exports.CustomError = CustomError;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseConnectionError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class DatabaseConnectionError extends custom_error_1.CustomError {
|
|
5
6
|
constructor() {
|
|
6
7
|
super('Error conencting to DB');
|
|
8
|
+
this.reason = 'Error connecting to database';
|
|
9
|
+
this.statusCode = 500;
|
|
7
10
|
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
8
11
|
}
|
|
9
12
|
serializeErrors() {
|
|
@@ -12,3 +15,4 @@ export class DatabaseConnectionError extends CustomError {
|
|
|
12
15
|
];
|
|
13
16
|
}
|
|
14
17
|
}
|
|
18
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorisedError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class NotAuthorisedError extends custom_error_1.CustomError {
|
|
4
6
|
constructor() {
|
|
5
7
|
super('Not authorised');
|
|
8
|
+
this.statusCode = 401;
|
|
6
9
|
Object.setPrototypeOf(this, NotAuthorisedError.prototype);
|
|
7
10
|
}
|
|
8
11
|
serializeErrors() {
|
|
@@ -11,3 +14,4 @@ export class NotAuthorisedError extends CustomError {
|
|
|
11
14
|
];
|
|
12
15
|
}
|
|
13
16
|
}
|
|
17
|
+
exports.NotAuthorisedError = NotAuthorisedError;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 {
|
|
4
6
|
constructor() {
|
|
5
7
|
super('Route not found');
|
|
8
|
+
this.statusCode = 404;
|
|
6
9
|
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
7
10
|
}
|
|
8
11
|
serializeErrors() {
|
|
@@ -11,3 +14,4 @@ export class NotFoundError extends CustomError {
|
|
|
11
14
|
}];
|
|
12
15
|
}
|
|
13
16
|
}
|
|
17
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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 {
|
|
5
6
|
constructor(errors) {
|
|
6
7
|
super('Invalid request params');
|
|
7
8
|
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
8
10
|
// Only because we are extending a build in class
|
|
9
11
|
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
10
12
|
}
|
|
@@ -17,3 +19,4 @@ export class RequestValidationError extends CustomError {
|
|
|
17
19
|
});
|
|
18
20
|
}
|
|
19
21
|
}
|
|
22
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
+
__exportStar(require("./errors/bad-request-error"), exports);
|
|
18
|
+
__exportStar(require("./errors/custom-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/database-connection-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/not-authorised-error"), exports);
|
|
21
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
|
22
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
|
24
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/require-auth"), exports);
|
|
26
|
+
__exportStar(require("./middlewares/validateRequest"), exports);
|
|
@@ -1,5 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.currentUser = void 0;
|
|
37
|
+
const jwt = __importStar(require("jsonwebtoken"));
|
|
38
|
+
const currentUser = (req, res, next) => {
|
|
3
39
|
if (!req.session?.jwt) {
|
|
4
40
|
return next();
|
|
5
41
|
}
|
|
@@ -12,3 +48,4 @@ export const currentUser = (req, res, next) => {
|
|
|
12
48
|
}
|
|
13
49
|
next();
|
|
14
50
|
};
|
|
51
|
+
exports.currentUser = currentUser;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const custom_error_1 = require("../errors/custom-error");
|
|
5
|
+
const errorHandler = (err, req, res, next) => {
|
|
6
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
4
7
|
return res.status(err.statusCode).send({
|
|
5
8
|
errors: err.serializeErrors()
|
|
6
9
|
});
|
|
7
10
|
}
|
|
11
|
+
console.log(err);
|
|
8
12
|
res.status(500).send({
|
|
9
13
|
errors: [{ message: 'Internal Server Error' }]
|
|
10
14
|
});
|
|
11
15
|
};
|
|
16
|
+
exports.errorHandler = errorHandler;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
|
+
const not_authorised_error_1 = require("../errors/not-authorised-error");
|
|
5
|
+
const requireAuth = (req, res, next) => {
|
|
3
6
|
if (!req.currentUser) {
|
|
4
|
-
throw new NotAuthorisedError();
|
|
7
|
+
throw new not_authorised_error_1.NotAuthorisedError();
|
|
5
8
|
}
|
|
6
9
|
next();
|
|
7
10
|
};
|
|
11
|
+
exports.requireAuth = requireAuth;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
5
8
|
if (!errors.isEmpty()) {
|
|
6
|
-
throw new RequestValidationError(errors.array());
|
|
9
|
+
throw new request_validation_error_1.RequestValidationError(errors.array());
|
|
7
10
|
}
|
|
8
11
|
next();
|
|
9
12
|
};
|
|
13
|
+
exports.validateRequest = validateRequest;
|