Skip to content

Simplify FetchRequestDelegate.prepareHeadersForRequest#798

Merged
dhh merged 1 commit intohotwired:mainfrom
seanpdoyle:prepare-request-delegate
Nov 27, 2022
Merged

Simplify FetchRequestDelegate.prepareHeadersForRequest#798
dhh merged 1 commit intohotwired:mainfrom
seanpdoyle:prepare-request-delegate

Conversation

@seanpdoyle
Copy link
Copy Markdown
Contributor

With the introduction of FetchRequest.acceptResponseType() in hotwired/turbo#647, the
FetchRequestDelegate.prepareHeadersForRequest callback interacts with more than just the FetchRequestHeaders instance passes as its first argument.

Additionally, every implementation of the FetchRequestDelegate interface implements the prepareHeadersForRequest method, so the fact that it's listed as optional and invoked with a guard clause are unnecessary.

This commit renames the method to prepareRequest, reduces the signature to a single FetchRequest argument, and no longer declares it as a conditional property on the interface.

With the introduction of `FetchRequest.acceptResponseType()` in
[hotwired#647][], the
`FetchRequestDelegate.prepareHeadersForRequest` callback interacts with
more than just the `FetchRequestHeaders` instance passes as its first
argument.

Additionally, every implementation of the `FetchRequestDelegate`
interface implements the `prepareHeadersForRequest` method, so the fact
that it's listed as optional and invoked with a guard clause are
unnecessary.

This commit renames the method to `prepareRequest`, reduces the
signature to a single `FetchRequest` argument, and no longer declares it
as a conditional property on the interface.

[hotwired#647]: https://github.com/hotwired/turbo/pull/647/files#diff-d4ee4683f7121e24a87566ef6854ee6090ee723a5299430338f5602febea8c1f
@seanpdoyle seanpdoyle force-pushed the prepare-request-delegate branch from 709afe1 to 6de2ad4 Compare November 20, 2022 20:28
@seanpdoyle
Copy link
Copy Markdown
Contributor Author

Hey @kevinmcconnell, this came out of some of the Turbo Stream work you've done. If you're available, I'd really appreciate some code review!

Copy link
Copy Markdown
Collaborator

@kevinmcconnell kevinmcconnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, @seanpdoyle! Thanks for pinging me on this 🙏

referrer?: URL

prepareHeadersForRequest?(headers: FetchRequestHeaders, request: FetchRequest): void
prepareRequest(request: FetchRequest): void
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍 I agree that this makes it simpler. As you say, this method no longer needs to be optional, given that every delegate implements it.

The headers are still the only part that gets modified at this stage. So if we wanted to keep the purpose of the method the same, another idea could be to move the acceptResponseType helper out of FetchRequest, and have it operate on a FetchRequestHeaders instance instead. That way the prepareHeadersForRequest method name still works. So something more like

acceptResponseType(headers, StreamMessage.contentType)

But, I like the way you have it here. Logically this method is used to "prepare the request", regardless of which specific properties are being set, so the more general name makes sense.

@dhh dhh merged commit 2b41a93 into hotwired:main Nov 27, 2022
@seanpdoyle seanpdoyle deleted the prepare-request-delegate branch November 27, 2022 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants