tweak js http client get/post api a little

This commit is contained in:
Felix Hamann 2019-05-15 22:28:22 +02:00
parent 3dcb5a2b19
commit d95edd662e
5 changed files with 25 additions and 26 deletions

View File

@ -11,19 +11,19 @@
} }
} }
function _fetch(url, method, additionalHeaders, body) { function _fetch(options) {
var requestOptions = { var requestOptions = {
credentials: 'same-origin', credentials: 'same-origin',
headers: { }, headers: { },
method: method, method: options.method,
body: body, body: options.body,
}; };
Object.keys(additionalHeaders).forEach(function(headerKey) { Object.keys(options.headers).forEach(function(headerKey) {
requestOptions.headers[headerKey] = additionalHeaders[headerKey]; requestOptions.headers[headerKey] = options.headers[headerKey];
}); });
return fetch(url, requestOptions).then( return fetch(options.url, requestOptions).then(
function(response) { function(response) {
_responseInterceptors.forEach(function(interceptor) { interceptor(response); }); _responseInterceptors.forEach(function(interceptor) { interceptor(response); });
return Promise.resolve(response); return Promise.resolve(response);
@ -65,11 +65,13 @@
} }
return { return {
get: function(url, headers) { get: function(args) {
return _fetch(url, 'GET', headers); args.method = 'GET';
return _fetch(args);
}, },
post: function(url, headers, body) { post: function(args) {
return _fetch(url, 'POST', headers, body); args.method = 'POST';
return _fetch(args);
}, },
addResponseInterceptor: addResponseInterceptor, addResponseInterceptor: addResponseInterceptor,
parseHTML: parseHTML, parseHTML: parseHTML,

View File

@ -96,7 +96,7 @@
headers[MODAL_HEADER_KEY] = MODAL_HEADER_VALUE; headers[MODAL_HEADER_KEY] = MODAL_HEADER_VALUE;
} }
HttpClient.post(url, headers, body) HttpClient.post({ url: url, headers: headers, body: body })
.then(function(response) { .then(function(response) {
if (response.headers.get("content-type").indexOf("application/json") !== -1) {// checking response header if (response.headers.get("content-type").indexOf("application/json") !== -1) {// checking response header
return response.json(); return response.json();

View File

@ -319,7 +319,7 @@
[asyncTableHeader]: asyncTableId [asyncTableHeader]: asyncTableId
}; };
HttpClient.get(url, headers).then( HttpClient.get({ url: url, headers: headers }).then(
response => HttpClient.parseHTML(response, element.id) response => HttpClient.parseHTML(response, element.id)
).then(function(data) { ).then(function(data) {
setLocalStorageParameter('currentTableUrl', url.href); setLocalStorageParameter('currentTableUrl', url.href);

View File

@ -121,17 +121,14 @@
if (enctype !== 'multipart/form-data') if (enctype !== 'multipart/form-data')
headers['Content-Type'] = enctype; headers['Content-Type'] = enctype;
requestFn( requestFn({ url: url, headers: headers, body: requestBody })
url, .then(response => HttpClient.parseHTML(response, element.id))
headers, .then(function(response) {
requestBody, processResponse(response);
).then(response => HttpClient.parseHTML(response, element.id) if (isAddCell) {
).then(function(response) { reFocusAddCell();
processResponse(response); }
if (isAddCell) { });
reFocusAddCell();
}
});
} }
}; };
} }

View File

@ -167,7 +167,7 @@
throw new Error('HttpClient not found! Can\'t fetch modal content from ' + url); 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(response => HttpClient.parseHTML(response, element.id))
.then(processResponse); .then(processResponse);
} }