Skip to content
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

Simplify FetchRequestDelegate.prepareHeadersForRequest #798

Merged
merged 1 commit into from
Nov 27, 2022

Conversation

seanpdoyle
Copy link
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
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
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 🙏

@@ -18,7 +18,7 @@ export type TurboFetchRequestErrorEvent = CustomEvent<{
export interface FetchRequestDelegate {
referrer?: URL

prepareHeadersForRequest?(headers: FetchRequestHeaders, request: FetchRequest): void
prepareRequest(request: FetchRequest): void
Copy link
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