UNPKG

1.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.KafkaRequestSerializer = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5/**
6 * @publicApi
7 */
8class KafkaRequestSerializer {
9 serialize(value) {
10 const isNotKafkaMessage = (0, shared_utils_1.isNil)(value) ||
11 !(0, shared_utils_1.isObject)(value) ||
12 (!('key' in value) && !('value' in value));
13 if (isNotKafkaMessage) {
14 value = { value };
15 }
16 value.value = this.encode(value.value);
17 if (!(0, shared_utils_1.isNil)(value.key)) {
18 value.key = this.encode(value.key);
19 }
20 if ((0, shared_utils_1.isNil)(value.headers)) {
21 value.headers = {};
22 }
23 return value;
24 }
25 encode(value) {
26 const isObjectOrArray = !(0, shared_utils_1.isNil)(value) && !(0, shared_utils_1.isString)(value) && !Buffer.isBuffer(value);
27 if (isObjectOrArray) {
28 return (0, shared_utils_1.isPlainObject)(value) || Array.isArray(value)
29 ? JSON.stringify(value)
30 : value.toString();
31 }
32 else if ((0, shared_utils_1.isUndefined)(value)) {
33 return null;
34 }
35 return value;
36 }
37}
38exports.KafkaRequestSerializer = KafkaRequestSerializer;