npm

Join us for the "JavaScript Supply Chain Security" tech talk, presented by VP of Security, Adam Baldwin. 6/20 at 10am PT.Sign up here »

tsbuf

0.2.0 • Public • Published

tsbuf npm@version Build Status Coverage Status

Generate TypeScript enum and interface with proto buffer.

Usage

npm install -g tsbuf
tsbuf example/proto -o example/typescript/global
# or 
tsbuf example/proto -o example/typescript/module -m module
 

See example/

$ tsbuf -h
Usage: tsbuf [options] <inputPath>
 
protobuf-parser
Generate TypeScript interface with Protobuf.
 
Options:
  -V, --version          output the version number
  -o, --output <output>  output path (default: ".")
  -m, --mode <mode>      "global": Global Definition, "module": Module Definition (default: "global")
  -h, --help             output usage information
 

Example

syntax = "proto3";
 
service MyService {
  rpc rpcMethod(Fruit) returns (Package) {}
}
 
enum Fruit {
  Apple = 0;
  Banana = 1;
}
 
message Package {
  string id = 1;
  float price = 2;
}
 

Will be transformed to

interface MyService {
  rpcMethod: {
    request: Request;
    response: Response;
  };
}
 
declare enum Fruit {
  Apple = 0,
  Banana = 1,
}
 
interface Package {
  id: string;
  price: number;
}

Or TypeScript module

export interface MyService {
  rpcMethod: {
    request: Request;
    response: Response;
  };
}
 
export enum Fruit {
  Apple = 0,
  Banana = 1,
}
 
export interface Package {
  id: string;
  price: number;
}
 

How to use interfaces from service?

Create specified types using TypeScript as follows.

import { MyService } from '...';
import { Observable } from 'rxjs';
 
interface BaseServiceDefinition {
  [key: string]: {
    request: any;
    response: any;
  };
}
 
type RxService<T extends BaseServiceDefinition> = {
  [K in keyof T]: (request: T[K]['request']) => Observable<T[K]['response']>;
};
 
/**
 * `RxService<MyService>` equals:
 *
 * interface {
 *   rpcMethod(request: Request): Observable<Response>;
 * }
 *
 **/
 

Roadmap

  • Basic Support
  • ExtendedType Field
  • Cli
  • Oneof Field
  • Map Field
  • Nested Type
  • Generate Global Declaration
  • Import (Generate Module)
  • Other Options

License

MIT

install

npm i tsbuf

Downloadsweekly downloads

6

version

0.2.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability