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

Rename key while encoding? #291

Closed
aaronsky opened this issue Dec 27, 2018 · 3 comments
Closed

Rename key while encoding? #291

aaronsky opened this issue Dec 27, 2018 · 3 comments

Comments

@aaronsky
Copy link

Hey! Thanks for making this package, I'm finding it much more robust than the custom querystring constructor I wrote myself.

Here's my question: is there a way to rename a key while encoding its value? I'm wrestling with an API that expects the consumer to potentially provide limit?: number and limit?: { // ... } on some object, potentially at the same time. It's weird and frustrating, since I can't have two members with the same key on the query object. However, when expressed in the querystring limit=20&limit[apps]=30, it's technically valid. One way I'm getting around this is to name the object-typed limit key limitField to maintain symmetry with other parts of the query. In my custom stringify-er, I renamed the limitField key back to limit manually. Is it possible with the feature set that qs has today to perform that rename during processing?

@ljharb
Copy link
Owner

ljharb commented Jan 1, 2019

I have no concept of how limit=20&limit[apps]=30 makes any sense given that rails, php, and express will likely be unable to parse that into an object structure properly.

It certainly seems reasonable to provide (in both parse and stringify), a kind of mapKey option, that takes a key and its value and returns a mapped key name. Would that suffice? I'm thinking you could provide something like mapKey: (key, value) => key !== 'limitField' ? key : 'limit'.

This might be complex to implement, however, so unless someone submits a PR it might not happen quickly.

@aaronsky
Copy link
Author

aaronsky commented Jan 1, 2019

Yeah, it’s a real frustrating decision they made.

That solution sounds great! I’ll have to dig around to see what needs to happen to support this. Thanks for getting back to me!

@ljharb
Copy link
Owner

ljharb commented Sep 21, 2019

@aaronsky you closed #293; are you no longer interested in this feature?

Closing; but happy to reopen if there's still interest.

@ljharb ljharb closed this as completed Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants