Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc-gen-grpc-gateway-ts plugin for generating Typescript types #1182

Closed
majelbstoat opened this issue Mar 23, 2020 · 6 comments
Closed

protoc-gen-grpc-gateway-ts plugin for generating Typescript types #1182

majelbstoat opened this issue Mar 23, 2020 · 6 comments

Comments

@majelbstoat
Copy link

grpc-gateway to JSON using the standard JSON marshaller. Unfortunately, the JS implementation of protobuf appends List to the field names of repeated fields (see protocolbuffers/protobuf-javascript#42). This means that typescript definitions from those protos don't work. e.g.

// ontology_service.proto
syntax = "proto3";
package pinian;

import "pinian/question.proto";

import "google/api/annotations.proto";
import "google/protobuf/empty.proto";

service Ontology {
  rpc GetQuestions (google.protobuf.Empty) returns (QuestionsResponse) {
    option (google.api.http) = {
      get: "/v1/questions"
    };
  }
// ontology_service_pb.d.ts
import * as pinian_question_pb from "../pinian/question_pb";

export namespace QuestionsResponse {
    export type AsObject = {
        questionsList: Array<pinian_question_pb.Question.AsObject>, // wrong
    }
}

In addition, it treats int64s as numbers, instead of strings.

The result of this is that we can't strongly type responses from the gateway. I think you're actually doing the right thing, so I wrote a protoc plugin to generate grpc-gateway Typescript types.

Given the same input, it will output:

// ontology_service_gw.ts
import { Question } from '../pinian/question_gw'

export type QuestionsResponse = {
  questions: Question[]
}

Not submitted as a PR, because it's pretty ropey and I don't know if you want it in core. It works well enough for me, but YMMV. Either way, the code is here, and maybe it will be helpful for folks even if it doesn't get merged.

@johanbrandhorst
Copy link
Collaborator

Hey Jamie, thanks for the issue! Are these types generated straight from the proto? We usually see users generate typescript types and functions using the generated swagger file as an intermediate. Maybe that would work for your use case too?

As you surmise I'm not sure we want to take on this in the main repo, especially since it can just live as a third party plugin.

@majelbstoat
Copy link
Author

Yeah, these are generated straight from the proto :)

@achew22
Copy link
Collaborator

achew22 commented Mar 23, 2020

Jamie, I would be happy to add a link to your repo in the README. WDYT?

@majelbstoat
Copy link
Author

Oh yeah, you're welcome to. It's a gist, because I don't want to give anyone the impression it's fully supported. Understand if that changes your decision. 😂

https://gist.github.com/majelbstoat/5f73f58a057123c8d46db66ebbaae987

@achew22
Copy link
Collaborator

achew22 commented Mar 23, 2020

Ah, my mistake. I think we should only promote it if it has its own repo with issue tracking.

@guyisra
Copy link

guyisra commented Apr 6, 2020

just my 2 cents - we had the same issue.
ended up regex the List away after generation

grep -rl -e List gen/js | xargs -I {} perl -pi -e 's/(?<!Object)List//g' {}

need negative lookup since there are ObjectList vars in the generated code which should remain that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants