babel-plugin-import-proto
Babel plugin enabling import
syntax for .proto
files.
Prerequisites
npm install -D @babel/core @grpc/proto-loader
npm install @grpc/grpc-js
Install
npm install -D babel-plugin-import-proto
In .babelrc
{
'plugins': ['import-proto']
}
Each time you modify a Protobuf file, the cache must be cleared for the changes to take effect.
Options
Option | Type | Default | Description |
---|---|---|---|
keepCase |
Boolean |
false |
Preserve field names. The default is to change them to camel case. |
longs |
`'String' | 'Number' | 'Long'` |
enums |
`'String' | 'Number'` | 'String' |
bytes |
`'String' | 'Array' | 'Buffer'` |
defaults |
Boolean |
false |
Set default values on output objects. |
arrays |
Boolean |
false |
Set empty arrays for missing array values even if defaults is false . |
objects |
Boolean |
false |
Set empty objects for missing object values even if defaults is false . |
oneofs |
Boolean |
false |
Set virtual oneof properties to the present field's name. |
includeDirs |
Array<String> |
[] |
A list of search paths for imported .proto files. |
Examples
import { test } from './test/fixtures/example.proto'
import grpc from 'grpc'
// server.js
const server = new grpc.Server()
server.addProtoService(test.fixture.exampleService.service, new ExampleServiceServerImplem())
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
// client.js
const client = new test.fixture.exampleService('localhost:50051', grpc.credentials.createInsecure())
client.getExampleEntity({ id: 0 }, function (err, exampleEntity) {
if (err) {
// do something
} else {
// do something
}
})