tweak js http client get/post api a little
This commit is contained in:
parent
3dcb5a2b19
commit
d95edd662e
@ -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);
|
||||||
@ -48,7 +48,7 @@
|
|||||||
|
|
||||||
return response.text().then(function (responseText) {
|
return response.text().then(function (responseText) {
|
||||||
var docFrag = document.createRange().createContextualFragment(responseText);
|
var docFrag = document.createRange().createContextualFragment(responseText);
|
||||||
|
|
||||||
var idAttrs = ['id', 'for', 'data-conditional-input', 'data-modal-trigger'];
|
var idAttrs = ['id', 'for', 'data-conditional-input', 'data-modal-trigger'];
|
||||||
idAttrs.forEach(function(attr) {
|
idAttrs.forEach(function(attr) {
|
||||||
Array.from(docFrag.querySelectorAll('[' + attr + ']')).forEach(function(input) {
|
Array.from(docFrag.querySelectorAll('[' + attr + ']')).forEach(function(input) {
|
||||||
@ -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,
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -120,18 +120,15 @@
|
|||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user