diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 45364f45..3b27b17e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,9 +20,7 @@ jobs: uses: actions/setup-node@v2 with: node-version: 14.x - - shell: bash - run: yarn test - - shell: bash + - shell: powershell run: cd examples/pure && yarn --frozen-lockfile && yarn test test: runs-on: ${{ matrix.os }} diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..0d36f4d4 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +proseWrap: always +singleQuote: false +trailingComma: all diff --git a/README.md b/README.md index 8447ccb2..e53c3ed8 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,8 @@ ![npm](https://img.shields.io/npm/v/protoc-gen-ts) ![npm](https://img.shields.io/npm/dm/protoc-gen-ts) -Generates appropriate Protocol Buffer sources from Proto files directly through _TypeScript Compiler API_. - -This plugin generates plain **Typescript** files that can be used AMD, UMD, CommonJS module systems. +Aim of this protoc plugin is to make usage of protocol buffers easy in Javascript/Typescript by taking modern approaches. This plugin generates plain **Typescript** files that can be used AMD, UMD, CommonJS module systems. -Aim of this protoc plugin is to make usage of protocol buffers easy in Javascript/Typescript by taking modern approaches. ## Example @@ -72,7 +69,7 @@ To overcome this problem, every generated message class has a static method call which can handle the mapping bidirectionally for you, even with the deeply structured messages. since it is aware of the field graph, it does not rely on any runtime type information thus we get the chance to keep it fast. -given the change example above, one can write code as; +One can write code as; ```typescript const change = Change.fromObject({ @@ -85,13 +82,15 @@ const change = Change.fromObject({ role: "maintainer" } }); + console.log(change.author instanceof Author) // true ``` ## Usage with `@grpc/grpc-js` or `grpc` -There is a seperate documentation for the usage of protoc-gen-ts along with either `@grpc/grpc-js` or `grpc`. +There is a seperate documentation for the usage of protoc-gen-ts along with either `@grpc/grpc-js` or `grpc`. By default +this generated gRPC interfaces will use `@grpc/grpc-js`. Checkout [rpcs](docs/rpc.md). @@ -140,20 +139,12 @@ ts_proto_library( # Checkout the examples/bazel directory for an example. ``` -## Environment variables - -```sh -# This controls experimental features such as 'Promise' based rpcs. -export EXPERIMENTAL_FEATURES=true; +## Supported Options +* With `--ts_opt=unary_rpc_promise=true`, the service definition will contain a promise based rpc with a calling pattern of `const result = await client.METHOD(message)`. Note: all othe `metadata` and `options` parameters are still available to you. -# This controls the "import statement" for the outputs. -# this is here for legacy purposes. -export GRPC_PACKAGE_NAME="@grpc/grpc-js"; -# or -export GRPC_PACKAGE_NAME="@grpc/grpc"; +* With `--ts_opt=grpc_package=xxxx`, you can specify a different package to import rather than `@grpc/grpc-js`. -``` ## Roadmap - Support for repeated non-integer fields @@ -178,6 +169,8 @@ export GRPC_PACKAGE_NAME="@grpc/grpc"; ## Development +Generates appropriate Protocol Buffer sources from Proto files directly through _TypeScript Compiler API_. + ```sh # to run test invoke yarn test @@ -185,3 +178,7 @@ yarn test yarn test --test_output=errors ``` + +## Contributors + +![GitHub Contributors Image](https://contrib.rocks/image?repo=thesayyn/protoc-gen-ts) diff --git a/examples/pure/package.json b/examples/pure/package.json index a410f7ea..4828d014 100644 --- a/examples/pure/package.json +++ b/examples/pure/package.json @@ -1,7 +1,6 @@ { "name": "example", "scripts": { - "preinstall": "cd ../.. && yarn bazel build :package", "postinstall": "./scripts/download_protoc.sh", "test": "protoc -I=src --ts_out=src test.proto && tsc && node ./dist/index" }, @@ -10,7 +9,7 @@ "google-protobuf": "^3.15.8" }, "devDependencies": { - "protoc-gen-ts": "file:../../bazel-bin/package", + "protoc-gen-ts": "file:../../", "typescript": "^4.2.4" } } \ No newline at end of file diff --git a/index.bzl b/index.bzl index b074573f..097ad87c 100644 --- a/index.bzl +++ b/index.bzl @@ -1,111 +1,140 @@ -load("@rules_proto//proto:defs.bzl", "ProtoInfo") - -def _proto_path(proto): - """ - The proto path is not really a file path - It's the path to the proto that was seen when the descriptor file was generated. - """ - path = proto.path - root = proto.root.path - ws = proto.owner.workspace_root - if path.startswith(root): - path = path[len(root):] - if path.startswith("/"): - path = path[1:] - if path.startswith(ws): - path = path[len(ws):] - if path.startswith("/"): - path = path[1:] - return path - -def _ts_proto_library(ctx): - - transitive_descriptors = [] - direct_sources = [] - - for target in ctx.attr.deps: - if ProtoInfo not in target: - fail("All targets in the deps attribute should be proto_library target.") - else: - info = target[ProtoInfo] - transitive_descriptors.extend(info.transitive_descriptor_sets.to_list()) - direct_sources.extend(info.direct_sources) - - ts_outputs = [] - - - for proto in direct_sources: - normalizedProtoName = proto.path.replace(ctx.label.package, "").lstrip("/")[:-len(proto.extension) - 1] - ts_outputs.append(ctx.actions.declare_file("%s.ts" % (normalizedProtoName))) - - protoc_args = ctx.actions.args() - - protoc_args.add("--plugin=protoc-gen-ts=%s" % (ctx.executable.protoc_gen_ts_bin.path)) - - protoc_args.add("--ts_out=%s" % (ctx.bin_dir.path)) - - protoc_args.add("--descriptor_set_in=%s" % (":".join([desc.path for desc in transitive_descriptors]))) - - protoc_args.add_all(direct_sources) - - print(args) - - env = dict() - - env["GRPC_PACKAGE_NAME"] = ctx.attr.grpc_package_name - - if ctx.attr.experimental_features: - env['EXPERIMENTAL_FEATURES'] = "true" - - ctx.actions.run( - inputs = direct_sources + transitive_descriptors, - tools = ctx.files.protoc_gen_ts_bin, - executable = ctx.executable._protoc, - outputs = ts_outputs, - arguments = [protoc_args], - env = env, - progress_message = "Generating Protocol Buffers for Typescript %s" % ctx.label, - ) - - return [ - DefaultInfo(files = depset(ts_outputs)) - ] - - - - - -ts_proto_library = rule( - implementation = _ts_proto_library, - attrs = { - "deps": attr.label_list( - doc = "List of proto_library targets.", - providers = [ProtoInfo], - mandatory = True - ), - "experimental_features": attr.bool( - doc = "Enable experimental features.", - default = False - ), - "grpc_package_name": attr.string( - doc = "Configures name of the grpc package to use. '@grpc/grpc-js' or 'grpc'", - default = "@grpc/grpc-js" - ), - "protoc_gen_ts_bin": attr.label( - executable = True, - cfg = "host", - default = ( - "//protoc-gen-ts/bin:protoc-gen-ts" - ), - ), - "_protoc": attr.label( - cfg = "host", - executable = True, - allow_single_file = True, - default = ( - "@com_google_protobuf//:protoc" - ), - ), - - } -) \ No newline at end of file +load("@rules_proto//proto:defs.bzl", "ProtoInfo") + +def _proto_path(proto): + """ + The proto path is not really a file path + It's the path to the proto that was seen when the descriptor file was generated. + """ + path = proto.path + root = proto.root.path + ws = proto.owner.workspace_root + if path.startswith(root): + path = path[len(root):] + if path.startswith("/"): + path = path[1:] + if path.startswith(ws): + path = path[len(ws):] + if path.startswith("/"): + path = path[1:] + return path + +def _ts_proto_library(ctx): + + transitive_descriptors = [] + direct_sources = [] + + for target in ctx.attr.deps: + if ProtoInfo not in target: + fail("All targets in the deps attribute should be proto_library target.") + else: + info = target[ProtoInfo] + transitive_descriptors.extend(info.transitive_descriptor_sets.to_list()) + direct_sources.extend(info.direct_sources) + + ts_outputs = [] + + + for proto in direct_sources: + normalizedProtoName = proto.path.replace(ctx.label.package, "").lstrip("/")[:-len(proto.extension) - 1] + ts_outputs.append(ctx.actions.declare_file("%s.ts" % (normalizedProtoName))) + + args = ctx.actions.args() + + args.add("--plugin=protoc-gen-ts=%s" % (ctx.executable.protoc_gen_ts_bin.path)) + + args.add("--ts_out=%s" % (ctx.bin_dir.path)) + + args.add("--descriptor_set_in=%s" % (":".join([desc.path for desc in transitive_descriptors]))) + + args.add_all(direct_sources) + + args.add("--ts_opt=grpc_package=%s" % ctx.attr.grpc_package_name) + + if ctx.attr.experimental_features: + args.add("--ts_opt=unary_rpc_promise") + + args.add_all(direct_sources) + + + executable = "" + + is_windows_host = ctx.configuration.host_path_separator == ";" + + + if is_windows_host: + executable = ctx.actions.declare_file("_protoc.cmd") + ctx.actions.write( + executable, + content = +"""@echo off +CALL "{protoc}" %* +""".format( + protoc = ctx.executable._protoc.path, + ), + is_executable = True, + ) + else: + executable = ctx.actions.declare_file("_protoc.sh") + ctx.actions.write( + executable, + content = +"""#!/usr/bin/env bash +set -e +{protoc} $@ +""".format( + protoc = ctx.executable._protoc.path, + ), + is_executable = True, + ) + + ctx.actions.run( + inputs = direct_sources + transitive_descriptors, + tools = [ctx.executable.protoc_gen_ts_bin, ctx.executable._protoc], + executable = executable, + outputs = ts_outputs, + arguments = [args], + progress_message = "Generating Protocol Buffers for Typescript %s" % ctx.label, + ) + + return [ + DefaultInfo(files = depset(ts_outputs)) + ] + + + + + +ts_proto_library = rule( + implementation = _ts_proto_library, + attrs = { + "deps": attr.label_list( + doc = "List of proto_library targets.", + providers = [ProtoInfo], + mandatory = True + ), + "experimental_features": attr.bool( + doc = "Enable experimental features.", + default = False + ), + "grpc_package_name": attr.string( + doc = "Configures name of the grpc package to use. '@grpc/grpc-js' or 'grpc'", + default = "@grpc/grpc-js" + ), + "protoc_gen_ts_bin": attr.label( + executable = True, + cfg = "host", + default = ( + "//protoc-gen-ts/bin:protoc-gen-ts" + ), + ), + "_protoc": attr.label( + cfg = "host", + executable = True, + allow_single_file = True, + default = ( + "@com_google_protobuf//:protoc" + ), + ), + + } +) diff --git a/package.json b/package.json index 2ffcb9f9..143da050 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "protoc-gen-ts", "description": "Compile protocol buffers descriptors to Typescript.", - "version": "0.5.0", + "version": "0.6.0", "license": "MIT", "author": { "email": "thesayyn@gmail.com", diff --git a/src/index.js b/src/index.js index 3b54feb7..854a25ac 100644 --- a/src/index.js +++ b/src/index.js @@ -19,11 +19,55 @@ function replaceExtension(filename, extension = ".ts") { return filename.replace(/\.[^/.]+$/, extension) } +/** + * @typedef {{ unary_rpc_promise: boolean, grpc_package: string }} ConfigParameters + */ + +/** + * @param {string | undefined | null} parameters + * @return{ConfigParameters} + */ +function parseParameters(parameters) { + /** @type{ConfigParameters} */ + const defaultValues = { + unary_rpc_promise: false, + grpc_package: "@grpc/grpc-js", + }; + + /** @type{{ [K keyof ConfigParameters]: (value: string) => ConfigParameters[K] }} */ + const parsers = { + unary_rpc_promise: (value) => value === "true", + grpc_package: (value) => value, + }; + + /** @type{Partial} */ + const inputParams = {}; + + // comma separated + (parameters || "").split(',').forEach(param => { + const [key, value = "true"] = param.split('=', 2) + + if (key in parsers) { + inputParams[key] = parsers[key](value); + } + }) + + // Legacy Environment variables + const legacy = { + ...(process.env.EXPERIMENTAL_FEATURES ? { unary_rpc_promise: true } : {}), + ...(process.env.GRPC_PACKAGE_NAME ? { grpc_package: process.env.GRPC_PACKAGE_NAME } : {}), + } + + return { ...defaultValues, ...legacy, ...inputParams } +} + const request = plugin.CodeGeneratorRequest.deserialize(new Uint8Array(fs.readFileSync(0))); const response = new plugin.CodeGeneratorResponse({ supported_features: plugin.CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL }); +const configParams = parseParameters(request.parameter) + for (const descriptor of request.proto_file) { type.preprocess(descriptor, descriptor.name, `.${descriptor.package || ""}`); } @@ -36,7 +80,6 @@ for (const fileDescriptor of request.proto_file) { // Will keep track of import statements const importStatements = []; - // Create all named imports from dependencies for (const dependency of fileDescriptor.dependency) { const identifier = ts.factory.createUniqueName("dependency"); @@ -65,18 +108,37 @@ for (const fileDescriptor of request.proto_file) { importStatements.push(createImport(pbIdentifier, "google-protobuf")); } - // Create all services and clients - for (const serviceDescriptor of fileDescriptor.service) { - statements.push(rpc.createUnimplementedServer(fileDescriptor, serviceDescriptor, grpcIdentifier)); - statements.push(rpc.createServiceClient(fileDescriptor, serviceDescriptor, grpcIdentifier)); - } - - // Import grpc only if there is service statements if (fileDescriptor.service.length) { - importStatements.push(createImport(grpcIdentifier, process.env.GRPC_PACKAGE_NAME || "@grpc/grpc-js")); + // Import grpc only if there is service statements + importStatements.push( + createImport( + grpcIdentifier, + configParams.grpc_package, + ) + ); + statements.push( + ...rpc.createGrpcInterfaceType(fileDescriptor, grpcIdentifier, configParams) + ); + // Create all services and clients + for (const serviceDescriptor of fileDescriptor.service) { + statements.push( + rpc.createUnimplementedServer( + fileDescriptor, + serviceDescriptor, + grpcIdentifier + ) + ); + statements.push( + rpc.createServiceClient( + fileDescriptor, + serviceDescriptor, + grpcIdentifier, + configParams, + ) + ); + } } - const {major, minor, patch} = request.compiler_version || {major: 0, minor: 0, patch: 0}; const doNotEditComment = ts.factory.createJSDocComment( @@ -89,7 +151,6 @@ for (const fileDescriptor of request.proto_file) { // Wrap statements within the namespace if (fileDescriptor.package) { - statements = [ doNotEditComment, ...importStatements, @@ -127,4 +188,4 @@ for (const fileDescriptor of request.proto_file) { type.resetDependencyMap(); } -process.stdout.write(response.serialize()); \ No newline at end of file +process.stdout.write(response.serialize()); diff --git a/src/rpc.js b/src/rpc.js index 6970a233..2b35b757 100644 --- a/src/rpc.js +++ b/src/rpc.js @@ -1,310 +1,286 @@ - -const type = require('./type'); -const descriptor = require('./compiler/descriptor'); -const ts = require('typescript'); +const type = require("./type"); +const descriptor = require("./compiler/descriptor"); +const ts = require("typescript"); /** * Returns grpc-node compatible service description - * @param {descriptor.FieldDescriptorProto} rootDescriptor - * @param {descriptor.ServiceDescriptorProto} serviceDescriptor + * @param {descriptor.FieldDescriptorProto} rootDescriptor + * @param {descriptor.ServiceDescriptorProto} serviceDescriptor */ function createServiceDefinition(rootDescriptor, serviceDescriptor) { - return ts.factory.createObjectLiteralExpression( - serviceDescriptor.method.map((methodDescriptor) => { - return ts.factory.createPropertyAssignment( - methodDescriptor.name, - ts.factory.createObjectLiteralExpression( - [ - ts.factory.createPropertyAssignment( - "path", - ts.factory.createStringLiteral( - getRPCPath( - rootDescriptor, - serviceDescriptor, - methodDescriptor - ) - ) - ), - ts.factory.createPropertyAssignment( - "requestStream", - methodDescriptor.client_streaming - ? ts.factory.createTrue() - : ts.factory.createFalse() - ), - ts.factory.createPropertyAssignment( - "responseStream", - methodDescriptor.server_streaming - ? ts.factory.createTrue() - : ts.factory.createFalse() - ), - ts.factory.createPropertyAssignment( - "requestSerialize", - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "message", - undefined, - ts.factory.createTypeReferenceNode( - getRPCInputType(rootDescriptor, methodDescriptor), - undefined - ) - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createIdentifier("Buffer"), - "from" - ), - undefined, - [ - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createIdentifier("message"), - "serialize" - ), - undefined, - undefined - ), - ] - ) - ) - ), - ts.factory.createPropertyAssignment( - "requestDeserialize", - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "bytes", - undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createIdentifier("Buffer"), - undefined - ) - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - getRPCInputType(rootDescriptor, methodDescriptor), - "deserialize" - ), - undefined, - [ - ts.factory.createNewExpression( - ts.factory.createIdentifier("Uint8Array"), - undefined, - [ts.factory.createIdentifier("bytes")] - ), - ] - ) - ) - ), - ts.factory.createPropertyAssignment( - "responseSerialize", - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "message", - undefined, - ts.factory.createTypeReferenceNode( - getRPCOutputType(rootDescriptor, methodDescriptor), - undefined - ) - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createIdentifier("Buffer"), - "from" - ), - undefined, - [ - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createIdentifier("message"), - "serialize" - ), - undefined, - [] - ), - ] - ) - ) - ), - ts.factory.createPropertyAssignment( - "responseDeserialize", - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "bytes", - undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createIdentifier("Buffer"), - undefined - ) - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - getRPCOutputType(rootDescriptor, methodDescriptor), - "deserialize" - ), - undefined, - [ - ts.factory.createNewExpression( - ts.factory.createIdentifier("Uint8Array"), - undefined, - [ts.factory.createIdentifier("bytes")] - ), - ] - ) - ) - ), - ], - true - ) - ); - }), - true - ) + return ts.factory.createObjectLiteralExpression( + serviceDescriptor.method.map((methodDescriptor) => { + return ts.factory.createPropertyAssignment( + methodDescriptor.name, + ts.factory.createObjectLiteralExpression( + [ + ts.factory.createPropertyAssignment( + "path", + ts.factory.createStringLiteral( + getRPCPath(rootDescriptor, serviceDescriptor, methodDescriptor), + ), + ), + ts.factory.createPropertyAssignment( + "requestStream", + methodDescriptor.client_streaming + ? ts.factory.createTrue() + : ts.factory.createFalse(), + ), + ts.factory.createPropertyAssignment( + "responseStream", + methodDescriptor.server_streaming + ? ts.factory.createTrue() + : ts.factory.createFalse(), + ), + ts.factory.createPropertyAssignment( + "requestSerialize", + ts.factory.createArrowFunction( + undefined, + undefined, + [ + createParameter( + "message", + ts.factory.createTypeReferenceNode( + getRPCInputType(rootDescriptor, methodDescriptor), + undefined, + ), + ), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier("Buffer"), + "from", + ), + undefined, + [ + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier("message"), + "serialize", + ), + undefined, + undefined, + ), + ], + ), + ), + ), + ts.factory.createPropertyAssignment( + "requestDeserialize", + ts.factory.createArrowFunction( + undefined, + undefined, + [ + createParameter( + "bytes", + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier("Buffer"), + undefined, + ), + ), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + getRPCInputType(rootDescriptor, methodDescriptor), + "deserialize", + ), + undefined, + [ + ts.factory.createNewExpression( + ts.factory.createIdentifier("Uint8Array"), + undefined, + [ts.factory.createIdentifier("bytes")], + ), + ], + ), + ), + ), + ts.factory.createPropertyAssignment( + "responseSerialize", + ts.factory.createArrowFunction( + undefined, + undefined, + [ + createParameter( + "message", + ts.factory.createTypeReferenceNode( + getRPCOutputType(rootDescriptor, methodDescriptor), + undefined, + ), + ), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier("Buffer"), + "from", + ), + undefined, + [ + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier("message"), + "serialize", + ), + undefined, + [], + ), + ], + ), + ), + ), + ts.factory.createPropertyAssignment( + "responseDeserialize", + ts.factory.createArrowFunction( + undefined, + undefined, + [ + createParameter( + "bytes", + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier("Buffer"), + undefined, + ), + ), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + getRPCOutputType(rootDescriptor, methodDescriptor), + "deserialize", + ), + undefined, + [ + ts.factory.createNewExpression( + ts.factory.createIdentifier("Uint8Array"), + undefined, + [ts.factory.createIdentifier("bytes")], + ), + ], + ), + ), + ), + ], + true, + ), + ); + }), + true, + ); } /** * Returns interface definition of the service description - * @param {descriptor.FileDescriptorProto} rootDescriptor - * @param {descriptor.ServiceDescriptorProto} serviceDescriptor - * @param {ts.Identifier} grpcIdentifier + * @param {descriptor.FileDescriptorProto} rootDescriptor + * @param {descriptor.ServiceDescriptorProto} serviceDescriptor + * @param {ts.Identifier} grpcIdentifier */ -function createUnimplementedService(rootDescriptor, serviceDescriptor, grpcIdentifier) { - const members = [ - ts.factory.createPropertyDeclaration( - undefined, - [ts.factory.createModifier(ts.SyntaxKind.StaticKeyword)], - "definition", - undefined, - undefined, - createServiceDefinition(rootDescriptor, serviceDescriptor) +function createUnimplementedService( + rootDescriptor, + serviceDescriptor, + grpcIdentifier, +) { + const members = [ + ts.factory.createPropertyDeclaration( + undefined, + [ts.factory.createModifier(ts.SyntaxKind.StaticKeyword)], + "definition", + undefined, + undefined, + createServiceDefinition(rootDescriptor, serviceDescriptor), + ), + ts.factory.createIndexSignature( + undefined, + undefined, + [createParameter("method", ts.factory.createIdentifier("string"))], + ts.factory.createTypeReferenceNode( + ts.factory.createPropertyAccessExpression( + grpcIdentifier, + "UntypedHandleCall", ), - ts.factory.createIndexSignature( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "method", - undefined, - ts.factory.createIdentifier("string") - ) - ], - ts.factory.createTypeReferenceNode( - ts.factory.createPropertyAccessExpression(grpcIdentifier, "UntypedHandleCall") - ) - ) - ]; - - for (const methodDescriptor of serviceDescriptor.method) { - const parameters = []; - let callType; + ), + ), + ]; - if (isUnary(methodDescriptor)) { - callType = "ServerUnaryCall"; - } else if (isClientStreaming(methodDescriptor)) { - callType = "ServerReadableStream"; - } else if (isServerStreaming(methodDescriptor)) { - callType = "ServerWritableStream"; - } else if (isBidi(methodDescriptor)) { - callType = "ServerDuplexStream"; - } + for (const methodDescriptor of serviceDescriptor.method) { + const parameters = []; + let callType; - parameters.push( - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "call", - undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, ts.factory.createIdentifier(callType)), - [ - getRPCInputType(rootDescriptor, methodDescriptor), - getRPCOutputType(rootDescriptor, methodDescriptor) - ] - ) - ) - ); + if (isUnary(methodDescriptor)) { + callType = "ServerUnaryCall"; + } else if (isClientStreaming(methodDescriptor)) { + callType = "ServerReadableStream"; + } else if (isServerStreaming(methodDescriptor)) { + callType = "ServerWritableStream"; + } else if (isBidi(methodDescriptor)) { + callType = "ServerDuplexStream"; + } - if (isUnary(methodDescriptor) || isClientStreaming(methodDescriptor)) { - parameters.push( - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "callback", - undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, ts.factory.createIdentifier("requestCallback")), - [getRPCOutputType(rootDescriptor, methodDescriptor)] - ) - ) - ); - } + parameters.push( + createParameter( + "call", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName( + grpcIdentifier, + ts.factory.createIdentifier(callType), + ), + [ + getRPCInputType(rootDescriptor, methodDescriptor), + getRPCOutputType(rootDescriptor, methodDescriptor), + ], + ), + ), + ); - members.push( - ts.factory.createMethodDeclaration( - undefined, - [ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)], - undefined, - methodDescriptor.name, - undefined, - undefined, - parameters, - ts.factory.createTypeReferenceNode("void") - ) - ) + if (isUnary(methodDescriptor) || isClientStreaming(methodDescriptor)) { + parameters.push( + createParameter( + "callback", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName( + grpcIdentifier, + ts.factory.createIdentifier("requestCallback"), + ), + [getRPCOutputType(rootDescriptor, methodDescriptor)], + ), + ), + ); } - return ts.factory.createClassDeclaration( + members.push( + ts.factory.createMethodDeclaration( undefined, - [ - ts.factory.createModifier(ts.SyntaxKind.ExportKeyword), - ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword) - ], - ts.factory.createIdentifier(`Unimplemented${serviceDescriptor.name}Service`), + [ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)], undefined, + methodDescriptor.name, undefined, - members - ) -} + undefined, + parameters, + ts.factory.createTypeReferenceNode("void"), + ), + ); + } + return ts.factory.createClassDeclaration( + undefined, + [ + ts.factory.createModifier(ts.SyntaxKind.ExportKeyword), + ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword), + ], + ts.factory.createIdentifier( + `Unimplemented${serviceDescriptor.name}Service`, + ), + undefined, + undefined, + members, + ); +} /** * Returns grpc-node compatible client unary promise method @@ -312,415 +288,622 @@ function createUnimplementedService(rootDescriptor, serviceDescriptor, grpcIdent * @param {descriptor.MethodDescriptorProto} methodDescriptor */ function createUnaryRpcPromiseMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier + rootDescriptor, + methodDescriptor, + grpcIdentifier, ) { - const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); - const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); + const requestType = getRPCInputType(rootDescriptor, methodDescriptor); - const promiseBody = ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression(ts.factory.createSuper(), methodDescriptor.name), + // super.put(message, metadata, options, (error: grpc_1.ServiceError, response: Result) => { + // if (error) { + // reject(error); + // } else { + // resolve(response); + // } + // } + const promiseBody = ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createSuper(), + methodDescriptor.name, + ), + undefined, + [ + ts.factory.createIdentifier("message"), + ts.factory.createIdentifier("metadata"), + ts.factory.createIdentifier("options"), + ts.factory.createArrowFunction( + undefined, undefined, [ - ts.factory.createIdentifier("request"), - ts.factory.createIdentifier("metadata"), - ts.factory.createIdentifier("options"), - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "error", - undefined, - ts.factory.createQualifiedName(grpcIdentifier, "ServiceError") - ), - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "response", - undefined, - responseType - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createBlock( - [ - ts.factory.createIfStatement( - ts.factory.createIdentifier("error"), - ts.factory.createBlock([ - ts.factory.createExpressionStatement( - ts.factory.createCallExpression(ts.factory.createIdentifier("reject"), undefined, [ - ts.factory.createIdentifier("error"), - ]) - ), - ]), - ts.factory.createBlock([ - ts.factory.createExpressionStatement( - ts.factory.createCallExpression(ts.factory.createIdentifier("resolve"), undefined, [ - ts.factory.createIdentifier("response"), - ]) - ), - ]) - ), - ], - true - ) + createParameter( + "error", + ts.factory.createQualifiedName(grpcIdentifier, "ServiceError"), + ), + createParameter("response", responseType), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ + ts.factory.createIfStatement( + ts.factory.createIdentifier("error"), + ts.factory.createBlock([ + ts.factory.createExpressionStatement( + ts.factory.createCallExpression( + ts.factory.createIdentifier("reject"), + undefined, + [ts.factory.createIdentifier("error")], + ), + ), + ]), + ts.factory.createBlock([ + ts.factory.createExpressionStatement( + ts.factory.createCallExpression( + ts.factory.createIdentifier("resolve"), + undefined, + [ts.factory.createIdentifier("response")], + ), + ), + ]), ), - ] - ); + ], + true, + ), + ), + ], + ); - return ts.factory.createMethodDeclaration( - undefined, - undefined, + // { + // if (!metadata) metadata = new grpc_1.Metadata; + // if (!options) options = {}; + // return new Promise((resolve, reject) => PROMISE_BODY) + // } + const functionBody = ts.factory.createBlock([ + ts.factory.createIfStatement( + ts.factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + ts.factory.createIdentifier("metadata"), + ), + ts.factory.createBlock([ + ts.factory.createExpressionStatement( + ts.factory.createBinaryExpression( + ts.factory.createIdentifier("metadata"), + ts.factory.createToken(ts.SyntaxKind.EqualsToken), + ts.factory.createNewExpression( + ts.factory.createPropertyAccessExpression( + grpcIdentifier, + "Metadata", + ), + ), + ), + ), + ]), + ), + ts.factory.createIfStatement( + ts.factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + ts.factory.createIdentifier("options"), + ), + ts.factory.createBlock([ + ts.factory.createExpressionStatement( + ts.factory.createBinaryExpression( + ts.factory.createIdentifier("options"), + ts.factory.createToken(ts.SyntaxKind.EqualsToken), + ts.factory.createObjectLiteralExpression([]), + ), + ), + ]), + ), + ts.factory.createReturnStatement( + ts.factory.createNewExpression( + ts.factory.createIdentifier("Promise"), undefined, - methodDescriptor.name, + [ + ts.factory.createArrowFunction( + undefined, + undefined, + [createParameter("resolve"), createParameter("reject")], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + promiseBody, + ), + ], + ), + ), + ]); + + const messageParameter = createParameter("message", requestType); + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + true, + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + true, + ); + const returnType = ts.factory.createTypeReferenceNode("Promise", [ + responseType, + ]); + + return [ + ts.factory.createPropertyDeclaration( + undefined, + undefined, + methodDescriptor.name, + undefined, + ts.factory.createTypeReferenceNode("GrpcPromiseServiceInterface", [ + requestType, + responseType, + ]), + + ts.factory.createArrowFunction( undefined, undefined, [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "request", - undefined, - requestType - ), - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "metadata", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createQualifiedName(grpcIdentifier, "Metadata") - ), - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createQualifiedName(grpcIdentifier, "CallOptions") - ), + messageParameter, + createParameter( + "metadata", + ts.factory.createUnionTypeNode([ + metadataParameter.type, + callOptionsParameter.type, + ]), + true, + ), + callOptionsParameter, ], - ts.factory.createTypeReferenceNode("Promise", [responseType]), - ts.factory.createBlock( - [ - ts.factory.createIfStatement( - ts.factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - ts.factory.createIdentifier("metadata") - ), - ts.factory.createExpressionStatement( - ts.factory.createBinaryExpression( - ts.factory.createIdentifier("metadata"), - ts.factory.createToken(ts.SyntaxKind.EqualsToken), - ts.factory.createNewExpression( - ts.factory.createPropertyAccessExpression(grpcIdentifier, "Metadata") - ) - ) - ) - ), - ts.factory.createIfStatement( - ts.factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - ts.factory.createIdentifier("options") - ), - ts.factory.createExpressionStatement( - ts.factory.createBinaryExpression( - ts.factory.createIdentifier("options"), - ts.factory.createToken(ts.SyntaxKind.EqualsToken), - ts.factory.createObjectLiteralExpression([]) - ) - ) - ), - ts.factory.createReturnStatement( - ts.factory.createNewExpression(ts.factory.createIdentifier("Promise"), undefined, [ - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration(undefined, undefined, undefined, "resolve"), - ts.factory.createParameterDeclaration(undefined, undefined, undefined, "reject"), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - promiseBody - ), - ]) - ), - ], - true - ) - ); + returnType, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + functionBody, + ), + ), + ]; } /** - * Returns grpc-node compatible client unary method + * Create typed parameter + * + * @param {string} name + * @param {ts.TypeReferenceNode | ts.QualifiedName | undefined} typename + */ +function createParameter(name, typename, optional = false) { + return ts.factory.createParameterDeclaration( + undefined, + undefined, + undefined, + name, + optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined, + typename, + ); +} + +/** + * Returns grpc-node compatible service interface. + * * @param {descriptor.FieldDescriptorProto} rootDescriptor * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {ts.Identifier} grpcIdentifier + * @param {ConfigParameters} config + * @returns {ts.Statement[]} */ - function createUnaryRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier -) { - const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); - const requestType = getRPCInputType(rootDescriptor, methodDescriptor); - const messageParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "message", +function createGrpcInterfaceType(rootDescriptor, grpcIdentifier, config) { + const messageParameter = createParameter( + "message", + ts.factory.createTypeReferenceNode("P"), + ); + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + ); + const callOptionsParameterOpt = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + true, + ); + const callbackParameter = createParameter( + "callback", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), + [ts.factory.createTypeReferenceNode("R")], + ), + ); + const unaryReturnType = ts.factory.createQualifiedName( + grpcIdentifier, + "ClientUnaryCall", + ); + const streamReturnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientReadableStream"), + [ts.factory.createTypeReferenceNode("R")], + ); + const writableReturnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientWritableStream"), + [ts.factory.createTypeReferenceNode("P")], + ); + const chunkReturnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientDuplexStream"), + [ + ts.factory.createTypeReferenceNode("P"), + ts.factory.createTypeReferenceNode("R"), + ], + ); + const promiseReturnType = ts.factory.createTypeReferenceNode("Promise", [ + ts.factory.createTypeReferenceNode("R"), + ]); + + // interface GrpcUnaryInterface { + // (message: P, metadata: grpc_1.Metadata, options: grpc_1.CallOptions, callback: grpc_1.requestCallback) : grpc_1.ClientUnaryCall; + // (message: P, metadata: grpc_1.Metadata, callback: grpc_1.requestCallback) : grpc_1.ClientUnaryCall; + // (message: P, options: grpc_1.CallOptions, callback: grpc_1.requestCallback) : grpc_1.ClientUnaryCall; + // (message: P, callback: grpc_1.requestCallback) : grpc_1.ClientUnaryCall; + // } + const unaryIface = ts.factory.createInterfaceDeclaration( + undefined, + undefined, + "GrpcUnaryServiceInterface", + [ + ts.factory.createTypeParameterDeclaration("P"), + ts.factory.createTypeParameterDeclaration("R"), + ], + undefined, + [ + ts.factory.createCallSignature( undefined, - requestType - ); - const metadataParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", + [ + messageParameter, + metadataParameter, + callOptionsParameter, + callbackParameter, + ], + unaryReturnType, + ), + ts.factory.createCallSignature( undefined, - ts.factory.createQualifiedName(grpcIdentifier, "Metadata") - ); - const callOptionsParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", + [messageParameter, metadataParameter, callbackParameter], + unaryReturnType, + ), + ts.factory.createCallSignature( undefined, - ts.factory.createQualifiedName(grpcIdentifier, "CallOptions") - ); - const callbackParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "callback", + [messageParameter, callOptionsParameter, callbackParameter], + unaryReturnType, + ), + ts.factory.createCallSignature( undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), - [responseType] - ) - ); - const returnType = ts.factory.createQualifiedName(grpcIdentifier, "ClientUnaryCall"); + [messageParameter, callbackParameter], + unaryReturnType, + ), + ], + ); - return [ - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, metadataParameter, callOptionsParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, metadataParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, callOptionsParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, - undefined, - undefined, - methodDescriptor.name, - undefined, - undefined, - [ - messageParameter, - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", - undefined, - ts.factory.createUnionTypeNode([ - metadataParameter.type, - callOptionsParameter.type, - callbackParameter.type - ]) - ), - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createUnionTypeNode([ - callOptionsParameter.type, - callbackParameter.type - ]) + // interface GrpcPromiseServerInterface { + // (request: P, metadata?: grpc_1.Metadata, options?: grpc_1.CallOptions): Promise { + // (request: P, options?: grpc_1.CallOptions): Promise { + // } + const promiseIface = ts.factory.createInterfaceDeclaration( + undefined, + undefined, + "GrpcPromiseServiceInterface", + [ + ts.factory.createTypeParameterDeclaration("P"), + ts.factory.createTypeParameterDeclaration("R"), + ], + undefined, + [ + ts.factory.createCallSignature( + undefined, + [messageParameter, metadataParameter, callOptionsParameterOpt], + promiseReturnType, + ), + ts.factory.createCallSignature( + undefined, + [messageParameter, callOptionsParameterOpt], + promiseReturnType, + ), + ], + ); + + // interface GrpcStreamInterface { + // (message: P, metadata: grpc_1.Metadata, options?: grpc_1.CallOptions): grpc_1.ClientReadableStream; + // (message: P, options?: grpc_1.CallOptions): grpc_1.ClientReadableStream; + // } + const streamIface = ts.factory.createInterfaceDeclaration( + undefined, + undefined, + "GrpcStreamServiceInterface", + [ + ts.factory.createTypeParameterDeclaration("P"), + ts.factory.createTypeParameterDeclaration("R"), + ], + undefined, + [ + ts.factory.createCallSignature( + undefined, + [messageParameter, metadataParameter, callOptionsParameterOpt], + streamReturnType, + ), + ts.factory.createCallSignature( + undefined, + [messageParameter, callOptionsParameterOpt], + streamReturnType, + ), + ], + ); + + // interface GrpcWritableStreamInterface { + // (metadata: grpc_1.Metadata, options: grpc_1.CallOptions, callback: grpc_1.requestCallback): grpc_1.ClientWritableStream

; + // (metadata: grpc_1.Metadata, callback: grpc_1.requestCallback): grpc_1.ClientWritableStream

; + // (options: grpc_1.CallOptions, callback: grpc_1.requestCallback): grpc_1.ClientWritableStream

; + // (callback: grpc_1.requestCallback<_Object>): grpc_1.ClientWritableStream

; + // } + const writableIface = ts.factory.createInterfaceDeclaration( + undefined, + undefined, + "GrpWritableServiceInterface", + [ + ts.factory.createTypeParameterDeclaration("P"), + ts.factory.createTypeParameterDeclaration("R"), + ], + undefined, + [ + ts.factory.createCallSignature( + undefined, + [metadataParameter, callOptionsParameter, callbackParameter], + writableReturnType, + ), + ts.factory.createCallSignature( + undefined, + [metadataParameter, callbackParameter], + writableReturnType, + ), + ts.factory.createCallSignature( + undefined, + [callOptionsParameter, callbackParameter], + writableReturnType, + ), + ts.factory.createCallSignature( + undefined, + [callbackParameter], + writableReturnType, + ), + ], + ); + + // interface GrpcChunkInterface { + // (metadata: grpc_1.Metadata, options?: grpc_1.CallOptions): grpc_1.ClientDuplexStream; + // (options?: grpc_1.CallOptions): grpc_1.ClientDuplexStream; + // } + const chunkIface = ts.factory.createInterfaceDeclaration( + undefined, + undefined, + "GrpcChunkServiceInterface", + [ + ts.factory.createTypeParameterDeclaration("P"), + ts.factory.createTypeParameterDeclaration("R"), + ], + undefined, + [ + ts.factory.createCallSignature( + undefined, + [metadataParameter, callOptionsParameterOpt], + chunkReturnType, + ), + ts.factory.createCallSignature( + undefined, + [callOptionsParameterOpt], + chunkReturnType, + ), + ], + ); + + return [unaryIface, streamIface, writableIface, chunkIface, promiseIface]; +} + +/** + * Returns grpc-node compatible client unary method + * @param {descriptor.FieldDescriptorProto} rootDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor + */ +function createUnaryRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, +) { + const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); + const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + const messageParameter = createParameter("message", requestType); + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + ); + const callbackParameter = createParameter( + "callback", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), + [responseType], + ), + ); + const returnType = ts.factory.createQualifiedName( + grpcIdentifier, + "ClientUnaryCall", + ); + + // addTodo: GrpcInterface = (message: addTodoParams, metadata: grpc_1.Metadata | grpc_1.CallOptions | grpc_1.requestCallback, options?: grpc_1.CallOptions | grpc_1.requestCallback, callback?: grpc_1.requestCallback) : grpc_1.ClientUnaryCall => { + // return super.addTodo(message, metadata, options, callback); + // } + + return [ + ts.factory.createPropertyDeclaration( + undefined, + undefined, + methodDescriptor.name, + undefined, + ts.factory.createTypeReferenceNode("GrpcUnaryServiceInterface", [ + requestType, + responseType, + ]), + ts.factory.createArrowFunction( + undefined, + undefined, + [ + messageParameter, + createParameter( + "metadata", + ts.factory.createUnionTypeNode([ + metadataParameter.type, + callOptionsParameter.type, + callbackParameter.type, + ]), + ), + createParameter( + "options", + ts.factory.createUnionTypeNode([ + callOptionsParameter.type, + callbackParameter.type, + ]), + true, + ), + createParameter( + "callback", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), + [responseType], + ), + true, + ), + ], + returnType, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ + ts.factory.createReturnStatement( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createSuper(), + methodDescriptor.name, ), - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "callback", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), - [responseType] - ) - ) - ], - returnType, - ts.factory.createBlock( + undefined, [ - - ts.factory.createReturnStatement( - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createSuper(), - methodDescriptor.name - ), - undefined, - [ - ts.factory.createIdentifier("message"), - ts.factory.createIdentifier("metadata"), - ts.factory.createIdentifier("options"), - ts.factory.createIdentifier("callback"), - ] - ) - ), + ts.factory.createIdentifier("message"), + ts.factory.createIdentifier("metadata"), + ts.factory.createIdentifier("options"), + ts.factory.createIdentifier("callback"), ], - true - ) - ) - ]; + ), + ), + ], + true, + ), + ), + ), + ]; } - /** * Returns grpc-node compatible client streaming call method * @param {descriptor.FieldDescriptorProto} rootDescriptor * @param {descriptor.MethodDescriptorProto} methodDescriptor */ function createClientStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier + rootDescriptor, + methodDescriptor, + grpcIdentifier, ) { - const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); - const requestType = getRPCInputType(rootDescriptor, methodDescriptor); - const metadataParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", - undefined, - ts.factory.createQualifiedName(grpcIdentifier, "Metadata") - ); - const callOptionsParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", + const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); + const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + ); + const callbackParameter = createParameter( + "callback", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), + [responseType], + ), + ); + const returnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientWritableStream"), + [requestType], + ); + + // put(metadata: grpc_1.Metadata | grpc_1.CallOptions | grpc_1.requestCallback<_Object>, + // options?: grpc_1.CallOptions | grpc_1.requestCallback<_Object>, + // callback?: grpc_1.requestCallback<_Object>): grpc_1.ClientWritableStream { + + return [ + ts.factory.createPropertyDeclaration( + undefined, + undefined, + methodDescriptor.name, + undefined, + ts.factory.createTypeReferenceNode("GrpWritableServiceInterface", [ + requestType, + responseType, + ]), + + ts.factory.createArrowFunction( undefined, - ts.factory.createQualifiedName(grpcIdentifier, "CallOptions") - ); - const callbackParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "callback", undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), - [responseType] - ) - ); - const returnType = ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "ClientWritableStream"), - [requestType] - ) - return [ - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [metadataParameter, callOptionsParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [metadataParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [callOptionsParameter, callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [callbackParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, - undefined, - undefined, - methodDescriptor.name, - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", - undefined, - ts.factory.createUnionTypeNode([ - metadataParameter.type, - callOptionsParameter.type, - callbackParameter.type - ]) - ), - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createUnionTypeNode([ - callOptionsParameter.type, - callbackParameter.type - ]) + [ + createParameter( + "metadata", + ts.factory.createUnionTypeNode([ + metadataParameter.type, + callOptionsParameter.type, + callbackParameter.type, + ]), + ), + createParameter( + "options", + ts.factory.createUnionTypeNode([ + callOptionsParameter.type, + callbackParameter.type, + ]), + true, + ), + createParameter( + "callback", + ts.factory.createUnionTypeNode([callbackParameter.type]), + true, + ), + ], + returnType, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ + ts.factory.createReturnStatement( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createSuper(), + methodDescriptor.name, ), - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "callback", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "requestCallback"), - [responseType] - ) - ) - ], - returnType, - ts.factory.createBlock( + undefined, [ - - ts.factory.createReturnStatement( - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createSuper(), - methodDescriptor.name - ), - undefined, - [ - ts.factory.createIdentifier("metadata"), - ts.factory.createIdentifier("options"), - ts.factory.createIdentifier("callback"), - ] - ) - ), + ts.factory.createIdentifier("metadata"), + ts.factory.createIdentifier("options"), + ts.factory.createIdentifier("callback"), ], - true - ) - ) - ]; + ), + ), + ], + true, + ), + ), + ), + ]; } /** @@ -728,397 +911,369 @@ function createClientStreamingRpcMethod( * @param {descriptor.FieldDescriptorProto} rootDescriptor * @param {descriptor.MethodDescriptorProto} methodDescriptor */ - function createServerStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier +function createServerStreamingRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, ) { - const requestType = getRPCInputType(rootDescriptor, methodDescriptor); - const messageParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "message", + const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + const messageParameter = createParameter("message", requestType); + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + true, + ); + const returnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientReadableStream"), + [requestType], + ); + + return [ + ts.factory.createPropertyDeclaration( + undefined, + undefined, + methodDescriptor.name, + undefined, + ts.factory.createTypeReferenceNode("GrpcStreamServiceInterface", [ + requestType, + requestType, + ]), + + ts.factory.createArrowFunction( undefined, - requestType - ); - const metadataParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", undefined, - ts.factory.createQualifiedName(grpcIdentifier, "Metadata") - ); - const callOptionsParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createQualifiedName(grpcIdentifier, "CallOptions") - ); - const returnType = ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "ClientReadableStream"), - [requestType] - ) - return [ - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, metadataParameter, callOptionsParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [messageParameter, callOptionsParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, - undefined, - undefined, - methodDescriptor.name, - undefined, - undefined, - [ - messageParameter, - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createUnionTypeNode([ - metadataParameter.type, - callOptionsParameter.type - ]) + [ + messageParameter, + createParameter( + "metadata", + ts.factory.createUnionTypeNode([ + metadataParameter.type, + callOptionsParameter.type, + ]), + true, + ), + createParameter( + "options", + ts.factory.createUnionTypeNode([callOptionsParameter.type]), + true, + ), + ], + returnType, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ + ts.factory.createReturnStatement( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createSuper(), + methodDescriptor.name, ), - callOptionsParameter - ], - returnType, - ts.factory.createBlock( + undefined, [ - - ts.factory.createReturnStatement( - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createSuper(), - methodDescriptor.name - ), - undefined, - [ - ts.factory.createIdentifier("message"), - ts.factory.createIdentifier("metadata"), - ts.factory.createIdentifier("options"), - ] - ) - ), + ts.factory.createIdentifier("message"), + ts.factory.createIdentifier("metadata"), + ts.factory.createIdentifier("options"), ], - true - ) - ) - ]; + ), + ), + ], + true, + ), + ), + ), + ]; } - /** * Returns grpc-node compatible client streaming call method * @param {descriptor.FieldDescriptorProto} rootDescriptor * @param {descriptor.MethodDescriptorProto} methodDescriptor */ - function createBidiStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier +function createBidiStreamingRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, ) { - const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); - const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + const responseType = getRPCOutputType(rootDescriptor, methodDescriptor); + const requestType = getRPCInputType(rootDescriptor, methodDescriptor); + + const metadataParameter = createParameter( + "metadata", + ts.factory.createQualifiedName(grpcIdentifier, "Metadata"), + ); + const callOptionsParameter = createParameter( + "options", + ts.factory.createQualifiedName(grpcIdentifier, "CallOptions"), + true, + ); + const returnType = ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName(grpcIdentifier, "ClientDuplexStream"), + [requestType, responseType], + ); - const metadataParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", + return [ + ts.factory.createPropertyDeclaration( + undefined, + undefined, + methodDescriptor.name, + undefined, + ts.factory.createTypeReferenceNode("GrpcChunkServiceInterface", [ + requestType, + responseType, + ]), + + ts.factory.createArrowFunction( undefined, - ts.factory.createQualifiedName(grpcIdentifier, "Metadata") - ); - const callOptionsParameter = ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createQualifiedName(grpcIdentifier, "CallOptions") - ); - const returnType = ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "ClientDuplexStream"), - [requestType, responseType] - ) - return [ - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [metadataParameter, callOptionsParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, undefined, undefined, - methodDescriptor.name, - undefined, undefined, - [callOptionsParameter], - returnType - ), - ts.factory.createMethodDeclaration( - undefined, - undefined, - undefined, - methodDescriptor.name, - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, undefined, undefined, - "metadata", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createUnionTypeNode([ - metadataParameter.type, - callOptionsParameter.type - ]) + undefined, + [ + createParameter( + "metadata", + ts.factory.createUnionTypeNode([ + metadataParameter.type, + callOptionsParameter.type, + ]), + true, + ), + createParameter( + "options", + ts.factory.createUnionTypeNode([callOptionsParameter.type]), + true, + ), + ], + returnType, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ + ts.factory.createReturnStatement( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + ts.factory.createSuper(), + methodDescriptor.name, ), - callOptionsParameter - ], - returnType, - ts.factory.createBlock( + undefined, [ - - ts.factory.createReturnStatement( - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - ts.factory.createSuper(), - methodDescriptor.name - ), - undefined, - [ - ts.factory.createIdentifier("metadata"), - ts.factory.createIdentifier("options"), - ] - ) - ), + ts.factory.createIdentifier("metadata"), + ts.factory.createIdentifier("options"), ], - true - ) - ) - ]; + ), + ), + ], + true, + ), + ), + ), + ]; } - - /** * Returns grpc-node compatible service client. * @param {descriptor.FieldDescriptorProto} rootDescriptor * @param {descriptor.MethodDescriptorProto} methodDescriptor - * @param {ts.Identifier} grpcIdentifier - * @returns + * @param {ts.Identifier} grpcIdentifier + * @returns */ function createServiceClient( - rootDescriptor, - serviceDescriptor, - grpcIdentifier + rootDescriptor, + serviceDescriptor, + grpcIdentifier, + params, ) { - const members = [ - ts.factory.createConstructorDeclaration( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "address", - undefined, - ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword) - ), - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "credentials", - undefined, - ts.factory.createTypeReferenceNode( - ts.factory.createQualifiedName(grpcIdentifier, "ChannelCredentials") - ) - ), - ts.factory.createParameterDeclaration( - undefined, - undefined, - undefined, - "options", - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createTypeReferenceNode( - 'Partial', - [ - ts.factory.createQualifiedName(grpcIdentifier, "ChannelOptions") - ] - ) - ), - ], - - ts.factory.createBlock( - [ - ts.factory.createCallExpression(ts.factory.createSuper(), undefined, [ - ts.factory.createIdentifier("address"), - ts.factory.createIdentifier("credentials"), - ts.factory.createIdentifier("options"), - ]), - ], - true - ) + const members = [ + ts.factory.createConstructorDeclaration( + undefined, + undefined, + [ + createParameter( + "address", + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), + ), + createParameter( + "credentials", + ts.factory.createTypeReferenceNode( + ts.factory.createQualifiedName( + grpcIdentifier, + "ChannelCredentials", + ), + ), ), - ]; + createParameter( + "options", + ts.factory.createTypeReferenceNode("Partial", [ + ts.factory.createQualifiedName(grpcIdentifier, "ChannelOptions"), + ]), + true, + ), + ], - for (const methodDescriptor of serviceDescriptor.method) { - if (isUnary(methodDescriptor)) { - if (!process.env.EXPERIMENTAL_FEATURES) { - members.push( - ...createUnaryRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier - ) - ); - } else { - members.push( - createUnaryRpcPromiseMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier - ) - ); - } - } else if (isClientStreaming(methodDescriptor)) { - members.push( - ...createClientStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier - ) - ) - } else if (isServerStreaming(methodDescriptor)) { - members.push( - ...createServerStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier - ) - ) - } else if (isBidi(methodDescriptor)) { - members.push( - ...createBidiStreamingRpcMethod( - rootDescriptor, - methodDescriptor, - grpcIdentifier - ) - ) - } + ts.factory.createBlock( + [ + ts.factory.createCallExpression(ts.factory.createSuper(), undefined, [ + ts.factory.createIdentifier("address"), + ts.factory.createIdentifier("credentials"), + ts.factory.createIdentifier("options"), + ]), + ], + true, + ), + ), + ]; + for (const methodDescriptor of serviceDescriptor.method) { + if (isUnary(methodDescriptor)) { + if (!params.unary_rpc_promise) { + members.push( + ...createUnaryRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, + ), + ); + } else { + members.push( + ...createUnaryRpcPromiseMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, + ), + ); + } + } else if (isClientStreaming(methodDescriptor)) { + members.push( + ...createClientStreamingRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, + ), + ); + } else if (isServerStreaming(methodDescriptor)) { + members.push( + ...createServerStreamingRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, + ), + ); + } else if (isBidi(methodDescriptor)) { + members.push( + ...createBidiStreamingRpcMethod( + rootDescriptor, + methodDescriptor, + grpcIdentifier, + ), + ); } + } - return ts.factory.createClassDeclaration( - undefined, - [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], - ts.factory.createIdentifier(`${serviceDescriptor.name}Client`), - undefined, - [ - ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [ - ts.factory.createExpressionWithTypeArguments( - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - grpcIdentifier, - "makeGenericClientConstructor" - ), - undefined, - [ - ts.factory.createPropertyAccessExpression( - ts.factory.createIdentifier(`Unimplemented${serviceDescriptor.name}Service`), - "definition" - ), - ts.factory.createStringLiteral(serviceDescriptor.name), - ts.factory.createObjectLiteralExpression(), - ] - ) + return ts.factory.createClassDeclaration( + undefined, + [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], + ts.factory.createIdentifier(`${serviceDescriptor.name}Client`), + undefined, + [ + ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [ + ts.factory.createExpressionWithTypeArguments( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + grpcIdentifier, + "makeGenericClientConstructor", + ), + undefined, + [ + ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier( + `Unimplemented${serviceDescriptor.name}Service`, ), - ]), - ], - members - ); + "definition", + ), + ts.factory.createStringLiteral(serviceDescriptor.name), + ts.factory.createObjectLiteralExpression(), + ], + ), + ), + ]), + ], + members, + ); } /** - * @param {descriptor.FileDescriptorProto} rootDescriptor - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.FileDescriptorProto} rootDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor */ function getRPCOutputType(rootDescriptor, methodDescriptor) { - return type.getTypeReference(rootDescriptor, methodDescriptor.output_type); + return type.getTypeReference(rootDescriptor, methodDescriptor.output_type); } /** -* @param {descriptor.FileDescriptorProto} rootDescriptor -* @param {descriptor.MethodDescriptorProto} methodDescriptor -*/ + * @param {descriptor.FileDescriptorProto} rootDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor + */ function getRPCInputType(rootDescriptor, methodDescriptor) { - return type.getTypeReference(rootDescriptor, methodDescriptor.input_type); + return type.getTypeReference(rootDescriptor, methodDescriptor.input_type); } - /** - * @param {descriptor.FileDescriptorProto} rootDescriptor - * @param {descriptor.ServiceDescriptorProto} serviceDescriptor - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.FileDescriptorProto} rootDescriptor + * @param {descriptor.ServiceDescriptorProto} serviceDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor */ function getRPCPath(rootDescriptor, serviceDescriptor, methodDescriptor) { - let name = serviceDescriptor.name; - if (rootDescriptor.package) { - name = `${rootDescriptor.package}.${name}`; - } - return `/${name}/${methodDescriptor.name}`; + let name = serviceDescriptor.name; + if (rootDescriptor.package) { + name = `${rootDescriptor.package}.${name}`; + } + return `/${name}/${methodDescriptor.name}`; } /** - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor * @returns {boolean} */ function isUnary(methodDescriptor) { - return ( - methodDescriptor.client_streaming == false && - methodDescriptor.server_streaming == false - ); + return ( + methodDescriptor.client_streaming == false && + methodDescriptor.server_streaming == false + ); } /** - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor * @returns {boolean} */ function isClientStreaming(methodDescriptor) { - return ( - methodDescriptor.client_streaming == true && - methodDescriptor.server_streaming == false - ); + return ( + methodDescriptor.client_streaming == true && + methodDescriptor.server_streaming == false + ); } /** - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor * @returns {boolean} */ function isServerStreaming(methodDescriptor) { - return ( - methodDescriptor.client_streaming == false && - methodDescriptor.server_streaming == true - ); + return ( + methodDescriptor.client_streaming == false && + methodDescriptor.server_streaming == true + ); } /** - * @param {descriptor.MethodDescriptorProto} methodDescriptor + * @param {descriptor.MethodDescriptorProto} methodDescriptor * @returns {boolean} */ function isBidi(methodDescriptor) { - return ( - methodDescriptor.client_streaming == true && - methodDescriptor.server_streaming == true - ); + return ( + methodDescriptor.client_streaming == true && + methodDescriptor.server_streaming == true + ); } - - -module.exports = { createUnimplementedServer: createUnimplementedService, createServiceClient }; \ No newline at end of file +module.exports = { + createUnimplementedServer: createUnimplementedService, + createServiceClient, + createGrpcInterfaceType, +}; diff --git a/yarn.lock b/yarn.lock index d3273158..8ad2d1b2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -136,9 +136,9 @@ integrity sha512-se3yX7UHv5Bscf8f1ERKvQOD6sTyycH3hdaoozvaLxgUiY5lIGEeH37AD0G0Qi9kPqihPn0HOfd2yaIEN9VwEg== ansi-regex@^5.0.0: - version "5.0.0" - resolved "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz" - integrity sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg== + version "5.0.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304" + integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ== ansi-styles@^4.0.0: version "4.3.0"