question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for codecs ( `stringify` and `parse` ) in transformer

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KATTcommented, Feb 5, 2022

I think it’s a good idea to opt-in on. I agree pretty urls are nice.

0reactions
github-actions[bot]commented, Oct 4, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found