Skip to content

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

Closed
@majelbstoat

Description

@majelbstoat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions