infinitered / apisauce

Axios + standardized errors + request/response transforms.
MIT License
2.79k stars 185 forks source link

Pure functions for transforms #162

Open willian opened 6 years ago

willian commented 6 years ago

Hi, first of all: thanks for all the hard work on this library.

This is now an issue, but more a curiosity. Why the callback we add for a response transform can't be a pure function? Instead, it expects mutation to the response object from its arguments so it can be passed to axios internally.

For example, this doesn't work:

api.addResponseTransform(response => {
  const badluck = Math.floor(Math.random() * 10) === 0
  if (badluck) {
    return {
      ...response,
      data: {
        ...response.data,
        doorsOpen: false,
        message: 'I cannot let you do that.'
      }
    }
  }

  return response
})
skellock commented 6 years ago

It boils down to a bad judgement call and then being stuck. I wish I had a better answer. 🤣

I'd be open to deprecating it and moving into 2.0 with a better api entirely. =)

willian commented 6 years ago

@skellock thanks for answering me back. When you say "moving into 2.0..." do you mean a new version of apisauce? If so, do you have a list of things you think the new version should have?

Also, I think changing transforms functions to be pure will break compatibility with 1.x, right?

skellock commented 6 years ago

I guess technically that would be 1.0 eh? Since we're 0.15.x currently. I keep forgetting that.

Yes, that would be a breaking change. I'm ok with that. 0.15.x is stable enough to remain here.

So, now that you've got me thinking about it, here's whats on my wish list:

divyanshu-srivastava-vs commented 1 week ago

what is the status of this ticket ?