Monad for API response

I want to create an ApiClient that converts API responses to Monad.

Usually a successful response looks like this:

{
  ok: true,
  data: { ... },
  headers,
}

      

And an error response like this:

{
  ok: false,
  error: { ... },
  headers,
}

      

My first approach used the Libo monad , so I no longer need the ok field . The value of the left would be a mistake, and Right - one. But what should I do with headers and other meta information in the response?

Most of the time I care about the api response meta information. I love how nun Either helps me focus my code on the most important meaning. However, now I have to hack my data and error fields.

Is there another monad suitable for these cases? Or is this problem solved by some other approach?

+3


source to share


1 answer


It depends on how you want to use it later. you have either with two extra fields on it. I would recommend replacing the ok, data and error properties with type or or result and keep the headers and meta in the object.



0


source







All Articles