mpyw / axios-case-converter

Axios transformer/interceptor that converts snake_case/camelCase
MIT License
160 stars 18 forks source link

preservedKeys nested wildcards #28

Closed glenncoppens closed 3 years ago

glenncoppens commented 4 years ago

Hi,

Nice work you did! So there's two options to preserve key names, string[] and Function. I'm currently using the string[] approach, but it might be useful to only apply the middleware only on a subset of the request/response data.

Eg in a Json api there's (very often) extra metadata included in the response:

const requestData = {
  data : {
    data: {
      name: 'name',
      billing_street: 'street name',
      ...
    },
    meta: {
      current_page: 1,
      from: 1, 
      ...
    },
    ...
  }
}

Can we use the Functions-approach here so that the casing middleware is only applied on the data.data attribute?

Thanks!

mpyw commented 4 years ago

Nice idea! But it looks a little hard to implement 🤔

glenncoppens commented 4 years ago

I didn't really check your current underlying approach to rename the keys.

But what if the object is flattened and thén the keys are being renamed (and then unflatten again)? Then you could include 'data.data' or exclude 'data.meta'.. But I'm afraid the problem here may be performance degradation 🤔 Not sure what performance impact of eg https://github.com/hughsk/flat is..

mpyw commented 4 years ago

Yes, flattening all elements will have a large performance degradation. And we'll get into trouble on reshaping objects because this library accepts not only plain objects but also FormData URLSearchParams...

mpyw commented 4 years ago

Extend CaseFunction to accept 3rd argument like context? But it may have a medium or large impact...

image

mpyw commented 3 years ago

I'll close this since it looks quite hard to implement the feature without large breaking changes. But any PRs are still welcome, thanks.