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

Result parsing from interface compatible object #71

Closed
github-christophe-oudar opened this issue Mar 27, 2024 · 3 comments
Closed

Result parsing from interface compatible object #71

github-christophe-oudar opened this issue Mar 27, 2024 · 3 comments

Comments

@github-christophe-oudar

Hello,

I've been using boxed for API result validation using BlitzJS and I'm using a small helper as the class is obviously not transferred from backend to frontend through the "magic" link they introduce based on react queries.
Therefore to be able to access Result methods, I need to recreate them.
To do so, I use:

import { Result } from "@swan-io/boxed"

// util for result from swan-io/boxed
export function dictToResult<T1, T2>(
  d: { tag: "Ok"; value: T1 } | { tag: "Error"; error: T2 },
): Result<T1, T2> {
  if (d.tag === "Ok") {
    return Result.Ok(d.value)
  } else {
    return Result.Error(d.error)
  }
}

It might not be optimal (I'm not a Typescript expert) but it works.
I guess it could be useful to have a similar function added as static function to the Result object (like the typeguards etc) to make is accessible to more lib users.

It might be also safer for me as I bumped from 1.X to 2.X and saw that it moved the value to error field 🤫

@bloodyowl
Copy link
Member

hi! long time no see 😄

adding #72, will add those to the docs later this week

@github-christophe-oudar
Copy link
Author

Haha yep it's been a while since meatspace 😅! Say hi to Alix 🙃

I think it should work 👍
I might need to check how to force to use a specific JSON serializer for the framework magic but it looks like the __boxed_type__ metadata field is not mandatory if I plan to use Result.fromJSON at read time 👌

@bloodyowl
Copy link
Member

the __boxed_type__ has been removed from the fromJSON/toJSON logic (replaced with a non-iterable symbol), available in 2.2.0!

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