UNPKG

1.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.NatsRecordSerializer = void 0;
4const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const record_builders_1 = require("../record-builders");
7let natsPackage = {};
8class NatsRecordSerializer {
9 constructor() {
10 natsPackage = (0, load_package_util_1.loadPackage)('nats', NatsRecordSerializer.name, () => require('nats'));
11 this.jsonCodec = natsPackage.JSONCodec();
12 }
13 serialize(packet) {
14 const natsMessage = packet?.data && (0, shared_utils_1.isObject)(packet.data) && packet.data instanceof record_builders_1.NatsRecord
15 ? packet.data
16 : new record_builders_1.NatsRecordBuilder(packet?.data).build();
17 return {
18 data: this.jsonCodec.encode({ ...packet, data: natsMessage.data }),
19 headers: natsMessage.headers,
20 };
21 }
22}
23exports.NatsRecordSerializer = NatsRecordSerializer;