@misalon/common 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class BadRequestError extends CustomError {
3
+ message: string;
4
+ statusCode: number;
5
+ constructor(message: string);
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.BadRequestError = void 0;
19
+ var custom_error_1 = require("./custom-error");
20
+ var BadRequestError = /** @class */ (function (_super) {
21
+ __extends(BadRequestError, _super);
22
+ function BadRequestError(message) {
23
+ var _this = _super.call(this, message) || this;
24
+ _this.message = message;
25
+ _this.statusCode = 400;
26
+ Object.setPrototypeOf(_this, BadRequestError.prototype);
27
+ return _this;
28
+ }
29
+ BadRequestError.prototype.serializeErrors = function () {
30
+ return [{ message: this.message }];
31
+ };
32
+ return BadRequestError;
33
+ }(custom_error_1.CustomError));
34
+ exports.BadRequestError = BadRequestError;
@@ -0,0 +1,8 @@
1
+ export declare abstract class CustomError extends Error {
2
+ abstract statusCode: number;
3
+ constructor(message: string);
4
+ abstract serializeErrors(): {
5
+ message: string;
6
+ field?: string;
7
+ }[];
8
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.CustomError = void 0;
19
+ var CustomError = /** @class */ (function (_super) {
20
+ __extends(CustomError, _super);
21
+ function CustomError(message) {
22
+ var _this = _super.call(this, message) || this;
23
+ Object.setPrototypeOf(_this, CustomError.prototype);
24
+ return _this;
25
+ }
26
+ return CustomError;
27
+ }(Error));
28
+ exports.CustomError = CustomError;
@@ -0,0 +1,9 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class DatabaseConnectionError extends CustomError {
3
+ statusCode: number;
4
+ reason: string;
5
+ constructor();
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.DatabaseConnectionError = void 0;
19
+ var custom_error_1 = require("./custom-error");
20
+ var DatabaseConnectionError = /** @class */ (function (_super) {
21
+ __extends(DatabaseConnectionError, _super);
22
+ function DatabaseConnectionError() {
23
+ var _this = _super.call(this, 'Error connecting to db') || this;
24
+ _this.statusCode = 500;
25
+ _this.reason = 'Error connecting to database';
26
+ Object.setPrototypeOf(_this, DatabaseConnectionError.prototype);
27
+ return _this;
28
+ }
29
+ DatabaseConnectionError.prototype.serializeErrors = function () {
30
+ return [{ message: this.reason }];
31
+ };
32
+ return DatabaseConnectionError;
33
+ }(custom_error_1.CustomError));
34
+ exports.DatabaseConnectionError = DatabaseConnectionError;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class NotAuthorizedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.NotAuthorizedError = void 0;
19
+ var custom_error_1 = require("./custom-error");
20
+ var NotAuthorizedError = /** @class */ (function (_super) {
21
+ __extends(NotAuthorizedError, _super);
22
+ function NotAuthorizedError() {
23
+ var _this = _super.call(this, 'Not Authorized') || this;
24
+ _this.statusCode = 401;
25
+ Object.setPrototypeOf(_this, NotAuthorizedError.prototype);
26
+ return _this;
27
+ }
28
+ NotAuthorizedError.prototype.serializeErrors = function () {
29
+ return [{ message: 'Not authorized' }];
30
+ };
31
+ return NotAuthorizedError;
32
+ }(custom_error_1.CustomError));
33
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.NotFoundError = void 0;
19
+ var custom_error_1 = require("./custom-error");
20
+ var NotFoundError = /** @class */ (function (_super) {
21
+ __extends(NotFoundError, _super);
22
+ function NotFoundError() {
23
+ var _this = _super.call(this, 'Route not found') || this;
24
+ _this.statusCode = 404;
25
+ Object.setPrototypeOf(_this, NotFoundError.prototype);
26
+ return _this;
27
+ }
28
+ NotFoundError.prototype.serializeErrors = function () {
29
+ return [{ message: 'Not Found' }];
30
+ };
31
+ return NotFoundError;
32
+ }(custom_error_1.CustomError));
33
+ exports.NotFoundError = NotFoundError;
@@ -0,0 +1,14 @@
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
+ message: any;
12
+ field?: undefined;
13
+ })[];
14
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.RequestValidationError = void 0;
19
+ var custom_error_1 = require("./custom-error");
20
+ var RequestValidationError = /** @class */ (function (_super) {
21
+ __extends(RequestValidationError, _super);
22
+ function RequestValidationError(errors) {
23
+ var _this = _super.call(this, 'Invalid request parameters') || this;
24
+ _this.errors = errors;
25
+ _this.statusCode = 400;
26
+ // Only because we are extending a built in class
27
+ Object.setPrototypeOf(_this, RequestValidationError.prototype);
28
+ return _this;
29
+ }
30
+ RequestValidationError.prototype.serializeErrors = function () {
31
+ return this.errors.map(function (err) {
32
+ if (err.type === 'field') {
33
+ return { message: err.msg, field: err.path };
34
+ }
35
+ return { message: err.msg };
36
+ });
37
+ };
38
+ return RequestValidationError;
39
+ }(custom_error_1.CustomError));
40
+ exports.RequestValidationError = RequestValidationError;
package/build/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
- interface Color {
2
- red: number;
3
- blue: number;
4
- green: number;
5
- }
6
- declare const color: Color;
7
- export default color;
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/current-user';
8
+ export * from './middlewares/error-handler';
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.defineProperty(exports, "__esModule", { value: true });
3
- var color = {
4
- red: 20,
5
- blue: 20,
6
- green: 20
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
- console.log(color);
9
- exports.default = color;
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-authorized-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/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,20 @@
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
+ var jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ var currentUser = function (req, res, next) {
9
+ var _a;
10
+ if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
11
+ return next();
12
+ }
13
+ try {
14
+ var payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
15
+ req.currentUser = payload;
16
+ }
17
+ catch (err) { }
18
+ next();
19
+ };
20
+ exports.currentUser = currentUser;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ var custom_error_1 = require("../errors/custom-error");
5
+ var errorHandler = function (err, req, res, next) {
6
+ if (err instanceof custom_error_1.CustomError) {
7
+ return res.status(err.statusCode).send({ errors: err.serializeErrors() });
8
+ }
9
+ res.status(400).send({
10
+ errors: [{ message: 'Something went wrong' }]
11
+ });
12
+ };
13
+ exports.errorHandler = errorHandler;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAuth = void 0;
4
+ var not_authorized_error_1 = require("../errors/not-authorized-error");
5
+ var requireAuth = function (req, res, next) {
6
+ if (!req.currentUser) {
7
+ throw new not_authorized_error_1.NotAuthorizedError();
8
+ }
9
+ next();
10
+ };
11
+ exports.requireAuth = requireAuth;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateRequest = void 0;
4
+ var express_validator_1 = require("express-validator");
5
+ var request_validation_error_1 = require("../errors/request-validation-error");
6
+ var validateRequest = function (req, res, next) {
7
+ var errors = (0, express_validator_1.validationResult)(req);
8
+ if (!errors.isEmpty()) {
9
+ throw new request_validation_error_1.RequestValidationError(errors.array());
10
+ }
11
+ next();
12
+ };
13
+ exports.validateRequest = validateRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@misalon/common",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -18,5 +18,14 @@
18
18
  "devDependencies": {
19
19
  "del-cli": "^6.0.0",
20
20
  "typescript": "^5.6.3"
21
+ },
22
+ "dependencies": {
23
+ "@types/cookie-session": "^2.0.49",
24
+ "@types/express": "^5.0.0",
25
+ "@types/jsonwebtoken": "^9.0.7",
26
+ "cookie-session": "^2.1.0",
27
+ "express": "^4.21.1",
28
+ "express-validator": "^7.2.0",
29
+ "jsonwebtoken": "^9.0.2"
21
30
  }
22
31
  }