1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createGrpcMethodMetadata = exports.GrpcStreamCall = exports.GrpcStreamMethod = exports.GrpcMethod = exports.MessagePattern = exports.GrpcMethodStreamingType = void 0;
|
4 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
5 |
|
6 | const constants_1 = require("../constants");
|
7 | const pattern_handler_enum_1 = require("../enums/pattern-handler.enum");
|
8 | const enums_1 = require("../enums");
|
9 | var GrpcMethodStreamingType;
|
10 | (function (GrpcMethodStreamingType) {
|
11 | GrpcMethodStreamingType["NO_STREAMING"] = "no_stream";
|
12 | GrpcMethodStreamingType["RX_STREAMING"] = "rx_stream";
|
13 | GrpcMethodStreamingType["PT_STREAMING"] = "pt_stream";
|
14 | })(GrpcMethodStreamingType || (exports.GrpcMethodStreamingType = GrpcMethodStreamingType = {}));
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const MessagePattern = (metadata, transportOrExtras, maybeExtras) => {
|
21 | let transport;
|
22 | let extras;
|
23 | if (((0, shared_utils_1.isNumber)(transportOrExtras) || (0, shared_utils_1.isSymbol)(transportOrExtras)) &&
|
24 | (0, shared_utils_1.isNil)(maybeExtras)) {
|
25 | transport = transportOrExtras;
|
26 | }
|
27 | else if ((0, shared_utils_1.isObject)(transportOrExtras) && (0, shared_utils_1.isNil)(maybeExtras)) {
|
28 | extras = transportOrExtras;
|
29 | }
|
30 | else {
|
31 | transport = transportOrExtras;
|
32 | extras = maybeExtras;
|
33 | }
|
34 | return (target, key, descriptor) => {
|
35 | Reflect.defineMetadata(constants_1.PATTERN_METADATA, [].concat(metadata), descriptor.value);
|
36 | Reflect.defineMetadata(constants_1.PATTERN_HANDLER_METADATA, pattern_handler_enum_1.PatternHandler.MESSAGE, descriptor.value);
|
37 | Reflect.defineMetadata(constants_1.TRANSPORT_METADATA, transport, descriptor.value);
|
38 | Reflect.defineMetadata(constants_1.PATTERN_EXTRAS_METADATA, {
|
39 | ...Reflect.getMetadata(constants_1.PATTERN_EXTRAS_METADATA, descriptor.value),
|
40 | ...extras,
|
41 | }, descriptor.value);
|
42 | return descriptor;
|
43 | };
|
44 | };
|
45 | exports.MessagePattern = MessagePattern;
|
46 | function GrpcMethod(service, method) {
|
47 | return (target, key, descriptor) => {
|
48 | const metadata = createGrpcMethodMetadata(target, key, service, method);
|
49 | return (0, exports.MessagePattern)(metadata, enums_1.Transport.GRPC)(target, key, descriptor);
|
50 | };
|
51 | }
|
52 | exports.GrpcMethod = GrpcMethod;
|
53 | function GrpcStreamMethod(service, method) {
|
54 | return (target, key, descriptor) => {
|
55 | const metadata = createGrpcMethodMetadata(target, key, service, method, GrpcMethodStreamingType.RX_STREAMING);
|
56 | return (0, exports.MessagePattern)(metadata, enums_1.Transport.GRPC)(target, key, descriptor);
|
57 | };
|
58 | }
|
59 | exports.GrpcStreamMethod = GrpcStreamMethod;
|
60 | function GrpcStreamCall(service, method) {
|
61 | return (target, key, descriptor) => {
|
62 | const metadata = createGrpcMethodMetadata(target, key, service, method, GrpcMethodStreamingType.PT_STREAMING);
|
63 | return (0, exports.MessagePattern)(metadata, enums_1.Transport.GRPC)(target, key, descriptor);
|
64 | };
|
65 | }
|
66 | exports.GrpcStreamCall = GrpcStreamCall;
|
67 | function createGrpcMethodMetadata(target, key, service, method, streaming = GrpcMethodStreamingType.NO_STREAMING) {
|
68 | const capitalizeFirstLetter = (str) => str.charAt(0).toUpperCase() + str.slice(1);
|
69 | if (!service) {
|
70 | const { name } = target.constructor;
|
71 | return {
|
72 | service: name,
|
73 | rpc: capitalizeFirstLetter(key),
|
74 | streaming,
|
75 | };
|
76 | }
|
77 | if (service && !method) {
|
78 | return { service, rpc: capitalizeFirstLetter(key), streaming };
|
79 | }
|
80 | return { service, rpc: method, streaming };
|
81 | }
|
82 | exports.createGrpcMethodMetadata = createGrpcMethodMetadata;
|