From 412ecf42324c4c8ee1456901a60b977839d0aaa6 Mon Sep 17 00:00:00 2001 From: Fabrizio Fenoglio Date: Tue, 22 Jun 2021 13:01:43 +0100 Subject: [PATCH] fixed generated client interface for multi-service --- package.json | 2 +- src/protoc-gen-twirp-ts/generator.ts | 58 ++++++++++++++-------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 0781c8f..3d96291 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "twirp-ts", - "version": "1.4.2", + "version": "1.5.0", "description": "Typescript implementation of Twirp protocol specification", "main": "build/twirp/index.js", "bin": { diff --git a/src/protoc-gen-twirp-ts/generator.ts b/src/protoc-gen-twirp-ts/generator.ts index e0eba55..d0d4aa7 100644 --- a/src/protoc-gen-twirp-ts/generator.ts +++ b/src/protoc-gen-twirp-ts/generator.ts @@ -50,14 +50,14 @@ function genClient(ctx: any, file: FileDescriptorProto, service: ServiceDescript ): Promise; } - ${genTwirpClientInterface(ctx, file, service)} + ${genTwirpClientInterface(ctx, service)} ${genTwripClientJSONImpl(ctx, file, service)} ${genTwripClientProtobufImpl(ctx, file, service)} ` } -function genTwirpClientInterface(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { +function genTwirpClientInterface(ctx: any, service: ServiceDescriptorProto) { const methods = service.method.map((method) => { return code` ${method.name}(request: ${messageToTypeName(ctx, method.inputType)}): Promise<${messageToTypeName(ctx, method.outputType)}> @@ -154,38 +154,36 @@ function genTwripClientProtobufImpl(ctx: any, file: FileDescriptorProto, service /** * Generates twirp service definition * @param ctx - * @param file + * @param service */ -function genTwirpService(ctx: any, file: FileDescriptorProto) { - return file.service.map((service) => { - const importService = service.name; +function genTwirpService(ctx: any, service: ServiceDescriptorProto) { + const importService = service.name; - const serverMethods = service.method.map((method) => { - return code` - ${method.name}(ctx: ${TwirpContext}, request: ${messageToTypeName(ctx, method.inputType)}): Promise<${messageToTypeName(ctx, method.outputType)}> - ` - }) + const serverMethods = service.method.map((method) => { + return code` + ${method.name}(ctx: ${TwirpContext}, request: ${messageToTypeName(ctx, method.inputType)}): Promise<${messageToTypeName(ctx, method.outputType)}> + ` + }) - const methodEnum = service.method.map((method) => { - return code`${method.name} = "${method.name}",` - }) + const methodEnum = service.method.map((method) => { + return code`${method.name} = "${method.name}",` + }) - const methodList = service.method.map((method) => { - return code`${importService}Method.${method.name}` - }) + const methodList = service.method.map((method) => { + return code`${importService}Method.${method.name}` + }) - return code` - export interface ${importService}Twirp { - ${joinCode(serverMethods, {on: `\n`})} - } - - export enum ${importService}Method { - ${joinCode(methodEnum, {on: "\n"})} - } - - export const ${importService}MethodList = [${joinCode(methodList, {on: ","})}]; - ` - }); + return code` + export interface ${importService}Twirp { + ${joinCode(serverMethods, {on: `\n`})} + } + + export enum ${importService}Method { + ${joinCode(methodEnum, {on: "\n"})} + } + + export const ${importService}MethodList = [${joinCode(methodList, {on: ","})}]; + ` } /** @@ -203,7 +201,7 @@ function genServer(ctx: any, file: FileDescriptorProto, service: ServiceDescript // Server Code // //==================================// - ${genTwirpService(ctx, file)} + ${genTwirpService(ctx, service)} export function create${importService}Server(service: ${importService}Twirp) { return new ${TwirpServer}<${importService}Twirp>({