connectrpc / connect-es

The TypeScript implementation of Connect: Protobuf RPC that works.

Home Page:https://connectrpc.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connect Web - Add Separate Models to be returned from .toJson instead of returning a JsonValue

seanmcquaid opened this issue · comments

Is your feature request related to a problem? Please describe.
Presently, our team needs to utilize the .toJson method for our returned models from our auto generated services. However, we lose all semblance of references to the original model in the process. In grpc-web they have a similar method that returns a totally separate model that is a representation of the JSON returned from their method.

Describe the solution you'd like
It would be great if the .toJson method would return an auto generated model for the JSON instead of JsonValue.

Hey @seanmcquaid. Just to clarify what you're looking for here - you're trying to put a Protobuf message in a store that requires it to be a plain object that is JSON serializable, correct? If that is the case, then using toPlainMessage on the message may be your best approach. I say 'may' because you could run into issues if your schema has 64-bit integrals or bytes fields. But, if not, doing something like:

import { toPlainMessage } from "@bufbuild/protobuf";
...
const messageAsPlainType = toPlainMessage(message);

might be the best solution. You could then use the messageAsPlainType object in your store.

If you do have a schema with 64-bit integrals, then this may not work for you quite yet. However, we are going to be implementing an option that allows you to generate your 64-bit numbers as strings (see issue), so that would hopefully become a non-issue.

If none of this works for you or is not what you're looking for, we do have an issue for creating typed JSON on our radar, it's just not prioritized yet.

Ah yes, the mentioned issue is more inline with what I'm looking for! I am good to close this out.