Sha256: ba2ba7b2841cf7e90b2d855113fbc01af86f426c4ba9c3aecae7a3bc2ffaef45

Contents?: true

Size: 790 Bytes

Versions: 3

Compression:

Stored size: 790 Bytes

Contents

/* 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 }))
    })
}

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))
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.10.0 frontend/js/core/fetch.js
trestle-0.10.0.pre2 frontend/js/core/fetch.js
trestle-0.10.0.pre frontend/js/core/fetch.js