2018-06-13 14:38:36 +00:00
|
|
|
export const DEFAULT_TIMEOUT = 20000
|
|
|
|
export const MEDIA_WRITE_TIMEOUT = 90000 // media uploads can take awhile
|
|
|
|
export const WRITE_TIMEOUT = 45000 // allow more time if the user did a write action
|
2018-02-11 18:49:32 +00:00
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
function fetchWithTimeout (url, fetchOptions, timeout) {
|
2018-02-24 02:23:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-13 14:38:36 +00:00
|
|
|
fetch(url, fetchOptions).then(resolve, reject)
|
|
|
|
setTimeout(() => reject(new Error(`Timed out after ${timeout / 1000} seconds`)), timeout)
|
2018-02-24 02:23:36 +00:00
|
|
|
})
|
2018-02-11 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
function makeFetchOptions (method, headers) {
|
2018-04-18 05:08:15 +00:00
|
|
|
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-06-13 14:38:36 +00:00
|
|
|
async function _fetch (url, fetchOptions, options) {
|
|
|
|
let response
|
|
|
|
if (options && options.timeout) {
|
|
|
|
response = await fetchWithTimeout(url, fetchOptions, options.timeout)
|
|
|
|
} else {
|
|
|
|
response = await fetch(url, fetchOptions)
|
|
|
|
}
|
2018-02-25 03:25:33 +00:00
|
|
|
return throwErrorIfInvalidResponse(response)
|
2018-01-13 22:19:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 22:23:40 +00:00
|
|
|
async function _putOrPostOrPatch (method, url, body, headers, options) {
|
2018-06-13 14:38:36 +00:00
|
|
|
let fetchOptions = makeFetchOptions(method, headers)
|
2018-04-10 01:30:15 +00:00
|
|
|
if (body) {
|
2018-04-18 05:08:15 +00:00
|
|
|
if (body instanceof FormData) {
|
2018-06-13 14:38:36 +00:00
|
|
|
fetchOptions.body = body
|
2018-04-18 05:08:15 +00:00
|
|
|
} else {
|
2018-06-13 14:38:36 +00:00
|
|
|
fetchOptions.body = JSON.stringify(body)
|
|
|
|
fetchOptions.headers['Content-Type'] = 'application/json'
|
2018-04-18 05:08:15 +00:00
|
|
|
}
|
2018-04-10 01:30:15 +00:00
|
|
|
}
|
2018-06-13 14:38:36 +00:00
|
|
|
return _fetch(url, fetchOptions, options)
|
2018-02-24 02:23:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
export async function put (url, body, headers, options) {
|
2018-08-19 22:23:40 +00:00
|
|
|
return _putOrPostOrPatch('PUT', url, body, headers, options)
|
2018-02-24 02:23:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
export async function post (url, body, headers, options) {
|
2018-08-19 22:23:40 +00:00
|
|
|
return _putOrPostOrPatch('POST', url, body, headers, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function patch (url, body, headers, options) {
|
|
|
|
return _putOrPostOrPatch('PATCH', url, body, headers, options)
|
2018-02-24 02:23:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
export async function get (url, headers, options) {
|
|
|
|
return _fetch(url, makeFetchOptions('GET', headers), options)
|
2018-02-24 02:23:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:38:36 +00:00
|
|
|
export async function del (url, headers, options) {
|
|
|
|
return _fetch(url, makeFetchOptions('DELETE', headers), options)
|
2018-03-11 00:21:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|