Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return '?' + arr.join('&')
}

function _fetch (method, url, opts, data, queryParams) {
function _fetch (method, url, opts, data, queryParams, errorHandler) {
opts.method = method
opts.headers = opts.headers || {}
opts.responseAs = (opts.responseAs && ['json', 'text'].indexOf(opts.responseAs) >= 0) ? opts.responseAs : 'json'
Expand All @@ -36,11 +36,16 @@
}
var err = new Error(response.statusText)
err.response = response

if(errorHandler) {
errorHandler(response)
}

throw err
})
}

function fetchival (url, opts) {
function fetchival (url, opts, errorHandler) {
opts = opts || {}

var _ = function (u, o) {
Expand All @@ -52,23 +57,24 @@
}

_.get = function (queryParams) {
return _fetch('GET', url, opts, null, queryParams)
return _fetch('GET', url, opts, null, queryParams, errorHandler)
}

_.post = function (data) {
return _fetch('POST', url, opts, data)
return _fetch('POST', url, opts, data, null, errorHandler)
}

_.put = function (data) {
return _fetch('PUT', url, opts, data)
console.log('datadatadatadatadata', data);
return _fetch('PUT', url, opts, data, null, errorHandler)
}

_.patch = function (data) {
return _fetch('PATCH', url, opts, data)
return _fetch('PATCH', url, opts, data, null, errorHandler)
}

_.delete = function () {
return _fetch('DELETE', url, opts)
return _fetch('DELETE', url, opts, null, null, errorHandler)
}

return _
Expand Down