} */
+ 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"