2018-04-10 01:33:23 +00:00
|
|
|
const TIMEOUT = process.browser ? 60000 : 120000
|
2018-02-11 18:49:32 +00:00
|
|
|
|
2018-02-11 22:11:03 +00:00
|
|
|
function fetchWithTimeout (url, options) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(url, options).then(resolve, reject)
|
|
|
|
setTimeout(() => reject(new Error(`Timed out after ${TIMEOUT / 1000} seconds`)), TIMEOUT)
|
|
|
|
})
|
2018-02-11 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
function makeOpts (method, headers) {
|
|
|
|
return {
|
|
|
|
method,
|
|
|
|
headers: Object.assign(headers || {}, {
|
|
|
|
'Accept': 'application/json'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 02:20:33 +00:00
|
|
|
async function throwErrorIfInvalidResponse (response) {
|
2018-02-25 03:25:33 +00:00
|
|
|
let json = await response.json()
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
if (json && json.error) {
|
|
|
|
throw new Error(response.status + ': ' + json.error)
|
|
|
|
}
|
|
|
|
throw new Error('Request failed: ' + response.status)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
async function _fetch (url, options, timeout) {
|
2018-02-24 02:23:36 +00:00
|
|
|
let fetchFunc = timeout ? fetchWithTimeout : fetch
|
2018-04-18 05:08:15 +00:00
|
|
|
let response = await fetchFunc(url, options)
|
2018-02-25 03:25:33 +00:00
|
|
|
return throwErrorIfInvalidResponse(response)
|
2018-01-13 22:19:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
async function _putOrPost (url, body, headers, timeout, method) {
|
|
|
|
let opts = makeOpts(method, headers)
|
2018-04-10 01:30:15 +00:00
|
|
|
if (body) {
|
2018-04-18 05:08:15 +00:00
|
|
|
if (body instanceof FormData) {
|
|
|
|
opts.body = body
|
|
|
|
} else {
|
|
|
|
opts.body = JSON.stringify(body)
|
|
|
|
opts.headers['Content-Type'] = 'application/json'
|
|
|
|
}
|
2018-04-10 01:30:15 +00:00
|
|
|
}
|
2018-04-18 05:08:15 +00:00
|
|
|
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) {
|
|
|
|
return _putOrPost(url, body, headers, timeout, 'PUT')
|
2018-04-10 01:30:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 02:23:36 +00:00
|
|
|
async function _get (url, headers, timeout) {
|
2018-04-18 05:08:15 +00:00
|
|
|
return _fetch(url, makeOpts('GET', headers), timeout)
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-02-24 02:23:36 +00:00
|
|
|
|
2018-03-11 00:21:10 +00:00
|
|
|
async function _delete (url, headers, timeout) {
|
2018-04-18 05:08:15 +00:00
|
|
|
return _fetch(url, makeOpts('DELETE', headers), timeout)
|
2018-03-11 00:21:10 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function put (url, body, headers) {
|
2018-04-10 01:30:15 +00:00
|
|
|
return _put(url, body, headers, false)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function putWithTimeout (url, body, headers) {
|
2018-04-10 01:30:15 +00:00
|
|
|
return _put(url, body, headers, true)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function post (url, body, headers) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return _post(url, body, headers, false)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function postWithTimeout (url, body, headers) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return _post(url, body, headers, true)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function getWithTimeout (url, headers) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return _get(url, headers, true)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function get (url, headers) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return _get(url, headers, false)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:08:15 +00:00
|
|
|
export async function deleteWithTimeout (url, headers) {
|
2018-03-11 00:21:10 +00:00
|
|
|
return _delete(url, headers, true)
|
|
|
|
}
|
|
|
|
|
2018-02-24 02:23:36 +00:00
|
|
|
export function paramsString (paramsObject) {
|
2018-03-09 08:08:23 +00:00
|
|
|
let res = ''
|
|
|
|
Object.keys(paramsObject).forEach((key, i) => {
|
|
|
|
if (i > 0) {
|
|
|
|
res += '&'
|
|
|
|
}
|
|
|
|
res += encodeURIComponent(key) + '=' + encodeURIComponent(paramsObject[key])
|
2018-02-24 02:23:36 +00:00
|
|
|
})
|
2018-03-09 08:08:23 +00:00
|
|
|
return res
|
2018-02-24 22:49:28 +00:00
|
|
|
}
|