reststate / reststate-client

Zero-configuration JSON:API web service client
https://client.reststate.codingitwrong.com
Other
9 stars 11 forks source link

[Feature Request] Options query should be parsed recursively and allow custom formats #35

Open Niels-NTG opened 5 years ago

Niels-NTG commented 5 years ago

When supplying a request with an option object like this:

                {
                    page: {
                        page: this.currentPage,
                        itemsPerPage: this.itemsPerPage,
                    },
                    sort: this.sort
                }

nested objects like page aren't parsed like expected, resulting in an URL like /organisations?page=%5Bobject%20Object%5D&sort=id instead of /organisations?page[page]=1&page[itemsPerPage]=20&sort=id

I can think of 3 solutions to this issue:

  1. use a library like qs and make the formatting of the output configurable.
  2. Remove the getOptionsQuery method and let the API client deal with the parsing instead by passing the params to the .get method: return this.api.get(url, options). 👈
    get(path, options) {
        const params = qs.stringify(options, {
            arrayFormat: 'brackets',
            encode: false
        })
        path = `${path}?${params}`
        return APIClient.get(path)
    },
  3. Make the getOptionsQuery method overridable so people can replace it with their own parsing functions. Something like this:
    reststateClient.getOptionsQuery = (optionsObject = {}) => {
    return qs.stringify(optionsObject, {
        arrayFormat: 'brackets',
        encode: false
    })
    }
CodingItWrong commented 3 years ago

FYI, this library will be unmaintained going forward.

If you need new features or fixes, I recommend forking the repo and making changes, or finding an alternate library that meets your needs.