Skip to content

Commit

Permalink
allow to override turbo fetch response
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Mar 23, 2023
1 parent 9bc1f00 commit 8de3c39
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type TurboBeforeFetchRequestEvent = CustomEvent<{
fetchOptions: RequestInit
url: URL
resume: (value?: any) => void
response?: Promise<any>
}>
export type TurboBeforeFetchResponseEvent = CustomEvent<{
fetchResponse: FetchResponse
Expand Down Expand Up @@ -104,10 +105,10 @@ export class FetchRequest {
async perform(): Promise<FetchResponse | void> {
const { fetchOptions } = this
this.delegate.prepareRequest(this)
await this.allowRequestToBeIntercepted(fetchOptions)
const event = await this.allowRequestToBeIntercepted(fetchOptions)
try {
this.delegate.requestStarted(this)
const response = await fetch(this.url.href, fetchOptions)
const response = await (event.detail.response || fetch(this.url.href, fetchOptions))
return await this.receive(response)
} catch (error) {
if ((error as Error).name !== "AbortError") {
Expand Down Expand Up @@ -180,6 +181,8 @@ export class FetchRequest {
target: this.target as EventTarget,
})
if (event.defaultPrevented) await requestInterception

return event
}

private willDelegateErrorHandling(error: Error) {
Expand Down

0 comments on commit 8de3c39

Please sign in to comment.