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

Skipping transformation #283

Open
luixo opened this issue Dec 14, 2023 · 4 comments
Open

Skipping transformation #283

luixo opened this issue Dec 14, 2023 · 4 comments

Comments

@luixo
Copy link

luixo commented Dec 14, 2023

Hello,
I'm using superjson in my project (with tRPC) and there is a bunch of fields which are optional - hence emitting undefined in a server response going client-side.
It resolves to emptiness in json field, but a bunch of undefineds in meta which I don't actually need as I don't go through object props anywhere in my project.
Can I turn off this transformation somehow? Can I register an override over this type? If so - how can I make it be omitted?

@Skn0tt
Copy link
Collaborator

Skn0tt commented Dec 14, 2023

Hi @luixo! Can you elaborate on why you’re looking to cut out those superfluous fields in meta? If it‘s because of bandwidth concerns, I recommend to check how much of a size impact they really have after going through gzip. meta tends to be highly compressable.

If you still want to omit those bytes afterwards, you could traverse the object before passing it to SuperJSON and delete all undefined fields.

I try to keep SuperJSON light on config, so I‘m always recommending user-land solutions first. Let me know if that works for you!

@luixo
Copy link
Author

luixo commented Dec 15, 2023

It's mostly DX.
Going through a big amount of superfluous fields just feels.. sad.

so I‘m always recommending user-land solutions first

That would be great, actually.
Can I override (through register function) a transformer for a type that already has a transformer? If yes - can I make a serializer emit nothing to meta?

@Skn0tt
Copy link
Collaborator

Skn0tt commented Dec 20, 2023

Going through a big amount of superfluous fields just feels.. sad.

I get that feeling :D But I promise you, if you neatly tuck it away into its own little function, it will spark a little more joy, and the function will feel a sense of purpose in its life.

Jokes aside - I get it, but this is a matter of complexity on SuperJSON's end, and i'd like to keep that as low as possible.

Can I override (through register function) a transformer for a type that already has a transformer?

Going off the current implementation, it looks like custom-registered transformers are taking precedence over built-in transformers for undefined:

https://github.com/blitz-js/superjson/blob/0130a809f87df142b5e190e1b519be0c0280fb16/src/transformer.ts#L313-L339

So this should work, yes! If you end up using this, please send a pull request with a test for this, so we can make sure there's no regressions to it.

If yes - can I make a serializer emit nothing to meta?

Try it out and let us know what you found!

@luixo
Copy link
Author

luixo commented Feb 23, 2024

Try it out and let us know what you found!

I investigated the source code and found no way to omit transforming a non-deep object (which is not a plain object, array, map, set or registered class).
Should it be added? Is it in the scope of this package to let objects change between serialization / deserialization?

Or should I do deep omitting of undefined values myself before feeding the object to superjson?

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