DEV Community

Discussion on: ReasonML for production React Apps? ๐Ÿค” (Part 2)

yawaramin profile image
Yawar Amin • Edited

Check out , it's a JSON codec PPX. It should work something like this:

[@decco] type error = {message: string};
[@decco] type response = {
  // other response fields,
  error: option(error),

Now it can decode a Js.Json.t that you got from Fetch.Response.json:

|> then_(response =>
  switch (response_decode(response)) {
  | Belt.Result.Ok({error: Some({message})}) =>
  | response =>

This will resolve with a Js.Promise.t(Belt.Result.t(response, error)) meaning the service didn't return an error message but the JSON may or may not have parsed successfully, or reject with your exception containing the service error message.

Thread Thread
seif_ghezala profile image
Seif Ghezala ๐Ÿ‡ฉ๐Ÿ‡ฟ Author • Edited

This syntax looks much cleaner ๐Ÿคฉ. I will give it a try soon with the Request module and comment about it in this part of the article. Thank you!