UNPKG

@protobuf-ts/plugin

Version:

The protocol buffer compiler plugin "protobuf-ts" generates TypeScript, gRPC-web, Twirp, and more.

67 lines (66 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MethodInfoGenerator = void 0; const rt = require("@protobuf-ts/runtime"); const ts = require("typescript"); const typescript_literal_from_value_1 = require("../framework/typescript-literal-from-value"); /** * Generates TypeScript code for runtime method information, * from method field information. */ class MethodInfoGenerator { constructor(imports) { this.imports = imports; } createMethodInfoLiterals(source, methodInfos) { const mi = methodInfos .map(mi => MethodInfoGenerator.denormalizeMethodInfo(mi)) .map(mi => this.createMethodInfoLiteral(source, mi)); return ts.createArrayLiteral(mi, true); } createMethodInfoLiteral(source, methodInfo) { methodInfo = MethodInfoGenerator.denormalizeMethodInfo(methodInfo); const properties = []; // name: The name of the method as declared in .proto // localName: The name of the method in the runtime. // idempotency: The idempotency level as specified in .proto. // serverStreaming: Was the rpc declared with server streaming? // clientStreaming: Was the rpc declared with client streaming? // options: Contains custom method options from the .proto source in JSON format. for (let key of ["name", "localName", "idempotency", "serverStreaming", "clientStreaming", "options"]) { if (methodInfo[key] !== undefined) { properties.push(ts.createPropertyAssignment(key, typescript_literal_from_value_1.typescriptLiteralFromValue(methodInfo[key]))); } } // I: The generated type handler for the input message. properties.push(ts.createPropertyAssignment(ts.createIdentifier('I'), ts.createIdentifier(this.imports.typeByName(source, methodInfo.I.typeName)))); // O: The generated type handler for the output message. properties.push(ts.createPropertyAssignment(ts.createIdentifier('O'), ts.createIdentifier(this.imports.typeByName(source, methodInfo.O.typeName)))); return ts.createObjectLiteral(properties, false); } /** * Turn normalized method info returned by normalizeMethodInfo() back into * the minimized form. */ static denormalizeMethodInfo(info) { let partial = Object.assign({}, info); delete partial.service; if (info.localName === rt.lowerCamelCase(info.name)) { delete partial.localName; } if (!info.serverStreaming) { delete partial.serverStreaming; } if (!info.clientStreaming) { delete partial.clientStreaming; } if (info.options && Object.keys(info.options).length) { delete partial.info; } if (info.idempotency === undefined) { delete partial.idempotency; } return partial; } } exports.MethodInfoGenerator = MethodInfoGenerator;