@greeksquard/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/build/errors/bad-request-error.d.ts +9 -0
- package/build/errors/bad-request-error.js +16 -0
- package/build/errors/custom-errors.d.ts +7 -0
- package/build/errors/custom-errors.js +6 -0
- package/build/errors/database-connection-error.d.ts +8 -0
- package/build/errors/database-connection-error.js +15 -0
- package/build/errors/not-authorised-error.d.ts +8 -0
- package/build/errors/not-authorised-error.js +15 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +15 -0
- package/build/errors/request-validation-error.d.ts +14 -0
- package/build/errors/request-validation-error.js +21 -0
- package/build/index.d.ts +10 -6
- package/build/index.js +24 -5
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +19 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +10 -0
- package/build/middlewares/request-validation.d.ts +2 -0
- package/build/middlewares/request-validation.js +13 -0
- package/build/middlewares/required-auth.d.ts +2 -0
- package/build/middlewares/required-auth.js +11 -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_errors_1 = require("./custom-errors");
|
|
5
|
+
class BadRequestError extends custom_errors_1.CustomErrors {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.message = message;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializedError() {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseConnectionError = void 0;
|
|
4
|
+
const custom_errors_1 = require("./custom-errors");
|
|
5
|
+
class DatabaseConnectionError extends custom_errors_1.CustomErrors {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Error Connection to database");
|
|
8
|
+
this.statusCode = 500;
|
|
9
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializedError() {
|
|
12
|
+
return ([{ message: "Error Connecting to database" }]);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorisedError = void 0;
|
|
4
|
+
const custom_errors_1 = require("./custom-errors");
|
|
5
|
+
class NotAuthorisedError extends custom_errors_1.CustomErrors {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Not Authorized");
|
|
8
|
+
this.statusCode = 401;
|
|
9
|
+
Object.setPrototypeOf(this, NotAuthorisedError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializedError() {
|
|
12
|
+
return [{ message: "Not Authorized" }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.NotAuthorisedError = NotAuthorisedError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundError = void 0;
|
|
4
|
+
const custom_errors_1 = require("./custom-errors");
|
|
5
|
+
class NotFoundError extends custom_errors_1.CustomErrors {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Page not found");
|
|
8
|
+
this.statusCode = 404;
|
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializedError() {
|
|
12
|
+
return [{ message: "Page not found" }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomErrors } from "./custom-errors";
|
|
2
|
+
import { ValidationError } from 'express-validator';
|
|
3
|
+
export declare class RequestValidationError extends CustomErrors {
|
|
4
|
+
private errors;
|
|
5
|
+
statusCode: number;
|
|
6
|
+
constructor(errors: ValidationError[]);
|
|
7
|
+
serializedError(): ({
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
} | {
|
|
11
|
+
message: any;
|
|
12
|
+
field?: undefined;
|
|
13
|
+
})[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const custom_errors_1 = require("./custom-errors");
|
|
5
|
+
class RequestValidationError extends custom_errors_1.CustomErrors {
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super("Validation Failed");
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializedError() {
|
|
13
|
+
return this.errors.map((err) => {
|
|
14
|
+
if (err.type === 'field') {
|
|
15
|
+
return { message: err.msg, field: err.path };
|
|
16
|
+
}
|
|
17
|
+
return { message: err.msg };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export * from "./errors/bad-request-error";
|
|
2
|
+
export * from "./errors/custom-errors";
|
|
3
|
+
export * from "./errors/database-connection-error";
|
|
4
|
+
export * from "./errors/not-authorised-error";
|
|
5
|
+
export * from "./errors/not-found-error";
|
|
6
|
+
export * from "./errors/request-validation-error";
|
|
7
|
+
export * from "./middlewares/current-user";
|
|
8
|
+
export * from "./middlewares/error-handler";
|
|
9
|
+
export * from "./middlewares/request-validation";
|
|
10
|
+
export * from "./middlewares/required-auth";
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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);
|
|
6
15
|
};
|
|
7
|
-
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./errors/bad-request-error"), exports);
|
|
18
|
+
__exportStar(require("./errors/custom-errors"), 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/request-validation"), exports);
|
|
26
|
+
__exportStar(require("./middlewares/required-auth"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
interface UserPayload {
|
|
3
|
+
email: string;
|
|
4
|
+
id: 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,19 @@
|
|
|
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, _b;
|
|
10
|
+
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
req.currentUser = jsonwebtoken_1.default.verify((_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt, process.env.JWT_KEY);
|
|
15
|
+
}
|
|
16
|
+
catch (err) { }
|
|
17
|
+
next();
|
|
18
|
+
};
|
|
19
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const custom_errors_1 = require("../errors/custom-errors");
|
|
5
|
+
const errorHandler = (err, req, res, next) => {
|
|
6
|
+
if (err instanceof custom_errors_1.CustomErrors) {
|
|
7
|
+
res.status(err.statusCode).send({ errors: err.serializedError() });
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requestValidation = void 0;
|
|
4
|
+
const express_validator_1 = require("express-validator");
|
|
5
|
+
const request_validation_error_1 = require("../errors/request-validation-error");
|
|
6
|
+
const requestValidation = (req, res, next) => {
|
|
7
|
+
const error = (0, express_validator_1.validationResult)(req);
|
|
8
|
+
if (!error.isEmpty()) {
|
|
9
|
+
throw new request_validation_error_1.RequestValidationError(error.array());
|
|
10
|
+
}
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
13
|
+
exports.requestValidation = requestValidation;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requiredAuth = void 0;
|
|
4
|
+
const not_authorised_error_1 = require("../errors/not-authorised-error");
|
|
5
|
+
const requiredAuth = (req, res, next) => {
|
|
6
|
+
if (!req.currentUser) {
|
|
7
|
+
throw new not_authorised_error_1.NotAuthorisedError();
|
|
8
|
+
}
|
|
9
|
+
next();
|
|
10
|
+
};
|
|
11
|
+
exports.requiredAuth = requiredAuth;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greeksquard/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -19,5 +19,14 @@
|
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"del-cli": "^7.0.0",
|
|
21
21
|
"typescript": "^5.9.3"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/cookie-session": "^2.0.49",
|
|
25
|
+
"@types/express": "^4.17.24",
|
|
26
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
27
|
+
"cookie-session": "^2.1.1",
|
|
28
|
+
"express": "^4.21.2",
|
|
29
|
+
"express-validator": "^7.3.0",
|
|
30
|
+
"jsonwebtoken": "^9.0.2"
|
|
22
31
|
}
|
|
23
32
|
}
|