• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

protoc-gen-grpc-gateway-ts is a Typescript client generator for the grpc-gateway project. It generates idiomatic Typescript clients that connect the web frontend and golang backend fronted by grpc-gateway.

protoc-gen-grpc-gateway-ts

protoc-gen-grpc-gateway-ts is a TypeScript client generator for the grpc-gateway project. It generates idiomatic TypeScript clients that connect the web frontend and golang backend fronted by grpc-gateway.

Features:

  1. Idiomatic Typescript clients and messages.
  2. Supports both one way and server side streaming gRPC calls.
  3. POJO request construction guarded by message type definitions, which is way easier compare to grpc-web.
  4. No need to use swagger/open api to generate client code for the web.

Getting Started:

Install protoc-gen-grpc-gateway-ts

You will need to install protoc-gen-grpc-gateway-ts before it could be picked up by the protoc command. Just run go install github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts

Sample Usage:

protoc-gen-grpc-gateway-ts should be used along with the protoc command. A sample invocation looks like the following:

protoc --grpc-gateway-ts_out=ts_import_roots=$(pwd),ts_import_root_aliases=base:. input.proto

As a result the generated file will be input.pb.ts in the same directory.

Parameters:

ts_import_roots

Since protoc plugins do not get the import path information as what's specified in protoc -I, this parameter gives the plugin the same information to figure out where a specific type is coming from so that it can generate import statement at the top of the generated typescript file. Defaults to $(pwd)

ts_import_root_aliases

If a project has setup an alias for their import. This parameter can be used to keep up with the project setup. It will print out alias instead of relative path in the import statement. Default to "".

ts_import_roots & ts_import_root_aliases are useful when you have setup import alias in your project with the project asset bundler, e.g. Webpack.

fetch_module_directory and fetch_module_filename

protoc-gen-grpc-gateway-ts generates a shared typescript file with communication functions. These two parameters together will determine where the fetch module file is located. Default to $(pwd)/fetch.pb.ts

use_proto_names

To keep the same convention with grpc-gateway v2 & protojson. The field name in message generated by this library is in lowerCamelCase by default. If you prefer to make it stick the same with what is defined in the proto file, this option needs to be set to true.

logtostderr

Turn on logging to stderr. Default to false.

loglevel

Defines the logging levels. Default to info. Valid values are: debug, info, warn, error

Notes:

Zero-value fields are omitted from the URL query parameter list for GET requests. Therefore for a request payload such as { a: "A", b: "" c: 1, d: 0, e: false } will become /path/query?a=A&c=1. A sample implementation is present within this proto file in theintegration_tests folder. For further explanation please read the following:

Examples:

The following shows how to use the generated TypeScript code.

Proto file: counter.proto

// file: counter.proto
message Request {
  int32 counter = 1;
}

message Response {
  int32 result = 1;
}

service CounterService {
  rpc Increase(Request) returns (Response);
  rpc Increase10X(Request) returns (stream Response);
}

Run the following command to generate the TypeScript client:

protoc --grpc-gateway-ts_out=. counter.proto

Then a counter.pb.ts file will be available at the current directory. You can use it like the following example.

import {CounterService} from './counter.pb'

// increase the given number once  
async function increase(base: number): Promise<number> {
  const resp = await CounterService.Increase({counter: base})
  return resp.result
} 

// increase the base repeatedly and return all results returned back from server
// the notifier after the request will be called once a result comes back from server streaming
async function increaseRepeatedly(base: number): Promise<number[]> {
  let results = []
  await CounterService.Increase10X({base}, (resp: Response) => {
    result.push(resp.result)
  })

  return results
}

License

Copyright 2020 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
Go
17,873
star
2

awesome-grpc

A curated list of useful resources for gRPC
7,459
star
3

go-grpc-middleware

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.
Go
6,275
star
4

grpc-spring

Spring Boot starter module for gRPC framework.
Java
3,518
star
5

grpc-health-probe

A command-line tool to perform health-checks for gRPC applications in Kubernetes and elsewhere
Go
1,414
star
6

go-grpc-prometheus

Prometheus monitoring for your gRPC Go servers.
Go
1,334
star
7

polyglot

A universal grpc command line client
Java
530
star
8

grpc-opentracing

OpenTracing is a set of consistent, expressive, vendor-neutral APIs for distributed tracing and context propagation
Python
470
star
9

java-grpc-prometheus

Java interceptors which can be used to monitor Grpc services using Prometheus.
Java
224
star
10

grpc-httpjson-transcoding

Transcoding to provide HTTP/JSON interface for gRPC Service
C++
161
star
11

grpcdebug

grpcdebug is a command line interface focusing on simplifying the debugging process of gRPC applications.
Go
144
star
12

grpc-simon-says

Multiplayer Simon Says game using bidirectional gRPC streaming
Go
133
star
13

grpc-cloud-run-example

Go
120
star
14

meetup-kit

gRPC meet up kit in a box
37
star
15

grift

This repository hosts gRPC's support for Thrift IDL and protocol
Java
10
star
16

grpc-exchange-o-gram

Exchange-o-gram demo
C#
7
star
17

proto-converter

C++
4
star
18

grpcz-stackdriver

grpcz-monitoring - description: standalone agent for monitoring statistics from grpc library and publishing to various sinks (like stackdriver, serial port etc).
4
star
19

proto-field-extraction

C++
3
star
20

grpc-codelabs

Completed codelabs from the gRPC project.
Java
3
star
21

proto_processing_lib

C++
1
star