Skip to content

Instantly share code, notes, and snippets.

@morbusg
Created April 13, 2018 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morbusg/415cb78cb31ce6b3c06d1a14eb80abe9 to your computer and use it in GitHub Desktop.
Save morbusg/415cb78cb31ce6b3c06d1a14eb80abe9 to your computer and use it in GitHub Desktop.
ES6 fetch helper for couch
couch = (uri, opts = {body: null, params: {include_docs: true}}) =>
fetch(uri.concat('?', Object.entries(opts.params).map(ary =>
ary.map(encodeURIComponent).join('=')).join('&')),
Object.assign({
...(opts.body && { body: JSON.stringify(opts.body) })
, headers: {
'Accept': 'application/json'
, 'Content-Type': 'application/json'
}
, method: 'GET'
}, opts)).
then(resp => resp.ok ? resp.json() : Promise.reject(resp.statusText)).
then(json => json.error ? Promise.reject(json.error) : json).
then(json => json.rows ? json.rows.map(r => r.doc ? r.doc : r.value) :
json).
catch(console.debug)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment