2018-02-11 19:49:32 +01:00
|
|
|
const TIMEOUT = 15000
|
|
|
|
|
2018-02-11 23:11:03 +01:00
|
|
|
function fetchWithTimeout (url, options) {
|
2018-02-11 19:49:32 +01:00
|
|
|
return Promise.race([
|
|
|
|
fetch(url, options),
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => reject(new Error(`Timed out after ${TIMEOUT / 1000} seconds`)), TIMEOUT)
|
|
|
|
})
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function post (url, body) {
|
2018-02-11 19:49:32 +01:00
|
|
|
return (await fetchWithTimeout(url, {
|
2018-01-13 23:19:51 +01:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
})).json()
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export function paramsString (paramsObject) {
|
2018-01-13 23:19:51 +01:00
|
|
|
let params = new URLSearchParams()
|
|
|
|
Object.keys(paramsObject).forEach(key => {
|
2018-01-14 04:23:05 +01:00
|
|
|
params.set(key, paramsObject[key])
|
2018-01-13 23:19:51 +01:00
|
|
|
})
|
|
|
|
return params.toString()
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function get (url, headers = {}) {
|
2018-02-11 19:49:32 +01:00
|
|
|
return (await fetchWithTimeout(url, {
|
2018-01-13 23:19:51 +01:00
|
|
|
method: 'GET',
|
|
|
|
headers: Object.assign(headers, {
|
2018-02-09 07:29:29 +01:00
|
|
|
'Accept': 'application/json'
|
2018-01-13 23:19:51 +01:00
|
|
|
})
|
|
|
|
})).json()
|
2018-02-09 07:29:29 +01:00
|
|
|
}
|