-
Notifications
You must be signed in to change notification settings - Fork 22
fix: honor request options #50
fix: honor request options #50
Conversation
honor the case when no queryParams are passed instead of initializing them to an empty object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I prefer making the change here like you've done as opposed to checking if the options are empty before operating on them: https://github.com/ExpediaGroup/service-client/blob/master/lib/http/request.js#L187-L203
I ran a couple benchmarks for the different ways to go about this change:
For the sake of being more clear (and given the test results above), can you implement the more explicit code seen in the 3rd case test case in the links above?
We'll get this added to the 2.x version too once this is merged in.
This reverts commit 686d001.
ensure original query params in path are preserved even if a query params object is passed to the request
The only affected option is
So the new approach ensures not breaking the existing query string in path, if it exists, ie:
|
lib/http/request.js
Outdated
@@ -190,7 +190,7 @@ const create = async function (method, path, options, hooks = {}) { | |||
}) | |||
|
|||
if (options.queryParams) { | |||
path += `?${QueryString.stringify(options.queryParams)}` | |||
path += `${path.includes('?') ? '&' : '?'}${QueryString.stringify(options.queryParams)}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we also need to account for if path
already ends in a &
. We wouldn't want to have ?foo=bar&&fizz=buzz
.
Some options to address this:
- strip any trailing
&
before processing anything - don't add a
&
if one already exists - parse the path's query string into an object, merge
options.queryParams
into the object, then stringify and re-attach to the path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i took the third approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is great! I'll get this merged and released in v2 and v3 asap.
* honor request options honor the case when no queryParams are passed instead of initializing them to an empty object * Revert "honor request options" This reverts commit 686d001. * fix-honor-requests-options ensure original query params in path are preserved even if a query params object is passed to the request * refactor refactor to merge queryParams from path and from options Co-authored-by: Leonso <[email protected]>
@leonsomed Released in v2.1.2 and v3.0.1. |
Description
This PR enforces honoring on queryParams, pathParams, and header like it was supported in version < 2.1.1.
Motivation and Context
Previous to version 2.1.1 queryParams, pathParams, and header were honored to remain as the options that were explicitly passed to the request method. However, version 2.1.1 introduces a change where it will initialize queryParams, pathParams, and header to an empty object when they are not passed to the request method.
The behavior described above causes an issue with requests that contain queryParams in the path. For example:
/users?lucky=true
, this path contains an embeded queryParam but it will be stripped away before the execution because it is not included in the queryParams object. Prior to version 2.1.1 this was not a problem because an undefined queryParams will be ignored, however after version 2.1.1 an undefined queryParams will be initialized to an empty object and it will cause the removal of the embeddedlucky
query param.The reason this is important is because there are APIs that respond with link resources with embedded queryParams.
How Has This Been Tested?
Previously this call will strip away the lucky query param
client.request({ method: 'GET', path: '/users?lucky=true' });
Types of changes
Checklist: