@duvdu-v1/duvdu 1.0.4 → 1.0.5
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 +9 -0
- package/build/errors/bad-request-error.js +15 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +10 -0
- package/build/errors/data-base-connections.d.ts +9 -0
- package/build/errors/data-base-connections.js +20 -0
- package/build/errors/notfound-error.d.ts +9 -0
- package/build/errors/notfound-error.js +15 -0
- package/build/errors/unauthenticated-error.d.ts +9 -0
- package/build/errors/unauthenticated-error.js +15 -0
- package/build/errors/unauthorized-error.d.ts +9 -0
- package/build/errors/unauthorized-error.js +15 -0
- package/build/errors/validation-error.d.ts +11 -0
- package/build/errors/validation-error.js +20 -0
- package/build/events/base-listener.d.ts +18 -0
- package/build/events/base-listener.js +30 -0
- package/build/events/base-publisher.d.ts +13 -0
- package/build/events/base-publisher.js +18 -0
- package/build/events/subject.d.ts +3 -0
- package/build/events/subject.js +7 -0
- package/build/index.d.ts +13 -0
- package/build/index.js +32 -0
- package/build/middlewares/database-connection.d.ts +1 -0
- package/build/middlewares/database-connection.js +19 -0
- package/build/middlewares/global-error-handling.middleware.d.ts +2 -0
- package/build/middlewares/global-error-handling.middleware.js +29 -0
- package/build/middlewares/global-validator.middleware.d.ts +2 -0
- package/build/middlewares/global-validator.middleware.js +12 -0
- package/build/types/JwtPayload.d.ts +3 -0
- package/build/types/JwtPayload.js +2 -0
- package/build/types/Pagination.d.ts +7 -0
- package/build/types/Pagination.js +2 -0
- package/build/types/UrlQuery.d.ts +9 -0
- package/build/types/UrlQuery.js +2 -0
- package/build/types/auth/User.d.ts +62 -0
- package/build/types/auth/User.js +2 -0
- package/build/types/auth/endpoints.d.ts +6 -0
- package/build/types/auth/endpoints.js +2 -0
- package/build/utils/api-feature.d.ts +39 -0
- package/build/utils/api-feature.js +73 -0
- package/package.json +6 -5
|
@@ -0,0 +1,15 @@
|
|
|
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 || 'bad-request error');
|
|
8
|
+
this.statusCode = 400;
|
|
9
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializeError() {
|
|
12
|
+
return [{ message: this.message }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -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(message);
|
|
7
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CustomError = CustomError;
|
|
@@ -0,0 +1,20 @@
|
|
|
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(message) {
|
|
7
|
+
super(message || '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
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,15 @@
|
|
|
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(message) {
|
|
7
|
+
super(message || '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;
|
|
@@ -0,0 +1,15 @@
|
|
|
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(message) {
|
|
7
|
+
super(message || '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;
|
|
@@ -0,0 +1,15 @@
|
|
|
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(message) {
|
|
7
|
+
super(message || '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;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationError as vError } from 'express-validator';
|
|
2
|
+
import { CustomError } from './custom-error';
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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;
|
|
@@ -0,0 +1,18 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,30 @@
|
|
|
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;
|
|
@@ -0,0 +1,13 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,18 @@
|
|
|
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/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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';
|
|
13
|
+
export * from './types/JwtPayload';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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/data-base-connections"), exports);
|
|
23
|
+
__exportStar(require("./errors/notfound-error"), exports);
|
|
24
|
+
__exportStar(require("./errors/unauthenticated-error"), exports);
|
|
25
|
+
__exportStar(require("./errors/unauthorized-error"), exports);
|
|
26
|
+
__exportStar(require("./errors/validation-error"), exports);
|
|
27
|
+
__exportStar(require("./middlewares/global-error-handling.middleware"), exports);
|
|
28
|
+
// export * from "./types/Pagination";
|
|
29
|
+
// export * from "./types/UrlQuery";
|
|
30
|
+
__exportStar(require("./utils/api-feature"), exports);
|
|
31
|
+
__exportStar(require("./middlewares/database-connection"), exports);
|
|
32
|
+
__exportStar(require("./types/JwtPayload"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const dbConnection: (URI: string) => Promise<void>;
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
11
|
+
.connect(URI)
|
|
12
|
+
.then((conn) => {
|
|
13
|
+
console.log(`database connected in : ${conn.connection.host}`);
|
|
14
|
+
})
|
|
15
|
+
.catch(() => {
|
|
16
|
+
throw new data_base_connections_1.DatabaseConnectionError();
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
exports.dbConnection = dbConnection;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.globalErrorHandlingMiddleware = void 0;
|
|
4
|
+
const multer_1 = require("multer");
|
|
5
|
+
const custom_error_1 = require("../errors/custom-error");
|
|
6
|
+
// eslint-disable-next-line
|
|
7
|
+
const globalErrorHandlingMiddleware = (err, req, res, next) => {
|
|
8
|
+
// custom error
|
|
9
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
10
|
+
return res.status(err.statusCode).json({ errors: err.serializeError() });
|
|
11
|
+
}
|
|
12
|
+
// mongo dublicate error
|
|
13
|
+
if (err.name === 'MongoServerError' && err.code == '11000')
|
|
14
|
+
return res
|
|
15
|
+
.status(422)
|
|
16
|
+
.json({ errors: [{ message: `${Object.keys(err.keyPattern)} is already exists` }] });
|
|
17
|
+
// unhandled multer error
|
|
18
|
+
if (err instanceof multer_1.MulterError)
|
|
19
|
+
return res.status(400).json({ errors: [{ message: `${err.field} is invalid` }] });
|
|
20
|
+
// JWT invalid token
|
|
21
|
+
if (err.name === 'JsonWebTokenError')
|
|
22
|
+
return res.status(401).json({ errors: [{ message: 'invalid token' }] });
|
|
23
|
+
// JWT expired token
|
|
24
|
+
if (err.name === 'TokenExpiredError')
|
|
25
|
+
return res.status(401).json({ errors: [{ message: 'expired token' }] });
|
|
26
|
+
// unHandled error
|
|
27
|
+
res.status(500).json({ errors: [{ message: 'server error' }] });
|
|
28
|
+
};
|
|
29
|
+
exports.globalErrorHandlingMiddleware = globalErrorHandlingMiddleware;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.globalValidatorMiddleware = void 0;
|
|
4
|
+
const express_validator_1 = require("express-validator");
|
|
5
|
+
const validation_error_1 = require("../errors/validation-error");
|
|
6
|
+
const globalValidatorMiddleware = (req, res, next) => {
|
|
7
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
8
|
+
if (!errors.isEmpty())
|
|
9
|
+
return next(new validation_error_1.ValidationError(errors.array()));
|
|
10
|
+
next();
|
|
11
|
+
};
|
|
12
|
+
exports.globalValidatorMiddleware = globalValidatorMiddleware;
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duvdu-v1/duvdu",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"main": "./build/index.js",
|
|
5
5
|
"types": "./build/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -19,16 +19,17 @@
|
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@duvdu-v1/duvdu": "^1.0.2",
|
|
22
|
+
"@types/express": "^4.17.21",
|
|
23
|
+
"@types/multer": "^1.4.11",
|
|
22
24
|
"@types/node": "^20.11.0",
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
|
26
|
+
"@typescript-eslint/parser": "^6.19.0",
|
|
23
27
|
"express": "^4.18.2",
|
|
24
28
|
"express-validator": "^7.0.1",
|
|
25
29
|
"mongoose": "^8.0.4",
|
|
26
30
|
"multer": "^1.4.5-lts.1",
|
|
27
31
|
"node-nats-streaming": "^0.3.2",
|
|
28
|
-
"
|
|
29
|
-
"@types/multer": "^1.4.11",
|
|
30
|
-
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
|
31
|
-
"@typescript-eslint/parser": "^6.19.0",
|
|
32
|
+
"rimraf": "^5.0.5",
|
|
32
33
|
"typescript": "^5.3.3"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|