UNPKG

typescriptkit

Version:

Basic functionality for TypeScript projects

55 lines (53 loc) 2.32 kB
"use strict"; /* istanbul ignore next */ Object.defineProperty(exports, "__esModule", { value: true }); const ObjectExtensions_1 = require("../Extensions/ObjectExtensions"); const StringExtensions_1 = require("../Extensions/StringExtensions"); /* istanbul ignore next */ /** * Implementation of the xmlHttpclient converting eventhandlers to callbacks (For internal use) */ class HttpRequestExecutor { constructor() { /** * execte the Http GET call * @param url The url to point the request to * @param httpheaders (optional) The httpHeaders array to append to the request * @param success (optional) The callback after a succesfull request * @param error (optional) The callback after a failed request */ this.executeHttpGet = (url, httpHeaders, success, error) => { if (StringExtensions_1.StringExtensions.isNullOrWhitespace(url)) throw new URIError('Please provide a valid URL to call to!'); // Todo: validate url const httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = () => { if (httpRequest.readyState !== 4) return; if (httpRequest.status === 200) { success(httpRequest.responseText); } else { error(httpRequest.responseText, httpRequest.status); } }; httpRequest.open(`GET`, url, true); // true for asynchronous if (!ObjectExtensions_1.ObjectExtensions.isNullOrUndefined(httpHeaders) && httpHeaders.length > 0) this.applyRequestHeaders(httpRequest, httpHeaders); httpRequest.send(null); }; } applyRequestHeaders(xmlHttp, httpHeaders) { if (ObjectExtensions_1.ObjectExtensions.isNullOrUndefined(httpHeaders)) return; for (let headerKey in httpHeaders) { if (!httpHeaders.hasOwnProperty(headerKey)) continue; const headerValue = httpHeaders[headerKey]; xmlHttp.setRequestHeader(headerKey, headerValue); } } } exports.HttpRequestExecutor = HttpRequestExecutor; exports.default = HttpRequestExecutor; //# sourceMappingURL=HttpRequestExecutor.js.map