From d95edd662e2e176e0374e61600ff7f662ab719b6 Mon Sep 17 00:00:00 2001 From: Felix Hamann Date: Wed, 15 May 2019 22:28:22 +0200 Subject: [PATCH] tweak js http client get/post api a little --- static/js/services/httpClient.js | 24 +++++++++++++----------- static/js/utils/asyncForm.js | 2 +- static/js/utils/asyncTable.js | 2 +- static/js/utils/massInput.js | 21 +++++++++------------ static/js/utils/modal.js | 2 +- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/static/js/services/httpClient.js b/static/js/services/httpClient.js index a69d788fd..4d6dc1820 100644 --- a/static/js/services/httpClient.js +++ b/static/js/services/httpClient.js @@ -11,19 +11,19 @@ } } - function _fetch(url, method, additionalHeaders, body) { + function _fetch(options) { var requestOptions = { credentials: 'same-origin', headers: { }, - method: method, - body: body, + method: options.method, + body: options.body, }; - Object.keys(additionalHeaders).forEach(function(headerKey) { - requestOptions.headers[headerKey] = additionalHeaders[headerKey]; + Object.keys(options.headers).forEach(function(headerKey) { + requestOptions.headers[headerKey] = options.headers[headerKey]; }); - return fetch(url, requestOptions).then( + return fetch(options.url, requestOptions).then( function(response) { _responseInterceptors.forEach(function(interceptor) { interceptor(response); }); return Promise.resolve(response); @@ -48,7 +48,7 @@ return response.text().then(function (responseText) { var docFrag = document.createRange().createContextualFragment(responseText); - + var idAttrs = ['id', 'for', 'data-conditional-input', 'data-modal-trigger']; idAttrs.forEach(function(attr) { Array.from(docFrag.querySelectorAll('[' + attr + ']')).forEach(function(input) { @@ -65,11 +65,13 @@ } return { - get: function(url, headers) { - return _fetch(url, 'GET', headers); + get: function(args) { + args.method = 'GET'; + return _fetch(args); }, - post: function(url, headers, body) { - return _fetch(url, 'POST', headers, body); + post: function(args) { + args.method = 'POST'; + return _fetch(args); }, addResponseInterceptor: addResponseInterceptor, parseHTML: parseHTML, diff --git a/static/js/utils/asyncForm.js b/static/js/utils/asyncForm.js index eaf6f9221..2f6d40284 100644 --- a/static/js/utils/asyncForm.js +++ b/static/js/utils/asyncForm.js @@ -96,7 +96,7 @@ headers[MODAL_HEADER_KEY] = MODAL_HEADER_VALUE; } - HttpClient.post(url, headers, body) + HttpClient.post({ url: url, headers: headers, body: body }) .then(function(response) { if (response.headers.get("content-type").indexOf("application/json") !== -1) {// checking response header return response.json(); diff --git a/static/js/utils/asyncTable.js b/static/js/utils/asyncTable.js index 78d9ae90c..577ffb753 100644 --- a/static/js/utils/asyncTable.js +++ b/static/js/utils/asyncTable.js @@ -319,7 +319,7 @@ [asyncTableHeader]: asyncTableId }; - HttpClient.get(url, headers).then( + HttpClient.get({ url: url, headers: headers }).then( response => HttpClient.parseHTML(response, element.id) ).then(function(data) { setLocalStorageParameter('currentTableUrl', url.href); diff --git a/static/js/utils/massInput.js b/static/js/utils/massInput.js index 4f0400b6a..e7b594b3f 100644 --- a/static/js/utils/massInput.js +++ b/static/js/utils/massInput.js @@ -120,18 +120,15 @@ if (enctype !== 'multipart/form-data') headers['Content-Type'] = enctype; - - requestFn( - url, - headers, - requestBody, - ).then(response => HttpClient.parseHTML(response, element.id) - ).then(function(response) { - processResponse(response); - if (isAddCell) { - reFocusAddCell(); - } - }); + + requestFn({ url: url, headers: headers, body: requestBody }) + .then(response => HttpClient.parseHTML(response, element.id)) + .then(function(response) { + processResponse(response); + if (isAddCell) { + reFocusAddCell(); + } + }); } }; } diff --git a/static/js/utils/modal.js b/static/js/utils/modal.js index e0ba9c7c1..20c453873 100644 --- a/static/js/utils/modal.js +++ b/static/js/utils/modal.js @@ -167,7 +167,7 @@ throw new Error('HttpClient not found! Can\'t fetch modal content from ' + url); } - HttpClient.get(url, MODAL_HEADERS) + HttpClient.get({ url: url, headers: MODAL_HEADERS }) .then(response => HttpClient.parseHTML(response, element.id)) .then(processResponse); }