fix: handle cases where Request input is Request or RequestInit #69212
+3
−2
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.
For Contributors
Fixing a Bug
Related Issues:
Tests Added:
Request
andRequestInit
objects are passed as inputs to ensure proper handling without altering HTTP methods or other options.Error Handling:
What?
This update improves the handling of inputs in our custom
Request
class, ensuring that when aRequest
orRequestInit
object is passed, all existing options are preserved.Why?
The current implementation fails to maintain options from the original
Request
orRequestInit
object, leading to unexpected behavior. Specifically, it results in scenarios where POST requests unintentionally become GET requests, as seen in my case withinmiddleware.ts
. This change prevents such issues, maintaining the integrity of the request configuration.How?
The fix is implemented by modifying the
Request
class constructor to properly handle instances where the input is either aRequest
object or aRequestInit
object. Specifically:Type Checking:
Request
or if it’s a non-string object containing aurl
property. This is done using the condition:Request
object.Preservation of Input Properties:
super(input, init)
, ensuring that all properties and methods from the inputRequest
orRequestInit
object are preserved when initializing the newRequest
.Fallback:
Request
object with the givenurl
andinit
options:This ensures that the
Request
object is constructed correctly in all scenarios, maintaining the integrity of HTTP methods and any additional request options.Note: This is my first contribution to this project. Feedback and guidance on the approach or any improvements are welcome.
Next Steps:
Request for Feedback:
Could a maintainer review this approach? I'm especially interested in whether there's a more optimal way to handle preserving options when constructing a new
Request
object.