@gwankarltech/common 1.0.2 → 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.
@@ -1,13 +1,16 @@
1
- import { CustomError } from './custom-error';
2
- export class BadRequestError extends CustomError {
3
- message;
4
- statusCode = 400;
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 {
5
6
  constructor(message) {
6
7
  super(message);
7
8
  this.message = message;
9
+ this.statusCode = 400;
8
10
  Object.setPrototypeOf(this, BadRequestError.prototype);
9
11
  }
10
12
  serializeErrors() {
11
13
  return [{ message: this.message }];
12
14
  }
13
15
  }
16
+ exports.BadRequestError = BadRequestError;
@@ -1,6 +1,10 @@
1
- export class CustomError extends Error {
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,12 +1,16 @@
1
- import { CustomError } from './custom-error';
2
- export class DatabaseConnectionError extends CustomError {
3
- statusCode = 500;
4
- reason = 'Error connecting to database';
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 connecting to db');
8
+ this.statusCode = 500;
9
+ this.reason = 'Error connecting to database';
7
10
  Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
8
11
  }
9
12
  serializeErrors() {
10
13
  return [{ message: this.reason }];
11
14
  }
12
15
  }
16
+ exports.DatabaseConnectionError = DatabaseConnectionError;
@@ -1,11 +1,15 @@
1
- import { CustomError } from './custom-error';
2
- export class NotAuthorizedError extends CustomError {
3
- statusCode = 401;
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 {
4
6
  constructor() {
5
7
  super('Not Authorized');
8
+ this.statusCode = 401;
6
9
  Object.setPrototypeOf(this, NotAuthorizedError.prototype);
7
10
  }
8
11
  serializeErrors() {
9
12
  return [{ message: 'Not authorized' }];
10
13
  }
11
14
  }
15
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -1,11 +1,15 @@
1
- import { CustomError } from './custom-error';
2
- export class NotFoundError extends CustomError {
3
- statusCode = 404;
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() {
9
12
  return [{ message: 'Not Found' }];
10
13
  }
11
14
  }
15
+ exports.NotFoundError = NotFoundError;
@@ -1,10 +1,12 @@
1
- import { CustomError } from './custom-error';
2
- export class RequestValidationError extends CustomError {
3
- errors;
4
- statusCode = 400;
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 parameters');
7
8
  this.errors = errors;
9
+ this.statusCode = 400;
8
10
  // Only because we are extending a built 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
- 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';
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-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);
@@ -1,12 +1,20 @@
1
- import jwt from 'jsonwebtoken';
2
- export const currentUser = (req, res, next) => {
3
- if (!req.session?.jwt) {
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)) {
4
11
  return next();
5
12
  }
6
13
  try {
7
- const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
14
+ const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
8
15
  req.currentUser = payload;
9
16
  }
10
17
  catch (err) { }
11
18
  next();
12
19
  };
20
+ exports.currentUser = currentUser;
@@ -1,2 +1,2 @@
1
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>>;
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -1,9 +1,13 @@
1
- import { CustomError } from '../errors/custom-error';
2
- export const errorHandler = (err, req, res, next) => {
3
- if (err instanceof CustomError) {
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({ errors: err.serializeErrors() });
5
8
  }
6
9
  res.status(400).send({
7
10
  errors: [{ message: 'Something went wrong' }],
8
11
  });
9
12
  };
13
+ exports.errorHandler = errorHandler;
@@ -1,7 +1,11 @@
1
- import { NotAuthorizedError } from '../errors/not-authorized-error';
2
- export const requireAuth = (req, res, next) => {
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 = (req, res, next) => {
3
6
  if (!req.currentUser) {
4
- throw new NotAuthorizedError();
7
+ throw new not_authorized_error_1.NotAuthorizedError();
5
8
  }
6
9
  next();
7
10
  };
11
+ exports.requireAuth = requireAuth;
@@ -1,9 +1,13 @@
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);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gwankarltech/common",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "keywords": [],
15
15
  "author": "",
16
16
  "license": "ISC",
17
- "type": "module",
17
+ "type": "commonjs",
18
18
  "devDependencies": {
19
19
  "typescript": "^5.9.3"
20
20
  },