grpc_tools_node_protoc_ts
Aim
Generate corresponding TypeScript d.ts codes according to js codes generated by grpc_tools_node_protoc
More information about grpc_tools_node_protoc:
Breaking changes
v2.2.0
Fix definition changes according to the version change of grpc official TypeScript definition, see: index.d.ts@1.9.0.
Detailed changes could be found here: PR#14.
v2.0.0
Since v2.x.x, current project supports the official definition of grpc, see: index.d.ts@1.8.4.
Though the usage of tool, and generated codes shall not been changed, it's good to be double checked in your project when upgrade.
TSLint has been disabled in generated files. Please see the conversation: #13.
Note
This tools is using an unofficial grpc.d.ts definition, see: grpc-tsd.
If you want to use this tool, you have to use definition mentioned.
How to use
npm install grpc_tools_node_protoc_ts --save-dev
grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:./your_dest_dir \
--grpc_out=./your_dest_dir \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./proto \
./your_proto_dir/*.proto
protoc \
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
--ts_out=./your_dest_dir \
-I ./proto \
./your_proto_dir/*.proto
Sample
There is a complete & runnable sample in folder examples
.
Dirs:
- proto: sample proto definition
- bash: useful commands
- build.sh: build js & d.ts codes from proto file, and tsc to build/*.js
- server.sh: start the sample server
- client.sh: start the client & send requests
book.proto
syntax = "proto3";
package com.book;
message Book {
int64 isbn = 1;
string title = 2;
string author = 3;
}
message GetBookRequest {
int64 isbn = 1;
}
message GetBookViaAuthor {
string author = 1;
}
service BookService {
rpc GetBook (GetBookRequest) returns (Book) {}
rpc GetBooksViaAuthor (GetBookViaAuthor) returns (stream Book) {}
rpc GetGreatestBook (stream GetBookRequest) returns (Book) {}
rpc GetBooks (stream GetBookRequest) returns (stream Book) {}
}
message BookStore {
string name = 1;
map<int64, string> books = 2;
}
enum EnumSample {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 1;
}
book_pb.d.ts
import * as jspb from "google-protobuf";
export class Book extends jspb.Message {
getIsbn(): number;
setIsbn(value: number): void;
getTitle(): string;
setTitle(value: string): void;
getAuthor(): string;
setAuthor(value: string): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Book.AsObject;
static toObject(includeInstance: boolean, msg: Book): Book.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: Book, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): Book;
static deserializeBinaryFromReader(message: Book, reader: jspb.BinaryReader): Book;
}
export namespace Book {
export type AsObject = {
isbn: number,
title: string,
author: string,
}
}
export class GetBookRequest extends jspb.Message {
getIsbn(): number;
setIsbn(value: number): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): GetBookRequest.AsObject;
static toObject(includeInstance: boolean, msg: GetBookRequest): GetBookRequest.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: GetBookRequest, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): GetBookRequest;
static deserializeBinaryFromReader(message: GetBookRequest, reader: jspb.BinaryReader): GetBookRequest;
}
export namespace GetBookRequest {
export type AsObject = {
isbn: number,
}
}
export class GetBookViaAuthor extends jspb.Message {
getAuthor(): string;
setAuthor(value: string): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): GetBookViaAuthor.AsObject;
static toObject(includeInstance: boolean, msg: GetBookViaAuthor): GetBookViaAuthor.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: GetBookViaAuthor, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): GetBookViaAuthor;
static deserializeBinaryFromReader(message: GetBookViaAuthor, reader: jspb.BinaryReader): GetBookViaAuthor;
}
export namespace GetBookViaAuthor {
export type AsObject = {
author: string,
}
}
export class BookStore extends jspb.Message {
getName(): string;
setName(value: string): void;
getBooksMap(): jspb.Map<number, string>;
clearBooksMap(): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): BookStore.AsObject;
static toObject(includeInstance: boolean, msg: BookStore): BookStore.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: BookStore, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): BookStore;
static deserializeBinaryFromReader(message: BookStore, reader: jspb.BinaryReader): BookStore;
}
export namespace BookStore {
export type AsObject = {
name: string,
booksMap: Array<[number, string]>,
}
}
export enum EnumSample {
UNKNOWN = 0,
STARTED = 1,
RUNNING = 1,
}
book_grpc_pb.d.ts
import * as grpc from "grpc";
import * as book_pb from "./book_pb";
interface IBookServiceService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
getBook: IGetBook;
getBooksViaAuthor: IGetBooksViaAuthor;
getGreatestBook: IGetGreatestBook;
getBooks: IGetBooks;
}
interface IGetBook {
path: string;
requestStream: boolean;
responseStream: boolean;
requestType: book_pb.GetBookRequest;
responseType: book_pb.Book;
requestSerialize: (arg: book_pb.GetBookRequest) => Buffer;
requestDeserialize: (buffer: Uint8Array) => book_pb.GetBookRequest;
responseSerialize: (arg: book_pb.Book) => Buffer;
responseDeserialize: (buffer: Uint8Array) => book_pb.Book;
}
interface IGetBooksViaAuthor {
path: string;
requestStream: boolean;
responseStream: boolean;
requestType: book_pb.GetBookViaAuthor;
responseType: book_pb.Book;
requestSerialize: (arg: book_pb.GetBookViaAuthor) => Buffer;
requestDeserialize: (buffer: Uint8Array) => book_pb.GetBookViaAuthor;
responseSerialize: (arg: book_pb.Book) => Buffer;
responseDeserialize: (buffer: Uint8Array) => book_pb.Book;
}
interface IGetGreatestBook {
path: string;
requestStream: boolean;
responseStream: boolean;
requestType: book_pb.GetBookRequest;
responseType: book_pb.Book;
requestSerialize: (arg: book_pb.GetBookRequest) => Buffer;
requestDeserialize: (buffer: Uint8Array) => book_pb.GetBookRequest;
responseSerialize: (arg: book_pb.Book) => Buffer;
responseDeserialize: (buffer: Uint8Array) => book_pb.Book;
}
interface IGetBooks {
path: string;
requestStream: boolean;
responseStream: boolean;
requestType: book_pb.GetBookRequest;
responseType: book_pb.Book;
requestSerialize: (arg: book_pb.GetBookRequest) => Buffer;
requestDeserialize: (buffer: Uint8Array) => book_pb.GetBookRequest;
responseSerialize: (arg: book_pb.Book) => Buffer;
responseDeserialize: (buffer: Uint8Array) => book_pb.Book;
}
export interface IBookServiceClient {
getBook(request: book_pb.GetBookRequest, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
getBook(request: book_pb.GetBookRequest, metadata: grpc.Metadata, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
getBook(request: book_pb.GetBookRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
getBooksViaAuthor(request: book_pb.GetBookViaAuthor, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<book_pb.GetBookViaAuthor>;
getBooksViaAuthor(request: book_pb.GetBookViaAuthor, metadata?: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<book_pb.GetBookViaAuthor>;
getGreatestBook(callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
getGreatestBook(metadata: grpc.Metadata, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
getGreatestBook(options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
getGreatestBook(metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
getBooks(): grpc.ClientDuplexStream<book_pb.GetBookRequest, book_pb.Book>;
getBooks(options: Partial<grpc.CallOptions>): grpc.ClientDuplexStream<book_pb.GetBookRequest, book_pb.Book>;
getBooks(metadata: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientDuplexStream<book_pb.GetBookRequest, book_pb.Book>;
}
export const BookServiceService: IBookServiceService;
export class BookServiceClient extends grpc.Client implements IBookServiceClient {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
public getBook(request: book_pb.GetBookRequest, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
public getBook(request: book_pb.GetBookRequest, metadata: grpc.Metadata, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
public getBook(request: book_pb.GetBookRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientUnaryCall;
public getBooksViaAuthor(request: book_pb.GetBookViaAuthor, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<book_pb.GetBookViaAuthor>;
public getBooksViaAuthor(request: book_pb.GetBookViaAuthor, metadata?: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<book_pb.GetBookViaAuthor>;
public getGreatestBook(callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
public getGreatestBook(metadata: grpc.Metadata, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
public getGreatestBook(options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
public getGreatestBook(metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: Error | null, response: book_pb.Book) => void): grpc.ClientWritableStream<book_pb.GetBookRequest>;
public getBooks(options?: Partial<grpc.CallOptions>): grpc.ClientDuplexStream<book_pb.GetBookRequest, book_pb.Book>;
public getBooks(metadata?: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientDuplexStream<book_pb.GetBookRequest, book_pb.Book>;
}
Changes
2.2.3
Fix definitions.
Environment
node --version
npm --version
protoc --version
grpc_tools_node_protoc --version
npm list -g --depth=0 | grep grpc-tools