UNPKG

3.82 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createGrpcMethodMetadata = exports.GrpcStreamCall = exports.GrpcStreamMethod = exports.GrpcMethod = exports.MessagePattern = exports.GrpcMethodStreamingType = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5/* eslint-disable @typescript-eslint/no-use-before-define */
6const constants_1 = require("../constants");
7const pattern_handler_enum_1 = require("../enums/pattern-handler.enum");
8const enums_1 = require("../enums");
9var 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 * Subscribes to incoming messages which fulfils chosen pattern.
17 *
18 * @publicApi
19 */
20const 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};
45exports.MessagePattern = MessagePattern;
46function 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}
52exports.GrpcMethod = GrpcMethod;
53function 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}
59exports.GrpcStreamMethod = GrpcStreamMethod;
60function 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}
66exports.GrpcStreamCall = GrpcStreamCall;
67function 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}
82exports.createGrpcMethodMetadata = createGrpcMethodMetadata;