supermacro / neverthrow

Type-Safe Errors for JS & TypeScript
MIT License
4.03k stars 84 forks source link

How to serialise and deserialise result types? #613

Open plushdohn opened 1 week ago

plushdohn commented 1 week ago

I'm using neverthrow in the context of a full-stack app with typed RPCs. I'd like to be able to handle on the frontend the Result's that the backend returns, except they aren't serialisable because they contain functions.

Even if functions get stripped by a framework and only valid JSON is sent over the wire, I don't know how to recreate the respective Result on the frontend. Is there an API like fromSerialized(result)? Is this an intended use-case for neverthrow?

janglad commented 3 days ago

It's not super difficult to implement an "intermediary" result type that can be serialised, I did this here with ActionResult.

https://github.com/janglad/safe-fn/blob/main/packages/safe-fn/src/result.ts