Add convenience methods to request() functions for HTTP verbs #765
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds convenience methods for HTTP verbs to the
request()
function returned byuseRequest()
, as discussed here. For example, the following two calls will send the same request:The parameters of the convenience methods are the same as they were in the
request
mixin.I want to make the
request()
function returned byuseRequest()
and therequest()
method ofrequestData
resources as similar as possible (see #675). With that in mind, I've also added the convenience methods to therequest()
method. Therequest()
method usually sends GET requests, but there are several cases where it sends a non-GET request.What has been done to verify that this works as intended?
New tests.
Why is this the best possible solution? Were any other approaches considered?
In this PR,
useRequest()
andrequestData
call a shared function namedwithHttpMethods()
, which adds convenience methods to anyrequest()
function/method. The main question I had was how to implementwithHttpMethods()
. I wanted to avoid eagerly creating five additional functions for every component that callsuseRequest()
, because most components will use at most one convenience method (and many components won't use a convenience method at all). I tried two different working approaches, which are both in the commit history.One approach I considered was to subclass
Function
. The convenience methods would go on the subclass's prototype. However, after briefly investigating this option, it sounded like extendingFunction
involved complications.The next approach I considered was to wrap the
request()
function in a proxy. The proxy would behave the same as therequest()
function except when getting one of the convenience methods. In that case, the proxy would create the convenience method on the fly. I got this approach to work, and you can see it in the commit history.I then realized that if the convenience methods are properties of the
request()
function, they can access therequest()
function viathis
. That means that we don't need to create different convenience methods for eachrequest()
function: allrequest()
functions can use the same convenience methods. So the approach I ended up taking was to define the convenience methods once, then havewithHttpMethods()
assign them on therequest()
function.this
ended up coming up in another issue I encountered, which is that calling a convenience method results in a differentthis
compared to callingrequest()
directly. That isn't an issue foruseRequest()
, but it comes up forrequestData
, because therequest()
method referencesthis
. For example, if you callsomeResource.request()
, thenthis
issomeResource
. But if you callsomeResource.request.post()
, thenthis
issomeResource.request
. I was able to resolve this by bindingsomeResource.request
tosomeResource
.How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?
This change is not user-facing.
Does this change require updates to user documentation? If so, please file an issue here and include the link below.
No changes needed.
Before submitting this PR, please make sure you have:
npm run test
andnpm run lint
and confirmed all checks still pass OR confirm CircleCI build passes