@hastehaul/common 1.0.15 → 1.0.18
Sign up to get free protection for your applications and to get access to all the features.
- package/build/errors/not-authorized-error.d.ts +30 -0
- package/build/errors/not-authorized-error.js +38 -0
- package/build/errors/resource-not-found.d.ts +30 -0
- package/build/errors/resource-not-found.js +37 -0
- package/build/errors/too-many-request-error.d.ts +33 -0
- package/build/errors/too-many-request-error.js +43 -0
- package/build/events/base/base-consumer.d.ts +3 -1
- package/build/events/base/base-publisher.d.ts +1 -1
- package/build/events/base/base-stream.d.ts +1 -1
- package/build/index.d.ts +173 -5
- package/build/index.js +189 -5
- package/build/middlewares/current-user.js +0 -1
- package/build/middlewares/error-handler.js +1 -3
- package/build/middlewares/is-allowed.d.ts +24 -0
- package/build/middlewares/is-allowed.js +71 -0
- package/build/middlewares/require-auth.d.ts +18 -0
- package/build/middlewares/require-auth.js +27 -0
- package/build/middlewares/validate-body.d.ts +57 -0
- package/build/middlewares/validate-body.js +102 -0
- package/build/utils/base_classes/jobs/base-producers.js +2 -2
- package/build/utils/base_classes/jobs/base-worker.js +2 -2
- package/build/utils/schemas.d.ts +11 -0
- package/build/utils/schemas.js +162 -0
- package/package.json +2 -1
@@ -0,0 +1,30 @@
|
|
1
|
+
import { StatusCodes } from '../utils/enums';
|
2
|
+
import { CustomError } from './custom-error';
|
3
|
+
/**
|
4
|
+
* Custom error class representing a "Not Authorized" error.
|
5
|
+
*
|
6
|
+
* The "NotAuthorizedError" class extends the base "CustomError" class to handle errors related to unauthorized access to a resource or action.
|
7
|
+
* It sets the HTTP status code to 401 (UNAUTHORIZED) and provides a default error message of "Not Authorized".
|
8
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
9
|
+
*/
|
10
|
+
export declare class NotAuthorizedError extends CustomError {
|
11
|
+
/**
|
12
|
+
* HTTP status code for the "Not Authorized" error.
|
13
|
+
*/
|
14
|
+
statusCode: StatusCodes;
|
15
|
+
/**
|
16
|
+
* Creates a new instance of the "NotAuthorizedError" class with a default error message of "Not Authorized".
|
17
|
+
*/
|
18
|
+
constructor();
|
19
|
+
/**
|
20
|
+
* Serializes the error to be sent as a response to the client.
|
21
|
+
*
|
22
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Not Authorized" error.
|
23
|
+
* Subclasses can override this method to provide custom error serialization.
|
24
|
+
*
|
25
|
+
* @returns An array containing an object with the error message for the "Not Authorized" error.
|
26
|
+
*/
|
27
|
+
serializeErrors(): {
|
28
|
+
message: string;
|
29
|
+
}[];
|
30
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NotAuthorizedError = void 0;
|
4
|
+
const enums_1 = require("../utils/enums");
|
5
|
+
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Custom error class representing a "Not Authorized" error.
|
8
|
+
*
|
9
|
+
* The "NotAuthorizedError" class extends the base "CustomError" class to handle errors related to unauthorized access to a resource or action.
|
10
|
+
* It sets the HTTP status code to 401 (UNAUTHORIZED) and provides a default error message of "Not Authorized".
|
11
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
12
|
+
*/
|
13
|
+
class NotAuthorizedError extends custom_error_1.CustomError {
|
14
|
+
/**
|
15
|
+
* Creates a new instance of the "NotAuthorizedError" class with a default error message of "Not Authorized".
|
16
|
+
*/
|
17
|
+
constructor() {
|
18
|
+
super('Not Authorized');
|
19
|
+
/**
|
20
|
+
* HTTP status code for the "Not Authorized" error.
|
21
|
+
*/
|
22
|
+
this.statusCode = enums_1.StatusCodes.UNAUTHORIZED;
|
23
|
+
// Ensures correct prototype chain for subclassing.
|
24
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
25
|
+
}
|
26
|
+
/**
|
27
|
+
* Serializes the error to be sent as a response to the client.
|
28
|
+
*
|
29
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Not Authorized" error.
|
30
|
+
* Subclasses can override this method to provide custom error serialization.
|
31
|
+
*
|
32
|
+
* @returns An array containing an object with the error message for the "Not Authorized" error.
|
33
|
+
*/
|
34
|
+
serializeErrors() {
|
35
|
+
return [{ message: 'Not authorized' }];
|
36
|
+
}
|
37
|
+
}
|
38
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { StatusCodes } from '../utils/enums';
|
2
|
+
import { CustomError } from './custom-error';
|
3
|
+
/**
|
4
|
+
* Custom error class representing a "Resource Not Found" error.
|
5
|
+
*
|
6
|
+
* The "ResourceNotFound" class extends the base "CustomError" class to handle errors related to resources that cannot be found.
|
7
|
+
* It sets the HTTP status code to 404 (NOT_FOUND) and provides a default error message of "Resource not found".
|
8
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
9
|
+
*/
|
10
|
+
export declare class ResourceNotFound extends CustomError {
|
11
|
+
/**
|
12
|
+
* HTTP status code for the "Resource Not Found" error.
|
13
|
+
*/
|
14
|
+
statusCode: StatusCodes;
|
15
|
+
/**
|
16
|
+
* Creates a new instance of the "ResourceNotFound" class with a default error message of "Resource not found".
|
17
|
+
*/
|
18
|
+
constructor();
|
19
|
+
/**
|
20
|
+
* Serializes the error to be sent as a response to the client.
|
21
|
+
*
|
22
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Resource Not Found" error.
|
23
|
+
* Subclasses can override this method to provide custom error serialization.
|
24
|
+
*
|
25
|
+
* @returns An array containing an object with the error message for the "Resource Not Found" error.
|
26
|
+
*/
|
27
|
+
serializeErrors(): {
|
28
|
+
message: string;
|
29
|
+
}[];
|
30
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ResourceNotFound = void 0;
|
4
|
+
const enums_1 = require("../utils/enums");
|
5
|
+
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Custom error class representing a "Resource Not Found" error.
|
8
|
+
*
|
9
|
+
* The "ResourceNotFound" class extends the base "CustomError" class to handle errors related to resources that cannot be found.
|
10
|
+
* It sets the HTTP status code to 404 (NOT_FOUND) and provides a default error message of "Resource not found".
|
11
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
12
|
+
*/
|
13
|
+
class ResourceNotFound extends custom_error_1.CustomError {
|
14
|
+
/**
|
15
|
+
* Creates a new instance of the "ResourceNotFound" class with a default error message of "Resource not found".
|
16
|
+
*/
|
17
|
+
constructor() {
|
18
|
+
super('Resource not found');
|
19
|
+
/**
|
20
|
+
* HTTP status code for the "Resource Not Found" error.
|
21
|
+
*/
|
22
|
+
this.statusCode = enums_1.StatusCodes.NOT_FOUND;
|
23
|
+
Object.setPrototypeOf(this, ResourceNotFound.prototype);
|
24
|
+
}
|
25
|
+
/**
|
26
|
+
* Serializes the error to be sent as a response to the client.
|
27
|
+
*
|
28
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Resource Not Found" error.
|
29
|
+
* Subclasses can override this method to provide custom error serialization.
|
30
|
+
*
|
31
|
+
* @returns An array containing an object with the error message for the "Resource Not Found" error.
|
32
|
+
*/
|
33
|
+
serializeErrors() {
|
34
|
+
return [{ message: 'Resource Not Found' }];
|
35
|
+
}
|
36
|
+
}
|
37
|
+
exports.ResourceNotFound = ResourceNotFound;
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { CustomError } from './custom-error';
|
2
|
+
/**
|
3
|
+
* Custom error class representing a "Too Many Requests" error.
|
4
|
+
*
|
5
|
+
* The "TooManyRequestError" class extends the base "CustomError" class to handle errors related to receiving too many requests from a client within a specific time frame.
|
6
|
+
* It sets the HTTP status code to 429 (TOO_MANY_REQUESTS) and provides a default error message of "Too Many Requests".
|
7
|
+
* The class also includes a `retrySec` property to specify the number of seconds after which the client can retry the request.
|
8
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
9
|
+
*/
|
10
|
+
export declare class TooManyRequestError extends CustomError {
|
11
|
+
retrySec: string;
|
12
|
+
/**
|
13
|
+
* HTTP status code for the "Too Many Requests" error.
|
14
|
+
*/
|
15
|
+
statusCode: number;
|
16
|
+
/**
|
17
|
+
* Creates a new instance of the "TooManyRequestError" class with a default error message of "Too Many Requests".
|
18
|
+
*
|
19
|
+
* @param retrySec The number of seconds after which the client can retry the request.
|
20
|
+
*/
|
21
|
+
constructor(retrySec: string);
|
22
|
+
/**
|
23
|
+
* Serializes the error to be sent as a response to the client.
|
24
|
+
*
|
25
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Too Many Requests" error.
|
26
|
+
* Subclasses can override this method to provide custom error serialization.
|
27
|
+
*
|
28
|
+
* @returns An array containing an object with the error message for the "Too Many Requests" error.
|
29
|
+
*/
|
30
|
+
serializeErrors(): {
|
31
|
+
message: string;
|
32
|
+
}[];
|
33
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TooManyRequestError = void 0;
|
4
|
+
const enums_1 = require("../utils/enums");
|
5
|
+
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Custom error class representing a "Too Many Requests" error.
|
8
|
+
*
|
9
|
+
* The "TooManyRequestError" class extends the base "CustomError" class to handle errors related to receiving too many requests from a client within a specific time frame.
|
10
|
+
* It sets the HTTP status code to 429 (TOO_MANY_REQUESTS) and provides a default error message of "Too Many Requests".
|
11
|
+
* The class also includes a `retrySec` property to specify the number of seconds after which the client can retry the request.
|
12
|
+
* Subclasses can customize the error message or additional fields as needed by overriding the "serializeErrors" method.
|
13
|
+
*/
|
14
|
+
class TooManyRequestError extends custom_error_1.CustomError {
|
15
|
+
/**
|
16
|
+
* Creates a new instance of the "TooManyRequestError" class with a default error message of "Too Many Requests".
|
17
|
+
*
|
18
|
+
* @param retrySec The number of seconds after which the client can retry the request.
|
19
|
+
*/
|
20
|
+
constructor(retrySec) {
|
21
|
+
super('Too Many Requests');
|
22
|
+
this.retrySec = retrySec;
|
23
|
+
/**
|
24
|
+
* HTTP status code for the "Too Many Requests" error.
|
25
|
+
*/
|
26
|
+
this.statusCode = enums_1.StatusCodes.TOO_MANY_REQUEST;
|
27
|
+
Object.setPrototypeOf(this, TooManyRequestError.prototype);
|
28
|
+
}
|
29
|
+
/**
|
30
|
+
* Serializes the error to be sent as a response to the client.
|
31
|
+
*
|
32
|
+
* The "serializeErrors" method returns an array containing an object with the error message for the "Too Many Requests" error.
|
33
|
+
* Subclasses can override this method to provide custom error serialization.
|
34
|
+
*
|
35
|
+
* @returns An array containing an object with the error message for the "Too Many Requests" error.
|
36
|
+
*/
|
37
|
+
serializeErrors() {
|
38
|
+
return [
|
39
|
+
{ message: `Too Many Requests. Try again later` }
|
40
|
+
];
|
41
|
+
}
|
42
|
+
}
|
43
|
+
exports.TooManyRequestError = TooManyRequestError;
|
@@ -1,6 +1,8 @@
|
|
1
1
|
import { NatsConnection, ConsumerConfig, JsMsg } from "nats";
|
2
|
-
import { DurableName, Subjects, Streams } from "../common/enums";
|
3
2
|
import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
|
3
|
+
import { Subjects } from '../common/enums/subjects';
|
4
|
+
import { DurableName } from '../common/enums/durable-names';
|
5
|
+
import { Streams } from '../common/enums/streams';
|
4
6
|
/**
|
5
7
|
* Interface representing an event published to NATS.
|
6
8
|
*/
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { PubAck, NatsConnection } from "nats";
|
2
|
-
import { Subjects } from "../common/enums";
|
3
2
|
import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
|
3
|
+
import { Subjects } from '../common/enums/subjects';
|
4
4
|
/**
|
5
5
|
* Interface representing an event to be published to NATS.
|
6
6
|
*/
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { NatsConnection } from "nats";
|
2
|
-
import { Streams } from "../common/enums";
|
3
2
|
import { MainSubjects } from "../common/enums/subjects/main-subjects";
|
3
|
+
import { Streams } from '../common/enums/streams';
|
4
4
|
/**
|
5
5
|
* Interface representing an event to be used with NATS streams.
|
6
6
|
*/
|
package/build/index.d.ts
CHANGED
@@ -1,5 +1,173 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
/**
|
2
|
+
* Re-exports all the contents from the "redis-connection-wrapper" module.
|
3
|
+
*
|
4
|
+
* The "redis-connection-wrapper" module contains a class that wraps the Redis client connection and provides easy access to the Redis client instance.
|
5
|
+
* It also includes error handling and connection management features.
|
6
|
+
*/
|
7
|
+
export * from "./connections-wrappers/redis-connection-wrapper";
|
8
|
+
/**
|
9
|
+
* Re-exports all the contents from the "socket-connection-wrapper" module.
|
10
|
+
*
|
11
|
+
* The "socket-connection-wrapper" module contains a class that wraps the Socket.IO server connection and provides easy access to the Socket.IO server instance.
|
12
|
+
* It includes error handling and connection management features for Socket.IO server connections.
|
13
|
+
*/
|
14
|
+
export * from "./connections-wrappers/socket-connection-wrapper";
|
15
|
+
/**
|
16
|
+
* Re-exports all the contents from the "nats-wrapper" module.
|
17
|
+
*
|
18
|
+
* The "nats-wrapper" module contains a class that wraps the NATS (NATS Streaming Server) client connection and provides easy access to the NATS client instance.
|
19
|
+
* It includes error handling and connection management features for NATS client connections.
|
20
|
+
*/
|
21
|
+
export * from "./connections-wrappers/nats-wrapper";
|
22
|
+
/**
|
23
|
+
* Re-exports the "BadRequestError" class from the "bad-request-error" file.
|
24
|
+
*
|
25
|
+
* The "BadRequestError" class represents a specific "Bad Request" error and is used to handle client requests with invalid or malformed data.
|
26
|
+
*/
|
27
|
+
export * from "./errors/bad-request-error";
|
28
|
+
/**
|
29
|
+
* Re-exports the "CustomError" abstract class from the "custom-error" file.
|
30
|
+
*
|
31
|
+
* The "CustomError" abstract class serves as the foundation for creating specific error classes for different types of errors.
|
32
|
+
*/
|
33
|
+
export * from "./errors/custom-error";
|
34
|
+
/**
|
35
|
+
* Re-exports the "DatabaseError" class from the "database-error" file.
|
36
|
+
*
|
37
|
+
* The "DatabaseError" class represents a specific error related to database operations or connectivity issues.
|
38
|
+
*/
|
39
|
+
export * from "./errors/database-error";
|
40
|
+
/**
|
41
|
+
* Re-exports the "NotFoundError" class from the "not-found-error" file.
|
42
|
+
*
|
43
|
+
* The "NotFoundError" class represents a specific "Not Found" error and is used when a requested resource is not found.
|
44
|
+
*/
|
45
|
+
export * from "./errors/not-found-error";
|
46
|
+
/**
|
47
|
+
* Re-exports the "SystemError" class from the "system-error" file.
|
48
|
+
*
|
49
|
+
* The "SystemError" class represents a specific "System Error" that can occur due to unexpected issues in the system.
|
50
|
+
*/
|
51
|
+
export * from "./errors/system-error";
|
52
|
+
export * from "./errors/too-many-request-error";
|
53
|
+
export * from "./errors/resource-not-found";
|
54
|
+
export * from "./errors/not-authorized-error";
|
55
|
+
/**
|
56
|
+
* Re-exports the "currentUser" middleware from the "current-user" module.
|
57
|
+
*
|
58
|
+
* The "currentUser" middleware is used to extract and verify the current user's information from the request headers.
|
59
|
+
* It checks for a valid bearer token in the "Authorization" header, verifies the JWT token, and attaches the user payload to the request object for further processing in subsequent middleware or route handlers.
|
60
|
+
*/
|
61
|
+
export * from "./middlewares/current-user";
|
62
|
+
/**
|
63
|
+
* Re-exports the "errorHandler" middleware from the "error-handler" module.
|
64
|
+
*
|
65
|
+
* The "errorHandler" middleware is a custom error-handling middleware used to handle and respond to errors that occur during request processing.
|
66
|
+
* It intercepts any uncaught errors in the application, such as custom errors or other exceptions, and sends appropriate error responses to the client.
|
67
|
+
* If the error is a custom error (e.g., BadRequestError, NotFoundError, DatabaseError, etc.), the middleware serializes the error and sends it with the corresponding status code and error messages.
|
68
|
+
* If the error is not a custom error, a generic error response is sent with a 500 status code and a default error message.
|
69
|
+
*/
|
70
|
+
export * from "./middlewares/error-handler";
|
71
|
+
export * from "./middlewares/is-allowed";
|
72
|
+
export * from "./middlewares/validate-body";
|
73
|
+
export * from "./middlewares/require-auth";
|
74
|
+
/**
|
75
|
+
*? Re-exports all the enums from the "enums" module.
|
76
|
+
*
|
77
|
+
* The "enums" module contains various enumerations used in the application.
|
78
|
+
* Enumerations are used to define named constant values, such as status codes, subject names, or stream names.
|
79
|
+
*/
|
80
|
+
export * from "./utils/enums";
|
81
|
+
/**
|
82
|
+
*? Re-exports all the functions related to reading directories from the "read-dir" module.
|
83
|
+
*
|
84
|
+
* The "read-dir" module contains utility functions to read and access files and directories in the application.
|
85
|
+
* These functions are used for dynamic loading, configuration, and other file-related operations.
|
86
|
+
*/
|
87
|
+
export * from "./utils/read-dir";
|
88
|
+
export * from "./utils/schemas";
|
89
|
+
/**
|
90
|
+
*? Re-exports all the contents from the "base-producers" module.
|
91
|
+
*
|
92
|
+
* The "base-producers" module contains base classes and utilities for implementing job producers in the application.
|
93
|
+
* Job producers are responsible for generating and queuing jobs to be processed by job workers.
|
94
|
+
*/
|
95
|
+
export * from "./utils/base_classes/jobs/base-producers";
|
96
|
+
/**
|
97
|
+
*? Re-exports all the contents from the "base-worker" module.
|
98
|
+
*
|
99
|
+
* The "base-worker" module contains base classes and utilities for implementing job workers in the application.
|
100
|
+
* Job workers are responsible for processing jobs generated by job producers and executing the required tasks.
|
101
|
+
*/
|
102
|
+
export * from "./utils/base_classes/jobs/base-worker";
|
103
|
+
/**
|
104
|
+
*? Re-exports all the contents from the "base-namespace" module.
|
105
|
+
*
|
106
|
+
* The "base-namespace" module contains base classes and utilities for implementing custom namespaces in the application.
|
107
|
+
* Custom namespaces provide separate channels for communication between connected clients and the server, allowing for organized event handling within separate contexts.
|
108
|
+
*/
|
109
|
+
export * from "./utils/base_classes/listeners/base-namespace";
|
110
|
+
/**
|
111
|
+
*? Re-exports the "BaseConsumer" class from the "base-consumer" file.
|
112
|
+
*
|
113
|
+
* The "BaseConsumer" class serves as a base class for implementing event consumers.
|
114
|
+
* It provides common functionality and methods that can be extended by specific consumer implementations.
|
115
|
+
*/
|
116
|
+
export * from "./events/base/base-consumer";
|
117
|
+
/**
|
118
|
+
* Re-exports the "BasePublisher" class from the "base-publisher" file.
|
119
|
+
*
|
120
|
+
* The "BasePublisher" class serves as a base class for implementing event publishers.
|
121
|
+
* It provides common functionality and methods that can be extended by specific publisher implementations.
|
122
|
+
*/
|
123
|
+
export * from "./events/base/base-publisher";
|
124
|
+
/**
|
125
|
+
* Re-exports the "BaseStream" class from the "base-stream" file.
|
126
|
+
*
|
127
|
+
* The "BaseStream" class serves as a base class for implementing event streams.
|
128
|
+
* It provides common functionality and methods that can be extended by specific stream implementations.
|
129
|
+
*/
|
130
|
+
export * from "./events/base/base-stream";
|
131
|
+
/**
|
132
|
+
* Re-exports the "OrderCancelledEvent" class from the "order-cancelled-event" file.
|
133
|
+
*
|
134
|
+
* The "OrderCancelledEvent" class represents an event related to the cancellation of an order.
|
135
|
+
*/
|
136
|
+
export * from "./events/common/interfaces/order-events-interfaces/order-cancelled-event";
|
137
|
+
/**
|
138
|
+
* Re-exports the "OrderCompletedEvent" class from the "order-completed-event" file.
|
139
|
+
*
|
140
|
+
* The "OrderCompletedEvent" class represents an event related to the completion of an order.
|
141
|
+
*/
|
142
|
+
export * from "./events/common/interfaces/order-events-interfaces/order-completed-event";
|
143
|
+
/**
|
144
|
+
* Re-exports the "OrderRequestedEvent" class from the "order-requested-event" file.
|
145
|
+
*
|
146
|
+
* The "OrderRequestedEvent" class represents an event related to a request for an order.
|
147
|
+
*/
|
148
|
+
export * from "./events/common/interfaces/order-events-interfaces/order-requested-event";
|
149
|
+
/**
|
150
|
+
* Re-exports all the interfaces related to streams from the "stream-interfaces" file.
|
151
|
+
*
|
152
|
+
* The "stream-interfaces" file contains interfaces for various streams used in the application.
|
153
|
+
* For example, "OrderStreamEvent" represents an event associated with the "Order" stream.
|
154
|
+
*/
|
155
|
+
export * from "./events/common/interfaces/stream-interfaces/order-stream-interface";
|
156
|
+
/**
|
157
|
+
* Re-exports all the types related to durable names from the "durable-names" file.
|
158
|
+
*
|
159
|
+
* The types include enums and utility types used to define durable names for consumers of different event types.
|
160
|
+
*/
|
161
|
+
export * from "./events/common/enums/durable-names";
|
162
|
+
/**
|
163
|
+
* Re-exports the type related to streams from the "streams" file.
|
164
|
+
*
|
165
|
+
* The "Streams" type alias represents a JetStream stream that can handle different event types.
|
166
|
+
*/
|
167
|
+
export * from "./events/common/enums/streams";
|
168
|
+
/**
|
169
|
+
* Re-exports the type related to subjects from the "subjects" file.
|
170
|
+
*
|
171
|
+
* The "Subjects" type alias represents the subjects for different event types.
|
172
|
+
*/
|
173
|
+
export * from "./events/common/enums/subjects";
|
package/build/index.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
+
//* Import and re-export multiple modules related to connection wrappers.
|
2
3
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
4
|
if (k2 === undefined) k2 = k;
|
4
5
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
@@ -14,8 +15,191 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
15
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
16
|
};
|
16
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
/**
|
19
|
+
* Re-exports all the contents from the "redis-connection-wrapper" module.
|
20
|
+
*
|
21
|
+
* The "redis-connection-wrapper" module contains a class that wraps the Redis client connection and provides easy access to the Redis client instance.
|
22
|
+
* It also includes error handling and connection management features.
|
23
|
+
*/
|
24
|
+
__exportStar(require("./connections-wrappers/redis-connection-wrapper"), exports);
|
25
|
+
/**
|
26
|
+
* Re-exports all the contents from the "socket-connection-wrapper" module.
|
27
|
+
*
|
28
|
+
* The "socket-connection-wrapper" module contains a class that wraps the Socket.IO server connection and provides easy access to the Socket.IO server instance.
|
29
|
+
* It includes error handling and connection management features for Socket.IO server connections.
|
30
|
+
*/
|
31
|
+
__exportStar(require("./connections-wrappers/socket-connection-wrapper"), exports);
|
32
|
+
/**
|
33
|
+
* Re-exports all the contents from the "nats-wrapper" module.
|
34
|
+
*
|
35
|
+
* The "nats-wrapper" module contains a class that wraps the NATS (NATS Streaming Server) client connection and provides easy access to the NATS client instance.
|
36
|
+
* It includes error handling and connection management features for NATS client connections.
|
37
|
+
*/
|
38
|
+
__exportStar(require("./connections-wrappers/nats-wrapper"), exports);
|
39
|
+
//* Import and re-export custom error classes for easier access.
|
40
|
+
/**
|
41
|
+
* Re-exports the "BadRequestError" class from the "bad-request-error" file.
|
42
|
+
*
|
43
|
+
* The "BadRequestError" class represents a specific "Bad Request" error and is used to handle client requests with invalid or malformed data.
|
44
|
+
*/
|
45
|
+
__exportStar(require("./errors/bad-request-error"), exports);
|
46
|
+
/**
|
47
|
+
* Re-exports the "CustomError" abstract class from the "custom-error" file.
|
48
|
+
*
|
49
|
+
* The "CustomError" abstract class serves as the foundation for creating specific error classes for different types of errors.
|
50
|
+
*/
|
51
|
+
__exportStar(require("./errors/custom-error"), exports);
|
52
|
+
/**
|
53
|
+
* Re-exports the "DatabaseError" class from the "database-error" file.
|
54
|
+
*
|
55
|
+
* The "DatabaseError" class represents a specific error related to database operations or connectivity issues.
|
56
|
+
*/
|
57
|
+
__exportStar(require("./errors/database-error"), exports);
|
58
|
+
/**
|
59
|
+
* Re-exports the "NotFoundError" class from the "not-found-error" file.
|
60
|
+
*
|
61
|
+
* The "NotFoundError" class represents a specific "Not Found" error and is used when a requested resource is not found.
|
62
|
+
*/
|
63
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
64
|
+
/**
|
65
|
+
* Re-exports the "SystemError" class from the "system-error" file.
|
66
|
+
*
|
67
|
+
* The "SystemError" class represents a specific "System Error" that can occur due to unexpected issues in the system.
|
68
|
+
*/
|
69
|
+
__exportStar(require("./errors/system-error"), exports);
|
70
|
+
//TODO: DOCUMENT
|
71
|
+
__exportStar(require("./errors/too-many-request-error"), exports);
|
72
|
+
//TODO: DOCUMENT
|
73
|
+
__exportStar(require("./errors/resource-not-found"), exports);
|
74
|
+
//TODO: DOCUMENT
|
75
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
76
|
+
//* Import and re-export multiple middleware modules.
|
77
|
+
/**
|
78
|
+
* Re-exports the "currentUser" middleware from the "current-user" module.
|
79
|
+
*
|
80
|
+
* The "currentUser" middleware is used to extract and verify the current user's information from the request headers.
|
81
|
+
* It checks for a valid bearer token in the "Authorization" header, verifies the JWT token, and attaches the user payload to the request object for further processing in subsequent middleware or route handlers.
|
82
|
+
*/
|
83
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
84
|
+
/**
|
85
|
+
* Re-exports the "errorHandler" middleware from the "error-handler" module.
|
86
|
+
*
|
87
|
+
* The "errorHandler" middleware is a custom error-handling middleware used to handle and respond to errors that occur during request processing.
|
88
|
+
* It intercepts any uncaught errors in the application, such as custom errors or other exceptions, and sends appropriate error responses to the client.
|
89
|
+
* If the error is a custom error (e.g., BadRequestError, NotFoundError, DatabaseError, etc.), the middleware serializes the error and sends it with the corresponding status code and error messages.
|
90
|
+
* If the error is not a custom error, a generic error response is sent with a 500 status code and a default error message.
|
91
|
+
*/
|
92
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
93
|
+
//TODO: DOCUMENT
|
94
|
+
__exportStar(require("./middlewares/is-allowed"), exports);
|
95
|
+
//TODO: DOCUMENT
|
96
|
+
__exportStar(require("./middlewares/validate-body"), exports);
|
97
|
+
//TODO: DOCUMENT
|
98
|
+
__exportStar(require("./middlewares/require-auth"), exports);
|
99
|
+
//* Import and re-export multiple modules for enums, base classes, and read-dir functionality.
|
100
|
+
/**
|
101
|
+
*? Re-exports all the enums from the "enums" module.
|
102
|
+
*
|
103
|
+
* The "enums" module contains various enumerations used in the application.
|
104
|
+
* Enumerations are used to define named constant values, such as status codes, subject names, or stream names.
|
105
|
+
*/
|
106
|
+
__exportStar(require("./utils/enums"), exports);
|
107
|
+
/**
|
108
|
+
*? Re-exports all the functions related to reading directories from the "read-dir" module.
|
109
|
+
*
|
110
|
+
* The "read-dir" module contains utility functions to read and access files and directories in the application.
|
111
|
+
* These functions are used for dynamic loading, configuration, and other file-related operations.
|
112
|
+
*/
|
113
|
+
__exportStar(require("./utils/read-dir"), exports);
|
114
|
+
//TODO: DOCUMENT
|
115
|
+
__exportStar(require("./utils/schemas"), exports);
|
116
|
+
/**
|
117
|
+
*? Re-exports all the contents from the "base-producers" module.
|
118
|
+
*
|
119
|
+
* The "base-producers" module contains base classes and utilities for implementing job producers in the application.
|
120
|
+
* Job producers are responsible for generating and queuing jobs to be processed by job workers.
|
121
|
+
*/
|
122
|
+
__exportStar(require("./utils/base_classes/jobs/base-producers"), exports);
|
123
|
+
/**
|
124
|
+
*? Re-exports all the contents from the "base-worker" module.
|
125
|
+
*
|
126
|
+
* The "base-worker" module contains base classes and utilities for implementing job workers in the application.
|
127
|
+
* Job workers are responsible for processing jobs generated by job producers and executing the required tasks.
|
128
|
+
*/
|
129
|
+
__exportStar(require("./utils/base_classes/jobs/base-worker"), exports);
|
130
|
+
/**
|
131
|
+
*? Re-exports all the contents from the "base-namespace" module.
|
132
|
+
*
|
133
|
+
* The "base-namespace" module contains base classes and utilities for implementing custom namespaces in the application.
|
134
|
+
* Custom namespaces provide separate channels for communication between connected clients and the server, allowing for organized event handling within separate contexts.
|
135
|
+
*/
|
136
|
+
__exportStar(require("./utils/base_classes/listeners/base-namespace"), exports);
|
137
|
+
//* Import and re-export base classes for consumers, publishers, and streams.
|
138
|
+
/**
|
139
|
+
*? Re-exports the "BaseConsumer" class from the "base-consumer" file.
|
140
|
+
*
|
141
|
+
* The "BaseConsumer" class serves as a base class for implementing event consumers.
|
142
|
+
* It provides common functionality and methods that can be extended by specific consumer implementations.
|
143
|
+
*/
|
144
|
+
__exportStar(require("./events/base/base-consumer"), exports);
|
145
|
+
/**
|
146
|
+
* Re-exports the "BasePublisher" class from the "base-publisher" file.
|
147
|
+
*
|
148
|
+
* The "BasePublisher" class serves as a base class for implementing event publishers.
|
149
|
+
* It provides common functionality and methods that can be extended by specific publisher implementations.
|
150
|
+
*/
|
151
|
+
__exportStar(require("./events/base/base-publisher"), exports);
|
152
|
+
/**
|
153
|
+
* Re-exports the "BaseStream" class from the "base-stream" file.
|
154
|
+
*
|
155
|
+
* The "BaseStream" class serves as a base class for implementing event streams.
|
156
|
+
* It provides common functionality and methods that can be extended by specific stream implementations.
|
157
|
+
*/
|
158
|
+
__exportStar(require("./events/base/base-stream"), exports);
|
159
|
+
//* Import and re-export multiple interfaces related to different events and streams.
|
160
|
+
/**
|
161
|
+
* Re-exports the "OrderCancelledEvent" class from the "order-cancelled-event" file.
|
162
|
+
*
|
163
|
+
* The "OrderCancelledEvent" class represents an event related to the cancellation of an order.
|
164
|
+
*/
|
165
|
+
__exportStar(require("./events/common/interfaces/order-events-interfaces/order-cancelled-event"), exports);
|
166
|
+
/**
|
167
|
+
* Re-exports the "OrderCompletedEvent" class from the "order-completed-event" file.
|
168
|
+
*
|
169
|
+
* The "OrderCompletedEvent" class represents an event related to the completion of an order.
|
170
|
+
*/
|
171
|
+
__exportStar(require("./events/common/interfaces/order-events-interfaces/order-completed-event"), exports);
|
172
|
+
/**
|
173
|
+
* Re-exports the "OrderRequestedEvent" class from the "order-requested-event" file.
|
174
|
+
*
|
175
|
+
* The "OrderRequestedEvent" class represents an event related to a request for an order.
|
176
|
+
*/
|
177
|
+
__exportStar(require("./events/common/interfaces/order-events-interfaces/order-requested-event"), exports);
|
178
|
+
//! Interfaces related to payment events are currently commented out using '//'.
|
179
|
+
// export * from "./payment-event-interfaces";
|
180
|
+
/**
|
181
|
+
* Re-exports all the interfaces related to streams from the "stream-interfaces" file.
|
182
|
+
*
|
183
|
+
* The "stream-interfaces" file contains interfaces for various streams used in the application.
|
184
|
+
* For example, "OrderStreamEvent" represents an event associated with the "Order" stream.
|
185
|
+
*/
|
186
|
+
__exportStar(require("./events/common/interfaces/stream-interfaces/order-stream-interface"), exports);
|
187
|
+
//* Import and re-export types from other files in events enums for easier access.
|
188
|
+
/**
|
189
|
+
* Re-exports all the types related to durable names from the "durable-names" file.
|
190
|
+
*
|
191
|
+
* The types include enums and utility types used to define durable names for consumers of different event types.
|
192
|
+
*/
|
193
|
+
__exportStar(require("./events/common/enums/durable-names"), exports);
|
194
|
+
/**
|
195
|
+
* Re-exports the type related to streams from the "streams" file.
|
196
|
+
*
|
197
|
+
* The "Streams" type alias represents a JetStream stream that can handle different event types.
|
198
|
+
*/
|
199
|
+
__exportStar(require("./events/common/enums/streams"), exports);
|
200
|
+
/**
|
201
|
+
* Re-exports the type related to subjects from the "subjects" file.
|
202
|
+
*
|
203
|
+
* The "Subjects" type alias represents the subjects for different event types.
|
204
|
+
*/
|
205
|
+
__exportStar(require("./events/common/enums/subjects"), exports);
|
@@ -64,7 +64,6 @@ const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, functi
|
|
64
64
|
// If the token is expired, you may consider refreshing the token or taking appropriate actions.
|
65
65
|
// Uncomment the following line if you want to handle token expiration using the AuthController.
|
66
66
|
// await AuthController.refreshToken(req, res);
|
67
|
-
// await AuthController.refreshToken(req, res)
|
68
67
|
}
|
69
68
|
else {
|
70
69
|
// If any other error occurs during token verification, set currentUser to null.
|
@@ -15,8 +15,6 @@ const errorHandler = (err, req, res, next) => {
|
|
15
15
|
if (err instanceof custom_error_1.CustomError) {
|
16
16
|
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
17
17
|
}
|
18
|
-
res
|
19
|
-
.status(enums_1.StatusCodes.BAD_REQUEST)
|
20
|
-
.send({ errors: [{ message: "Something went wrong" }] });
|
18
|
+
res.status(enums_1.StatusCodes.BAD_REQUEST).send({ errors: [{ message: "Something went wrong" }] });
|
21
19
|
};
|
22
20
|
exports.errorHandler = errorHandler;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
2
|
+
/**
|
3
|
+
* Middleware to check if a user is allowed to perform a specific action on a resource based on their role and permissions.
|
4
|
+
* If the user is not authorized, it throws a NotAuthorizedError to indicate access denied.
|
5
|
+
*
|
6
|
+
* @param req - The Express request object.
|
7
|
+
* @param res - The Express response object.
|
8
|
+
* @param next - The next middleware function.
|
9
|
+
* @param hasVersion - Set to true if the API path includes the version (e.g., /api/v1/...). Default is false.
|
10
|
+
*
|
11
|
+
* @throws BadRequestError - If the action is not supported or if the session has expired.
|
12
|
+
* @throws ResourceNotFound - If the requested resource is not found.
|
13
|
+
* @throws NotAuthorizedError - If the user is not authorized to perform the action on the resource.
|
14
|
+
*
|
15
|
+
* @example
|
16
|
+
* ```js
|
17
|
+
* // Route that requires authorization based on user role and permissions
|
18
|
+
* router.get("/resource/:id", isAllowed, (req, res) => {
|
19
|
+
* // Only authorized users with the appropriate permissions can access this route
|
20
|
+
* res.send("You are authorized!");
|
21
|
+
* });
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare const isAllowed: (req: Request, res: Response, next: NextFunction, hasVersion?: boolean) => void;
|
@@ -0,0 +1,71 @@
|
|
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.isAllowed = void 0;
|
7
|
+
const accesscontrol_1 = __importDefault(require("@vsky/accesscontrol"));
|
8
|
+
const resource_not_found_1 = require("../errors/resource-not-found");
|
9
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
10
|
+
const bad_request_error_1 = require("../errors/bad-request-error");
|
11
|
+
// Mapping object for HTTP method to action
|
12
|
+
const methodToAction = {
|
13
|
+
GET: "view",
|
14
|
+
POST: "create",
|
15
|
+
DELETE: "delete",
|
16
|
+
PUT: "update",
|
17
|
+
PATCH: "update",
|
18
|
+
};
|
19
|
+
/**
|
20
|
+
* Middleware to check if a user is allowed to perform a specific action on a resource based on their role and permissions.
|
21
|
+
* If the user is not authorized, it throws a NotAuthorizedError to indicate access denied.
|
22
|
+
*
|
23
|
+
* @param req - The Express request object.
|
24
|
+
* @param res - The Express response object.
|
25
|
+
* @param next - The next middleware function.
|
26
|
+
* @param hasVersion - Set to true if the API path includes the version (e.g., /api/v1/...). Default is false.
|
27
|
+
*
|
28
|
+
* @throws BadRequestError - If the action is not supported or if the session has expired.
|
29
|
+
* @throws ResourceNotFound - If the requested resource is not found.
|
30
|
+
* @throws NotAuthorizedError - If the user is not authorized to perform the action on the resource.
|
31
|
+
*
|
32
|
+
* @example
|
33
|
+
* ```js
|
34
|
+
* // Route that requires authorization based on user role and permissions
|
35
|
+
* router.get("/resource/:id", isAllowed, (req, res) => {
|
36
|
+
* // Only authorized users with the appropriate permissions can access this route
|
37
|
+
* res.send("You are authorized!");
|
38
|
+
* });
|
39
|
+
* ```
|
40
|
+
*/
|
41
|
+
const isAllowed = (req, res, next, hasVersion = false) => {
|
42
|
+
var _a, _b;
|
43
|
+
let STABLEVERSION = `${process.env.STABLE_VERSION}`;
|
44
|
+
if (!STABLEVERSION) {
|
45
|
+
STABLEVERSION = "v1";
|
46
|
+
}
|
47
|
+
// Regular expression to match the route path and extract the resource
|
48
|
+
const regexString = `^\\/api\\/${hasVersion ? `${STABLEVERSION}\\/` : ""}(\\w+)(\\/.*)?$`;
|
49
|
+
const regex = new RegExp(regexString);
|
50
|
+
const path = req.originalUrl;
|
51
|
+
const match = path.match(regex);
|
52
|
+
const resource = match && match[1];
|
53
|
+
// Determine the action based on the HTTP method
|
54
|
+
const action = methodToAction[req.method.toUpperCase()];
|
55
|
+
if (!action) {
|
56
|
+
throw new bad_request_error_1.BadRequestError("Session expired. Please relogin.");
|
57
|
+
}
|
58
|
+
if (!resource) {
|
59
|
+
throw new resource_not_found_1.ResourceNotFound();
|
60
|
+
}
|
61
|
+
// Get the roles and role of the current user from the session
|
62
|
+
const roles = ((_a = req.currentUser) === null || _a === void 0 ? void 0 : _a.role_permission) || [];
|
63
|
+
const role = (_b = req.currentUser) === null || _b === void 0 ? void 0 : _b.role;
|
64
|
+
// Check if the user can perform the action on the resource
|
65
|
+
const canPerform = new accesscontrol_1.default(roles).canPerformAction(role, resource, action);
|
66
|
+
if (!canPerform) {
|
67
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
68
|
+
}
|
69
|
+
next();
|
70
|
+
};
|
71
|
+
exports.isAllowed = isAllowed;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
2
|
+
/**
|
3
|
+
* Middleware to check if a user is authenticated before allowing access to certain routes.
|
4
|
+
* If the user is not authenticated, it throws a NotAuthorizedError to indicate access denied.
|
5
|
+
* @param req The Express request object.
|
6
|
+
* @param res The Express response object.
|
7
|
+
* @param next The next middleware function.
|
8
|
+
* @throws NotAuthorizedError if the user is not authenticated.
|
9
|
+
* @example
|
10
|
+
* ```js
|
11
|
+
// Route that requires authentication
|
12
|
+
router.get('/protected', requireAuth, (req, res) => {
|
13
|
+
// Only authenticated users can access this route
|
14
|
+
res.send('You are authorized!');
|
15
|
+
});
|
16
|
+
* ```
|
17
|
+
*/
|
18
|
+
export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.requireAuth = void 0;
|
4
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
5
|
+
/**
|
6
|
+
* Middleware to check if a user is authenticated before allowing access to certain routes.
|
7
|
+
* If the user is not authenticated, it throws a NotAuthorizedError to indicate access denied.
|
8
|
+
* @param req The Express request object.
|
9
|
+
* @param res The Express response object.
|
10
|
+
* @param next The next middleware function.
|
11
|
+
* @throws NotAuthorizedError if the user is not authenticated.
|
12
|
+
* @example
|
13
|
+
* ```js
|
14
|
+
// Route that requires authentication
|
15
|
+
router.get('/protected', requireAuth, (req, res) => {
|
16
|
+
// Only authenticated users can access this route
|
17
|
+
res.send('You are authorized!');
|
18
|
+
});
|
19
|
+
* ```
|
20
|
+
*/
|
21
|
+
const requireAuth = (req, res, next) => {
|
22
|
+
if (!req.currentUser) {
|
23
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
24
|
+
}
|
25
|
+
next();
|
26
|
+
};
|
27
|
+
exports.requireAuth = requireAuth;
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
2
|
+
/**
|
3
|
+
* Represents the required fields for each HTTP method.
|
4
|
+
* MethodRequiredProps is a mapping between HTTP methods ("POST", "DELETE", "PATCH", "PUT", "GET")
|
5
|
+
* and an object containing an array of required fields for that method.
|
6
|
+
*/
|
7
|
+
type MethodRequiredProps = {
|
8
|
+
[key in "POST" | "DELETE" | "PATCH" | "PUT" | "GET"]: {
|
9
|
+
required: string[];
|
10
|
+
};
|
11
|
+
};
|
12
|
+
/**
|
13
|
+
* Represents the schema for validating request body parameters.
|
14
|
+
* - `fields`: An object mapping each parameter name to its expected type.
|
15
|
+
* - `required`: An optional array of parameter names that are required in the request body.
|
16
|
+
* - `optional`: An optional array of parameter names that are allowed but not required in the request body.
|
17
|
+
*/
|
18
|
+
export type Schema = {
|
19
|
+
fields: {
|
20
|
+
[key: string]: string;
|
21
|
+
};
|
22
|
+
required?: string[] | MethodRequiredProps;
|
23
|
+
optional?: string[];
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* Class providing request body validation middleware.
|
27
|
+
*/
|
28
|
+
export declare class RequestValidator {
|
29
|
+
private model;
|
30
|
+
/**
|
31
|
+
* Constructor for the RequestValidator class.
|
32
|
+
* @param model The schema defining the expected fields and types in the request body.
|
33
|
+
*/
|
34
|
+
constructor(model: Schema);
|
35
|
+
/**
|
36
|
+
* Private method to validate required fields in the request body.
|
37
|
+
* @param obj The request body object.
|
38
|
+
* @param required The required fields or required fields for specific HTTP methods.
|
39
|
+
* @param method The HTTP method of the request.
|
40
|
+
* @returns An object containing the validation status and the element that caused the validation failure (if any).
|
41
|
+
*/
|
42
|
+
private required;
|
43
|
+
/**
|
44
|
+
* Private method to check if the fields in the request body match the defined schema.
|
45
|
+
* @param obj The request body object.
|
46
|
+
*/
|
47
|
+
private check;
|
48
|
+
/**
|
49
|
+
* Middleware function to validate the request body against the provided schema.
|
50
|
+
* @param req The Express request object.
|
51
|
+
* @param res The Express response object.
|
52
|
+
* @param next The next middleware function.
|
53
|
+
* @throws BadRequestError if any validation fails.
|
54
|
+
*/
|
55
|
+
validateReqBody: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
56
|
+
}
|
57
|
+
export {};
|
@@ -0,0 +1,102 @@
|
|
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.RequestValidator = void 0;
|
13
|
+
const bad_request_error_1 = require("../errors/bad-request-error");
|
14
|
+
/**
|
15
|
+
* Class providing request body validation middleware.
|
16
|
+
*/
|
17
|
+
class RequestValidator {
|
18
|
+
/**
|
19
|
+
* Constructor for the RequestValidator class.
|
20
|
+
* @param model The schema defining the expected fields and types in the request body.
|
21
|
+
*/
|
22
|
+
constructor(model) {
|
23
|
+
/**
|
24
|
+
* Middleware function to validate the request body against the provided schema.
|
25
|
+
* @param req The Express request object.
|
26
|
+
* @param res The Express response object.
|
27
|
+
* @param next The next middleware function.
|
28
|
+
* @throws BadRequestError if any validation fails.
|
29
|
+
*/
|
30
|
+
this.validateReqBody = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
31
|
+
const paramsLength = Object.keys(req.params).length;
|
32
|
+
const bodyLength = Object.keys(req.body).length;
|
33
|
+
const method = req.method;
|
34
|
+
const methods = ["POST", "PUT", "DELETE", "PATCH", "GET"];
|
35
|
+
const obj = paramsLength > 0 && bodyLength > 0 ? Object.assign(Object.assign({}, req.params), req.body) : bodyLength > 0 ? req.body : req.params;
|
36
|
+
if (methods.includes(method)) {
|
37
|
+
if (this.model.required) {
|
38
|
+
const { status, element } = this.required(obj, this.model.required, method);
|
39
|
+
if (!status) {
|
40
|
+
throw new bad_request_error_1.BadRequestError(`Required field: ${element}`);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
if (!obj.multiadd) {
|
45
|
+
this.check(obj);
|
46
|
+
}
|
47
|
+
else {
|
48
|
+
for (const element of obj.multiadd) {
|
49
|
+
this.check(element);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
next();
|
53
|
+
});
|
54
|
+
this.model = model;
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* Private method to validate required fields in the request body.
|
58
|
+
* @param obj The request body object.
|
59
|
+
* @param required The required fields or required fields for specific HTTP methods.
|
60
|
+
* @param method The HTTP method of the request.
|
61
|
+
* @returns An object containing the validation status and the element that caused the validation failure (if any).
|
62
|
+
*/
|
63
|
+
required(obj, required, method) {
|
64
|
+
const isArray = Array.isArray(required);
|
65
|
+
if (!obj.multiadd) {
|
66
|
+
const data = !isArray ? required[method]['required'] : required;
|
67
|
+
for (const requiredKey of data) {
|
68
|
+
if (!obj[requiredKey] || (typeof obj[requiredKey] === 'string' && !obj[requiredKey].trim())) {
|
69
|
+
return { status: false, element: requiredKey };
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
else {
|
74
|
+
if (isArray) {
|
75
|
+
for (const requiredKey of required) {
|
76
|
+
for (const element of obj.multiadd) {
|
77
|
+
if (!element[requiredKey]) {
|
78
|
+
return { status: false, element: requiredKey };
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
return { status: true, element: '' };
|
85
|
+
}
|
86
|
+
/**
|
87
|
+
* Private method to check if the fields in the request body match the defined schema.
|
88
|
+
* @param obj The request body object.
|
89
|
+
*/
|
90
|
+
check(obj) {
|
91
|
+
var _a, _b;
|
92
|
+
for (const key of Object.keys(obj)) {
|
93
|
+
if (!this.model.fields[key] && !((_a = this.model.optional) === null || _a === void 0 ? void 0 : _a.includes(key))) {
|
94
|
+
throw new bad_request_error_1.BadRequestError(`Invalid parameter: ${key}`);
|
95
|
+
}
|
96
|
+
if (typeof obj[key] !== this.model.fields[key] && !((_b = this.model.optional) === null || _b === void 0 ? void 0 : _b.includes(key))) {
|
97
|
+
throw new bad_request_error_1.BadRequestError(`Parameter type mismatch: ${key}`);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
exports.RequestValidator = RequestValidator;
|
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.AbstractJobQueue = void 0;
|
4
4
|
const bullmq_1 = require("bullmq");
|
5
|
-
const
|
5
|
+
const redis_connection_wrapper_1 = require("../../../connections-wrappers/redis-connection-wrapper");
|
6
6
|
/**
|
7
7
|
* Abstract class representing a Job Queue.
|
8
8
|
*/
|
@@ -14,7 +14,7 @@ class AbstractJobQueue {
|
|
14
14
|
*/
|
15
15
|
constructor(queueName) {
|
16
16
|
// Initialize the job queue with the provided name and a Redis connection from the redisWrapper.
|
17
|
-
this.que = new bullmq_1.Queue(queueName, { connection:
|
17
|
+
this.que = new bullmq_1.Queue(queueName, { connection: redis_connection_wrapper_1.redisWrapper.client });
|
18
18
|
}
|
19
19
|
}
|
20
20
|
exports.AbstractJobQueue = AbstractJobQueue;
|
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.AbstractWorker = void 0;
|
4
4
|
const bullmq_1 = require("bullmq");
|
5
|
-
const
|
5
|
+
const redis_connection_wrapper_1 = require("../../../connections-wrappers/redis-connection-wrapper");
|
6
6
|
/**
|
7
7
|
* Abstract class representing a worker for processing jobs from a Job Queue.
|
8
8
|
*/
|
@@ -15,7 +15,7 @@ class AbstractWorker {
|
|
15
15
|
constructor(queueName) {
|
16
16
|
// Create a worker instance with the provided queueName and bind the 'process' method to this worker.
|
17
17
|
this.worker = new bullmq_1.Worker(queueName, this.process.bind(this), {
|
18
|
-
connection:
|
18
|
+
connection: redis_connection_wrapper_1.redisWrapper.client,
|
19
19
|
autorun: false,
|
20
20
|
});
|
21
21
|
// Handle errors that may occur during job processing.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { Schema } from '../middlewares/validate-body';
|
2
|
+
export declare const RegisterSchema: Schema;
|
3
|
+
export declare const SigninSchema: Schema;
|
4
|
+
export declare const BranchSchema: Schema;
|
5
|
+
export declare const DenominationSchema: Schema;
|
6
|
+
export declare const CategorySchema: Schema;
|
7
|
+
export declare const WardSchema: Schema;
|
8
|
+
export declare const UserTypeSchema: Schema;
|
9
|
+
export declare const DelegateSchema: Schema;
|
10
|
+
export declare const CandidateSchema: Schema;
|
11
|
+
export declare const UserSchema: Schema;
|
@@ -0,0 +1,162 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UserSchema = exports.CandidateSchema = exports.DelegateSchema = exports.UserTypeSchema = exports.WardSchema = exports.CategorySchema = exports.DenominationSchema = exports.BranchSchema = exports.SigninSchema = exports.RegisterSchema = void 0;
|
4
|
+
exports.RegisterSchema = {
|
5
|
+
fields: {
|
6
|
+
user_name: 'string',
|
7
|
+
user_type: 'string',
|
8
|
+
password: 'string',
|
9
|
+
email: 'string',
|
10
|
+
contact: 'string',
|
11
|
+
address: 'string',
|
12
|
+
},
|
13
|
+
required: ['user_name', 'user_type', 'password']
|
14
|
+
};
|
15
|
+
exports.SigninSchema = {
|
16
|
+
fields: {
|
17
|
+
password: 'string',
|
18
|
+
user_name: 'string',
|
19
|
+
},
|
20
|
+
required: ['password', 'user_name']
|
21
|
+
};
|
22
|
+
exports.BranchSchema = {
|
23
|
+
fields: {
|
24
|
+
id: 'string',
|
25
|
+
name: 'string',
|
26
|
+
ward_id: 'string',
|
27
|
+
include: 'string',
|
28
|
+
code: 'string',
|
29
|
+
status: 'boolean',
|
30
|
+
},
|
31
|
+
required: {
|
32
|
+
'POST': { required: ['name', 'status', 'ward_id', 'code'] },
|
33
|
+
'PUT': { required: ['id', 'ward_id'] },
|
34
|
+
'PATCH': { required: [] },
|
35
|
+
'DELETE': { required: ['id'] },
|
36
|
+
'GET': { required: ['id'] },
|
37
|
+
},
|
38
|
+
optional: ['include', 'ward_id']
|
39
|
+
};
|
40
|
+
exports.DenominationSchema = {
|
41
|
+
fields: {
|
42
|
+
id: 'string',
|
43
|
+
include: 'string',
|
44
|
+
name: 'string',
|
45
|
+
status: 'boolean',
|
46
|
+
},
|
47
|
+
required: {
|
48
|
+
'POST': { required: ['name', 'status'] },
|
49
|
+
'PUT': { required: ['id'] },
|
50
|
+
'PATCH': { required: [] },
|
51
|
+
'DELETE': { required: ['id'] },
|
52
|
+
'GET': { required: ['id'] },
|
53
|
+
},
|
54
|
+
optional: ['include']
|
55
|
+
};
|
56
|
+
exports.CategorySchema = {
|
57
|
+
fields: {
|
58
|
+
id: 'string',
|
59
|
+
name: 'string',
|
60
|
+
status: 'boolean',
|
61
|
+
},
|
62
|
+
required: {
|
63
|
+
'POST': { required: ['name', 'status'] },
|
64
|
+
'PUT': { required: ['id'] },
|
65
|
+
'PATCH': { required: [] },
|
66
|
+
'DELETE': { required: ['id'] },
|
67
|
+
'GET': { required: ['id'] },
|
68
|
+
}
|
69
|
+
};
|
70
|
+
exports.WardSchema = {
|
71
|
+
fields: {
|
72
|
+
id: 'string',
|
73
|
+
name: 'string',
|
74
|
+
status: 'boolean',
|
75
|
+
},
|
76
|
+
required: {
|
77
|
+
'POST': { required: ['name', 'status'] },
|
78
|
+
'PUT': { required: ['id'] },
|
79
|
+
'PATCH': { required: [] },
|
80
|
+
'DELETE': { required: ['id'] },
|
81
|
+
'GET': { required: ['id'] },
|
82
|
+
}
|
83
|
+
};
|
84
|
+
exports.UserTypeSchema = {
|
85
|
+
fields: {
|
86
|
+
id: 'string',
|
87
|
+
name: 'string',
|
88
|
+
status: 'boolean',
|
89
|
+
},
|
90
|
+
required: {
|
91
|
+
'POST': { required: ['name', 'status'] },
|
92
|
+
'PUT': { required: ['id'] },
|
93
|
+
'PATCH': { required: [] },
|
94
|
+
'DELETE': { required: ['id'] },
|
95
|
+
'GET': { required: ['id'] },
|
96
|
+
}
|
97
|
+
};
|
98
|
+
exports.DelegateSchema = {
|
99
|
+
fields: {
|
100
|
+
id: 'string',
|
101
|
+
include: 'string',
|
102
|
+
name: 'string',
|
103
|
+
position: 'string',
|
104
|
+
voter_id_nnumber: 'string',
|
105
|
+
membership_number: 'string',
|
106
|
+
contact: 'string',
|
107
|
+
denomination_id: 'string',
|
108
|
+
status: 'boolean',
|
109
|
+
branch_id: 'string',
|
110
|
+
category_id: 'string',
|
111
|
+
is_potential_for: 'string',
|
112
|
+
multi_update: 'boolean'
|
113
|
+
},
|
114
|
+
required: {
|
115
|
+
'POST': { required: ['name', 'status', 'category_id'] },
|
116
|
+
'PUT': { required: ['id'] },
|
117
|
+
'PATCH': { required: [] },
|
118
|
+
'DELETE': { required: ['id'] },
|
119
|
+
'GET': { required: ['id'] },
|
120
|
+
},
|
121
|
+
optional: ['include']
|
122
|
+
};
|
123
|
+
exports.CandidateSchema = {
|
124
|
+
fields: {
|
125
|
+
id: 'string',
|
126
|
+
include: 'string',
|
127
|
+
name: 'string',
|
128
|
+
position: 'string',
|
129
|
+
voter_id_nnumber: 'string',
|
130
|
+
membership_number: 'string',
|
131
|
+
contact: 'string',
|
132
|
+
address: 'string',
|
133
|
+
denomination: 'string',
|
134
|
+
email: 'string',
|
135
|
+
status: 'boolean',
|
136
|
+
branch_id: 'string',
|
137
|
+
delegate_id: 'string',
|
138
|
+
},
|
139
|
+
required: {
|
140
|
+
'POST': { required: ['name', 'status', 'branch_id'] },
|
141
|
+
'PUT': { required: ['id'] },
|
142
|
+
'PATCH': { required: [] },
|
143
|
+
'DELETE': { required: ['id'] },
|
144
|
+
'GET': { required: ['id'] },
|
145
|
+
},
|
146
|
+
optional: ['include']
|
147
|
+
};
|
148
|
+
exports.UserSchema = {
|
149
|
+
fields: {
|
150
|
+
role: 'string',
|
151
|
+
password: 'string',
|
152
|
+
password_tries: 'number',
|
153
|
+
status: 'number',
|
154
|
+
ip_address: 'string',
|
155
|
+
employee_id: 'string',
|
156
|
+
company_id: 'string',
|
157
|
+
branch_id: 'string',
|
158
|
+
multiadd: 'object'
|
159
|
+
},
|
160
|
+
required: ['role', 'password', 'employee_id', 'company_id'],
|
161
|
+
optional: ['branch_id', 'multiadd']
|
162
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hastehaul/common",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.18",
|
4
4
|
"description": "",
|
5
5
|
"main": "./build/index.js",
|
6
6
|
"types": "./build/index.d.ts",
|
@@ -23,6 +23,7 @@
|
|
23
23
|
"typescript": "^5.1.6"
|
24
24
|
},
|
25
25
|
"dependencies": {
|
26
|
+
"@vsky/accesscontrol": "^3.0.14",
|
26
27
|
"bullmq": "^4.6.3",
|
27
28
|
"express": "^4.18.2",
|
28
29
|
"ioredis": "^5.3.2",
|