Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto-loader: Add TypeScript generator #1474

Merged
merged 60 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
da3fefb
proto-loader: Add TypeScript generator
murgatroid99 Jun 12, 2020
3d369e9
Update to prerelease version, fix copyright date
murgatroid99 Jun 12, 2020
8bfa472
Fix a couple of type name errors
murgatroid99 Jun 12, 2020
c6e17f2
Fix constructor argument declaration
murgatroid99 Jun 18, 2020
c7bbf04
Add json option and google.protobuf.Any wrapper type
murgatroid99 Jun 18, 2020
f37e7d4
A few more fixes to make the generated code compile
murgatroid99 Jun 24, 2020
110cb2f
Export service client interfaces
murgatroid99 Jun 25, 2020
c6d4ea5
Fix some type issues in the server interface
badsyntax Jul 3, 2020
945202d
Merge pull request #1 from badsyntax/generator-fixes
murgatroid99 Jul 3, 2020
06f63a2
Merge branch 'proto-loader_type_generator' of https://github.com/murg…
murgatroid99 Jul 6, 2020
f64599f
Bump to 0.6.0-pre6
murgatroid99 Jul 6, 2020
e03118d
Generate .ts files instead of .d.ts for better handling by tsc
murgatroid99 Jul 10, 2020
4cc4a17
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Jul 10, 2020
e80d894
Refactor shared code, remove mkdirp, sort some things for stable outp…
murgatroid99 Jul 10, 2020
e146672
Bump prerelease version again
murgatroid99 Jul 10, 2020
8e6dae7
Add option to generate comments
murgatroid99 Jul 10, 2020
cca1059
Add files for service definitions, remove redundant exports from root…
murgatroid99 Jul 13, 2020
4963b0d
Update protobufjs dependency to ^6.10.0
murgatroid99 Jul 20, 2020
b96cb3b
Make messages always optional, fix map type generation
murgatroid99 Jul 20, 2020
894f100
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Jul 20, 2020
5947186
Add 'golden' generator output from a file in gapic-showcase
murgatroid99 Jul 21, 2020
831755a
Merge branch 'proto-loader_type_generator' of https://github.com/murg…
murgatroid99 Jul 21, 2020
8438fc7
Fix golden file test
murgatroid99 Jul 21, 2020
437f534
Generate comments in golden files
murgatroid99 Jul 21, 2020
71e5cb9
Update submodules in test script
murgatroid99 Jul 21, 2020
d329387
Skip test properly on older versions
murgatroid99 Jul 21, 2020
29db7a8
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Jul 27, 2020
d8e0354
Fix imports of messages defined in other messages, add missing comma
murgatroid99 Aug 3, 2020
128d4e9
Add TS generator information to README
murgatroid99 Aug 4, 2020
bbff8cb
A few more service type fixes
murgatroid99 Aug 10, 2020
471c59f
Fix missing type argument in client streaming server handler
murgatroid99 Aug 10, 2020
7550c00
Regenerate golden files
murgatroid99 Aug 10, 2020
6389e92
Bump version to 0.6.0
murgatroid99 Aug 20, 2020
2d1cb15
Fix imports for messages without packages
murgatroid99 Aug 31, 2020
677741d
ConstructorParameters is already a TypeScript builtin
murgatroid99 Sep 2, 2020
acf403b
proto-loader: use method descriptor types to define server handlers
badsyntax Sep 24, 2020
829333c
Regenerate golden types
badsyntax Sep 24, 2020
4f52097
Merge pull request #2 from badsyntax/proto-loader_type_generator
murgatroid99 Sep 24, 2020
330d183
proto-loader: fix typescript generation callType for client streaming
badsyntax Oct 11, 2020
e85b72b
Regenerate golden types
badsyntax Oct 11, 2020
08f7e4e
Merge pull request #3 from badsyntax/fix-client-streaming-type
murgatroid99 Oct 11, 2020
96cba74
Use 'import type' where possible
murgatroid99 Oct 21, 2020
542f619
Merge branch 'proto-loader_type_generator' of https://github.com/murg…
murgatroid99 Oct 21, 2020
f2c82cc
Bump the version again to capture a merge
murgatroid99 Oct 21, 2020
94391ca
import type * as grpc, fix ConstructorParameters usage
murgatroid99 Oct 22, 2020
7c5c4e8
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Nov 25, 2020
4b3e0b6
Update version and dependencies for final release
murgatroid99 Nov 25, 2020
999634a
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Dec 1, 2020
d3ef8f3
proto-loader: Replace Windows CRLF pair with Unix LF
badsyntax Dec 20, 2020
f483384
Merge pull request #4 from badsyntax/proto-loader_type_generator--fix…
murgatroid99 Jan 5, 2021
ac86173
proto-loader: Add example usage to README
badsyntax Jan 8, 2021
cf9d0fd
proto-loader: Fix yargs types
badsyntax Jan 8, 2021
0496bcb
Merge pull request #5 from badsyntax/proto-loader_type_generator--fix…
murgatroid99 Jan 8, 2021
617cdd6
Merge branch 'master' into proto-loader_type_generator
murgatroid99 Jan 20, 2021
995540c
Update golden generated files to match recent changes
murgatroid99 Mar 18, 2021
5cf93cf
Fix a typo in a comment
murgatroid99 Mar 24, 2021
4742f9d
Combine output for input files with the same basename
murgatroid99 Mar 31, 2021
3ac1e6d
Address review comments
murgatroid99 Mar 31, 2021
0aa99dd
Merge branch 'proto-loader_type_generator' of github.com:murgatroid99…
murgatroid99 Mar 31, 2021
65f1eb4
Add default values to generator usage info
murgatroid99 Apr 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[submodule "packages/grpc-tools/deps/protobuf"]
path = packages/grpc-tools/deps/protobuf
url = https://github.com/protocolbuffers/protobuf
[submodule "packages/proto-loader/deps/gapic-showcase"]
path = packages/proto-loader/deps/gapic-showcase
url = https://github.com/googleapis/gapic-showcase.git
[submodule "packages/proto-loader/deps/googleapis"]
path = packages/proto-loader/deps/googleapis
url = https://github.com/googleapis/googleapis.git
[submodule "packages/grpc-js-xds/deps/envoy-api"]
path = packages/grpc-js-xds/deps/envoy-api
url = https://github.com/envoyproxy/data-plane-api.git
Expand Down
67 changes: 67 additions & 0 deletions packages/proto-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The options parameter is an object that can have the following optional properti
| `arrays` | `true` or `false` | Set empty arrays for missing array values even if `defaults` is `false` Defaults to `false`.
| `objects` | `true` or `false` | Set empty objects for missing object values even if `defaults` is `false` Defaults to `false`.
| `oneofs` | `true` or `false` | Set virtual oneof properties to the present field's name. Defaults to `false`.
| `json` | `true` or `false` | Represent `Infinity` and `NaN` as strings in `float` fields, and automatically decode `google.protobuf.Any` values. Defaults to `false`
| `includeDirs` | An array of strings | A list of search paths for imported `.proto` files.

