-
Notifications
You must be signed in to change notification settings - Fork 106
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
query parameters #118
Comments
I'm also interested to know this |
You just pass them in as part of the URL path. |
Would be great to have a query string option. |
you can use this: const objToQuery=obj=>Object.keys(obj).map(key=>`${key}=${obj[key]}`).join('&') |
you forgot about escaping :) |
if you need escaping then add it in. the point is it is very easy to do, no point adding it to this library which is minimalist on purpose |
I’ve gone back and forth on this one. One one hand, i hate having to do write this over and over again, but I also can’t think of a great way to add this without complicating things because we already use the When I first wrote this library |
What do people think about this:
const get = bent(url)
await get(‘/path’, bent.qs({ name: value })) We can even allow them to be used combinatorially, so you can set default values. const get = bent(url, bent.qs({ name: default })
await get(‘/path’, bent.qs({ name: value })) Or if you really want to make it difficult for us: const get = bent(url, bent.qs({ name: default })
await get(‘/path?name=valueThatWillBeOverwritten’, bent.qs({ name: value })) We can even force a key ordering when we produce the final URL so that we add some determinism to the queryParam generation that isn’t there by default. |
I was not aware of this URLSearchParams before, so looks like you can just do: new URLSearchParams(obj).toString() I think keep it simple and let the user do that themselves, is easy enough. A higher priority would be allowing setting the http method in the bent function #127 |
I think the await get('/path', bent.qs({ name: value })) |
It’s a little late to go changing the function signature. I’m actually quite fond of using destructuring this way, i do it a lot in other modules, but it’s a little verbose for |
I think the best thing to do is use const baseRequest = bent('https://example.com', 'json', {header: value})
await baseRequest('GET', '/path', bent.query(query), {header: value})
await baseRequest('POST', '/path', bent.body(body), {header: value}) |
I vote for this one. Currently, I'm using const getJSON = bent('http://XXX/notes?token=' + authToken, 'GET', 'json', 200)
// Later
const queryParams = new URLSearchParams({
page: pageNum,
}).toString()
const response = await getJSON('&' + queryParams) Assuming I'm thinking about this correctly, I could dramatically simplify the above code without all of the boiler plate. |
is there an option to pass a json object in to use as query parameters for a get operation ?
The text was updated successfully, but these errors were encountered: