@hastehaul/common 1.0.13 → 1.0.15
Sign up to get free protection for your applications and to get access to all the features.
- package/build/connections-wrappers/nats-wrapper.d.ts +19 -0
- package/build/connections-wrappers/nats-wrapper.js +19 -0
- package/build/errors/bad-request-error.d.ts +24 -2
- package/build/errors/bad-request-error.js +22 -0
- package/build/errors/custom-error.d.ts +24 -0
- package/build/errors/custom-error.js +11 -0
- package/build/errors/database-error.d.ts +24 -2
- package/build/errors/database-error.js +23 -1
- package/build/errors/index.d.ts +25 -0
- package/build/errors/index.js +26 -0
- package/build/errors/not-found-error.d.ts +24 -2
- package/build/errors/not-found-error.js +22 -0
- package/build/errors/system-error.d.ts +25 -2
- package/build/errors/system-error.js +24 -1
- package/build/events/base/base-consumer.d.ts +57 -0
- package/build/events/base/base-consumer.js +33 -0
- package/build/events/base/base-publisher.d.ts +19 -0
- package/build/events/base/base-publisher.js +13 -0
- package/build/events/base/base-stream.d.ts +21 -0
- package/build/events/base/base-stream.js +12 -0
- package/build/events/base/index.d.ts +21 -0
- package/build/events/base/index.js +38 -0
- package/build/events/common/enums/durable-names/index.d.ts +5 -0
- package/build/events/common/enums/durable-names/order-dname.d.ts +12 -0
- package/build/events/common/enums/durable-names/order-dname.js +12 -0
- package/build/events/common/enums/durable-names/payment-dname.d.ts +9 -0
- package/build/events/common/enums/durable-names/payment-dname.js +9 -0
- package/build/events/common/enums/index.d.ts +15 -0
- package/build/events/common/enums/index.js +16 -0
- package/build/events/common/enums/streams/index.d.ts +5 -0
- package/build/events/common/enums/streams/order-stream.d.ts +6 -0
- package/build/events/common/enums/streams/order-stream.js +6 -0
- package/build/events/common/enums/streams/payment-stream.d.ts +6 -0
- package/build/events/common/enums/streams/payment-stream.js +6 -0
- package/build/events/common/enums/subjects/index.d.ts +5 -0
- package/build/events/common/enums/subjects/main-subjects.d.ts +29 -0
- package/build/events/common/enums/subjects/main-subjects.js +11 -0
- package/build/events/common/enums/subjects/order-subjects.d.ts +12 -0
- package/build/events/common/enums/subjects/order-subjects.js +12 -0
- package/build/events/common/enums/subjects/payment-subjects.d.ts +9 -0
- package/build/events/common/enums/subjects/payment-subjects.js +9 -0
- package/build/events/common/index.d.ts +14 -0
- package/build/events/common/index.js +31 -0
- package/build/events/common/interfaces/index.d.ts +13 -0
- package/build/events/common/interfaces/index.js +33 -0
- package/build/events/common/interfaces/order-events-interfaces/index.d.ts +18 -0
- package/build/events/common/interfaces/order-events-interfaces/index.js +35 -0
- package/build/events/common/interfaces/order-events-interfaces/order-cancelled-event.d.ts +36 -0
- package/build/events/common/interfaces/order-events-interfaces/order-completed-event.d.ts +36 -0
- package/build/events/common/interfaces/order-events-interfaces/order-requested-event.d.ts +36 -0
- package/build/events/common/interfaces/payment-event-interfaces/index.d.ts +0 -0
- package/build/events/common/interfaces/payment-event-interfaces/index.js +3 -0
- package/build/events/common/interfaces/stream-interfaces/index.d.ts +1 -0
- package/build/events/common/interfaces/stream-interfaces/index.js +18 -0
- package/build/events/common/interfaces/stream-interfaces/order-stream-interface.d.ts +17 -0
- package/build/events/index.d.ts +14 -0
- package/build/events/index.js +31 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/utils/base_classes/index.d.ts +18 -0
- package/build/utils/base_classes/index.js +19 -0
- package/build/utils/base_classes/listeners/base-namespace.d.ts +15 -0
- package/build/utils/base_classes/listeners/base-namespace.js +7 -0
- package/build/utils/enums.d.ts +1 -1
- package/build/utils/enums.js +1 -1
- package/build/utils/index.d.ts +18 -0
- package/build/utils/index.js +19 -0
- package/package.json +1 -1
@@ -1,8 +1,27 @@
|
|
1
1
|
import { ConnectionOptions, NatsConnection } from 'nats';
|
2
|
+
/**
|
3
|
+
* A wrapper class for the NATS client to manage connections and provide a centralized client instance.
|
4
|
+
*/
|
2
5
|
declare class NatsWrapper {
|
3
6
|
private _client?;
|
7
|
+
/**
|
8
|
+
* Get the NATS client instance.
|
9
|
+
*
|
10
|
+
* @throws {Error} - Throws an error if the NATS client is accessed before connecting.
|
11
|
+
* @returns {NatsConnection} - The NATS client instance.
|
12
|
+
*/
|
4
13
|
get client(): NatsConnection;
|
14
|
+
/**
|
15
|
+
* Connect to the NATS server using the specified connection options.
|
16
|
+
*
|
17
|
+
* The client instance will be stored in the `_client` property for later access.
|
18
|
+
*
|
19
|
+
* @param {ConnectionOptions} opts - The options for connecting to the NATS server.
|
20
|
+
*/
|
5
21
|
connect(opts?: ConnectionOptions): Promise<void>;
|
6
22
|
}
|
23
|
+
/**
|
24
|
+
* Singleton instance of the NatsWrapper class to provide a centralized NATS client connection.
|
25
|
+
*/
|
7
26
|
export declare const natsWrapper: NatsWrapper;
|
8
27
|
export {};
|
@@ -11,12 +11,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.natsWrapper = void 0;
|
13
13
|
const nats_1 = require("nats");
|
14
|
+
/**
|
15
|
+
* A wrapper class for the NATS client to manage connections and provide a centralized client instance.
|
16
|
+
*/
|
14
17
|
class NatsWrapper {
|
18
|
+
/**
|
19
|
+
* Get the NATS client instance.
|
20
|
+
*
|
21
|
+
* @throws {Error} - Throws an error if the NATS client is accessed before connecting.
|
22
|
+
* @returns {NatsConnection} - The NATS client instance.
|
23
|
+
*/
|
15
24
|
get client() {
|
16
25
|
if (!this._client)
|
17
26
|
throw new Error("Cannot access NATS CLIENT before connecting");
|
18
27
|
return this._client;
|
19
28
|
}
|
29
|
+
/**
|
30
|
+
* Connect to the NATS server using the specified connection options.
|
31
|
+
*
|
32
|
+
* The client instance will be stored in the `_client` property for later access.
|
33
|
+
*
|
34
|
+
* @param {ConnectionOptions} opts - The options for connecting to the NATS server.
|
35
|
+
*/
|
20
36
|
connect(opts) {
|
21
37
|
return __awaiter(this, void 0, void 0, function* () {
|
22
38
|
try {
|
@@ -28,4 +44,7 @@ class NatsWrapper {
|
|
28
44
|
});
|
29
45
|
}
|
30
46
|
}
|
47
|
+
/**
|
48
|
+
* Singleton instance of the NatsWrapper class to provide a centralized NATS client connection.
|
49
|
+
*/
|
31
50
|
exports.natsWrapper = new NatsWrapper();
|
@@ -1,10 +1,32 @@
|
|
1
1
|
import { StatusCodes } from "../utils/enums";
|
2
2
|
import { CustomError } from "./custom-error";
|
3
|
+
/**
|
4
|
+
* Class representing a "Bad Request" error.
|
5
|
+
*
|
6
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "Bad Request" scenario.
|
7
|
+
* Instances of this class are thrown when the client's request is malformed or contains invalid data.
|
8
|
+
*/
|
3
9
|
export declare class BadRequestError extends CustomError {
|
4
10
|
message: string;
|
11
|
+
/**
|
12
|
+
* The HTTP status code associated with the "Bad Request" error (400).
|
13
|
+
*/
|
5
14
|
statusCode: StatusCodes;
|
15
|
+
/**
|
16
|
+
* Constructs a new instance of the "Bad Request" error.
|
17
|
+
*
|
18
|
+
* @param {string} message - The error message providing details about the "Bad Request" error.
|
19
|
+
*/
|
6
20
|
constructor(message: string);
|
7
|
-
|
21
|
+
/**
|
22
|
+
* Method to serialize the "Bad Request" error details into an array of error objects.
|
23
|
+
*
|
24
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
25
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
26
|
+
*
|
27
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
28
|
+
*/
|
29
|
+
serializeErrors(): Array<{
|
8
30
|
message: string;
|
9
|
-
}
|
31
|
+
}>;
|
10
32
|
}
|
@@ -3,13 +3,35 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BadRequestError = void 0;
|
4
4
|
const enums_1 = require("../utils/enums");
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Class representing a "Bad Request" error.
|
8
|
+
*
|
9
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "Bad Request" scenario.
|
10
|
+
* Instances of this class are thrown when the client's request is malformed or contains invalid data.
|
11
|
+
*/
|
6
12
|
class BadRequestError extends custom_error_1.CustomError {
|
13
|
+
/**
|
14
|
+
* Constructs a new instance of the "Bad Request" error.
|
15
|
+
*
|
16
|
+
* @param {string} message - The error message providing details about the "Bad Request" error.
|
17
|
+
*/
|
7
18
|
constructor(message) {
|
8
19
|
super(message);
|
9
20
|
this.message = message;
|
21
|
+
/**
|
22
|
+
* The HTTP status code associated with the "Bad Request" error (400).
|
23
|
+
*/
|
10
24
|
this.statusCode = enums_1.StatusCodes.BAD_REQUEST;
|
11
25
|
Object.setPrototypeOf(this, BadRequestError.prototype);
|
12
26
|
}
|
27
|
+
/**
|
28
|
+
* Method to serialize the "Bad Request" error details into an array of error objects.
|
29
|
+
*
|
30
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
31
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
32
|
+
*
|
33
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
34
|
+
*/
|
13
35
|
serializeErrors() {
|
14
36
|
return [{ message: this.message }];
|
15
37
|
}
|
@@ -1,6 +1,30 @@
|
|
1
|
+
/**
|
2
|
+
* Abstract class representing a custom error.
|
3
|
+
*
|
4
|
+
* This abstract class extends the base Error class and serves as a foundation for creating specific error classes for different types of errors.
|
5
|
+
* Classes that extend this abstract class must implement the `statusCode` property and the `serializeErrors` method.
|
6
|
+
*/
|
1
7
|
export declare abstract class CustomError extends Error {
|
8
|
+
/**
|
9
|
+
* The HTTP status code associated with the error.
|
10
|
+
*
|
11
|
+
* Classes extending `CustomError` must provide a value for this property.
|
12
|
+
*/
|
2
13
|
abstract statusCode: number;
|
14
|
+
/**
|
15
|
+
* Constructs a new instance of the custom error.
|
16
|
+
*
|
17
|
+
* @param {string} message - The error message.
|
18
|
+
*/
|
3
19
|
constructor(message?: string);
|
20
|
+
/**
|
21
|
+
* Abstract method that must be implemented in classes extending `CustomError`.
|
22
|
+
*
|
23
|
+
* This method returns an array of error objects containing error messages and optional field information.
|
24
|
+
* The specific implementation of this method depends on the type of custom error and how errors are serialized for responses.
|
25
|
+
*
|
26
|
+
* @returns {Array<{ message: string; field?: string }>} - An array of error objects containing error messages and optional field information.
|
27
|
+
*/
|
4
28
|
abstract serializeErrors(): {
|
5
29
|
message: string;
|
6
30
|
field?: string;
|
@@ -1,7 +1,18 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.CustomError = void 0;
|
4
|
+
/**
|
5
|
+
* Abstract class representing a custom error.
|
6
|
+
*
|
7
|
+
* This abstract class extends the base Error class and serves as a foundation for creating specific error classes for different types of errors.
|
8
|
+
* Classes that extend this abstract class must implement the `statusCode` property and the `serializeErrors` method.
|
9
|
+
*/
|
4
10
|
class CustomError extends Error {
|
11
|
+
/**
|
12
|
+
* Constructs a new instance of the custom error.
|
13
|
+
*
|
14
|
+
* @param {string} message - The error message.
|
15
|
+
*/
|
5
16
|
constructor(message) {
|
6
17
|
super(message);
|
7
18
|
Object.setPrototypeOf(this, CustomError.prototype);
|
@@ -1,9 +1,31 @@
|
|
1
1
|
import { StatusCodes } from '../utils/enums';
|
2
2
|
import { CustomError } from './custom-error';
|
3
|
+
/**
|
4
|
+
* Class representing a database-related error.
|
5
|
+
*
|
6
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for database-related errors.
|
7
|
+
* Instances of this class are thrown when there are errors related to connecting to or interacting with the database.
|
8
|
+
*/
|
3
9
|
export declare class DatabaseError extends CustomError {
|
10
|
+
/**
|
11
|
+
* The HTTP status code associated with the database-related error (500 - Internal Server Error).
|
12
|
+
*/
|
4
13
|
statusCode: StatusCodes;
|
14
|
+
/**
|
15
|
+
* Constructs a new instance of the database-related error.
|
16
|
+
*
|
17
|
+
* @param {string} message - The error message providing details about the database-related error.
|
18
|
+
*/
|
5
19
|
constructor(message?: string);
|
6
|
-
|
20
|
+
/**
|
21
|
+
* Method to serialize the database-related error details into an array of error objects.
|
22
|
+
*
|
23
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
24
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
25
|
+
*
|
26
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
27
|
+
*/
|
28
|
+
serializeErrors: () => Array<{
|
7
29
|
message: string;
|
8
|
-
}
|
30
|
+
}>;
|
9
31
|
}
|
@@ -3,10 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatabaseError = void 0;
|
4
4
|
const enums_1 = require("../utils/enums");
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Class representing a database-related error.
|
8
|
+
*
|
9
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for database-related errors.
|
10
|
+
* Instances of this class are thrown when there are errors related to connecting to or interacting with the database.
|
11
|
+
*/
|
6
12
|
class DatabaseError extends custom_error_1.CustomError {
|
13
|
+
/**
|
14
|
+
* Constructs a new instance of the database-related error.
|
15
|
+
*
|
16
|
+
* @param {string} message - The error message providing details about the database-related error.
|
17
|
+
*/
|
7
18
|
constructor(message = "Error connecting to database") {
|
8
19
|
super(message);
|
9
|
-
|
20
|
+
/**
|
21
|
+
* The HTTP status code associated with the database-related error (500 - Internal Server Error).
|
22
|
+
*/
|
23
|
+
this.statusCode = enums_1.StatusCodes.INTERNAL_SERVER_ERROR;
|
24
|
+
/**
|
25
|
+
* Method to serialize the database-related error details into an array of error objects.
|
26
|
+
*
|
27
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
28
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
29
|
+
*
|
30
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
31
|
+
*/
|
10
32
|
this.serializeErrors = () => [{ message: this.message }];
|
11
33
|
Object.setPrototypeOf(this, DatabaseError.prototype);
|
12
34
|
}
|
package/build/errors/index.d.ts
CHANGED
@@ -1,5 +1,30 @@
|
|
1
|
+
/**
|
2
|
+
* Re-exports the "BadRequestError" class from the "bad-request-error" file.
|
3
|
+
*
|
4
|
+
* The "BadRequestError" class represents a specific "Bad Request" error and is used to handle client requests with invalid or malformed data.
|
5
|
+
*/
|
1
6
|
export * from "./bad-request-error";
|
7
|
+
/**
|
8
|
+
* Re-exports the "CustomError" abstract class from the "custom-error" file.
|
9
|
+
*
|
10
|
+
* The "CustomError" abstract class serves as the foundation for creating specific error classes for different types of errors.
|
11
|
+
*/
|
2
12
|
export * from "./custom-error";
|
13
|
+
/**
|
14
|
+
* Re-exports the "DatabaseError" class from the "database-error" file.
|
15
|
+
*
|
16
|
+
* The "DatabaseError" class represents a specific error related to database operations or connectivity issues.
|
17
|
+
*/
|
3
18
|
export * from "./database-error";
|
19
|
+
/**
|
20
|
+
* Re-exports the "NotFoundError" class from the "not-found-error" file.
|
21
|
+
*
|
22
|
+
* The "NotFoundError" class represents a specific "Not Found" error and is used when a requested resource is not found.
|
23
|
+
*/
|
4
24
|
export * from "./not-found-error";
|
25
|
+
/**
|
26
|
+
* Re-exports the "SystemError" class from the "system-error" file.
|
27
|
+
*
|
28
|
+
* The "SystemError" class represents a specific "System Error" that can occur due to unexpected issues in the system.
|
29
|
+
*/
|
5
30
|
export * from "./system-error";
|
package/build/errors/index.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
+
// Import and re-export custom error classes for easier access.
|
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,33 @@ 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 });
|
18
|
+
/**
|
19
|
+
* Re-exports the "BadRequestError" class from the "bad-request-error" file.
|
20
|
+
*
|
21
|
+
* The "BadRequestError" class represents a specific "Bad Request" error and is used to handle client requests with invalid or malformed data.
|
22
|
+
*/
|
17
23
|
__exportStar(require("./bad-request-error"), exports);
|
24
|
+
/**
|
25
|
+
* Re-exports the "CustomError" abstract class from the "custom-error" file.
|
26
|
+
*
|
27
|
+
* The "CustomError" abstract class serves as the foundation for creating specific error classes for different types of errors.
|
28
|
+
*/
|
18
29
|
__exportStar(require("./custom-error"), exports);
|
30
|
+
/**
|
31
|
+
* Re-exports the "DatabaseError" class from the "database-error" file.
|
32
|
+
*
|
33
|
+
* The "DatabaseError" class represents a specific error related to database operations or connectivity issues.
|
34
|
+
*/
|
19
35
|
__exportStar(require("./database-error"), exports);
|
36
|
+
/**
|
37
|
+
* Re-exports the "NotFoundError" class from the "not-found-error" file.
|
38
|
+
*
|
39
|
+
* The "NotFoundError" class represents a specific "Not Found" error and is used when a requested resource is not found.
|
40
|
+
*/
|
20
41
|
__exportStar(require("./not-found-error"), exports);
|
42
|
+
/**
|
43
|
+
* Re-exports the "SystemError" class from the "system-error" file.
|
44
|
+
*
|
45
|
+
* The "SystemError" class represents a specific "System Error" that can occur due to unexpected issues in the system.
|
46
|
+
*/
|
21
47
|
__exportStar(require("./system-error"), exports);
|
@@ -1,9 +1,31 @@
|
|
1
1
|
import { StatusCodes } from "../utils/enums";
|
2
2
|
import { CustomError } from "./custom-error";
|
3
|
+
/**
|
4
|
+
* Class representing a "Not Found" error.
|
5
|
+
*
|
6
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "Not Found" scenario.
|
7
|
+
* Instances of this class are thrown when a requested resource is not found.
|
8
|
+
*/
|
3
9
|
export declare class NotFoundError extends CustomError {
|
10
|
+
/**
|
11
|
+
* The HTTP status code associated with the "Not Found" error (404).
|
12
|
+
*/
|
4
13
|
statusCode: StatusCodes;
|
14
|
+
/**
|
15
|
+
* Constructs a new instance of the "Not Found" error.
|
16
|
+
*
|
17
|
+
* @param {string} message - The error message providing details about the "Not Found" error.
|
18
|
+
*/
|
5
19
|
constructor(message: string);
|
6
|
-
|
20
|
+
/**
|
21
|
+
* Method to serialize the "Not Found" error details into an array of error objects.
|
22
|
+
*
|
23
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
24
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
25
|
+
*
|
26
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
27
|
+
*/
|
28
|
+
serializeErrors: () => Array<{
|
7
29
|
message: string;
|
8
|
-
}
|
30
|
+
}>;
|
9
31
|
}
|
@@ -3,10 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NotFoundError = void 0;
|
4
4
|
const enums_1 = require("../utils/enums");
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Class representing a "Not Found" error.
|
8
|
+
*
|
9
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "Not Found" scenario.
|
10
|
+
* Instances of this class are thrown when a requested resource is not found.
|
11
|
+
*/
|
6
12
|
class NotFoundError extends custom_error_1.CustomError {
|
13
|
+
/**
|
14
|
+
* Constructs a new instance of the "Not Found" error.
|
15
|
+
*
|
16
|
+
* @param {string} message - The error message providing details about the "Not Found" error.
|
17
|
+
*/
|
7
18
|
constructor(message) {
|
8
19
|
super(message);
|
20
|
+
/**
|
21
|
+
* The HTTP status code associated with the "Not Found" error (404).
|
22
|
+
*/
|
9
23
|
this.statusCode = enums_1.StatusCodes.NOT_FOUND;
|
24
|
+
/**
|
25
|
+
* Method to serialize the "Not Found" error details into an array of error objects.
|
26
|
+
*
|
27
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
28
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
29
|
+
*
|
30
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
31
|
+
*/
|
10
32
|
this.serializeErrors = () => [{ message: this.message }];
|
11
33
|
Object.setPrototypeOf(this, NotFoundError.prototype);
|
12
34
|
}
|
@@ -1,9 +1,32 @@
|
|
1
1
|
import { StatusCodes } from "../utils/enums";
|
2
2
|
import { CustomError } from "./custom-error";
|
3
|
+
/**
|
4
|
+
* Class representing a "System Error".
|
5
|
+
*
|
6
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "System Error".
|
7
|
+
* Instances of this class are thrown when there are unexpected issues in the system.
|
8
|
+
*/
|
3
9
|
export declare class SystemError extends CustomError {
|
10
|
+
/**
|
11
|
+
* The HTTP status code associated with the "System Error" (500 - Internal Server Error).
|
12
|
+
*/
|
4
13
|
statusCode: StatusCodes;
|
14
|
+
/**
|
15
|
+
* Constructs a new instance of the "System Error".
|
16
|
+
*
|
17
|
+
* @param {string} message - The error message providing details about the "System Error".
|
18
|
+
* If no message is provided, the default message "Something went wrong" is used.
|
19
|
+
*/
|
5
20
|
constructor(message?: string);
|
6
|
-
|
21
|
+
/**
|
22
|
+
* Method to serialize the "System Error" details into an array of error objects.
|
23
|
+
*
|
24
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
25
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
26
|
+
*
|
27
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
28
|
+
*/
|
29
|
+
serializeErrors: () => Array<{
|
7
30
|
message: string;
|
8
|
-
}
|
31
|
+
}>;
|
9
32
|
}
|
@@ -3,10 +3,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SystemError = void 0;
|
4
4
|
const enums_1 = require("../utils/enums");
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
|
+
/**
|
7
|
+
* Class representing a "System Error".
|
8
|
+
*
|
9
|
+
* This error class extends the `CustomError` abstract class and provides specific error details for a "System Error".
|
10
|
+
* Instances of this class are thrown when there are unexpected issues in the system.
|
11
|
+
*/
|
6
12
|
class SystemError extends custom_error_1.CustomError {
|
13
|
+
/**
|
14
|
+
* Constructs a new instance of the "System Error".
|
15
|
+
*
|
16
|
+
* @param {string} message - The error message providing details about the "System Error".
|
17
|
+
* If no message is provided, the default message "Something went wrong" is used.
|
18
|
+
*/
|
7
19
|
constructor(message = "Something went wrong") {
|
8
20
|
super(message);
|
9
|
-
|
21
|
+
/**
|
22
|
+
* The HTTP status code associated with the "System Error" (500 - Internal Server Error).
|
23
|
+
*/
|
24
|
+
this.statusCode = enums_1.StatusCodes.INTERNAL_SERVER_ERROR;
|
25
|
+
/**
|
26
|
+
* Method to serialize the "System Error" details into an array of error objects.
|
27
|
+
*
|
28
|
+
* This method returns an array containing a single error object with the error message provided during construction.
|
29
|
+
* The array format is consistent with the `CustomError` class's expectation for error serialization.
|
30
|
+
*
|
31
|
+
* @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
|
32
|
+
*/
|
10
33
|
this.serializeErrors = () => [{ message: this.message }];
|
11
34
|
Object.setPrototypeOf(this, SystemError.prototype);
|
12
35
|
}
|
@@ -1,26 +1,83 @@
|
|
1
1
|
import { NatsConnection, ConsumerConfig, JsMsg } from "nats";
|
2
2
|
import { DurableName, Subjects, Streams } from "../common/enums";
|
3
3
|
import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
|
4
|
+
/**
|
5
|
+
* Interface representing an event published to NATS.
|
6
|
+
*/
|
4
7
|
interface Event {
|
5
8
|
subject: ValidateSubjectFormat<Subjects, MainSubjects>;
|
6
9
|
durableName?: DurableName;
|
7
10
|
stream: Streams;
|
8
11
|
data: any;
|
9
12
|
}
|
13
|
+
/**
|
14
|
+
* Abstract class representing a NATS consumer for processing messages from a JetStream stream.
|
15
|
+
*
|
16
|
+
* @typeparam {T} - The type of event to be consumed, extending the Event interface.
|
17
|
+
*/
|
10
18
|
export declare abstract class NatsConsumer<T extends Event> {
|
11
19
|
protected client: NatsConnection;
|
12
20
|
protected jc: import("nats").Codec<T>;
|
13
21
|
protected sc: import("nats").Codec<string>;
|
22
|
+
/**
|
23
|
+
* The name of the JetStream stream to consume messages from.
|
24
|
+
*/
|
14
25
|
abstract stream: T["stream"];
|
26
|
+
/**
|
27
|
+
* The subject to filter messages for this consumer.
|
28
|
+
*/
|
15
29
|
abstract subject: T["subject"];
|
30
|
+
/**
|
31
|
+
* The name of the durable consumer, if applicable.
|
32
|
+
*/
|
16
33
|
abstract durableName: T["durableName"];
|
34
|
+
/**
|
35
|
+
* The maximum number of messages that can be processed concurrently.
|
36
|
+
*/
|
17
37
|
abstract maxConcurrentMessages: number;
|
18
38
|
protected ackWait: number;
|
39
|
+
/**
|
40
|
+
* Creates an instance of NatsConsumer.
|
41
|
+
*
|
42
|
+
* @param {NatsConnection} client - The NATS connection instance.
|
43
|
+
*/
|
19
44
|
constructor(client: NatsConnection);
|
45
|
+
/**
|
46
|
+
* Provides the consumer options for JetStream.
|
47
|
+
*
|
48
|
+
* @returns {Partial<ConsumerConfig>} - The partial consumer options.
|
49
|
+
*/
|
20
50
|
get consumerOptions(): Partial<ConsumerConfig>;
|
51
|
+
/**
|
52
|
+
* Consume messages from the JetStream stream and process them concurrently.
|
53
|
+
*
|
54
|
+
* Messages are processed asynchronously up to the maximum concurrent limit.
|
55
|
+
*/
|
21
56
|
consume(): Promise<void>;
|
57
|
+
/**
|
58
|
+
* Abstract method to process a single message received from the stream.
|
59
|
+
*
|
60
|
+
* This method should be implemented by subclasses to define custom logic for processing messages.
|
61
|
+
*
|
62
|
+
* @param {T["data"]} data - The data representing the message payload.
|
63
|
+
* @param {JsMsg} msg - The message object received from the stream.
|
64
|
+
* @returns {Promise<void>} - A Promise that resolves once the message processing is completed successfully.
|
65
|
+
*/
|
22
66
|
abstract processMessage(data: T["data"], msg: JsMsg): Promise<void>;
|
67
|
+
/**
|
68
|
+
* Closes the NATS connection when message processing is finished.
|
69
|
+
*
|
70
|
+
* @protected
|
71
|
+
* @returns {Promise<void>} - A Promise that resolves once the NATS connection is drained and closed.
|
72
|
+
*/
|
23
73
|
protected close(): Promise<void>;
|
74
|
+
/**
|
75
|
+
* Utility method to parse the message data using the appropriate codec (JSON or string).
|
76
|
+
*
|
77
|
+
* @protected
|
78
|
+
* @param {JsMsg} msg - The message object received from the stream.
|
79
|
+
* @returns {T["data"]} - The parsed message data.
|
80
|
+
*/
|
24
81
|
protected parsedMessage: (msg: JsMsg) => string | T;
|
25
82
|
}
|
26
83
|
export {};
|
@@ -18,14 +18,36 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
19
19
|
exports.NatsConsumer = void 0;
|
20
20
|
const nats_1 = require("nats");
|
21
|
+
/**
|
22
|
+
* Abstract class representing a NATS consumer for processing messages from a JetStream stream.
|
23
|
+
*
|
24
|
+
* @typeparam {T} - The type of event to be consumed, extending the Event interface.
|
25
|
+
*/
|
21
26
|
class NatsConsumer {
|
27
|
+
/**
|
28
|
+
* Creates an instance of NatsConsumer.
|
29
|
+
*
|
30
|
+
* @param {NatsConnection} client - The NATS connection instance.
|
31
|
+
*/
|
22
32
|
constructor(client) {
|
23
33
|
this.jc = (0, nats_1.JSONCodec)();
|
24
34
|
this.sc = (0, nats_1.StringCodec)();
|
25
35
|
this.ackWait = (0, nats_1.nanos)(5 * 1000);
|
36
|
+
/**
|
37
|
+
* Utility method to parse the message data using the appropriate codec (JSON or string).
|
38
|
+
*
|
39
|
+
* @protected
|
40
|
+
* @param {JsMsg} msg - The message object received from the stream.
|
41
|
+
* @returns {T["data"]} - The parsed message data.
|
42
|
+
*/
|
26
43
|
this.parsedMessage = (msg) => typeof msg.data === "string" ? this.sc.decode(msg.data) : this.jc.decode(msg.data);
|
27
44
|
this.client = client;
|
28
45
|
}
|
46
|
+
/**
|
47
|
+
* Provides the consumer options for JetStream.
|
48
|
+
*
|
49
|
+
* @returns {Partial<ConsumerConfig>} - The partial consumer options.
|
50
|
+
*/
|
29
51
|
get consumerOptions() {
|
30
52
|
return {
|
31
53
|
deliver_policy: nats_1.DeliverPolicy.All,
|
@@ -36,6 +58,11 @@ class NatsConsumer {
|
|
36
58
|
filter_subject: this.subject,
|
37
59
|
};
|
38
60
|
}
|
61
|
+
/**
|
62
|
+
* Consume messages from the JetStream stream and process them concurrently.
|
63
|
+
*
|
64
|
+
* Messages are processed asynchronously up to the maximum concurrent limit.
|
65
|
+
*/
|
39
66
|
consume() {
|
40
67
|
var _a, e_1, _b, _c;
|
41
68
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -87,6 +114,12 @@ class NatsConsumer {
|
|
87
114
|
}
|
88
115
|
});
|
89
116
|
}
|
117
|
+
/**
|
118
|
+
* Closes the NATS connection when message processing is finished.
|
119
|
+
*
|
120
|
+
* @protected
|
121
|
+
* @returns {Promise<void>} - A Promise that resolves once the NATS connection is drained and closed.
|
122
|
+
*/
|
90
123
|
close() {
|
91
124
|
return __awaiter(this, void 0, void 0, function* () {
|
92
125
|
yield this.client.drain();
|
@@ -1,14 +1,33 @@
|
|
1
1
|
import { PubAck, NatsConnection } from "nats";
|
2
2
|
import { Subjects } from "../common/enums";
|
3
3
|
import { MainSubjects, ValidateSubjectFormat } from "../common/enums/subjects/main-subjects";
|
4
|
+
/**
|
5
|
+
* Interface representing an event to be published to NATS.
|
6
|
+
*/
|
4
7
|
interface Event {
|
5
8
|
subject: ValidateSubjectFormat<Subjects, MainSubjects>;
|
6
9
|
data: any;
|
7
10
|
}
|
11
|
+
/**
|
12
|
+
* Abstract class representing a NATS publisher for publishing events to NATS.
|
13
|
+
*
|
14
|
+
* @typeparam {T} - The type of event to be published, extending the Event interface.
|
15
|
+
*/
|
8
16
|
export declare abstract class NatsPublisher<T extends Event> {
|
9
17
|
private jc;
|
10
18
|
private sc;
|
19
|
+
/**
|
20
|
+
* The subject representing the event type to be published.
|
21
|
+
*/
|
11
22
|
abstract subject: T["subject"];
|
23
|
+
/**
|
24
|
+
* Publishes an event to NATS using the provided NATS connection.
|
25
|
+
*
|
26
|
+
* @param {NatsConnection} nc - The NATS connection instance.
|
27
|
+
* @param {T["data"]} data - The data to be published as part of the event.
|
28
|
+
* @returns {Promise<PubAck>} - A Promise that resolves with the PubAck response from NATS.
|
29
|
+
* @throws {Error} - Throws an error if there is an issue while publishing the event.
|
30
|
+
*/
|
12
31
|
publish(nc: NatsConnection, data: T["data"]): Promise<PubAck>;
|
13
32
|
}
|
14
33
|
export {};
|