@riocrypto/common 1.0.0 → 1.0.1

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.
@@ -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,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomError = void 0;
4
+ class CustomError extends Error {
5
+ constructor(message) {
6
+ super();
7
+ Object.setPrototypeOf(this, CustomError.prototype);
8
+ }
9
+ }
10
+ 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,16 @@
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 {
6
+ constructor() {
7
+ super("Error connecting to database");
8
+ this.statusCode = 500;
9
+ this.reason = "Error connecting to database";
10
+ Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
11
+ }
12
+ serializeErrors() {
13
+ return [{ message: this.reason }];
14
+ }
15
+ }
16
+ exports.DatabaseConnectionError = DatabaseConnectionError;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class MessageFailedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageFailedError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class MessageFailedError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super("Failed to send verification code");
8
+ this.statusCode = 500;
9
+ Object.setPrototypeOf(this, MessageFailedError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: "Failed to send verification code" }];
13
+ }
14
+ }
15
+ exports.MessageFailedError = MessageFailedError;
@@ -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,15 @@
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 {
6
+ constructor() {
7
+ super("Route not found");
8
+ this.statusCode = 400;
9
+ Object.setPrototypeOf(this, NotFoundError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: "Not found" }];
13
+ }
14
+ }
15
+ exports.NotFoundError = NotFoundError;
@@ -0,0 +1,11 @@
1
+ import { CustomError } from "./custom-error";
2
+ import { ValidationError } from "express-validator";
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
+ }
@@ -0,0 +1,18 @@
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 {
6
+ constructor(errors) {
7
+ super("Invalid request parameters");
8
+ this.errors = errors;
9
+ this.statusCode = 400;
10
+ Object.setPrototypeOf(this, RequestValidationError.prototype);
11
+ }
12
+ serializeErrors() {
13
+ return this.errors.map((err) => {
14
+ return { message: err.msg, field: err.param };
15
+ });
16
+ }
17
+ }
18
+ exports.RequestValidationError = RequestValidationError;
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class VerificationFailedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VerificationFailedError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class VerificationFailedError extends custom_error_1.CustomError {
6
+ constructor() {
7
+ super("SMS verification failed");
8
+ this.statusCode = 422;
9
+ Object.setPrototypeOf(this, VerificationFailedError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: "Verification failed" }];
13
+ }
14
+ }
15
+ exports.VerificationFailedError = VerificationFailedError;
@@ -0,0 +1,8 @@
1
+ export * from "./errors/custom-error";
2
+ export * from "./errors/database-connection-error";
3
+ export * from "./errors/message-failed-error";
4
+ export * from "./errors/not-found-error";
5
+ export * from "./errors/request-validation-error";
6
+ export * from "./errors/verification-failed-error";
7
+ export * from "./middlewares/error-handler";
8
+ export * from "./middlewares/validate-request";
package/build/index.js ADDED
@@ -0,0 +1,24 @@
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/custom-error"), exports);
18
+ __exportStar(require("./errors/database-connection-error"), exports);
19
+ __exportStar(require("./errors/message-failed-error"), exports);
20
+ __exportStar(require("./errors/not-found-error"), exports);
21
+ __exportStar(require("./errors/request-validation-error"), exports);
22
+ __exportStar(require("./errors/verification-failed-error"), exports);
23
+ __exportStar(require("./middlewares/error-handler"), exports);
24
+ __exportStar(require("./middlewares/validate-request"), exports);
@@ -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
+ const custom_error_1 = require("../errors/custom-error");
5
+ const errorHandler = (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
+ erros: [{ 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 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
+ 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);
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,12 +1,26 @@
1
1
  {
2
2
  "name": "@riocrypto/common",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "./build/index.js",
6
+ "types": "./build/index.d.ts",
7
+ "files": [
8
+ "build/**/*"
9
+ ],
6
10
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "clean": "del ./build/*",
12
+ "build": "npm run clean && tsc"
8
13
  },
9
14
  "keywords": [],
10
15
  "author": "",
11
- "license": "ISC"
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "del-cli": "^5.0.0",
19
+ "typescript": "^4.7.4"
20
+ },
21
+ "dependencies": {
22
+ "@types/express": "^4.17.13",
23
+ "express": "^4.18.1",
24
+ "express-validator": "^6.14.2"
25
+ }
12
26
  }