frontend/js/core/fetch.js
/* global fetch */
import { renderStreamMessage } from '@hotwired/turbo'
import ErrorModal from './error_modal'
export function fetchWithErrorHandling (url, options = {}) {
return fetch(url, options)
.then(response => {
if (!response.ok) { throw response }
return response
})
.catch(response => {
const title = `${response.status} (${response.statusText})`
response.text().then(content => ErrorModal.show({ title, content }))
throw response
})
}
export function fetchTurboStream (url, options = {}) {
options = {
...options,
headers: {
Accept: 'text/vnd.turbo-stream.html',
...options.headers
}
}
return fetchWithErrorHandling(url, options)
.then(response => response.text())
.then(html => renderStreamMessage(html))
.catch(() => { /* Error already handled */ })
}