question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query string hook

See original GitHub issue

I love this library and its commitment to simplicity. I always reach for it on new projects. One thing I always need to add in is query string support. I’m glad this lib doesn’t include this out of the box, because there are so many ways to stringify query string objects.

Myself, and many devs I work with, aren’t satisfied with a system where the query string is built and then appended to the URI before passing it into xhr. In other words, this API:

const query = qs.stringify({ ... });
xhr.get(`/things?${query}`)

A declarative approach would be more preferable. However, it’s not trivial to wrap xhr to add in support for a qs option due to the numerous argument signatures it accepts. It’s not impossible, by any means, but this is what I currently do:

export default function request(uri, options, cb) {
  let params = {};
  // This handles the `xhr(options, cb)` syntax
  if (typeof uri === 'object') {
    params = uri;
  }
  // This handles the `xhr(uri, options, cb)` syntax
  else if (typeof uri === 'string' && typeof options === 'object') {
    params = options;
    params.uri = uri;
  }
  // This handles the `xhr(uri, cb)` syntax
  else {
    params.uri = uri;
  }

  // This adds support for the `qs` option
  const urlString = params.uri ? params.uri : params.url;
  params.uri = buildUrl(urlString, params);

  let callback;
  if (typeof options === 'function') {
    callback = options;
  } else if (typeof cb === 'function') {
    callback = cb;
  }

  return xhr(params, callback);
}

A simpler alternative would be a hook within this library that would work like follows:

  • If the hook is present, the value of the qs option will be passed to it. The return value of the hook is appended to the URI.
  • If the hook is not present, nothing happens
  • By default, there is no hook

The example code I wrote at the start of this issue would then become:

import xhr from 'xhr';
import qs from 'your-favorite-qs-lib';

xhr.queryStringStringify = qs.stringify;

xhr.get('/things', {qs: { ... });`

I’d be happy to whip up a PR if there’s interest. Otherwise, I understand the desire to keep this lib slim. No worries, either way!

Thanks for reading!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
naugturcommented, Apr 5, 2017

@jmeas Sure, just to see what the API could be, we can then iterate and see if we come up with something that’s not going to confuse anyone.

0reactions
jamespleasecommented, Apr 7, 2017

Resolved in #160 to be released in v3. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks - How to get parameter value from query string
with react class I use : const id = this.props.match.params.id;. reactjs · react-hooks · Share.
Read more >
trevorblades/use-query-string: A React hook that ... - GitHub
Given a location object and a history updater function, this hook will return an array who's first element is an object representing the...
Read more >
query_string | Hook - WordPress Developer Resources
Parameters. $query_string string. The query string to modify.
Read more >
Getting Query Strings (Search Params) in React Router
React Router v6 provides a useSearchParams() hook that we can use to read those query string search params that we need from the...
Read more >
useSearchParams v6.6.1 - React Router
The useSearchParams hook is used to read and modify the query string in the URL for the current location. Like React's own useState...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found