@sn1006/common 1.0.10 → 1.0.14

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,9 +1,9 @@
1
- import NormalizedError from './normalized-error';
1
+ import { NormalizedError } from './normalized-error';
2
2
  export declare class BadRequestError extends NormalizedError {
3
3
  message: string;
4
4
  status: number;
5
5
  constructor(message: string);
6
- serializeError(): {
6
+ serializeErrors(): {
7
7
  message: string;
8
8
  }[];
9
9
  }
@@ -1,18 +1,15 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.BadRequestError = void 0;
7
- const normalized_error_1 = __importDefault(require("./normalized-error"));
8
- class BadRequestError extends normalized_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class BadRequestError extends normalized_error_1.NormalizedError {
9
6
  constructor(message) {
10
7
  super(message);
11
8
  this.message = message;
12
9
  this.status = 400;
13
10
  Object.setPrototypeOf(this, BadRequestError.prototype);
14
11
  }
15
- serializeError() {
12
+ serializeErrors() {
16
13
  return [{ message: this.message }];
17
14
  }
18
15
  }
@@ -1,8 +1,8 @@
1
- import NormalizedError from './normalized-error';
1
+ import { NormalizedError } from './normalized-error';
2
2
  export default class DBError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  field?: string | undefined;
8
8
  }[];
@@ -1,16 +1,13 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const normalized_error_1 = __importDefault(require("./normalized-error"));
7
- class DBError extends normalized_error_1.default {
3
+ const normalized_error_1 = require("./normalized-error");
4
+ class DBError extends normalized_error_1.NormalizedError {
8
5
  constructor() {
9
6
  super('Error connecting to DB');
10
7
  this.status = 500;
11
8
  Object.setPrototypeOf(this, DBError.prototype);
12
9
  }
13
- serializeError() {
10
+ serializeErrors() {
14
11
  return [{ message: this.message }];
15
12
  }
16
13
  }
@@ -0,0 +1,3 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ import Joi from "joi";
3
+ export default function joiValidateRequest(schema: Joi.AnySchema, options?: Joi.ValidationOptions): (req: Request, res: Response, next: NextFunction) => any;
@@ -0,0 +1,18 @@
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
+ const validation_error_1 = __importDefault(require("./validation-error"));
7
+ function joiValidateRequest(schema, options) {
8
+ return (req, res, next) => {
9
+ const { error } = schema.validate(req, options);
10
+ if (error) {
11
+ // normalized the error to be similar to express-validator error
12
+ const errors = error.details.map((err) => (Object.assign(Object.assign({}, err), { msg: err.message, path: err.path.join(".") })));
13
+ return next(new validation_error_1.default(errors));
14
+ }
15
+ return next();
16
+ };
17
+ }
18
+ exports.default = joiValidateRequest;
@@ -1,9 +1,9 @@
1
- declare abstract class NormalizedError extends Error {
1
+ export declare abstract class NormalizedError extends Error {
2
2
  abstract status: number;
3
3
  constructor(message: string);
4
- abstract serializeError(): {
5
- message: string;
6
- field?: string;
7
- }[];
4
+ abstract serializeErrors(): SerializedError[];
5
+ }
6
+ export interface SerializedError {
7
+ message: string;
8
+ field?: string;
8
9
  }
9
- export default NormalizedError;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NormalizedError = void 0;
3
4
  // the purpose of this class is to "normalize" errors between services
4
5
  class NormalizedError extends Error {
5
6
  constructor(message) {
@@ -7,4 +8,4 @@ class NormalizedError extends Error {
7
8
  Object.setPrototypeOf(this, NormalizedError.prototype);
8
9
  }
9
10
  }
10
- exports.default = NormalizedError;
11
+ exports.NormalizedError = NormalizedError;
@@ -1,8 +1,8 @@
1
- import NormalizedError from './normalized-error';
1
+ import { NormalizedError } from './normalized-error';
2
2
  export declare class NotAuthorizedError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  }[];
8
8
  }
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.NotAuthorizedError = void 0;
7
- const normalized_error_1 = __importDefault(require("./normalized-error"));
8
- class NotAuthorizedError extends normalized_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class NotAuthorizedError extends normalized_error_1.NormalizedError {
9
6
  constructor() {
10
7
  super('Not authorized');
11
8
  this.status = 401;
12
9
  Object.setPrototypeOf(this, NotAuthorizedError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return [{ message: this.message }];
16
13
  }
17
14
  }
@@ -1,8 +1,8 @@
1
- import NormalizedError from './normalized-error';
1
+ import { NormalizedError } from './normalized-error';
2
2
  export declare class NotFoundError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  }[];
8
8
  }
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.NotFoundError = void 0;
7
- const normalized_error_1 = __importDefault(require("./normalized-error"));
8
- class NotFoundError extends normalized_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class NotFoundError extends normalized_error_1.NormalizedError {
9
6
  constructor() {
10
7
  super("Resource not found");
11
8
  this.status = 404;
12
9
  Object.setPrototypeOf(this, NotFoundError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return [{ message: this.message }];
16
13
  }
17
14
  }
@@ -1,11 +1,14 @@
1
- import NormalizedError from './normalized-error';
2
- import { ValidationError } from 'express-validator';
1
+ import { NormalizedError } from './normalized-error';
2
+ interface NonSerializedValidationError {
3
+ msg: string;
4
+ path: string;
5
+ }
3
6
  declare class CustomValidationError extends NormalizedError {
4
- errors: ValidationError[];
7
+ errors: NonSerializedValidationError[];
5
8
  status: number;
6
- constructor(errors: ValidationError[]);
7
- serializeError(): {
8
- message: any;
9
+ constructor(errors: NonSerializedValidationError[]);
10
+ serializeErrors(): {
11
+ message: string;
9
12
  field: string;
10
13
  }[];
11
14
  }
@@ -1,20 +1,16 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const normalized_error_1 = __importDefault(require("./normalized-error"));
7
- class CustomValidationError extends normalized_error_1.default {
3
+ const normalized_error_1 = require("./normalized-error");
4
+ class CustomValidationError extends normalized_error_1.NormalizedError {
8
5
  constructor(errors) {
9
6
  super("Validaton error");
10
7
  this.errors = errors;
11
8
  this.status = 400;
12
9
  Object.setPrototypeOf(this, CustomValidationError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return this.errors.map(err => {
16
- const ferr = err;
17
- return { message: ferr.msg, field: ferr.path };
13
+ return { message: err.msg, field: err.path };
18
14
  });
19
15
  }
20
16
  }
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.errorHandler = void 0;
7
- const normalized_error_1 = __importDefault(require("../errors/normalized-error"));
4
+ const normalized_error_1 = require("../errors/normalized-error");
8
5
  const errorHandler = (err, req, res, next) => {
9
6
  console.log(err);
10
- if (err instanceof normalized_error_1.default) {
11
- return res.status(err.status).send(err.serializeError());
7
+ if (err instanceof normalized_error_1.NormalizedError) {
8
+ return res.status(err.status).send(err.serializeErrors());
12
9
  }
13
10
  return res.status(400).send('Something went wrong');
14
11
  };
@@ -7,9 +7,10 @@ exports.validateRequest = void 0;
7
7
  const express_validator_1 = require("express-validator");
8
8
  const validation_error_1 = __importDefault(require("../errors/validation-error"));
9
9
  const validateRequest = (req, res, next) => {
10
- const errors = (0, express_validator_1.validationResult)(req);
11
- if (!errors.isEmpty()) {
12
- return next(new validation_error_1.default(errors.array()));
10
+ const error = (0, express_validator_1.validationResult)(req);
11
+ if (!error.isEmpty()) {
12
+ const errors = error.array();
13
+ return next(new validation_error_1.default(errors));
13
14
  }
14
15
  next();
15
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sn1006/common",
3
- "version": "1.0.10",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.0.0",
27
27
  "express": "^4.18.2",
28
28
  "express-validator": "^7.0.1",
29
+ "joi": "^17.12.1",
29
30
  "jsonwebtoken": "^9.0.1"
30
31
  }
31
32
  }