From 2d5714a2ea890530b57de742c771f15f238c6124 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Tue, 17 Apr 2018 22:08:15 -0700 Subject: [PATCH] simplify ajax.js implementation (#157) * simplify ajax.js implementation * fix typo --- routes/_utils/ajax.js | 97 +++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 59 deletions(-) diff --git a/routes/_utils/ajax.js b/routes/_utils/ajax.js index 928e384..81ca1de 100644 --- a/routes/_utils/ajax.js +++ b/routes/_utils/ajax.js @@ -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) { let json = await response.json() if (response.status >= 200 && response.status < 300) { @@ -18,96 +27,66 @@ async function throwErrorIfInvalidResponse (response) { 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 opts = { - 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) + let response = await fetchFunc(url, options) 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) { - 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) + return _putOrPost(url, body, headers, timeout, 'PUT') } async function _get (url, headers, timeout) { - let fetchFunc = timeout ? fetchWithTimeout : fetch - let response = await fetchFunc(url, { - method: 'GET', - headers: Object.assign(headers, { - 'Accept': 'application/json' - }) - }) - return throwErrorIfInvalidResponse(response) + return _fetch(url, makeOpts('GET', headers), timeout) } 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) + return _fetch(url, makeOpts('DELETE', headers), timeout) } -export async function put (url, body, headers = {}) { +export async function put (url, body, headers) { 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) } -export async function post (url, body, headers = {}) { +export async function post (url, body, headers) { 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) } -export async function getWithTimeout (url, headers = {}) { +export async function getWithTimeout (url, headers) { return _get(url, headers, true) } -export async function get (url, headers = {}) { +export async function get (url, headers) { return _get(url, headers, false) } -export async function deleteWithTimeout (url, headers = {}) { +export async function deleteWithTimeout (url, headers) { return _delete(url, headers, true) }