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

grpc-js: TypeScript Support? #1417

Closed
moloch-- opened this issue May 9, 2020 · 5 comments
Closed

grpc-js: TypeScript Support? #1417

moloch-- opened this issue May 9, 2020 · 5 comments

Comments

@moloch--
Copy link

moloch-- commented May 9, 2020

Problem description

I get that grpc-js is a newer library, and there doesn't seem to be any detailed documentation or examples yet of how to use grpc-js so maybe I'm just doing something wrong. I essentially have some proto3 .proto files from an existing project and some gRPC definitions I'd like to use with Node v14 and TypeScript. This doesn't seem possible:

  • proto.Loader doesn't support types, so I need to generate static code files/types
  • pbjs and pbts don't generate gRPC static code
  • proto-gen-ts plug doesn't generate code usable with grpc-js, replacing imports doesn't work
./node_modules/.bin/grpc_tools_node_protoc \
    -I ./protobuf \
    --plugin="protoc-gen-ts=./node_modules/.bin/protoc-gen-ts" \
    --ts_out="service=grpc-node:${OUT_DIR}" \
    --grpc_out="generate_package_definition:${OUT_DIR}" \
    /protobuf/rpc/services.proto

sed -i '' 's/\"grpc\"/\"\@grpc\/grpc-js\"/1' ${OUT_DIR}/rpc/services_grpc_pb.d.ts

Results in:

(node:6297) UnhandledPromiseRejectionWarning: TypeError: services_grpc_pb_1.RPCClient is not a constructor
    at /Users/moloch/git/sliver-script/lib/client.js:452:23
    at new Promise (<anonymous>)
    at Client.connect (/Users/moloch/git/script/lib/client.js:451:16)
    at /Users/moloch/git/sliver-script-test/index.js:50:49
    at step (/Users/moloch/git/script-test/index.js:33:23)
    at Object.next (/Users/moloch/git/script-test/index.js:14:53)
    at fulfilled (/Users/moloch/git/script-test/index.js:5:58)
(Use `node --trace-warnings ...` to show where the warning was created)

Environment

  • MacOS v10.15.4
  • Node v14.x
  • brew
@murgatroid99
Copy link
Member

@grpc/grpc-js exports the same API as grpc, so switching the imports should work. Have you checked that the result of that sed script is correct?

@badsyntax
Copy link
Contributor

badsyntax commented May 10, 2020

@moloch-- I've created an example repo you can refer to: https://github.com/badsyntax/grpc-js-types

@murgatroid99 any thoughts about #1380 ?

If that PR was merged, i'll then update ts-protoc-gen to use @grpc/grpc-js imports to avoid the sed hackyness.

@murgatroid99
Copy link
Member

I merged #1380. I think it's a reasonable change and I understand why some people would prefer it to the load_package_definition option. It will go out in the next release, which I can hopefully do soon.

@moloch--
Copy link
Author

Thanks @badsyntax @murgatroid99 was able to get my use case working with the sed hacks but looking forward to a release that doesn't require it!

@murgatroid99
Copy link
Member

I have published grpc-tools version 1.9.0 with that new option.

tiero added a commit to tdex-network/tdex-protobuf that referenced this issue May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants