Skip to content

Commit

Permalink
move Accept header to FormSubmission
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Jan 2, 2021
1 parent 2c9ebec commit 7dd1282
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 35 deletions.
2 changes: 1 addition & 1 deletion src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class FormSubmission {
// Fetch request delegate

additionalHeadersForRequest(request: FetchRequest) {
const headers: FetchRequestHeaders = {}
const headers: FetchRequestHeaders = { Accept: "text/html; turbo-stream" }
if (this.method != FetchMethod.get) {
const token = getCookieValue(getMetaContent("csrf-param")) || getMetaContent("csrf-token")
if (token) {
Expand Down
34 changes: 0 additions & 34 deletions src/observers/stream_observer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FormSubmission } from "../core/drive/form_submission"
import { FetchResponse } from "../http/fetch_response"
import { StreamMessage } from "../core/streams/stream_message"
import { StreamSource } from "../core/types"
Expand All @@ -19,16 +18,12 @@ export class StreamObserver {
start() {
if (!this.started) {
this.started = true
addEventListener("turbo:submit-start", this.prepareFormSubmission, true)
addEventListener("turbo:before-fetch-response", this.inspectFetchResponse, false)
}
}

stop() {
if (this.started) {
this.started = false
removeEventListener("turbo:submit-start", this.prepareFormSubmission, true)
removeEventListener("turbo:before-fetch-response", this.inspectFetchResponse, false)
}
}

Expand All @@ -50,23 +45,6 @@ export class StreamObserver {
return this.sources.has(source)
}

prepareFormSubmission = <EventListener>((event: CustomEvent) => {
const formSubmission: FormSubmission = event.detail?.formSubmission
const headers: any = formSubmission.fetchRequest.fetchOptions.headers

if (headers) {
headers.Accept = [ "text/html; turbo-stream", headers.Accept ].join(", ")
}
})

inspectFetchResponse = <EventListener>((event: CustomEvent) => {
const response = fetchResponseFromEvent(event)
if (response && fetchResponseIsStream(response)) {
event.preventDefault()
this.receiveMessageResponse(response)
}
})

receiveMessageEvent = (event: MessageEvent) => {
if (this.started && typeof event.data == "string") {
this.receiveMessageHTML(event.data)
Expand All @@ -84,15 +62,3 @@ export class StreamObserver {
this.delegate.receivedMessageFromStream(new StreamMessage(html))
}
}

function fetchResponseFromEvent(event: CustomEvent) {
const fetchResponse = event.detail?.fetchResponse
if (fetchResponse instanceof FetchResponse) {
return fetchResponse
}
}

function fetchResponseIsStream(response: FetchResponse) {
const contentType = response.contentType ?? ""
return /text\/html;.*\bturbo-stream\b/.test(contentType)
}

0 comments on commit 7dd1282

Please sign in to comment.