@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.
- package/build/errors/bad-request-error.d.ts +1 -1
- package/build/errors/data-base-connections.d.ts +9 -0
- package/build/errors/data-base-connections.js +21 -0
- package/build/errors/notfound-error.d.ts +1 -1
- package/build/errors/notfound-error.js +2 -2
- package/build/errors/unauthenticated-error.d.ts +1 -1
- package/build/errors/unauthenticated-error.js +2 -2
- package/build/errors/unauthorized-error.d.ts +1 -1
- package/build/errors/unauthorized-error.js +2 -2
- package/build/errors/validation-error.d.ts +2 -2
- package/build/errors/validation-error.js +2 -2
- package/build/events/base-listener.d.ts +4 -4
- package/build/events/base-listener.js +2 -2
- package/build/events/base-publisher.d.ts +4 -4
- package/build/index.d.ts +12 -10
- package/build/index.js +2 -0
- package/build/middlewares/database-connection.d.ts +1 -0
- package/build/middlewares/database-connection.js +16 -0
- package/build/middlewares/global-error-handling.middleware.d.ts +1 -1
- package/build/middlewares/global-error-handling.middleware.js +8 -7
- package/build/types/auth/User.d.ts +1 -1
- package/build/types/auth/endpoints.d.ts +3 -3
- package/build/utils/api-feature.d.ts +3 -3
- package/build/utils/api-feature.js +6 -6
- package/package.json +12 -2
|
@@ -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;
|
|
@@ -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(
|
|
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:
|
|
12
|
+
return [{ message: 'not found error' }];
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
exports.NotFound = NotFound;
|
|
@@ -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(
|
|
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:
|
|
12
|
+
return [{ message: 'un-authenticated error' }];
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
exports.UnauthenticatedError = UnauthenticatedError;
|
|
@@ -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(
|
|
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:
|
|
12
|
+
return [{ message: 'un-unauthorized error' }];
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
exports.UnauthorizedError = UnauthorizedError;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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(
|
|
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 ===
|
|
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
|
|
2
|
-
import { Subject } from
|
|
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[
|
|
10
|
-
abstract onMessage(data: T[
|
|
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(
|
|
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 ===
|
|
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
|
|
2
|
-
import { Subject } from
|
|
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[
|
|
8
|
+
abstract subject: T['subject'];
|
|
9
9
|
protected client: Stan;
|
|
10
10
|
constructor(client: Stan);
|
|
11
|
-
publish(data: T[
|
|
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
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
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
|
|
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 ===
|
|
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 ===
|
|
21
|
-
return res.status(401).json({ errors: [{ message:
|
|
21
|
+
if (err.name === 'JsonWebTokenError')
|
|
22
|
+
return res.status(401).json({ errors: [{ message: 'invalid token' }] });
|
|
22
23
|
// JWT expired token
|
|
23
|
-
if (err.name ===
|
|
24
|
-
return res.status(401).json({ errors: [{ message:
|
|
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:
|
|
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
|
|
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
|
|
2
|
-
import { Iuser } from
|
|
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,
|
|
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
|
|
26
|
-
import { Ipagination } from
|
|
27
|
-
import { IurlQuery } from
|
|
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 = [
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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:
|
|
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.
|
|
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
|
}
|