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

camelize keys #42

Closed
mmahalwy opened this issue Dec 1, 2016 · 5 comments
Closed

camelize keys #42

mmahalwy opened this issue Dec 1, 2016 · 5 comments

Comments

@mmahalwy
Copy link

mmahalwy commented Dec 1, 2016

Cannot do this in a transform:

response.data = camelizeKeys(response.data);
@mmahalwy
Copy link
Author

mmahalwy commented Dec 1, 2016

The way I am getting around this is:

api.addResponseTransform(response => {
    if (response.data) {
      const data = camelizeKeys(response.data);

      Object.keys(response.data).forEach(key => {
        delete response.data[key];
      });

      Object.keys(data).forEach(key => response.data[key] = data[key]);
    }
  });

It's not elegant at all. Seems like I cannot change response.data by assigning a new object. I can only change keys and values.

@skellock
Copy link
Contributor

skellock commented Dec 1, 2016

Agreed. I really didn't like how I implemented that at all.

The problem is I wanted to play it safe by passing a new object to each transform. I was encountering situations where I was able to break things if i mutated the wrong object (like config).

So although, it looks like the transform is receiving the response. It's not. It's receiving a decoy.

That's why changing references is messing up your evening.

I started out on the immutable route, but after seeing how slow transforms were once you started passing large data (like file uploads), i opted for the solution you see now.

@mmahalwy
Copy link
Author

mmahalwy commented Dec 1, 2016

@skellock makes sense. What do you suggest is the best way then? My solution works for now, but it's... not great.

@skellock
Copy link
Contributor

skellock commented Dec 3, 2016

Ok @mmahalwy , apisauce@0.7.0 is now out. Reassigning data (and everything else) is now a thing.

response.data = camelizeKeys(response.data)

@mmahalwy
Copy link
Author

mmahalwy commented Dec 4, 2016

woohooo! Thank you!

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