-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
refactor(useFetch): change internal logic #549
Conversation
Dont merge yet, I Will change this PR to correct content-type and we need also handle form data in the assignment if the payload type is not provided. |
feat: added `formEncoded` allowing submission of forms feat: added `application/x-www-form-urlencoded` handling via URLSearchParams
content-type
handling
the headers should be handled before the |
chore: adjust tests to use Headers instance
I need to do one more thing, encode the data when using |
I need to add some tests for |
@wheatjs it is confusing the logic inside I think we can change the behavior and so we can use the |
@wheatjs I will change it so that you can compare with the original, looking at changes you will see what I mean. |
@wheatjs I cannot figure out how to include jest tests for |
@wheatjs the only way I have found is to include it on the demo (I will also add once executed (can be executed multiple times, with blob content changing between request) |
…ded` to the demo page
With this PR I don't know why don't allow to change the config object once initialized, I think it can be modified just not allowing to change it while fetch is in progress. Why is |
This is on purpose as we don't want to allow users to change the method after they've already set it. I think this is probably okay to merge though @antfu. There are a few other changes that need to be made to |
content-type
handling
I think the PR is done, waiting for reviews, tmr I'm taking a vacation until next Monday / Tuesday ... but I'll do it with an eye here. |
…d` and `formData` chore: remove extra commas and change tabs size from the examples
One last thing I must include: some tests for encoding. I cannot figure out how to test |
…formEncoded` and `formData` docs: rephrasing some entries on `formEncoded` and `formData`
I feel like this PR is doing to much and it is getting hard to review. Perhaps you can refocus this PR on the original problem is was solving and we can address the other fetch issues on another branch |
Can you create a branch and merge this pr on it? Maybe this will help to review it. |
error: false, | ||
errorMessage: undefined | ||
} | ||
switch(response.status) { |
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.
Seems like this should be:
if (response.ok) {
context.data = await response.blob();
} else {
// handle exceptional cases here.
}
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.
Sorry for the drive-by review. I saw this PR in here while I was submitting one of my own, and I've recently done a lot of work in this area (for RxJS).
This PR looks like it was a lot of very well thought out work and consideration, but I would advise against merging it in its current state for a few reasons:
- It adds a lot more weight to what should otherwise be a pretty light-weight helper.
- The handling of setting content-type is weighty and incorrect.
- The area that deals with
URLSearchParams
will add additional JSON parsing burden to any server consuming what is (in the majority case) supposed to be simple value types. And it's a lot of unnecessary work. - Most of the ergonomics gains that could be made from this PR seem like they may result in unintentional pain and a lot of extra weight to cover what, in my opinion, would be edge cases that developers could easily handle with their own code.
Again, this is just my general review that was completely unsolicited, and I really respect the author for putting in the effort and sticking to the debate in this PR.
I think maybe a much leaner, smaller, incremental step, with obvious value, might be attainable by refactoring this PR.
@@ -168,6 +185,46 @@ function isFetchOptions(obj: object): obj is UseFetchOptions { | |||
return containsProp(obj, 'immediate', 'refetch', 'beforeFetch', 'afterFetch') | |||
} | |||
|
|||
async function toURLSearchParams(body: any): Promise<URLSearchParams> { |
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'm not sure I understand the purpose of this function. The goal seems to be to deal with the fact that the user may have accidentally passed a search param that is an object, rather than a primitive value type.
- I'm not sure why it's asynchronous. I don't see any async work being done.
- It doesn't seem like a good idea to JSON encode everything, because then whatever is on the other end of the network will have to JSON decode each value. In other words, it will get a bunch of key/value pairs, and then be forced to
JSON.parse
(or the server language equivalent) for every single value in there, even if they're all strings.
I strongly think the onus of doing this work (and deciding if it's even necessary) should be on the developer making the fetch, and we should throw this logic out and let them handle it. They know their server (presumably). And this is covering a use case that generally doesn't exist in the 99% case. Most of the time, if someone is sending search params, they're value-type primitives, not objects. This is penalizing the 99% use case to accommodate a one-off scenario.
if (config.payload) { | ||
let payload = context.options?.body || config.payload | ||
|
||
if (payload) { |
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.
Most of the work below here does not need to be done. fetch
and it's related types, like Request
, will automatically figure out the proper Content-Type
header.
It's worth noting that this is even the case with XHR. I recently wrote some code related to this for XHR, which handles this that is much more concise. Hopefully that helps.
I strongly advise that we don't do things like check a payloadType
configuration or the like, because it will lead to user confusion and odd behaviors.
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.
We need to avoid to include a breaking change.
It is a pain payloadType
option, we must deal with it.
The content type must be addressed since can be wrong, for example, adding it when using multipart, you can see the pictures included in this pr.
From a user perspective, it Will be more useful to provide the payload as a raw object rather than using fetch ones,.
I'm waiting to be included in a separated branch.
Going to close this out as we plan on introducing |
closes #542
closes #539
closes #553
closes #526