Created
April 13, 2018 11:27
-
-
Save morbusg/415cb78cb31ce6b3c06d1a14eb80abe9 to your computer and use it in GitHub Desktop.
ES6 fetch helper for couch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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