The following options object closely approximates the existing behavior of `grpc.load`:
Expand All @@ -51,3 +52,69 @@ const options = {
oneofs: true
}
```

## Generating TypeScript types

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:
--help Show help [boolean]
--version Show version number [boolean]
--keepCase Preserve the case of field names
[boolean] [default: false]
--longs The type that should be used to output 64 bit integer
values. Can be String, Number[string] [default: "Long"]
--enums The type that should be used to output enum fields. Can
be String [string] [default: "number"]
--bytes The type that should be used to output bytes fields.
Can be String, Array [string] [default: "Buffer"]
--defaults Output default values for omitted fields
[boolean] [default: false]
--arrays Output default values for omitted repeated fields even
if --defaults is not set [boolean] [default: false]
--objects Output default values for omitted message fields even
if --defaults is not set [boolean] [default: false]
--oneofs Output virtual oneof fields set to the present field's
name [boolean] [default: false]
--json Represent Infinity and NaN as strings in float fields.
Also decode google.protobuf.Any automatically
[boolean] [default: false]
--includeComments Generate doc comments from comments in the original
files [boolean] [default: false]
-I, --includeDirs Directories to search for included files [array]
-O, --outDir Directory in which to output files [string] [required]
--grpcLib The gRPC implementation library that these types will
be used with [string] [required]
```

### 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);
```
Loading