crazy4groovy
1/20/2018 - 4:30 AM

NodeJS module for storing (public) JSON via myjson.com

NodeJS module for storing (public) JSON via myjson.com

fetch = fecth || require('cross-fetch')

function myjson(json /* : Object */, id /* : string */) /* : Promise */ {
  const options = {
      method: id ? 'PUT' : 'POST',
      headers: { "Content-Type": "application/json; charset=utf-8" },
      body: JSON.stringify(json)
  }

  const uri = `https://api.myjson.com/bins${id ? `/${id}` : ''}`
  return fetch(uri, options)
    .then(resp => resp.json())
    .then(data => ({
      uri,
      id: id || data.uri.match(/bins\/(.*)/)[1]
      // data
    }))
    .catch(err => console.error('ERR myjson:', err) || Promise.reject(err))
}

try { module.exports = myjson } catch (ignore) {}