Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Created January 13, 2019 21:23
Show Gist options
  • Save littlefuntik/93f142f946952f456ece799f85a74c95 to your computer and use it in GitHub Desktop.
Save littlefuntik/93f142f946952f456ece799f85a74c95 to your computer and use it in GitHub Desktop.
Fan JS
/**
* @return {XMLHttpRequest|ActiveXObject}
*/
function request(options, callback) {
"use strict";
var method, url, headers, body, type, timeout, xhttp;
if (typeof options === "string") {
options = {"url": options};
}
if (typeof options !== "object") {
throw new TypeError(request.errors.invalid_options_type.replace("{type}", typeof options));
}
if ("method" in options) {
method = options.method;
if (!(method in request.methods)) {
throw new Error(request.errors.unsupported_method.replace("{method}", options.method));
}
}
if ("url" in options) {
url = options.url;
}
if ("headers" in options) {
headers = options.headers;
if (typeof headers !== "object") {
throw new TypeError(request.errors.invalid_headers_type.replace("{type}", typeof options.headers));
}
}
if ("body" in options) {
body = options.body;
}
if ("type" in options) {
type = options.type;
if (!(type in request.types)) {
throw new Error(request.errors.unsupported_type.replace("{type}", type));
}
}
if ("timeout" in options) {
timeout = options.timeout;
if (typeof timeout !== "number") {
throw new Error(request.errors.invalid_timeout_type.replace("{type}", typeof timeout));
}
}
if (url === undefined) {
throw new Error(request.errors.unknown_url);
}
switch (method) {
case request.methods.POST:
case request.methods.PUT:
case request.methods.PATCH:
if (body === undefined) {
throw new Error(request.errors.unknown_body);
}
if (type === undefined) {
throw new Error(request.errors.unknown_type);
}
break;
}
xhttp = request.createXmlHttpRequest();
xhttp.open(method, url);
xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
if (type !== undefined) {
xhttp.setRequestHeader("Content-Type", request.types[type]);
}
if (headers !== undefined) {
Object.getOwnPropertyNames(headers).forEach(function (headerName) {
xhttp.setRequestHeader(headerName, headers[headerName]);
});
}
if (timeout !== undefined) {
xhttp.timeout = timeout;
}
xhttp.onreadystatechange = function () {
// state request handler
var optionName = request.readyStateEvents[this.readyState];
if (optionName in options && typeof options[optionName] === "function") {
options[optionName](this);
}
if (typeof callback === "function") {
if (this.readyState === 0) {
callback(new Error("Request aborted."), this.responseText);
} else if (this.readyState === 4) {
callback(null, this.responseText);
}
}
};
xhttp.send(body);
return xhttp;
}
request.methods = {
"GET": "GET",
"POST": "POST",
"PUT": "PUT",
"PATCH": "PATCH",
"DELETE": "DELETE"
};
request.types = {
"json": "application/json",
"form": "application/x-www-form-urlencoded"
};
request.readyStateEvents = {
"0": request.events.ABORTED,
"1": request.events.SERVER_CONNECTION_ESTABLISHED,
"2": request.events.REQUEST_RECEIVED,
"3": request.events.PROCESSING_REQUEST,
"4": request.events.REQUEST_FINISHED
};
request.events = {
"SERVER_CONNECTION_ESTABLISHED": "connected",
"REQUEST_RECEIVED": "received",
"PROCESSING_REQUEST": "progress",
"REQUEST_FINISHED": "finished",
"ABORTED": "aborted"
};
request.errors = {
"invalid_options_type": "Invalid request options type: {type}.",
"invalid_headers_type": "Invalid request headers type: {type}.",
"invalid_timeout_type": "Invalid request timeout type: {type}.",
"unsupported_method": "Unsupported request method: {method}.",
"unsupported_type": "Unsupported request type of body data: {type}.",
"unknown_url": "Unknown request URL.",
"unknown_body": "Unknown request body of data.",
"unknown_type": "Unknown request type body of data."
};
/**
* @return {XMLHttpRequest|ActiveXObject}
*/
request.createXmlHttpRequest = function () {
if (window.XMLHttpRequest) {
// code for modern browsers
return new XMLHttpRequest();
} else {
// code for old IE browsers
return new ActiveXObject("Microsoft.XMLHTTP");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment