Skip to content

Commit

Permalink
fixed generated client interface for multi-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Fenoglio committed Jun 22, 2021
1 parent 916e647 commit 412ecf4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
58 changes: 28 additions & 30 deletions src/protoc-gen-twirp-ts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function genClient(ctx: any, file: FileDescriptorProto, service: ServiceDescript
): Promise<object | Uint8Array>;
}
${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)}>
Expand Down Expand Up @@ -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: ","})}];
`
}

/**
Expand All @@ -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>({
Expand Down

0 comments on commit 412ecf4

Please sign in to comment.