angular-ai-chat-bot
Version:
Angular 6 AI Chat Bot module with Google Api
125 lines (124 loc) • 5.17 kB
JavaScript
;
exports.__esModule = true;
var XhrRequest = /** @class */ (function () {
function XhrRequest() {
}
// Method that performs the ajax request
XhrRequest.ajax = function (method, url, args, headers, options) {
if (args === void 0) { args = null; }
if (headers === void 0) { headers = null; }
if (options === void 0) { options = {}; }
// Creating a promise
return new Promise(function (resolve, reject) {
// Instantiates the XMLHttpRequest
var client = XhrRequest.createXMLHTTPObject();
var uri = url;
var payload = null;
// Add given payload to get request
if (args && (method === XhrRequest.Method.GET)) {
uri += "?";
var argcount = 0;
for (var key in args) {
if (args.hasOwnProperty(key)) {
if (argcount++) {
uri += "&";
}
uri += encodeURIComponent(key) + "=" + encodeURIComponent(args[key]);
}
}
}
else if (args) {
if (!headers) {
headers = {};
}
headers["Content-Type"] = "application/json; charset=utf-8";
payload = JSON.stringify(args);
}
for (var key in options) {
if (key in client) {
client[key] = options[key];
}
}
// hack: method[method] is somewhat like .toString for enum Method
// should be made in normal way
client.open(XhrRequest.Method[method], uri, true);
// Add given headers
if (headers) {
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
client.setRequestHeader(key, headers[key]);
}
}
}
payload ? client.send(payload) : client.send();
client.onload = function () {
if (client.status >= 200 && client.status < 300) {
// Performs the function "resolve" when this.status is equal to 2xx
resolve(client);
}
else {
// Performs the function "reject" when this.status is different than 2xx
reject(client);
}
};
client.onerror = function () {
reject(client);
};
});
};
XhrRequest.get = function (url, payload, headers, options) {
if (payload === void 0) { payload = null; }
if (headers === void 0) { headers = null; }
if (options === void 0) { options = {}; }
return XhrRequest.ajax(XhrRequest.Method.GET, url, payload, headers, options);
};
XhrRequest.post = function (url, payload, headers, options) {
if (payload === void 0) { payload = null; }
if (headers === void 0) { headers = null; }
if (options === void 0) { options = {}; }
return XhrRequest.ajax(XhrRequest.Method.POST, url, payload, headers, options);
};
XhrRequest.put = function (url, payload, headers, options) {
if (payload === void 0) { payload = null; }
if (headers === void 0) { headers = null; }
if (options === void 0) { options = {}; }
return XhrRequest.ajax(XhrRequest.Method.PUT, url, payload, headers, options);
};
XhrRequest["delete"] = function (url, payload, headers, options) {
if (payload === void 0) { payload = null; }
if (headers === void 0) { headers = null; }
if (options === void 0) { options = {}; }
return XhrRequest.ajax(XhrRequest.Method.DELETE, url, payload, headers, options);
};
XhrRequest.createXMLHTTPObject = function () {
var xmlhttp = null;
for (var _i = 0, _a = XhrRequest.XMLHttpFactories; _i < _a.length; _i++) {
var i = _a[_i];
try {
xmlhttp = i();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
};
XhrRequest.XMLHttpFactories = [
function () { return new XMLHttpRequest(); },
function () { return new window["ActiveXObject"]("Msxml2.XMLHTTP"); },
function () { return new window["ActiveXObject"]("Msxml3.XMLHTTP"); },
function () { return new window["ActiveXObject"]("Microsoft.XMLHTTP"); }
];
return XhrRequest;
}());
(function (XhrRequest) {
var Method;
(function (Method) {
Method[Method["GET"] = "GET"] = "GET";
Method[Method["POST"] = "POST"] = "POST";
Method[Method["PUT"] = "PUT"] = "PUT";
Method[Method["DELETE"] = "DELETE"] = "DELETE";
})(Method = XhrRequest.Method || (XhrRequest.Method = {}));
})(XhrRequest || (XhrRequest = {}));
exports["default"] = XhrRequest;