@takentrade/takentrade-libs
Version:
TakeNTrade shared libraries
132 lines (131 loc) • 4.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TakeNTradeRpcV2 = exports.TakeNTradeRpc = void 0;
const common_1 = require("@nestjs/common");
const microservices_1 = require("@nestjs/microservices");
// Shared utility functions
class TakeNTradeRpcUtils {
static getValidationErrors(err) {
if (err instanceof common_1.BadRequestException) {
const response = err.getResponse();
if (typeof response === 'object' &&
response !== null &&
'errors' in response) {
return Array.isArray(response.errors)
? response.errors
: [];
}
}
return [];
}
static isErrorResponse(result) {
return typeof result === 'object' && result !== null && 'error' in result;
}
}
class TakeNTradeRpc {
/**
* Wraps a promise returned by an RPC request handler.
* @param promise - A Promise resolving to the response value.
* @param ctx - Optional NatsContext object.
*/
static async handleRequest(promise, ctx) {
return promise
.then((value) => value)
.catch((err) => this.catch(err, ctx));
}
/**
* Handles errors from an RPC request handler and returns a TakeNTradeRPCError wrapped in RpcException.
* @param err - Error to be handled.
* @param ctx - A NATS Context.
*/
static handleRequestError(err, ctx) {
return this.catch(err, ctx);
}
static catch(err, ctx) {
const subject = ctx ? ctx.getArgs()[0] : null;
const error = err instanceof Error ? err : new Error(String(err));
const statusCode = err instanceof common_1.HttpException ? err.getStatus() : 500;
const errorDetails = {
success: false,
subject,
message: error.message,
statusCode,
errors: TakeNTradeRpcUtils.getValidationErrors(err),
timestamp: new Date().toLocaleString(),
};
return new microservices_1.RpcException(errorDetails);
}
/**
* Handles NATS request response.
* @param obs - An instance of an Observable.
*/
static async withReply(obs) {
const result = await new Promise((resolve, reject) => {
obs.subscribe({
next: (value) => resolve(value),
error: (err) => reject(err instanceof Error ? err : new Error(String(err))),
});
});
if (TakeNTradeRpcUtils.isErrorResponse(result)) {
common_1.Logger.error(result.error);
throw new common_1.HttpException(result.error, result.error.statusCode, {
cause: result.error.errors,
});
}
return result;
}
}
exports.TakeNTradeRpc = TakeNTradeRpc;
class TakeNTradeRpcV2 {
/**
* Wraps a promise returned by an RPC request handler.
* @param promise - A Promise resolving to the response value.
* @param ctx - Optional NatsContext object.
*/
static async handleRequest(promise, ctx) {
return promise
.then((value) => value)
.catch((err) => this.catch(err, ctx));
}
/**
* Handles errors from an RPC request handler and returns a TakeNTradeRPCError.
* @param err - Error to be handled.
* @param ctx - A NATS Context.
*/
static handleRequestError(err, ctx) {
return this.catch(err, ctx);
}
static catch(err, ctx) {
const subject = ctx ? ctx.getArgs()[0] : null;
const error = err instanceof Error ? err : new Error(String(err));
const statusCode = err instanceof common_1.HttpException ? err.getStatus() : 500;
return {
success: false,
subject,
message: error.message,
statusCode,
errors: TakeNTradeRpcUtils.getValidationErrors(err),
timestamp: new Date().toLocaleString(),
};
}
/**
* Handles NATS request response.
* @param obs - An instance of an Observable.
*/
static async withReply(obs) {
const result = await new Promise((resolve, reject) => {
obs.subscribe({
next: (value) => resolve(value),
error: (err) => reject(err instanceof Error ? err : new Error(String(err))),
});
});
if (TakeNTradeRpcUtils.isErrorResponse(result)) {
common_1.Logger.error(result.error);
throw new common_1.HttpException(result.error, result.error.statusCode, {
cause: result.error.errors,
});
}
return result;
}
}
exports.TakeNTradeRpcV2 = TakeNTradeRpcV2;