@aichatwar/shared 1.0.7 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aichatwar/shared",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "./build/index.js",
5
5
  "typs": "./build/index.d.ts",
6
6
  "files": [
@@ -1,12 +0,0 @@
1
- import { CustomError } from "./customError";
2
- declare class BadRequestError extends CustomError {
3
- details: string;
4
- data: string;
5
- statusCode: number;
6
- constructor(details: string);
7
- serializeDetails(): {
8
- message: string;
9
- field?: string;
10
- }[];
11
- }
12
- export { BadRequestError };
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BadRequestError = void 0;
4
- const customError_1 = require("./customError");
5
- class BadRequestError extends customError_1.CustomError {
6
- constructor(details) {
7
- super(details);
8
- this.details = details;
9
- this.data = "Bad Request!";
10
- this.statusCode = 400;
11
- Object.setPrototypeOf(this, BadRequestError.prototype);
12
- }
13
- serializeDetails() {
14
- return [
15
- { message: this.details
16
- }
17
- ];
18
- }
19
- ;
20
- }
21
- exports.BadRequestError = BadRequestError;
@@ -1,9 +0,0 @@
1
- declare abstract class CustomError extends Error {
2
- abstract statusCode: number;
3
- constructor(errorMessage: string);
4
- abstract serializeDetails(): {
5
- message: string;
6
- field?: string;
7
- }[];
8
- }
9
- export { CustomError };
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomError = void 0;
4
- class CustomError extends Error {
5
- constructor(errorMessage) {
6
- super(errorMessage);
7
- Object.setPrototypeOf(this, CustomError.prototype);
8
- }
9
- }
10
- exports.CustomError = CustomError;
@@ -1,11 +0,0 @@
1
- import { CustomError } from "./customError";
2
- declare class DatabaseError extends CustomError {
3
- details: string[];
4
- data: string;
5
- statusCode: number;
6
- constructor(details: string[]);
7
- serializeDetails(): {
8
- message: string;
9
- }[];
10
- }
11
- export { DatabaseError };
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatabaseError = void 0;
4
- const customError_1 = require("./customError");
5
- class DatabaseError extends customError_1.CustomError {
6
- constructor(details) {
7
- super('Database error');
8
- this.details = details;
9
- this.data = 'Database error';
10
- this.statusCode = 500;
11
- Object.setPrototypeOf(this, DatabaseError.prototype);
12
- }
13
- serializeDetails() {
14
- return [{ message: this.data }];
15
- }
16
- }
17
- exports.DatabaseError = DatabaseError;
@@ -1,11 +0,0 @@
1
- import { CustomError } from "./customError";
2
- declare class NotAuthorizedError extends CustomError {
3
- details: string[];
4
- data: string;
5
- statusCode: number;
6
- constructor(details: string[]);
7
- serializeDetails(): {
8
- message: string;
9
- }[];
10
- }
11
- export { NotAuthorizedError };
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotAuthorizedError = void 0;
4
- const customError_1 = require("./customError");
5
- class NotAuthorizedError extends customError_1.CustomError {
6
- constructor(details) {
7
- super('not Authorized');
8
- this.details = details;
9
- this.data = 'not Authorized';
10
- this.statusCode = 401;
11
- Object.setPrototypeOf(this, NotAuthorizedError.prototype);
12
- }
13
- serializeDetails() {
14
- return [{ message: this.data }];
15
- }
16
- }
17
- exports.NotAuthorizedError = NotAuthorizedError;
@@ -1,11 +0,0 @@
1
- import { CustomError } from "./customError";
2
- declare class NotFoundError extends CustomError {
3
- data: string;
4
- statusCode: number;
5
- constructor();
6
- serializeDetails(): {
7
- message: string;
8
- field?: string;
9
- }[];
10
- }
11
- export { NotFoundError };
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotFoundError = void 0;
4
- const customError_1 = require("./customError");
5
- class NotFoundError extends customError_1.CustomError {
6
- constructor() {
7
- super("Rout not found!");
8
- this.data = "not found!";
9
- this.statusCode = 404;
10
- Object.setPrototypeOf(this, NotFoundError.prototype);
11
- }
12
- serializeDetails() {
13
- return [{ message: "not found" }];
14
- }
15
- }
16
- exports.NotFoundError = NotFoundError;
@@ -1,16 +0,0 @@
1
- import { CustomError } from "./customError";
2
- import { ValidationError } from "express-validator";
3
- declare class RequestValidationError extends CustomError {
4
- details: ValidationError[];
5
- data: string;
6
- statusCode: number;
7
- constructor(details: ValidationError[]);
8
- serializeDetails(): ({
9
- message: any;
10
- field: string;
11
- } | {
12
- message: any;
13
- field?: undefined;
14
- })[];
15
- }
16
- export { RequestValidationError };
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RequestValidationError = void 0;
4
- const customError_1 = require("./customError");
5
- class RequestValidationError extends customError_1.CustomError {
6
- constructor(details) {
7
- super('Invalid request');
8
- this.details = details;
9
- this.data = "Invalid request!";
10
- this.statusCode = 400;
11
- // extending a built in class
12
- Object.setPrototypeOf(this, RequestValidationError.prototype);
13
- }
14
- serializeDetails() {
15
- return this.details.map(detail => {
16
- if (detail.type == 'field') {
17
- return { message: detail.msg, field: detail.path };
18
- }
19
- return { message: detail.msg };
20
- });
21
- }
22
- }
23
- exports.RequestValidationError = RequestValidationError;
package/build/index.d.ts DELETED
@@ -1,10 +0,0 @@
1
- export * from "./errors/badRequestError";
2
- export * from "./errors/customError";
3
- export * from "./errors/databaseError";
4
- export * from "./errors/notAuthorizedError";
5
- export * from "./errors/notfoundError";
6
- export * from "./errors/requestValidationError";
7
- export * from "./middlewares/error-handler";
8
- export * from "./middlewares/jwt-extractor";
9
- export * from "./middlewares/login-required";
10
- export * from "./middlewares/validate-request";
package/build/index.js DELETED
@@ -1,26 +0,0 @@
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/badRequestError"), exports);
18
- __exportStar(require("./errors/customError"), exports);
19
- __exportStar(require("./errors/databaseError"), exports);
20
- __exportStar(require("./errors/notAuthorizedError"), exports);
21
- __exportStar(require("./errors/notfoundError"), exports);
22
- __exportStar(require("./errors/requestValidationError"), exports);
23
- __exportStar(require("./middlewares/error-handler"), exports);
24
- __exportStar(require("./middlewares/jwt-extractor"), exports);
25
- __exportStar(require("./middlewares/login-required"), exports);
26
- __exportStar(require("./middlewares/validate-request"), exports);
@@ -1,2 +0,0 @@
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>>;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.errorHandler = void 0;
4
- const customError_1 = require("../errors/customError");
5
- `common structure for errors:
6
- {
7
- errors:{
8
- message: string, field?: string
9
- }[]
10
- }
11
- `;
12
- const errorHandler = (err, req, res, next) => {
13
- console.log("Error:", err);
14
- if (err instanceof customError_1.CustomError) {
15
- return res.status(err.statusCode).send({ errors: err.serializeDetails() });
16
- }
17
- return res.status(400).send({
18
- errors: [{ message: "an error occurred!" }]
19
- });
20
- };
21
- exports.errorHandler = errorHandler;
@@ -1,14 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- interface JwtPayload {
3
- id: string;
4
- email: string;
5
- }
6
- declare global {
7
- namespace Express {
8
- interface Request {
9
- jwtPayload?: JwtPayload;
10
- }
11
- }
12
- }
13
- declare const extractJWTPayload: (req: Request, res: Response, next: NextFunction) => void;
14
- export { extractJWTPayload };
@@ -1,26 +0,0 @@
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.extractJWTPayload = void 0;
7
- const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
- ;
9
- const extractJWTPayload = (req, res, next) => {
10
- var _a;
11
- if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
12
- return next();
13
- }
14
- else {
15
- try {
16
- const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_DEV);
17
- console.log(req);
18
- req.jwtPayload = payload;
19
- }
20
- catch (error) {
21
- console.log(error.message);
22
- }
23
- return next();
24
- }
25
- };
26
- exports.extractJWTPayload = extractJWTPayload;
@@ -1,3 +0,0 @@
1
- import { Request, Response, NextFunction } from 'express';
2
- declare const loginRequired: (req: Request, res: Response, next: NextFunction) => Promise<void>;
3
- export { loginRequired };
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.loginRequired = void 0;
13
- const notAuthorizedError_1 = require("../errors/notAuthorizedError");
14
- const loginRequired = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
15
- if (!req.jwtPayload) {
16
- throw new notAuthorizedError_1.NotAuthorizedError(['not authorized']);
17
- }
18
- else {
19
- next();
20
- }
21
- });
22
- exports.loginRequired = loginRequired;
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateRequest = void 0;
4
- const requestValidationError_1 = require("../errors/requestValidationError");
5
- const express_validator_1 = require("express-validator");
6
- const validateRequest = (req, res, next) => {
7
- const errors = (0, express_validator_1.validationResult)(req);
8
- if (!errors.isEmpty()) {
9
- throw new requestValidationError_1.RequestValidationError(errors.array());
10
- }
11
- else {
12
- next();
13
- }
14
- };
15
- exports.validateRequest = validateRequest;