index.js

import { SERVER_URL } from '../constants/app'
import { GET } from '../constants/path'

export async function callBubblesApi({ verb = GET, path, payload, headers }) {
  const serverUrl = `${SERVER_URL}${path}`
  try {
    const response = await fetch(serverUrl, {
      method: verb,
      headers: {
        'Content-Type': 'application/json',
        ...headers
      },
      body: JSON.stringify(payload)
    })
    if (response.status !== 200) {
      return errorMessage(response)
    }
    const result = await response.json()
    return {
      data: result,
      err: false
    }
  } catch (error) {
    return errorMessage(null)
  }
}

function errorMessage(response) {
  if (!response) {
    return { err: true, msg: 'Could not connect to bubbles server' }
  }

  return { err: true, msg: 'Error occured' }
}