From ac86173a2036ab502edc9e9b94d568d0d5fc0742 Mon Sep 17 00:00:00 2001 From: Richard Willis Date: Fri, 8 Jan 2021 08:11:24 +0000 Subject: [PATCH] proto-loader: Add example usage to README --- packages/proto-loader/README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/proto-loader/README.md b/packages/proto-loader/README.md index 7a97a3b1..198f9d2b 100644 --- a/packages/proto-loader/README.md +++ b/packages/proto-loader/README.md @@ -57,7 +57,7 @@ const options = { The `proto-loader-gen-types` script distributed with this package can be used to generate TypeScript type information for the objects loaded at runtime. More information about how to use it can be found in [the *@grpc/proto-loader TypeScript Type Generator CLI Tool* proposal document](https://github.com/grpc/proposal/blob/master/L70-node-proto-loader-type-generator.md). The arguments mostly match the `load` function's options; the full usage information is as follows: -``` +```console proto-loader-gen-types.js [options] filenames... Options: @@ -85,4 +85,33 @@ Options: --outDir, -O Directory in which to output files [string] [required] --grpcLib The gRPC implementation library that these types will be used with [string] [required] -``` \ No newline at end of file +``` + +### Example Usage + +Generate the types: + +```sh +$(npm bin)/proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto +``` + +Consume the types: + +```ts +import * as grpc from '@grpc/grpc-js'; +import * as protoLoader from '@grpc/proto-loader'; +import { ProtoGrpcType } from './proto/example'; +import { ExampleHandlers } from './proto/example_package/Example'; + +const exampleServer: ExampleHandlers = { + // server handlers implementation... +}; + +const packageDefinition = protoLoader.loadSync('./proto/example.proto'); +const proto = (grpc.loadPackageDefinition( + packageDefinition +) as unknown) as ProtoGrpcType; + +const server = new grpc.Server(); +server.addService(proto.example_package.Example.service, exampleServer); +```