1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Printer_1 = require("../Printer");
|
4 | var common_1 = require("./common");
|
5 | var parameters_1 = require("../parameters");
|
6 | function generateGrpcNodeService(filename, descriptor, exportMap, modeParameter) {
|
7 | var definitionFilename = filename.replace(/_pb$/, "_grpc_pb.d.ts");
|
8 | return common_1.createFile(generateTypeScriptDefinition(descriptor, exportMap, modeParameter), definitionFilename);
|
9 | }
|
10 | exports.generateGrpcNodeService = generateGrpcNodeService;
|
11 | function generateTypeScriptDefinition(fileDescriptor, exportMap, modeParameter) {
|
12 | var serviceDescriptor = new common_1.GrpcServiceDescriptor(fileDescriptor, exportMap);
|
13 | var printer = new Printer_1.Printer(0);
|
14 | var hasServices = serviceDescriptor.services.length > 0;
|
15 | if (hasServices) {
|
16 | printer.printLn("// GENERATED CODE -- DO NOT EDIT!");
|
17 | printer.printEmptyLn();
|
18 | }
|
19 | else {
|
20 | printer.printLn("// GENERATED CODE -- NO SERVICES IN PROTO");
|
21 | return printer.getOutput();
|
22 | }
|
23 | printer.printLn("// package: " + serviceDescriptor.packageName);
|
24 | printer.printLn("// file: " + serviceDescriptor.filename);
|
25 | printer.printEmptyLn();
|
26 | serviceDescriptor.imports
|
27 | .forEach(function (importDescriptor) {
|
28 | printer.printLn("import * as " + importDescriptor.namespace + " from \"" + importDescriptor.path + "\";");
|
29 | });
|
30 | var importPackage = modeParameter === parameters_1.ModeParameter.GrpcJs ? "@grpc/grpc-js" : "grpc";
|
31 | printer.printLn("import * as grpc from \"" + importPackage + "\";");
|
32 | serviceDescriptor.services
|
33 | .forEach(function (service) {
|
34 | printer.printEmptyLn();
|
35 | printService(printer, service);
|
36 | printer.printEmptyLn();
|
37 | printServer(printer, service);
|
38 | printer.printEmptyLn();
|
39 | printClient(printer, service);
|
40 | });
|
41 | return printer.getOutput();
|
42 | }
|
43 | function printService(printer, service) {
|
44 | var serviceName = service.name + "Service";
|
45 | printer.printLn("interface I" + serviceName + " extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {");
|
46 | service.methods
|
47 | .forEach(function (method) {
|
48 | var methodType = "grpc.MethodDefinition<" + method.requestType + ", " + method.responseType + ">";
|
49 | printer.printIndentedLn(method.nameAsCamelCase + ": " + methodType + ";");
|
50 | });
|
51 | printer.printLn("}");
|
52 | printer.printEmptyLn();
|
53 | printer.printLn("export const " + serviceName + ": I" + serviceName + ";");
|
54 | }
|
55 | function printServer(printer, service) {
|
56 | var serverName = service.name + "Server";
|
57 | printer.printLn("export interface I" + serverName + " extends grpc.UntypedServiceImplementation {");
|
58 | service.methods
|
59 | .forEach(function (method) {
|
60 | if (!method.requestStream && !method.responseStream) {
|
61 | printer.printIndentedLn(method.nameAsCamelCase + ": grpc.handleUnaryCall<" + method.requestType + ", " + method.responseType + ">;");
|
62 | }
|
63 | else if (!method.requestStream) {
|
64 | printer.printIndentedLn(method.nameAsCamelCase + ": grpc.handleServerStreamingCall<" + method.requestType + ", " + method.responseType + ">;");
|
65 | }
|
66 | else if (!method.responseStream) {
|
67 | printer.printIndentedLn(method.nameAsCamelCase + ": grpc.handleClientStreamingCall<" + method.requestType + ", " + method.responseType + ">;");
|
68 | }
|
69 | else {
|
70 | printer.printIndentedLn(method.nameAsCamelCase + ": grpc.handleBidiStreamingCall<" + method.requestType + ", " + method.responseType + ">;");
|
71 | }
|
72 | });
|
73 | printer.printLn("}");
|
74 | }
|
75 | function printClient(printer, service) {
|
76 | printer.printLn("export class " + service.name + "Client extends grpc.Client {");
|
77 | printer.printIndentedLn("constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);");
|
78 | service.methods
|
79 | .forEach(function (method) {
|
80 | if (!method.requestStream && !method.responseStream) {
|
81 | printUnaryRequestMethod(printer, method);
|
82 | }
|
83 | else if (!method.requestStream) {
|
84 | printServerStreamRequestMethod(printer, method);
|
85 | }
|
86 | else if (!method.responseStream) {
|
87 | printClientStreamRequestMethod(printer, method);
|
88 | }
|
89 | else {
|
90 | printBidiStreamRequest(printer, method);
|
91 | }
|
92 | });
|
93 | printer.printLn("}");
|
94 | }
|
95 | var metadata = "metadata: grpc.Metadata | null";
|
96 | var options = "options: grpc.CallOptions | null";
|
97 | var metadataOrOptions = "metadataOrOptions: grpc.Metadata | grpc.CallOptions | null";
|
98 | var optionalMetadata = "metadata?: grpc.Metadata | null";
|
99 | var optionalOptions = "options?: grpc.CallOptions | null";
|
100 | var optionalMetadataOrOptions = "metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null";
|
101 | function printUnaryRequestMethod(printer, method) {
|
102 | var name = method.nameAsCamelCase;
|
103 | var argument = "argument: " + method.requestType;
|
104 | var callback = "callback: grpc.requestCallback<" + method.responseType + ">";
|
105 | var returnType = "grpc.ClientUnaryCall";
|
106 | printer.printIndentedLn(name + "(" + argument + ", " + callback + "): " + returnType + ";");
|
107 | printer.printIndentedLn(name + "(" + argument + ", " + metadataOrOptions + ", " + callback + "): " + returnType + ";");
|
108 | printer.printIndentedLn(name + "(" + argument + ", " + metadata + ", " + options + ", " + callback + "): " + returnType + ";");
|
109 | }
|
110 | function printServerStreamRequestMethod(printer, method) {
|
111 | var name = method.nameAsCamelCase;
|
112 | var argument = "argument: " + method.requestType;
|
113 | var returnType = "grpc.ClientReadableStream<" + method.responseType + ">";
|
114 | printer.printIndentedLn(name + "(" + argument + ", " + optionalMetadataOrOptions + "): " + returnType + ";");
|
115 | printer.printIndentedLn(name + "(" + argument + ", " + optionalMetadata + ", " + optionalOptions + "): " + returnType + ";");
|
116 | }
|
117 | function printClientStreamRequestMethod(printer, method) {
|
118 | var name = method.nameAsCamelCase;
|
119 | var callback = "callback: grpc.requestCallback<" + method.responseType + ">";
|
120 | var returnType = "grpc.ClientWritableStream<" + method.requestType + ">";
|
121 | printer.printIndentedLn(name + "(" + callback + "): grpc.ClientWritableStream<" + method.requestType + ">;");
|
122 | printer.printIndentedLn(name + "(" + metadataOrOptions + ", " + callback + "): " + returnType + ";");
|
123 | printer.printIndentedLn(name + "(" + metadata + ", " + options + ", " + callback + "): " + returnType + ";");
|
124 | }
|
125 | function printBidiStreamRequest(printer, method) {
|
126 | var name = method.nameAsCamelCase;
|
127 | var returnType = "grpc.ClientDuplexStream<" + method.requestType + ", " + method.responseType + ">";
|
128 | printer.printIndentedLn(name + "(" + optionalMetadataOrOptions + "): " + returnType + ";");
|
129 | printer.printIndentedLn(name + "(" + optionalMetadata + ", " + optionalOptions + "): " + returnType + ";");
|
130 | }
|
131 |
|
\ | No newline at end of file |