Monad for an API response

Viewed 159

I want to build an ApiClient that transforms API responses into a Monad.

Normally a successful response looks like this:

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

And the error response like this:

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

My first approach was using the Either monad so I no longer need the ok field. The Left value would be error and the Right one data. But what should I do with headers and other meta information of the response?

Most of the time I do not care that much about the meta information of an api response. I like how the Either monad helps to focus my code in the most important value. However now I must hack my data and error fields.

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

1 Answers

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

Related