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

option to specify qs query params array option #6

Closed
bargar opened this issue May 7, 2020 · 6 comments
Closed

option to specify qs query params array option #6

bargar opened this issue May 7, 2020 · 6 comments

Comments

@bargar
Copy link

bargar commented May 7, 2020

The qs arrayFormat option is currently hardcoded as comma

There are four options that may be useful depending on the api being accessed:

  • indices
  • brackets
  • repeat
  • comma

Happy to contribute a PR if useful.

@aribouius
Copy link
Owner

@bargar would be happy to support this, it had crossed my mind before.

A PR would we welcomed, if not I was planning on making some updates this weekend and could give it a go then.

@bargar
Copy link
Author

bargar commented May 8, 2020

@aribouius I started to work on this, and quickly ran into an issue.

Our current need is that our api expects filter to use the brackets option. I.e.

filter[post][]=1&filter[post][]=2

rather than

filter[post]=1,2

Using brackets for filter would be advantageous since the filter values themselves may have commas in them.

However, our api expects comma for include, which is clearly required by the json-api spec.

comma for filter is recommended by way of example but perhaps not strictly required. Of course, I'm assuming you implemented it as commas based on this precedent.

Though it would solve our problem, the disadvantage in applying different array formats for different portions of the query is clear. The serialization in the lib is currently universal and would be complicated by such a change.

Pausing for the moment to weigh various approaches. Certainly interested to hear if you had any thoughts.

@aribouius
Copy link
Owner

@bargar yeah that's a tricky situation. All of the API's I've worked with have supported the comma format for everything, so the issue hadn't really crossed my mind.

From what I could tell the qs library doesn't support an option to customize how a value gets stringified based on its key, so no help there.

I took a stab at providing a solution that meets your use-case. Basically it adds a stringify option that can either be an options hash, or a custom function. The idea is that you could manually specify how to operate on the object. It's not ideal for your scenario, but I couldn't quite think of a more elegant solution.

new ApiClient({
  stringify: (params, stringify) => {
    const { filter, ...rest } = params
    return query = stringify(rest) + (filter ? '&' + stringify({ filter }) : '')
  }
})

You can view the PR here, let me know what you think.

@bargar
Copy link
Author

bargar commented May 13, 2020

@aribouius excellent addition to jsonapi-react.

  • supports the default case as is with no configuration
  • low effort modification of the qs arrayFormat for those with that use case
  • outright override for full flexibility

The fact that we have access to the default stringify for the third case, in particular, is a nice touch, making it possible to configure different options without importing qs in the client and overriding jsonapi-react stringify completely.

This allows us to neatly solve our use case:

export default new ApiClient({
  stringify: ({ filter, ...otherParams }, stringify) => {
    return (
      stringify(otherParams) +
      (filter ? "&" + stringify({ filter }, { arrayFormat: "brackets" }) : "")
    );
  }
});

Nice work!

@aribouius
Copy link
Owner

@bargar this has been published, version 0.0.15 :)

@bargar
Copy link
Author

bargar commented May 20, 2020

Excellent news! Will grab forthwith...

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