src/utils.js
function ajax( opts ) {
  const method = (opts.method || 'get').toUpperCase()
  const {
    url,
    body,
    contentType,
    extraHeaders,
    useBearer = true,
    bearer
  } = opts || {}

  let requestInit = {
    method,
    headers: fetchHeaders({
      method,
      contentType,
      extraHeaders,
      useBearer,
      bearer
    }),
    credentials: 'same-origin'
  }
  if( method != 'GET' && method != 'HEAD' && method != 'OPTIONS') {
    requestInit.body = body
  }

  let request = new Request( url, requestInit )
  return fetch( request )
    .then( response => {
      if( !!response.ok ) {
        if( response.status == 204 ) {
          return {}
        }
        if( typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE ) {
          return response
        }
        if( !!response.json ) {
          return response.json()
        }
        else {
          return response
        }
      }
      if( !!response.json ) {
        return response.json()
                       .catch( e => Object({ status: response.status }) )
                       .then( e => Promise.reject( e ) )
      }
      else {
        return response 
      }
    })
}
function ajax( opts ) {
  const method = (opts.method || 'get').toUpperCase()
  const {
    url,
    body,
    contentType,
    extraHeaders,
    useBearer = true,
    bearer
  } = opts || {}

  let requestInit = {
    method,
    headers: fetchHeaders({
      method,
      contentType,
      extraHeaders,
      useBearer,
      bearer
    }),
    credentials: 'same-origin'
  }
  if( method != 'GET' && method != 'HEAD' && method != 'OPTIONS') {
    requestInit.body = body
  }

  let request = new Request( url, requestInit )
  return fetch( request )
    .then( response => {
      if( !!response.ok ) {
        if( response.status == 204 ) {
          return {}
        }
        if( typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE ) {
          return response
        }
        if( !!response.json ) {
          return response.json()
        }
        else {
          return response
        }
      }
      if( !!response.json ) {
        return response.json()
                       .catch( e => Object({ status: response.status }) )
                       .then( e => Promise.reject( e ) )
      }
      else {
        return response 
      }
    })
}
function ajax ( opts ) { const method = ( opts . method || 'get' ) . toUpperCase ( ) const { url , body , contentType , extraHeaders , useBearer = true , bearer } = opts || { } let requestInit = { method , headers : fetchHeaders ( { method , contentType , extraHeaders , useBearer , bearer } ) , credentials : 'same-origin' } if ( method != 'GET' && method != 'HEAD' && method != 'OPTIONS' ) { requestInit . body = body } let request = new Request ( url , requestInit ) return fetch ( request ) . then ( response => { if ( ! ! response . ok ) { if ( response . status == 204 ) { return { } } if ( typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE ) { return response } if ( ! ! response . json ) { return response . json ( ) } else { return response } } if ( ! ! response . json ) { return response . json ( ) . catch ( e => Object ( { status : response . status } ) ) . then ( e => Promise . reject ( e ) ) } else { return response } } ) }
Perform an ajax request.

Uses HTML5 fetch to perform an ajax request according to parameters
supplied via the options object.

@param {string} url - The URL to make the request to.
@param {string} method - The request method. Defaults to "get".
@param {string} body - Data to be sent with the request.
@param {string} contentType - The content type of the request. Defaults to "application/json".
@param {object} extraHeaders - Custom headers to add.
@param {boolean} useBearer - Flag indicating whether to include bearer authorization.
Perform an ajax request .