https://github.com/means88/tsbuf
Generate TypeScript enum and interface from proto buffer.
https://github.com/means88/tsbuf
parser protobuf
Last synced: 9 months ago
JSON representation
Generate TypeScript enum and interface from proto buffer.
- Host: GitHub
- URL: https://github.com/means88/tsbuf
- Owner: Means88
- Created: 2018-10-25T15:06:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-27T16:33:38.000Z (almost 2 years ago)
- Last Synced: 2025-05-01T20:13:45.312Z (9 months ago)
- Topics: parser, protobuf
- Language: TypeScript
- Homepage:
- Size: 3.28 MB
- Stars: 11
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tsbuf [](https://www.npmjs.com/package/tsbuf) [](https://travis-ci.org/Means88/tsbuf) [](https://coveralls.io/github/Means88/tsbuf?branch=master)
Generate TypeScript enum and interface with proto buffer.
## Usage
```bash
npm install -g tsbuf
tsbuf example/proto -o example/typescript/global
# or
tsbuf example/proto -o example/typescript/module -m module
```
See `example/`
```console
$ tsbuf -h
Usage: tsbuf [options]
protobuf-parser
Generate TypeScript interface with Protobuf.
Options:
-V, --version output the version number
-o, --output output path (default: ".")
-m, --mode "global": Global Definition, "module": Module Definition (default: "global")
-h, --help output usage information
```
## Example
```proto
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
```typescript
interface MyService {
rpcMethod: {
request: Request;
response: Response;
};
}
declare enum Fruit {
Apple = 0,
Banana = 1,
}
interface Package {
id: string;
price: number;
}
```
Or TypeScript module
```typescript
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.
```typescript
import { MyService } from '...';
import { Observable } from 'rxjs';
interface BaseServiceDefinition {
[key: string]: {
request: any;
response: any;
};
}
type RxService = {
[K in keyof T]: (request: T[K]['request']) => Observable;
};
/**
* `RxService` equals:
*
* interface {
* rpcMethod(request: Request): Observable;
* }
*
**/
```
## Roadmap
- [x] Basic Support
- [x] ExtendedType Field
- [x] Cli
- [x] Oneof Field
- [x] Map Field
- [x] Nested Type
- [x] Generate Global Declaration
- [x] Import (Generate Module)
- [ ] Other Options
## License
MIT