Skip to content

Commit

Permalink
Simplify FetchRequestDelegate.prepareHeadersForRequest (#798)
Browse files Browse the repository at this point in the history
With the introduction of `FetchRequest.acceptResponseType()` in
[#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.

[#647]: https://github.com/hotwired/turbo/pull/647/files#diff-d4ee4683f7121e24a87566ef6854ee6090ee723a5299430338f5602febea8c1f
  • Loading branch information
seanpdoyle authored Nov 27, 2022
1 parent 515ae56 commit 2b41a93
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchRequest, FetchMethod, fetchMethodFromString, FetchRequestHeaders } from "../../http/fetch_request"
import { FetchRequest, FetchMethod, fetchMethodFromString } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { expandURL } from "../url"
import { dispatch, getAttribute, getMetaContent, hasAttribute } from "../../util"
Expand Down Expand Up @@ -149,11 +149,11 @@ export class FormSubmission {

// Fetch request delegate

prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) {
prepareRequest(request: FetchRequest) {
if (!request.isIdempotent) {
const token = getCookieValue(getMetaContent("csrf-param")) || getMetaContent("csrf-token")
if (token) {
headers["X-CSRF-Token"] = token
request.headers["X-CSRF-Token"] = token
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/drive/visit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter } from "../native/adapter"
import { FetchMethod, FetchRequest, FetchRequestDelegate, FetchRequestHeaders } from "../../http/fetch_request"
import { FetchMethod, FetchRequest, FetchRequestDelegate } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { History } from "./history"
import { getAnchor } from "../url"
Expand Down Expand Up @@ -335,7 +335,7 @@ export class Visit implements FetchRequestDelegate {

// Fetch request delegate

prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) {
prepareRequest(request: FetchRequest) {
if (this.acceptsStreamResponse) {
request.acceptResponseType(StreamMessage.contentType)
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FrameLoadingStyle,
FrameElementObservedAttribute,
} from "../../elements/frame_element"
import { FetchMethod, FetchRequest, FetchRequestDelegate, FetchRequestHeaders } from "../../http/fetch_request"
import { FetchMethod, FetchRequest, FetchRequestDelegate } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { AppearanceObserver, AppearanceObserverDelegate } from "../../observers/appearance_observer"
import {
Expand Down Expand Up @@ -238,14 +238,14 @@ export class FrameController

this.formSubmission = new FormSubmission(this, element, submitter)
const { fetchRequest } = this.formSubmission
this.prepareHeadersForRequest(fetchRequest.headers, fetchRequest)
this.prepareRequest(fetchRequest)
this.formSubmission.start()
}

// Fetch request delegate

prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) {
headers["Turbo-Frame"] = this.id
prepareRequest(request: FetchRequest) {
request.headers["Turbo-Frame"] = this.id

if (this.currentNavigationElement?.hasAttribute("data-turbo-stream")) {
request.acceptResponseType(StreamMessage.contentType)
Expand Down
4 changes: 2 additions & 2 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type TurboFetchRequestErrorEvent = CustomEvent<{
export interface FetchRequestDelegate {
referrer?: URL

prepareHeadersForRequest?(headers: FetchRequestHeaders, request: FetchRequest): void
prepareRequest(request: FetchRequest): void
requestStarted(request: FetchRequest): void
requestPreventedHandlingResponse(request: FetchRequest, response: FetchResponse): void
requestSucceededWithResponse(request: FetchRequest, response: FetchResponse): void
Expand Down Expand Up @@ -103,7 +103,7 @@ export class FetchRequest {

async perform(): Promise<FetchResponse | void> {
const { fetchOptions } = this
this.delegate.prepareHeadersForRequest?.(this.headers, this)
this.delegate.prepareRequest(this)
await this.allowRequestToBeIntercepted(fetchOptions)
try {
this.delegate.requestStarted(this)
Expand Down

0 comments on commit 2b41a93

Please sign in to comment.