@duvdu-v1/duvdu 1.0.1 → 1.0.2

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,4 +1,4 @@
1
- import { CustomError } from "./custom-error";
1
+ import { CustomError } from './custom-error';
2
2
  export declare class BadRequestError extends CustomError {
3
3
  statusCode: number;
4
4
  constructor(message: string);
@@ -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
+ serializeError(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,21 @@
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
+ serializeError() {
13
+ return [
14
+ {
15
+ message: this.reason
16
+ }
17
+ ];
18
+ }
19
+ ;
20
+ }
21
+ exports.DatabaseConnectionError = DatabaseConnectionError;
@@ -1,4 +1,4 @@
1
- import { CustomError } from "./custom-error";
1
+ import { CustomError } from './custom-error';
2
2
  export declare class NotFound extends CustomError {
3
3
  statusCode: number;
4
4
  constructor();
@@ -4,12 +4,12 @@ exports.NotFound = void 0;
4
4
  const custom_error_1 = require("./custom-error");
5
5
  class NotFound extends custom_error_1.CustomError {
6
6
  constructor() {
7
- super("not found error");
7
+ super('not found error');
8
8
  this.statusCode = 404;
9
9
  Object.setPrototypeOf(this, NotFound.prototype);
10
10
  }
11
11
  serializeError() {
12
- return [{ message: "not found error" }];
12
+ return [{ message: 'not found error' }];
13
13
  }
14
14
  }
15
15
  exports.NotFound = NotFound;
@@ -1,4 +1,4 @@
1
- import { CustomError } from "./custom-error";
1
+ import { CustomError } from './custom-error';
2
2
  export declare class UnauthenticatedError extends CustomError {
3
3
  statusCode: number;
4
4
  constructor();
@@ -4,12 +4,12 @@ exports.UnauthenticatedError = void 0;
4
4
  const custom_error_1 = require("./custom-error");
5
5
  class UnauthenticatedError extends custom_error_1.CustomError {
6
6
  constructor() {
7
- super("un-authenticated error");
7
+ super('un-authenticated error');
8
8
  this.statusCode = 401;
9
9
  Object.setPrototypeOf(this, UnauthenticatedError.prototype);
10
10
  }
11
11
  serializeError() {
12
- return [{ message: "un-authenticated error" }];
12
+ return [{ message: 'un-authenticated error' }];
13
13
  }
14
14
  }
15
15
  exports.UnauthenticatedError = UnauthenticatedError;
@@ -1,4 +1,4 @@
1
- import { CustomError } from "./custom-error";
1
+ import { CustomError } from './custom-error';
2
2
  export declare class UnauthorizedError extends CustomError {
3
3
  statusCode: number;
4
4
  constructor();
@@ -4,12 +4,12 @@ exports.UnauthorizedError = void 0;
4
4
  const custom_error_1 = require("./custom-error");
5
5
  class UnauthorizedError extends custom_error_1.CustomError {
6
6
  constructor() {
7
- super("un-unauthorized error");
7
+ super('un-unauthorized error');
8
8
  this.statusCode = 403;
9
9
  Object.setPrototypeOf(this, UnauthorizedError.prototype);
10
10
  }
11
11
  serializeError() {
12
- return [{ message: "un-unauthorized error" }];
12
+ return [{ message: 'un-unauthorized error' }];
13
13
  }
14
14
  }
15
15
  exports.UnauthorizedError = UnauthorizedError;
@@ -1,5 +1,5 @@
1
- import { CustomError } from "./custom-error";
2
- import { ValidationError as vError } from "express-validator";
1
+ import { ValidationError as vError } from 'express-validator';
2
+ import { CustomError } from './custom-error';
3
3
  export declare class ValidationError extends CustomError {
4
4
  error: vError[];
5
5
  statusCode: number;
@@ -4,14 +4,14 @@ exports.ValidationError = void 0;
4
4
  const custom_error_1 = require("./custom-error");
5
5
  class ValidationError extends custom_error_1.CustomError {
6
6
  constructor(error) {
7
- super("validation error");
7
+ super('validation error');
8
8
  this.error = error;
9
9
  this.statusCode = 422;
10
10
  Object.setPrototypeOf(this, ValidationError.prototype);
11
11
  }
12
12
  serializeError() {
13
13
  return this.error.map((el) => {
14
- if (el.type === "field")
14
+ if (el.type === 'field')
15
15
  return { message: el.msg, field: el.path };
16
16
  return { message: el.msg };
17
17
  });
@@ -1,13 +1,13 @@
1
- import { Stan, Message } from "node-nats-streaming";
2
- import { Subject } from "./subject";
1
+ import { Stan, Message } from 'node-nats-streaming';
2
+ import { Subject } from './subject';
3
3
  interface Event {
4
4
  subject: Subject;
5
5
  data: any;
6
6
  }
7
7
  export declare abstract class Lisener<T extends Event> {
8
8
  abstract queueGroupName: string;
9
- abstract subject: T["subject"];
10
- abstract onMessage(data: T["data"], msg: Message): void;
9
+ abstract subject: T['subject'];
10
+ abstract onMessage(data: T['data'], msg: Message): void;
11
11
  protected client: Stan;
12
12
  protected ackWait: number;
13
13
  constructor(client: Stan);
@@ -16,7 +16,7 @@ class Lisener {
16
16
  }
17
17
  listen() {
18
18
  const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
19
- subscription.on("message", (msg) => {
19
+ subscription.on('message', (msg) => {
20
20
  console.log(`Message received: ${this.subject} / ${this.queueGroupName}`);
21
21
  const parsedData = this.parseMessage(msg);
22
22
  this.onMessage(parsedData, msg);
@@ -24,7 +24,7 @@ class Lisener {
24
24
  }
25
25
  parseMessage(msg) {
26
26
  const data = msg.getData();
27
- return typeof data === "string" ? JSON.parse(data) : JSON.parse(data.toString("utf-8"));
27
+ return typeof data === 'string' ? JSON.parse(data) : JSON.parse(data.toString('utf-8'));
28
28
  }
29
29
  }
30
30
  exports.Lisener = Lisener;
@@ -1,13 +1,13 @@
1
- import { Stan } from "node-nats-streaming";
2
- import { Subject } from "./subject";
1
+ import { Stan } from 'node-nats-streaming';
2
+ import { Subject } from './subject';
3
3
  interface Event {
4
4
  subject: Subject;
5
5
  data: any;
6
6
  }
7
7
  export declare abstract class Publisher<T extends Event> {
8
- abstract subject: T["subject"];
8
+ abstract subject: T['subject'];
9
9
  protected client: Stan;
10
10
  constructor(client: Stan);
11
- publish(data: T["data"]): Promise<void>;
11
+ publish(data: T['data']): Promise<void>;
12
12
  }
13
13
  export {};
package/build/index.d.ts CHANGED
@@ -1,10 +1,12 @@
1
- export * from "./events/base-listener";
2
- export * from "./events/base-publisher";
3
- export * from "./events/subject";
4
- export * from "./errors/bad-request-error";
5
- export * from "./errors/notfound-error";
6
- export * from "./errors/unauthenticated-error";
7
- export * from "./errors/unauthorized-error";
8
- export * from "./errors/validation-error";
9
- export * from "./middlewares/global-error-handling.middleware";
10
- export * from "./utils/api-feature";
1
+ export * from './events/base-listener';
2
+ export * from './events/base-publisher';
3
+ export * from './events/subject';
4
+ export * from './errors/bad-request-error';
5
+ export * from './errors/data-base-connections';
6
+ export * from './errors/notfound-error';
7
+ export * from './errors/unauthenticated-error';
8
+ export * from './errors/unauthorized-error';
9
+ export * from './errors/validation-error';
10
+ export * from './middlewares/global-error-handling.middleware';
11
+ export * from './utils/api-feature';
12
+ export * from './middlewares/database-connection';
package/build/index.js CHANGED
@@ -19,6 +19,7 @@ __exportStar(require("./events/base-publisher"), exports);
19
19
  __exportStar(require("./events/subject"), exports);
20
20
  __exportStar(require("./errors/bad-request-error"), exports);
21
21
  // export * from "./errors/custom-error";
22
+ __exportStar(require("./errors/data-base-connections"), exports);
22
23
  __exportStar(require("./errors/notfound-error"), exports);
23
24
  __exportStar(require("./errors/unauthenticated-error"), exports);
24
25
  __exportStar(require("./errors/unauthorized-error"), exports);
@@ -27,3 +28,4 @@ __exportStar(require("./middlewares/global-error-handling.middleware"), exports)
27
28
  // export * from "./types/Pagination";
28
29
  // export * from "./types/UrlQuery";
29
30
  __exportStar(require("./utils/api-feature"), exports);
31
+ __exportStar(require("./middlewares/database-connection"), exports);
@@ -0,0 +1 @@
1
+ export declare const dbConnection: (URI: string) => Promise<void>;
@@ -0,0 +1,16 @@
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.dbConnection = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const data_base_connections_1 = require("../errors/data-base-connections");
9
+ const dbConnection = (URI) => {
10
+ return mongoose_1.default.connect(URI).then(conn => {
11
+ console.log(`database connected in : ${conn.connection.host}`);
12
+ }).catch(err => {
13
+ throw new data_base_connections_1.DatabaseConnectionError();
14
+ });
15
+ };
16
+ exports.dbConnection = dbConnection;
@@ -1,2 +1,2 @@
1
- import { ErrorRequestHandler } from "express";
1
+ import { ErrorRequestHandler } from 'express';
2
2
  export declare const globalErrorHandlingMiddleware: ErrorRequestHandler;
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.globalErrorHandlingMiddleware = void 0;
4
- const custom_error_1 = require("../errors/custom-error");
5
4
  const multer_1 = require("multer");
5
+ const custom_error_1 = require("../errors/custom-error");
6
+ // eslint-disable-next-line
6
7
  const globalErrorHandlingMiddleware = (err, req, res, next) => {
7
8
  // custom error
8
9
  if (err instanceof custom_error_1.CustomError) {
9
10
  return res.status(err.statusCode).json({ errors: err.serializeError() });
10
11
  }
11
12
  // mongo dublicate error
12
- if (err.name === "MongoServerError" && err.code == "11000")
13
+ if (err.name === 'MongoServerError' && err.code == '11000')
13
14
  return res
14
15
  .status(422)
15
16
  .json({ errors: [{ message: `${Object.keys(err.keyPattern)} is already exists` }] });
@@ -17,12 +18,12 @@ const globalErrorHandlingMiddleware = (err, req, res, next) => {
17
18
  if (err instanceof multer_1.MulterError)
18
19
  return res.status(400).json({ errors: [{ message: `${err.field} is invalid` }] });
19
20
  // JWT invalid token
20
- if (err.name === "JsonWebTokenError")
21
- return res.status(401).json({ errors: [{ message: "invalid token" }] });
21
+ if (err.name === 'JsonWebTokenError')
22
+ return res.status(401).json({ errors: [{ message: 'invalid token' }] });
22
23
  // JWT expired token
23
- if (err.name === "TokenExpiredError")
24
- return res.status(401).json({ errors: [{ message: "expired token" }] });
24
+ if (err.name === 'TokenExpiredError')
25
+ return res.status(401).json({ errors: [{ message: 'expired token' }] });
25
26
  // unHandled error
26
- res.status(500).json({ errors: [{ message: "server error" }] });
27
+ res.status(500).json({ errors: [{ message: 'server error' }] });
27
28
  };
28
29
  exports.globalErrorHandlingMiddleware = globalErrorHandlingMiddleware;
@@ -22,7 +22,7 @@
22
22
  /// <reference types="mongoose/types/validation" />
23
23
  /// <reference types="mongoose/types/virtuals" />
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
- import { Types } from "mongoose";
25
+ import { Types } from 'mongoose';
26
26
  export interface Iuser {
27
27
  id: string;
28
28
  googleId?: string;
@@ -1,6 +1,6 @@
1
- import { RequestHandler } from "express";
2
- import { Iuser } from "./User";
1
+ import { RequestHandler } from 'express';
2
+ import { Iuser } from './User';
3
3
  export interface IsigninHandler extends RequestHandler<undefined, {
4
4
  token: string;
5
- }, Partial<Pick<Iuser, "username" | "phoneNumber" | "password">>, undefined> {
5
+ }, Partial<Pick<Iuser, 'username' | 'phoneNumber' | 'password'>>, undefined> {
6
6
  }
@@ -22,9 +22,9 @@
22
22
  /// <reference types="mongoose/types/validation" />
23
23
  /// <reference types="mongoose/types/virtuals" />
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
- import { Model, Query, Document } from "mongoose";
26
- import { Ipagination } from "../types/Pagination";
27
- import { IurlQuery } from "../types/UrlQuery";
25
+ import { Model, Query, Document } from 'mongoose';
26
+ import { Ipagination } from '../types/Pagination';
27
+ import { IurlQuery } from '../types/UrlQuery';
28
28
  export declare class Api_Feature {
29
29
  mongooseQuery: Query<Document[], Document, Model<Document>>;
30
30
  queryString: IurlQuery;
@@ -10,7 +10,7 @@ class Api_Feature {
10
10
  }
11
11
  filter() {
12
12
  const queryValues = Object.assign({}, this.queryString);
13
- const expectedQuery = ["limit", "fields", "page", "keyword", "sort"];
13
+ const expectedQuery = ['limit', 'fields', 'page', 'keyword', 'sort'];
14
14
  expectedQuery.forEach((val) => delete queryValues[val]);
15
15
  let queryStr = JSON.stringify(queryValues);
16
16
  queryStr = queryStr.replace(/\b(gte|ge|eq|lt|lte)\b/g, (match) => `$${match}`);
@@ -24,28 +24,28 @@ class Api_Feature {
24
24
  }
25
25
  sort() {
26
26
  if (this.queryString.sort) {
27
- const sortBy = this.queryString.sort.split(",").join(" ");
27
+ const sortBy = this.queryString.sort.split(',').join(' ');
28
28
  this.mongooseQuery = this.mongooseQuery.sort(sortBy);
29
29
  }
30
30
  else {
31
- this.mongooseQuery = this.mongooseQuery.sort("-createdAt");
31
+ this.mongooseQuery = this.mongooseQuery.sort('-createdAt');
32
32
  }
33
33
  return this;
34
34
  }
35
35
  limitFields() {
36
36
  if (this.queryString.fields) {
37
- const fields = this.queryString.fields.split(",").join(" ");
37
+ const fields = this.queryString.fields.split(',').join(' ');
38
38
  this.mongooseQuery = this.mongooseQuery.select(fields);
39
39
  }
40
40
  else {
41
- this.mongooseQuery = this.mongooseQuery.select("-__v");
41
+ this.mongooseQuery = this.mongooseQuery.select('-__v');
42
42
  }
43
43
  return this;
44
44
  }
45
45
  search(fieldName) {
46
46
  // search by keyword by name
47
47
  if (this.queryString.keyword) {
48
- this.filterQuery[fieldName] = { $regex: this.queryString.keyword, $options: "i" };
48
+ this.filterQuery[fieldName] = { $regex: this.queryString.keyword, $options: 'i' };
49
49
  this.mongooseQuery = this.mongooseQuery.find(this.filterQuery);
50
50
  }
51
51
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duvdu-v1/duvdu",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -10,7 +10,10 @@
10
10
  "scripts": {
11
11
  "clean": "rimraf ./build",
12
12
  "build": "npm run clean && tsc",
13
- "pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish"
13
+ "pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish",
14
+ "lint": "eslint .",
15
+ "lint:fix": "eslint --fix .",
16
+ "format": "prettier --write ."
14
17
  },
15
18
  "keywords": [],
16
19
  "author": "motemed khaled",
@@ -26,7 +29,14 @@
26
29
  "devDependencies": {
27
30
  "@types/express": "^4.17.21",
28
31
  "@types/multer": "^1.4.11",
32
+ "@typescript-eslint/eslint-plugin": "^6.19.0",
33
+ "@typescript-eslint/parser": "^6.19.0",
29
34
  "del-cli": "^5.1.0",
35
+ "eslint": "^8.56.0",
36
+ "eslint-config-prettier": "^9.1.0",
37
+ "eslint-plugin-import": "^2.29.1",
38
+ "eslint-plugin-prettier": "^5.1.3",
39
+ "prettier": "^3.2.4",
30
40
  "typescript": "^5.3.3"
31
41
  }
32
42
  }