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

Ts proto integration does not support wrapper types #161

Open
jessmorecroft opened this issue Jul 22, 2022 · 1 comment
Open

Ts proto integration does not support wrapper types #161

jessmorecroft opened this issue Jul 22, 2022 · 1 comment

Comments

@jessmorecroft
Copy link

Hi,

Firstly great work on these libraries. I'm working on a wrapper of sorts myself. Basically I find the protobuf format fustratingly lacking - seriously, no ability to define required types?? - so have wrapped it with io-ts decoders and encoders and send all messages using the Struct and ListValue protobuf types - ie. JSON. This means I lose some of the GRPC tooling nice-ness using utils like BloomRPC in that all messages are Structs or ListValues, but within VS Code at least my clients and server imps are dealing with types based on the io-ts defined types - that is, with all the allowed and powerful constraints of io-ts, not handicapped by Google's half-way house binary protocol!

My one real quirk with nice-grpc is the encoding/decoding step, which doesn't seem to support wrapper types like the other integrations. Because I'm exclusively using Struct and ListValue, this means I need to do the wrap/unwrap explicitly, whereas before the grpc-js integration for example just handled it.

Here's an example of how I wrap methods where I have JSON in, JSON out .....:

export type Transport = (request: any, options?: CallOptions) => Promise<any>;

export type Server = (request: any, context: CallContext) => Promise<any>;

export type StructTransport = (
  request: DeepPartial<Struct>,
  options?: CallOptions
) => Promise<Struct>;

export type StructServer = (
  request: Struct,
  context: CallContext
) => Promise<Struct>;

export const wrapStructTransport: (transport: StructTransport) => Transport =
  (transport) => (request, options) =>
    transport(Struct.wrap(request), options).then((res) => Struct.unwrap(res));

export const makeStructServer: (server: Server) => StructServer =
  (server) => (request, context) =>
    server(Struct.unwrap(request), context).then((res) => Struct.wrap(res));

Ideally I'd like not to do this at all and just feed in JSON and/or a list, or handle JSON and/or a list out, but that would require nice-grpc to do this step for me, which I believe the grpc-js integration does for example via these encode/decode methods:

https://github.com/stephenh/ts-proto/blob/main/src/encode.ts

I'm guessing there was probably a good reason it wasn't done this way and can workaround the current solution, but this tweak would definitely make nice-grpc even nicer, IMO! :)

@aikoven
Copy link
Contributor

aikoven commented Jul 28, 2022

Hey,

Yep, this would be nice to have. Should not be hard to implement it in ts-proto, reusing these generateEncoder and generateDecoder helpers.

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

2 participants