@duvdu-v1/duvdu 1.0.1 → 1.0.4
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 +19 -8
- package/build/errors/bad-request-error.d.ts +0 -9
- package/build/errors/bad-request-error.js +0 -15
- package/build/errors/custom-error.d.ts +0 -8
- package/build/errors/custom-error.js +0 -10
- package/build/errors/notfound-error.d.ts +0 -9
- package/build/errors/notfound-error.js +0 -15
- package/build/errors/unauthenticated-error.d.ts +0 -9
- package/build/errors/unauthenticated-error.js +0 -15
- package/build/errors/unauthorized-error.d.ts +0 -9
- package/build/errors/unauthorized-error.js +0 -15
- package/build/errors/validation-error.d.ts +0 -11
- package/build/errors/validation-error.js +0 -20
- package/build/events/base-listener.d.ts +0 -18
- package/build/events/base-listener.js +0 -30
- package/build/events/base-publisher.d.ts +0 -13
- package/build/events/base-publisher.js +0 -18
- package/build/events/subject.d.ts +0 -3
- package/build/events/subject.js +0 -7
- package/build/index.d.ts +0 -10
- package/build/index.js +0 -29
- package/build/middlewares/global-error-handling.middleware.d.ts +0 -2
- package/build/middlewares/global-error-handling.middleware.js +0 -28
- package/build/types/JwtPayload.d.ts +0 -3
- package/build/types/JwtPayload.js +0 -2
- package/build/types/Pagination.d.ts +0 -7
- package/build/types/Pagination.js +0 -2
- package/build/types/UrlQuery.d.ts +0 -9
- package/build/types/UrlQuery.js +0 -2
- package/build/types/auth/User.d.ts +0 -62
- package/build/types/auth/User.js +0 -2
- package/build/types/auth/endpoints.d.ts +0 -6
- package/build/types/auth/endpoints.js +0 -2
- package/build/utils/api-feature.d.ts +0 -39
- package/build/utils/api-feature.js +0 -73
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duvdu-v1/duvdu",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.4",
|
|
5
4
|
"main": "./build/index.js",
|
|
6
5
|
"types": "./build/index.d.ts",
|
|
7
6
|
"files": [
|
|
@@ -10,23 +9,35 @@
|
|
|
10
9
|
"scripts": {
|
|
11
10
|
"clean": "rimraf ./build",
|
|
12
11
|
"build": "npm run clean && tsc",
|
|
13
|
-
"pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish"
|
|
12
|
+
"pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish",
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"lint:fix": "eslint --fix .",
|
|
15
|
+
"format": "prettier --write ."
|
|
14
16
|
},
|
|
15
17
|
"keywords": [],
|
|
16
18
|
"author": "motemed khaled",
|
|
17
19
|
"license": "ISC",
|
|
18
20
|
"dependencies": {
|
|
21
|
+
"@duvdu-v1/duvdu": "^1.0.2",
|
|
19
22
|
"@types/node": "^20.11.0",
|
|
20
23
|
"express": "^4.18.2",
|
|
21
24
|
"express-validator": "^7.0.1",
|
|
22
25
|
"mongoose": "^8.0.4",
|
|
23
26
|
"multer": "^1.4.5-lts.1",
|
|
24
|
-
"node-nats-streaming": "^0.3.2"
|
|
25
|
-
},
|
|
26
|
-
"devDependencies": {
|
|
27
|
+
"node-nats-streaming": "^0.3.2",
|
|
27
28
|
"@types/express": "^4.17.21",
|
|
28
29
|
"@types/multer": "^1.4.11",
|
|
29
|
-
"
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
|
31
|
+
"@typescript-eslint/parser": "^6.19.0",
|
|
30
32
|
"typescript": "^5.3.3"
|
|
31
|
-
}
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"del-cli": "^5.1.0",
|
|
36
|
+
"eslint": "^8.56.0",
|
|
37
|
+
"eslint-config-prettier": "^9.1.0",
|
|
38
|
+
"eslint-plugin-import": "^2.29.1",
|
|
39
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
40
|
+
"prettier": "^3.2.4"
|
|
41
|
+
},
|
|
42
|
+
"description": ""
|
|
32
43
|
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BadRequestError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class BadRequestError extends custom_error_1.CustomError {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.statusCode = 400;
|
|
9
|
-
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
10
|
-
}
|
|
11
|
-
serializeError() {
|
|
12
|
-
return [{ message: this.message }];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.BadRequestError = BadRequestError;
|
|
@@ -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(message) {
|
|
6
|
-
super(message);
|
|
7
|
-
Object.setPrototypeOf(this, CustomError.prototype);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.CustomError = CustomError;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NotFound = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class NotFound extends custom_error_1.CustomError {
|
|
6
|
-
constructor() {
|
|
7
|
-
super("not found error");
|
|
8
|
-
this.statusCode = 404;
|
|
9
|
-
Object.setPrototypeOf(this, NotFound.prototype);
|
|
10
|
-
}
|
|
11
|
-
serializeError() {
|
|
12
|
-
return [{ message: "not found error" }];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.NotFound = NotFound;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnauthenticatedError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class UnauthenticatedError extends custom_error_1.CustomError {
|
|
6
|
-
constructor() {
|
|
7
|
-
super("un-authenticated error");
|
|
8
|
-
this.statusCode = 401;
|
|
9
|
-
Object.setPrototypeOf(this, UnauthenticatedError.prototype);
|
|
10
|
-
}
|
|
11
|
-
serializeError() {
|
|
12
|
-
return [{ message: "un-authenticated error" }];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.UnauthenticatedError = UnauthenticatedError;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnauthorizedError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class UnauthorizedError extends custom_error_1.CustomError {
|
|
6
|
-
constructor() {
|
|
7
|
-
super("un-unauthorized error");
|
|
8
|
-
this.statusCode = 403;
|
|
9
|
-
Object.setPrototypeOf(this, UnauthorizedError.prototype);
|
|
10
|
-
}
|
|
11
|
-
serializeError() {
|
|
12
|
-
return [{ message: "un-unauthorized error" }];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.UnauthorizedError = UnauthorizedError;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { CustomError } from "./custom-error";
|
|
2
|
-
import { ValidationError as vError } from "express-validator";
|
|
3
|
-
export declare class ValidationError extends CustomError {
|
|
4
|
-
error: vError[];
|
|
5
|
-
statusCode: number;
|
|
6
|
-
constructor(error: vError[]);
|
|
7
|
-
serializeError(): {
|
|
8
|
-
message: string;
|
|
9
|
-
field?: string | undefined;
|
|
10
|
-
}[];
|
|
11
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValidationError = void 0;
|
|
4
|
-
const custom_error_1 = require("./custom-error");
|
|
5
|
-
class ValidationError extends custom_error_1.CustomError {
|
|
6
|
-
constructor(error) {
|
|
7
|
-
super("validation error");
|
|
8
|
-
this.error = error;
|
|
9
|
-
this.statusCode = 422;
|
|
10
|
-
Object.setPrototypeOf(this, ValidationError.prototype);
|
|
11
|
-
}
|
|
12
|
-
serializeError() {
|
|
13
|
-
return this.error.map((el) => {
|
|
14
|
-
if (el.type === "field")
|
|
15
|
-
return { message: el.msg, field: el.path };
|
|
16
|
-
return { message: el.msg };
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.ValidationError = ValidationError;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Stan, Message } from "node-nats-streaming";
|
|
2
|
-
import { Subject } from "./subject";
|
|
3
|
-
interface Event {
|
|
4
|
-
subject: Subject;
|
|
5
|
-
data: any;
|
|
6
|
-
}
|
|
7
|
-
export declare abstract class Lisener<T extends Event> {
|
|
8
|
-
abstract queueGroupName: string;
|
|
9
|
-
abstract subject: T["subject"];
|
|
10
|
-
abstract onMessage(data: T["data"], msg: Message): void;
|
|
11
|
-
protected client: Stan;
|
|
12
|
-
protected ackWait: number;
|
|
13
|
-
constructor(client: Stan);
|
|
14
|
-
subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
|
|
15
|
-
listen(): void;
|
|
16
|
-
parseMessage(msg: Message): any;
|
|
17
|
-
}
|
|
18
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Lisener = void 0;
|
|
4
|
-
class Lisener {
|
|
5
|
-
constructor(client) {
|
|
6
|
-
this.ackWait = 5 * 1000;
|
|
7
|
-
this.client = client;
|
|
8
|
-
}
|
|
9
|
-
subscriptionOptions() {
|
|
10
|
-
return this.client
|
|
11
|
-
.subscriptionOptions()
|
|
12
|
-
.setDeliverAllAvailable()
|
|
13
|
-
.setManualAckMode(true)
|
|
14
|
-
.setAckWait(this.ackWait)
|
|
15
|
-
.setDurableName(this.queueGroupName);
|
|
16
|
-
}
|
|
17
|
-
listen() {
|
|
18
|
-
const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
|
|
19
|
-
subscription.on("message", (msg) => {
|
|
20
|
-
console.log(`Message received: ${this.subject} / ${this.queueGroupName}`);
|
|
21
|
-
const parsedData = this.parseMessage(msg);
|
|
22
|
-
this.onMessage(parsedData, msg);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
parseMessage(msg) {
|
|
26
|
-
const data = msg.getData();
|
|
27
|
-
return typeof data === "string" ? JSON.parse(data) : JSON.parse(data.toString("utf-8"));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.Lisener = Lisener;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Stan } from "node-nats-streaming";
|
|
2
|
-
import { Subject } from "./subject";
|
|
3
|
-
interface Event {
|
|
4
|
-
subject: Subject;
|
|
5
|
-
data: any;
|
|
6
|
-
}
|
|
7
|
-
export declare abstract class Publisher<T extends Event> {
|
|
8
|
-
abstract subject: T["subject"];
|
|
9
|
-
protected client: Stan;
|
|
10
|
-
constructor(client: Stan);
|
|
11
|
-
publish(data: T["data"]): Promise<void>;
|
|
12
|
-
}
|
|
13
|
-
export {};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Publisher = void 0;
|
|
4
|
-
class Publisher {
|
|
5
|
-
constructor(client) {
|
|
6
|
-
this.client = client;
|
|
7
|
-
}
|
|
8
|
-
publish(data) {
|
|
9
|
-
return new Promise((resolve, reject) => {
|
|
10
|
-
this.client.publish(this.subject, JSON.stringify(data), (err) => {
|
|
11
|
-
if (err)
|
|
12
|
-
reject(err);
|
|
13
|
-
resolve();
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.Publisher = Publisher;
|
package/build/events/subject.js
DELETED
package/build/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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";
|
package/build/index.js
DELETED
|
@@ -1,29 +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("./events/base-listener"), exports);
|
|
18
|
-
__exportStar(require("./events/base-publisher"), exports);
|
|
19
|
-
__exportStar(require("./events/subject"), exports);
|
|
20
|
-
__exportStar(require("./errors/bad-request-error"), exports);
|
|
21
|
-
// export * from "./errors/custom-error";
|
|
22
|
-
__exportStar(require("./errors/notfound-error"), exports);
|
|
23
|
-
__exportStar(require("./errors/unauthenticated-error"), exports);
|
|
24
|
-
__exportStar(require("./errors/unauthorized-error"), exports);
|
|
25
|
-
__exportStar(require("./errors/validation-error"), exports);
|
|
26
|
-
__exportStar(require("./middlewares/global-error-handling.middleware"), exports);
|
|
27
|
-
// export * from "./types/Pagination";
|
|
28
|
-
// export * from "./types/UrlQuery";
|
|
29
|
-
__exportStar(require("./utils/api-feature"), exports);
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.globalErrorHandlingMiddleware = void 0;
|
|
4
|
-
const custom_error_1 = require("../errors/custom-error");
|
|
5
|
-
const multer_1 = require("multer");
|
|
6
|
-
const globalErrorHandlingMiddleware = (err, req, res, next) => {
|
|
7
|
-
// custom error
|
|
8
|
-
if (err instanceof custom_error_1.CustomError) {
|
|
9
|
-
return res.status(err.statusCode).json({ errors: err.serializeError() });
|
|
10
|
-
}
|
|
11
|
-
// mongo dublicate error
|
|
12
|
-
if (err.name === "MongoServerError" && err.code == "11000")
|
|
13
|
-
return res
|
|
14
|
-
.status(422)
|
|
15
|
-
.json({ errors: [{ message: `${Object.keys(err.keyPattern)} is already exists` }] });
|
|
16
|
-
// unhandled multer error
|
|
17
|
-
if (err instanceof multer_1.MulterError)
|
|
18
|
-
return res.status(400).json({ errors: [{ message: `${err.field} is invalid` }] });
|
|
19
|
-
// JWT invalid token
|
|
20
|
-
if (err.name === "JsonWebTokenError")
|
|
21
|
-
return res.status(401).json({ errors: [{ message: "invalid token" }] });
|
|
22
|
-
// JWT expired token
|
|
23
|
-
if (err.name === "TokenExpiredError")
|
|
24
|
-
return res.status(401).json({ errors: [{ message: "expired token" }] });
|
|
25
|
-
// unHandled error
|
|
26
|
-
res.status(500).json({ errors: [{ message: "server error" }] });
|
|
27
|
-
};
|
|
28
|
-
exports.globalErrorHandlingMiddleware = globalErrorHandlingMiddleware;
|
package/build/types/UrlQuery.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
-
/// <reference types="mongoose/types/callback" />
|
|
3
|
-
/// <reference types="mongoose/types/collection" />
|
|
4
|
-
/// <reference types="mongoose/types/connection" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor" />
|
|
6
|
-
/// <reference types="mongoose/types/document" />
|
|
7
|
-
/// <reference types="mongoose/types/error" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes" />
|
|
12
|
-
/// <reference types="mongoose/types/models" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
-
/// <reference types="mongoose/types/populate" />
|
|
16
|
-
/// <reference types="mongoose/types/query" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
-
/// <reference types="mongoose/types/session" />
|
|
20
|
-
/// <reference types="mongoose/types/types" />
|
|
21
|
-
/// <reference types="mongoose/types/utility" />
|
|
22
|
-
/// <reference types="mongoose/types/validation" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
-
import { Types } from "mongoose";
|
|
26
|
-
export interface Iuser {
|
|
27
|
-
id: string;
|
|
28
|
-
googleId?: string;
|
|
29
|
-
appleId?: string;
|
|
30
|
-
name?: string;
|
|
31
|
-
phoneNumber: {
|
|
32
|
-
key: string;
|
|
33
|
-
number: string;
|
|
34
|
-
};
|
|
35
|
-
username: string;
|
|
36
|
-
password?: string;
|
|
37
|
-
verificationCode?: {
|
|
38
|
-
code: string;
|
|
39
|
-
expireAt: Date;
|
|
40
|
-
};
|
|
41
|
-
token?: string;
|
|
42
|
-
profileImage?: string;
|
|
43
|
-
coverImage?: string;
|
|
44
|
-
location?: {
|
|
45
|
-
lat: number;
|
|
46
|
-
lng: number;
|
|
47
|
-
};
|
|
48
|
-
categroy?: Types.ObjectId;
|
|
49
|
-
acceptedProjectsCounter: number;
|
|
50
|
-
profileViews: number;
|
|
51
|
-
about?: string;
|
|
52
|
-
isOnline: boolean;
|
|
53
|
-
isAvaliableToInstantProjects: boolean;
|
|
54
|
-
pricePerHour?: number;
|
|
55
|
-
plan: Types.ObjectId;
|
|
56
|
-
hasVerificationPadge: boolean;
|
|
57
|
-
avaliableContracts: number;
|
|
58
|
-
rate: {
|
|
59
|
-
ratersCounter: number;
|
|
60
|
-
totalRates: number;
|
|
61
|
-
};
|
|
62
|
-
}
|
package/build/types/auth/User.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
-
/// <reference types="mongoose/types/callback" />
|
|
3
|
-
/// <reference types="mongoose/types/collection" />
|
|
4
|
-
/// <reference types="mongoose/types/connection" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor" />
|
|
6
|
-
/// <reference types="mongoose/types/document" />
|
|
7
|
-
/// <reference types="mongoose/types/error" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes" />
|
|
12
|
-
/// <reference types="mongoose/types/models" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
-
/// <reference types="mongoose/types/populate" />
|
|
16
|
-
/// <reference types="mongoose/types/query" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
-
/// <reference types="mongoose/types/session" />
|
|
20
|
-
/// <reference types="mongoose/types/types" />
|
|
21
|
-
/// <reference types="mongoose/types/utility" />
|
|
22
|
-
/// <reference types="mongoose/types/validation" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals" />
|
|
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";
|
|
28
|
-
export declare class Api_Feature {
|
|
29
|
-
mongooseQuery: Query<Document[], Document, Model<Document>>;
|
|
30
|
-
queryString: IurlQuery;
|
|
31
|
-
filterQuery: Record<string, any>;
|
|
32
|
-
paginateResult: Partial<Ipagination>;
|
|
33
|
-
constructor(mongooseQuery: Query<Document[], Document, Model<Document>>, queryString: Partial<IurlQuery>);
|
|
34
|
-
filter(): this;
|
|
35
|
-
sort(): this;
|
|
36
|
-
limitFields(): this;
|
|
37
|
-
search(fieldName: string): this;
|
|
38
|
-
pagination(documentCount: number): this;
|
|
39
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Api_Feature = void 0;
|
|
4
|
-
class Api_Feature {
|
|
5
|
-
constructor(mongooseQuery, queryString) {
|
|
6
|
-
this.mongooseQuery = mongooseQuery;
|
|
7
|
-
this.queryString = queryString;
|
|
8
|
-
this.paginateResult = {};
|
|
9
|
-
this.filterQuery = {};
|
|
10
|
-
}
|
|
11
|
-
filter() {
|
|
12
|
-
const queryValues = Object.assign({}, this.queryString);
|
|
13
|
-
const expectedQuery = ["limit", "fields", "page", "keyword", "sort"];
|
|
14
|
-
expectedQuery.forEach((val) => delete queryValues[val]);
|
|
15
|
-
let queryStr = JSON.stringify(queryValues);
|
|
16
|
-
queryStr = queryStr.replace(/\b(gte|ge|eq|lt|lte)\b/g, (match) => `$${match}`);
|
|
17
|
-
if (this.queryString.keyword) {
|
|
18
|
-
this.filterQuery = JSON.parse(queryStr);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
this.mongooseQuery = this.mongooseQuery.find(JSON.parse(queryStr));
|
|
22
|
-
}
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
sort() {
|
|
26
|
-
if (this.queryString.sort) {
|
|
27
|
-
const sortBy = this.queryString.sort.split(",").join(" ");
|
|
28
|
-
this.mongooseQuery = this.mongooseQuery.sort(sortBy);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
this.mongooseQuery = this.mongooseQuery.sort("-createdAt");
|
|
32
|
-
}
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
limitFields() {
|
|
36
|
-
if (this.queryString.fields) {
|
|
37
|
-
const fields = this.queryString.fields.split(",").join(" ");
|
|
38
|
-
this.mongooseQuery = this.mongooseQuery.select(fields);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
this.mongooseQuery = this.mongooseQuery.select("-__v");
|
|
42
|
-
}
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
45
|
-
search(fieldName) {
|
|
46
|
-
// search by keyword by name
|
|
47
|
-
if (this.queryString.keyword) {
|
|
48
|
-
this.filterQuery[fieldName] = { $regex: this.queryString.keyword, $options: "i" };
|
|
49
|
-
this.mongooseQuery = this.mongooseQuery.find(this.filterQuery);
|
|
50
|
-
}
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
pagination(documentCount) {
|
|
54
|
-
const page = +(this.queryString.page || 1);
|
|
55
|
-
const limit = +(this.queryString.limit || 10);
|
|
56
|
-
const skip = (page - 1) * limit;
|
|
57
|
-
const endPageIndex = page * limit;
|
|
58
|
-
const pagination = {};
|
|
59
|
-
pagination.currentPage = page;
|
|
60
|
-
pagination.limit = limit;
|
|
61
|
-
pagination.numberOfPages = Math.ceil(documentCount / limit);
|
|
62
|
-
if (endPageIndex < documentCount) {
|
|
63
|
-
pagination.nextPage = page + 1;
|
|
64
|
-
}
|
|
65
|
-
if (skip > 0) {
|
|
66
|
-
pagination.previousPage = page - 1;
|
|
67
|
-
}
|
|
68
|
-
this.mongooseQuery = this.mongooseQuery.skip(skip).limit(limit);
|
|
69
|
-
this.paginateResult = pagination;
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.Api_Feature = Api_Feature;
|