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 = {
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,

View File

@ -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();

View File

@ -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);

View File

@ -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();
}
});
}
};
}

View File

@ -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);
}