What is @protobuf-ts/plugin?
@protobuf-ts/plugin is a TypeScript plugin for Protocol Buffers (protobuf). It generates TypeScript code from .proto files, enabling developers to work with protobuf messages in a type-safe manner. The plugin supports both client and server-side code generation, making it versatile for various use cases.
What are @protobuf-ts/plugin's main functionalities?
TypeScript Code Generation
This feature allows you to generate TypeScript code from .proto files. The generated code includes type definitions and service clients, making it easier to work with protobuf messages in a type-safe manner.
```json
{
"scripts": {
"generate": "protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --ts_out=./generated -I ./protos ./protos/*.proto"
}
}
```
Client-Side Code Generation
This feature generates client-side code for interacting with protobuf services. The generated client can be used to make RPC calls to a server that implements the protobuf service.
```typescript
import { MyServiceClient } from './generated/my-service.client';
const client = new MyServiceClient('http://localhost:8080');
client.myMethod({ myField: 'value' }).then(response => {
console.log(response);
});
```
Server-Side Code Generation
This feature generates server-side code for implementing protobuf services. The generated server can be used to handle RPC calls from clients.
```typescript
import { MyServiceServer } from './generated/my-service.server';
class MyServiceImpl implements MyServiceServer {
myMethod(request) {
return { myField: 'response' };
}
}
```
Other packages similar to @protobuf-ts/plugin
protobufjs
protobufjs is a popular library for working with Protocol Buffers in JavaScript and TypeScript. It provides a runtime library and a code generator. Unlike @protobuf-ts/plugin, protobufjs focuses more on runtime parsing and serialization rather than generating TypeScript code.
ts-proto
ts-proto is another TypeScript code generator for Protocol Buffers. It generates idiomatic TypeScript code from .proto files, similar to @protobuf-ts/plugin. However, ts-proto aims to be more lightweight and closer to the original protobuf definitions.
grpc-tools
grpc-tools is a set of tools for working with gRPC and Protocol Buffers. It includes a code generator for various languages, including TypeScript. While it provides similar functionality to @protobuf-ts/plugin, grpc-tools is more focused on gRPC and may not be as TypeScript-centric.
@protobuf-ts/plugin
The protocol buffer compiler plugin for TypeScript: protobuf-ts
Installation:
# with npm:
npm install -D @protobuf-ts/plugin
# with yarn:
yarn add --dev @protobuf-ts/plugin
This will install the plugin as a development dependency.
Basic usage:
npx protoc --ts_out . --proto_path protos protos/my.proto
With some options:
npx protoc \
--ts_out . \
--ts_opt long_type_string \
--ts_opt optimize_code_size \
--proto_path protos \
protos/my.proto
protoc
is the protocol buffer compiler. protobuf-ts
installs it automatically.
Plugin parameters are documented in the MANUAL.
For a quick overview of protobuf-ts, check the repository README.