@viardex/viardex-libs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/cache/cache.interface.d.ts +2 -0
- package/dist/cache/cache.interface.js +2 -0
- package/dist/cache/cache.module.d.ts +5 -0
- package/dist/cache/cache.module.js +27 -0
- package/dist/cache/cache.service.d.ts +25 -0
- package/dist/cache/cache.service.js +94 -0
- package/dist/cache/index.d.ts +2 -0
- package/dist/cache/index.js +18 -0
- package/dist/common/config/index.d.ts +1 -0
- package/dist/common/config/index.js +2 -0
- package/dist/common/config/nats.config.d.ts +9 -0
- package/dist/common/config/nats.config.js +12 -0
- package/dist/common/constants/index.d.ts +2 -0
- package/dist/common/constants/index.js +18 -0
- package/dist/common/constants/otp.constants.d.ts +15 -0
- package/dist/common/constants/otp.constants.js +18 -0
- package/dist/common/constants/queues.constants.d.ts +17 -0
- package/dist/common/constants/queues.constants.js +20 -0
- package/dist/common/enums/index.d.ts +2 -0
- package/dist/common/enums/index.js +18 -0
- package/dist/common/enums/support-action.enum.d.ts +7 -0
- package/dist/common/enums/support-action.enum.js +11 -0
- package/dist/common/enums/ticket.enum.d.ts +19 -0
- package/dist/common/enums/ticket.enum.js +25 -0
- package/dist/common/health/health.controller.d.ts +9 -0
- package/dist/common/health/health.controller.js +49 -0
- package/dist/common/health/health.module.d.ts +2 -0
- package/dist/common/health/health.module.js +22 -0
- package/dist/common/index.d.ts +3 -0
- package/dist/common/index.js +19 -0
- package/dist/common/utils/index.d.ts +3 -0
- package/dist/common/utils/index.js +19 -0
- package/dist/common/utils/random.utils.d.ts +10 -0
- package/dist/common/utils/random.utils.js +27 -0
- package/dist/common/utils/reference.utils.d.ts +1 -0
- package/dist/common/utils/reference.utils.js +17 -0
- package/dist/common/utils/string.utils.d.ts +10 -0
- package/dist/common/utils/string.utils.js +34 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +21 -0
- package/dist/nats/constants/nats.constants.d.ts +11 -0
- package/dist/nats/constants/nats.constants.js +15 -0
- package/dist/nats/decorators/nats-event-pattern.decorator.d.ts +2 -0
- package/dist/nats/decorators/nats-event-pattern.decorator.js +11 -0
- package/dist/nats/dto/nats-message.dto.d.ts +5 -0
- package/dist/nats/dto/nats-message.dto.js +27 -0
- package/dist/nats/index.d.ts +5 -0
- package/dist/nats/index.js +21 -0
- package/dist/nats/interceptors/nats-logging.interceptor.d.ts +6 -0
- package/dist/nats/interceptors/nats-logging.interceptor.js +33 -0
- package/dist/nats/interfaces/nats-options.interface.d.ts +23 -0
- package/dist/nats/interfaces/nats-options.interface.js +2 -0
- package/dist/notification/index.d.ts +4 -0
- package/dist/notification/index.js +20 -0
- package/dist/notification/notification.client.d.ts +31 -0
- package/dist/notification/notification.client.js +104 -0
- package/dist/notification/notification.enum.d.ts +10 -0
- package/dist/notification/notification.enum.js +14 -0
- package/dist/notification/notification.interface.d.ts +31 -0
- package/dist/notification/notification.interface.js +2 -0
- package/dist/notification/notification.module.d.ts +2 -0
- package/dist/notification/notification.module.js +20 -0
- package/dist/rpc/index.d.ts +54 -0
- package/dist/rpc/index.js +135 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ViardexRpcV2 = exports.ViardexRpc = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
6
|
+
class ViardexRpcUtils {
|
|
7
|
+
static getValidationErrors(err) {
|
|
8
|
+
if (err instanceof common_1.BadRequestException) {
|
|
9
|
+
const response = err.getResponse();
|
|
10
|
+
if (typeof response === 'object' &&
|
|
11
|
+
response !== null &&
|
|
12
|
+
'errors' in response) {
|
|
13
|
+
return Array.isArray(response.errors)
|
|
14
|
+
? response.errors
|
|
15
|
+
: [];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
static isErrorResponse(result) {
|
|
21
|
+
return typeof result === 'object' && result !== null && 'error' in result;
|
|
22
|
+
}
|
|
23
|
+
static formatSuccessResponse(data) {
|
|
24
|
+
return {
|
|
25
|
+
data,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class ViardexRpc {
|
|
30
|
+
/**
|
|
31
|
+
* Wraps a promise returned by an RPC request handler.
|
|
32
|
+
* @param promise - A Promise resolving to the response value.
|
|
33
|
+
* @param ctx - Optional NatsContext object.
|
|
34
|
+
* @param successMessage - Optional success message
|
|
35
|
+
*/
|
|
36
|
+
static async handleRequest(promise, ctx) {
|
|
37
|
+
return promise
|
|
38
|
+
.then((value) => ViardexRpcUtils.formatSuccessResponse(value))
|
|
39
|
+
.catch((err) => this.catch(err, ctx));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Handles errors from an RPC request handler and returns a ViardexRPCError wrapped in RpcException.
|
|
43
|
+
* @param err - Error to be handled.
|
|
44
|
+
* @param ctx - A NATS Context.
|
|
45
|
+
*/
|
|
46
|
+
static handleRequestError(err, ctx) {
|
|
47
|
+
return this.catch(err, ctx);
|
|
48
|
+
}
|
|
49
|
+
static catch(err, ctx) {
|
|
50
|
+
const subject = ctx ? ctx.getArgs()[0] : null;
|
|
51
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
52
|
+
const statusCode = err instanceof common_1.HttpException ? err.getStatus() : 500;
|
|
53
|
+
const errorDetails = {
|
|
54
|
+
success: false,
|
|
55
|
+
subject,
|
|
56
|
+
message: error.message,
|
|
57
|
+
statusCode,
|
|
58
|
+
errors: ViardexRpcUtils.getValidationErrors(err),
|
|
59
|
+
};
|
|
60
|
+
return new microservices_1.RpcException(errorDetails);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Handles NATS request response.
|
|
64
|
+
* @param obs - An instance of an Observable.
|
|
65
|
+
* @param successMessage - Optional success message
|
|
66
|
+
*/
|
|
67
|
+
static async withReply(obs, successMessage = 'Operation successful') {
|
|
68
|
+
const result = await new Promise((resolve, reject) => {
|
|
69
|
+
obs.subscribe({
|
|
70
|
+
next: (value) => resolve(value),
|
|
71
|
+
error: (err) => reject(err instanceof Error ? err : new Error(String(err))),
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
if (ViardexRpcUtils.isErrorResponse(result)) {
|
|
75
|
+
common_1.Logger.error(result.error);
|
|
76
|
+
throw new common_1.HttpException(result.error, result.error.statusCode, {
|
|
77
|
+
cause: result.error.errors,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return ViardexRpcUtils.formatSuccessResponse(result);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.ViardexRpc = ViardexRpc;
|
|
84
|
+
class ViardexRpcV2 {
|
|
85
|
+
/**
|
|
86
|
+
* Wraps a promise returned by an RPC request handler.
|
|
87
|
+
* @param promise - A Promise resolving to the response value.
|
|
88
|
+
* @param ctx - Optional NatsContext object.
|
|
89
|
+
*/
|
|
90
|
+
static async handleRequest(promise, ctx) {
|
|
91
|
+
return promise
|
|
92
|
+
.then((value) => ViardexRpcUtils.formatSuccessResponse(value))
|
|
93
|
+
.catch((err) => this.catch(err, ctx));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Handles errors from an RPC request handler and returns a ViardexRPCError.
|
|
97
|
+
* @param err - Error to be handled.
|
|
98
|
+
* @param ctx - A NATS Context.
|
|
99
|
+
*/
|
|
100
|
+
static handleRequestError(err, ctx) {
|
|
101
|
+
return this.catch(err, ctx);
|
|
102
|
+
}
|
|
103
|
+
static catch(err, ctx) {
|
|
104
|
+
const subject = ctx ? ctx.getArgs()[0] : null;
|
|
105
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
106
|
+
const statusCode = err instanceof common_1.HttpException ? err.getStatus() : 500;
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
subject,
|
|
110
|
+
message: error.message,
|
|
111
|
+
statusCode,
|
|
112
|
+
errors: ViardexRpcUtils.getValidationErrors(err),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Handles NATS request response.
|
|
117
|
+
* @param obs - An instance of an Observable.
|
|
118
|
+
*/
|
|
119
|
+
static async withReply(obs) {
|
|
120
|
+
const result = await new Promise((resolve, reject) => {
|
|
121
|
+
obs.subscribe({
|
|
122
|
+
next: (value) => resolve(value),
|
|
123
|
+
error: (err) => reject(err instanceof Error ? err : new Error(String(err))),
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
if (ViardexRpcUtils.isErrorResponse(result)) {
|
|
127
|
+
common_1.Logger.error(result.error);
|
|
128
|
+
throw new common_1.HttpException(result.error, result.error.statusCode, {
|
|
129
|
+
cause: result.error.errors,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return ViardexRpcUtils.formatSuccessResponse(result);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.ViardexRpcV2 = ViardexRpcV2;
|