OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-web-ui / web / js / api.js

40 lines · javascript ref:
/**
 * API Client — fetch wrapper for pear-git-v2 JSON API
 */

const BASE = window.location.origin

export async function get (path) {
  const res = await fetch(BASE + path, {
    headers: { 'Accept': 'application/json' }
  })
  if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
  return res.json()
}

export async function post (path, body) {
  const res = await fetch(BASE + path, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: JSON.stringify(body)
  })
  if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
  return res.json()
}

export async function patch (path, body) {
  const res = await fetch(BASE + path, {
    method: 'PATCH',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: JSON.stringify(body)
  })
  if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
  return res.json()
}