(function () { 'use strict'; window.HttpClient = (function() { var _responseInterceptors = []; function addResponseInterceptor(interceptor) { if (typeof interceptor === 'function') { _responseInterceptors.push(interceptor); } } function _fetch(url, method, additionalHeaders, body) { var requestOptions = { credentials: 'same-origin', headers: { }, method: method, body: body, }; Object.keys(additionalHeaders).forEach(function(headerKey) { requestOptions.headers[headerKey] = additionalHeaders[headerKey]; }); return fetch(url, requestOptions).then( function(response) { _responseInterceptors.forEach(function(interceptor) { interceptor(response); }); return Promise.resolve(response); }, function(error) { return Promise.reject(error); } ).catch(function(error) { console.error(error); }); } function parseHTML(response, idPrefix) { if (!idPrefix) { idPrefix = Math.floor(Math.random() * 100000); } var contentType = response.headers.get("content-type"); if (contentType.indexOf("text/html") === -1) { throw new Error('Server returned ' + contentType + ' when HTML was expected'); } 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) { var value = idPrefix + '__' + input.getAttribute(attr); input.setAttribute(attr, value); }); }); return Promise.resolve(docFrag); }, function (error) { return Promise.reject(error); }).catch(function (error) { console.error(error); }); } return { get: function(url, headers) { return _fetch(url, 'GET', headers); }, post: function(url, headers, body) { return _fetch(url, 'POST', headers, body); }, addResponseInterceptor: addResponseInterceptor, parseHTML: parseHTML, } })(); })();