simplify ajax.js implementation (#157)

* simplify ajax.js implementation

* fix typo
This commit is contained in:
Nolan Lawson 2018-04-17 22:08:15 -07:00 committed by GitHub
parent 74ea773c25
commit 2d5714a2ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,15 @@ function fetchWithTimeout (url, options) {
}) })
} }
function makeOpts (method, headers) {
return {
method,
headers: Object.assign(headers || {}, {
'Accept': 'application/json'
})
}
}
async function throwErrorIfInvalidResponse (response) { async function throwErrorIfInvalidResponse (response) {
let json = await response.json() let json = await response.json()
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
@ -18,96 +27,66 @@ async function throwErrorIfInvalidResponse (response) {
throw new Error('Request failed: ' + response.status) throw new Error('Request failed: ' + response.status)
} }
async function _post (url, body, headers, timeout) { async function _fetch (url, options, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch let fetchFunc = timeout ? fetchWithTimeout : fetch
let opts = { let response = await fetchFunc(url, options)
method: 'POST'
}
if (body) {
let isFormData = body instanceof FormData
opts.headers = Object.assign(headers, isFormData ? {
'Accept': 'application/json'
} : {
'Accept': 'application/json',
'Content-Type': 'application/json'
})
opts.body = isFormData ? body : JSON.stringify(body)
} else {
opts.headers = Object.assign(headers, {
'Accept': 'application/json'
})
}
let response = await fetchFunc(url, opts)
return throwErrorIfInvalidResponse(response) return throwErrorIfInvalidResponse(response)
} }
async function _putOrPost (url, body, headers, timeout, method) {
let opts = makeOpts(method, headers)
if (body) {
if (body instanceof FormData) {
opts.body = body
} else {
opts.body = JSON.stringify(body)
opts.headers['Content-Type'] = 'application/json'
}
}
return _fetch(url, opts, timeout)
}
async function _post (url, body, headers, timeout) {
return _putOrPost(url, body, headers, timeout, 'POST')
}
async function _put (url, body, headers, timeout) { async function _put (url, body, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch return _putOrPost(url, body, headers, timeout, 'PUT')
let opts = {
method: 'PUT'
}
if (body) {
opts.headers = Object.assign(headers, {
'Accept': 'application/json',
'Content-Type': 'application/json'
})
opts.body = JSON.stringify(body)
} else {
opts.headers = Object.assign(headers, {
'Accept': 'application/json'
})
}
let response = await fetchFunc(url, opts)
return throwErrorIfInvalidResponse(response)
} }
async function _get (url, headers, timeout) { async function _get (url, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch return _fetch(url, makeOpts('GET', headers), timeout)
let response = await fetchFunc(url, {
method: 'GET',
headers: Object.assign(headers, {
'Accept': 'application/json'
})
})
return throwErrorIfInvalidResponse(response)
} }
async function _delete (url, headers, timeout) { async function _delete (url, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch return _fetch(url, makeOpts('DELETE', headers), timeout)
let response = await fetchFunc(url, {
method: 'DELETE',
headers: Object.assign(headers, {
'Accept': 'application/json'
})
})
return throwErrorIfInvalidResponse(response)
} }
export async function put (url, body, headers = {}) { export async function put (url, body, headers) {
return _put(url, body, headers, false) return _put(url, body, headers, false)
} }
export async function putWithTimeout (url, body, headers = {}) { export async function putWithTimeout (url, body, headers) {
return _put(url, body, headers, true) return _put(url, body, headers, true)
} }
export async function post (url, body, headers = {}) { export async function post (url, body, headers) {
return _post(url, body, headers, false) return _post(url, body, headers, false)
} }
export async function postWithTimeout (url, body, headers = {}) { export async function postWithTimeout (url, body, headers) {
return _post(url, body, headers, true) return _post(url, body, headers, true)
} }
export async function getWithTimeout (url, headers = {}) { export async function getWithTimeout (url, headers) {
return _get(url, headers, true) return _get(url, headers, true)
} }
export async function get (url, headers = {}) { export async function get (url, headers) {
return _get(url, headers, false) return _get(url, headers, false)
} }
export async function deleteWithTimeout (url, headers = {}) { export async function deleteWithTimeout (url, headers) {
return _delete(url, headers, true) return _delete(url, headers, true)
} }