Pinafore-Web-Client-Frontend/routes/_utils/ajax.js

124 lines
3.2 KiB
JavaScript
Raw Normal View History

const TIMEOUT = process.browser ? 60000 : 120000
2018-02-11 23:11:03 +01:00
function fetchWithTimeout (url, options) {
2018-02-24 03:23:36 +01: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-25 03:20:33 +01:00
async function throwErrorIfInvalidResponse (response) {
2018-02-25 04:25:33 +01: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-02-24 03:23:36 +01:00
async function _post (url, body, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch
2018-02-24 23:49:28 +01:00
let opts = {
method: 'POST'
}
if (body) {
2018-03-02 06:21:49 +01:00
let isFormData = body instanceof FormData
opts.headers = Object.assign(headers, isFormData ? {
'Accept': 'application/json'
} : {
2018-01-13 23:19:51 +01:00
'Accept': 'application/json',
'Content-Type': 'application/json'
2018-02-24 23:49:28 +01:00
})
2018-03-02 06:21:49 +01:00
opts.body = isFormData ? body : JSON.stringify(body)
2018-02-24 23:49:28 +01:00
} else {
opts.headers = Object.assign(headers, {
'Accept': 'application/json'
})
}
2018-02-25 04:25:33 +01:00
let response = await fetchFunc(url, opts)
return throwErrorIfInvalidResponse(response)
2018-01-13 23:19:51 +01:00
}
async function _put (url, body, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch
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)
}
2018-02-24 03:23:36 +01:00
async function _get (url, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch
2018-02-25 04:25:33 +01:00
let response = await fetchFunc(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
})
2018-02-25 04:25:33 +01:00
})
return throwErrorIfInvalidResponse(response)
2018-02-09 07:29:29 +01:00
}
2018-02-24 03:23:36 +01:00
2018-03-11 01:21:10 +01:00
async function _delete (url, headers, timeout) {
let fetchFunc = timeout ? fetchWithTimeout : fetch
let response = await fetchFunc(url, {
method: 'DELETE',
headers: Object.assign(headers, {
'Accept': 'application/json'
})
})
return throwErrorIfInvalidResponse(response)
}
export async function put (url, body, headers = {}) {
return _put(url, body, headers, false)
}
export async function putWithTimeout (url, body, headers = {}) {
return _put(url, body, headers, true)
}
2018-02-24 03:23:36 +01:00
export async function post (url, body, headers = {}) {
return _post(url, body, headers, false)
}
export async function postWithTimeout (url, body, headers = {}) {
return _post(url, body, headers, true)
}
export async function getWithTimeout (url, headers = {}) {
return _get(url, headers, true)
}
export async function get (url, headers = {}) {
return _get(url, headers, false)
}
2018-03-11 01:21:10 +01:00
export async function deleteWithTimeout (url, headers = {}) {
return _delete(url, headers, true)
}
2018-02-24 03:23:36 +01:00
export function paramsString (paramsObject) {
2018-03-09 09:08:23 +01:00
let res = ''
Object.keys(paramsObject).forEach((key, i) => {
if (i > 0) {
res += '&'
}
res += encodeURIComponent(key) + '=' + encodeURIComponent(paramsObject[key])
2018-02-24 03:23:36 +01:00
})
2018-03-09 09:08:23 +01:00
return res
2018-02-24 23:49:28 +01:00
}