Add support for codecs ( `stringify` and `parse` ) in transformer
See original GitHub issueI’d like to have stringify
/encode
and parse
/decode
support in transformers along with serialize
and deserialize
, to replace encodeURIComponent
with something like jsurl which makes the resulting url way more readable and short, especially with superjson.
I’d probably use this only for queries, not mutations, so we need to handle the two separately. Also, you shouldn’t be allowed to provide only one of the two methods ( only parse
or stringify
).
I was thinking something like this:
type DataCodec = {
parse(value: string): any; // or decode
stringify(value: any): string; // or encode
}
type DataTransformer = {
serialize(object: any): any;
deserialize(object: any): any;
queryCodec?: DataCodec;
mutationCodec?: DataCodec;
}
Example:
import jsurl from 'jsurl'
import superjson from 'superjson'
const router = trpc.router().transformer({
serialize: superjson.serialize,
deserialize: superjson.deserialize,
queryCodec: {
parse: jsurl.parse,
stringify: jsurl.stringify,
},
mutationCodec: {
// ...
}
}) //...
In case you think this is worth it and agree on the design I’ll work on it myself. Let me know if you want me to move this in discussions.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
RFC: Refactoring transformers and Zod 3 · Issue #264 - GitHub
parse ? The user passes a value into stringToNumber.parse; The transformer passes this value through the parse function of its input schema ( ......
Read more >How To Use JSON.parse() and JSON.stringify() - DigitalOcean
A quick reference for the parse and stringify methods of the JSON object.
Read more >How to use JSON.stringify() and JSON.parse() in JavaScript
JSON.stringify() and JSON.parse() are useful tools for handling JSON-formatted content in JavaScript, though they have some limitations.
Read more >JSON Stringify Example – How to Parse a JSON Object with JS
parse () . Let's see how to do this with the fs.readFileSync() method. First, add the fs module to your project:
Read more >MessagePack: It's like JSON. but fast and small.
It's like JSON. but fast and small. MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think it’s a good idea to opt-in on. I agree pretty urls are nice.
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.