This repository has been archived on 2019-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
Typertext/build/typertext.js

585 lines
22 KiB
JavaScript
Raw Normal View History

2014-02-26 19:12:37 +00:00
var Typertext;
(function (Typertext) {
var BaseException = (function () {
function BaseException(message, code, custom) {
this.message = message;
this.code = code;
this.custom = custom;
}
BaseException.prototype.GetCode = function () {
return this.code;
};
BaseException.prototype.GetMessage = function () {
return this.message;
};
BaseException.prototype.GetCustom = function () {
return this.custom;
};
return BaseException;
})();
Typertext.BaseException = BaseException;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Transport) {
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
})(Typertext || (Typertext = {}));
var Typertext;
2014-02-26 19:12:37 +00:00
(function (Typertext) {
2014-02-28 17:45:37 +00:00
2014-02-26 19:12:37 +00:00
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
var GenericResponse = (function () {
function GenericResponse(status, responseHeaderGetter, httpResponseCode, responseBody) {
2014-02-26 19:12:37 +00:00
this.status = status;
this.headers = responseHeaderGetter;
2014-02-26 19:12:37 +00:00
this.httpStatus = httpResponseCode;
this.content = responseBody;
}
GenericResponse.prototype.GetContent = function () {
return this.content;
};
GenericResponse.prototype.GetContentType = function () {
return this.GetHeader("Content-Type");
2014-02-26 19:12:37 +00:00
};
GenericResponse.prototype.GetHeader = function (name) {
return this.headers(name);
2014-02-26 19:12:37 +00:00
};
GenericResponse.prototype.GetHttpStatus = function () {
return this.httpStatus;
};
GenericResponse.prototype.GetStatus = function () {
return this.status;
};
return GenericResponse;
})();
Typertext.GenericResponse = GenericResponse;
})(Typertext || (Typertext = {}));
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Typertext;
(function (Typertext) {
(function (Http) {
var HttpException = (function (_super) {
__extends(HttpException, _super);
function HttpException() {
_super.apply(this, arguments);
}
return HttpException;
})(Typertext.BaseException);
Http.HttpException = HttpException;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
(function (HttpMethod) {
HttpMethod[HttpMethod["DELETE"] = 0] = "DELETE";
HttpMethod[HttpMethod["GET"] = 1] = "GET";
HttpMethod[HttpMethod["HEAD"] = 2] = "HEAD";
HttpMethod[HttpMethod["OPTIONS"] = 3] = "OPTIONS";
HttpMethod[HttpMethod["POST"] = 4] = "POST";
HttpMethod[HttpMethod["PUT"] = 5] = "PUT";
HttpMethod[HttpMethod["TRACE"] = 6] = "TRACE";
2014-02-26 19:12:37 +00:00
})(Http.HttpMethod || (Http.HttpMethod = {}));
var HttpMethod = Http.HttpMethod;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
(function (HttpProtocol) {
HttpProtocol[HttpProtocol["http"] = 0] = "http";
HttpProtocol[HttpProtocol["https"] = 1] = "https";
})(Http.HttpProtocol || (Http.HttpProtocol = {}));
var HttpProtocol = Http.HttpProtocol;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
var TransportChooser = Typertext.Transport.TransportChooser;
2014-02-26 19:12:37 +00:00
var HttpRequest = (function () {
function HttpRequest() {
}
HttpRequest.prototype.Delete = function (request, callback) {
this.RawRequest(5 /* PUT */, request, {}, callback);
};
2014-02-26 19:12:37 +00:00
HttpRequest.prototype.Get = function (request, callback) {
this.RawRequest(1 /* GET */, request, {}, callback);
2014-02-26 19:12:37 +00:00
};
HttpRequest.prototype.Post = function (request, postData, callback) {
this.RawRequest(4 /* POST */, request, postData, callback);
};
HttpRequest.prototype.Put = function (request, putData, callback) {
this.RawRequest(5 /* PUT */, request, putData, callback);
2014-02-26 19:12:37 +00:00
};
HttpRequest.prototype.RawRequest = function (method, request, postData, callback, transport) {
2014-02-26 19:12:37 +00:00
if (typeof postData === "undefined") { postData = {}; }
if (!callback)
callback = function (c) {
return null;
};
if (!transport)
transport = TransportChooser.Transport(method, request, postData, callback);
var transportInstance = new transport(method, request, postData, callback);
transportInstance.Send();
2014-02-26 19:12:37 +00:00
};
return HttpRequest;
})();
Http.HttpRequest = HttpRequest;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
var HttpResponse = (function (_super) {
__extends(HttpResponse, _super);
function HttpResponse(status, responseHeaderGetter, httpResponseCode, responseBody) {
_super.call(this, status, responseHeaderGetter, httpResponseCode, responseBody);
2014-02-26 19:12:37 +00:00
}
return HttpResponse;
})(Typertext.GenericResponse);
Http.HttpResponse = HttpResponse;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
(function (HttpResponseStatus) {
HttpResponseStatus[HttpResponseStatus["success"] = 0] = "success";
HttpResponseStatus[HttpResponseStatus["serverError"] = 1] = "serverError";
HttpResponseStatus[HttpResponseStatus["clientError"] = 2] = "clientError";
HttpResponseStatus[HttpResponseStatus["responseError"] = 3] = "responseError";
HttpResponseStatus[HttpResponseStatus["unknownError"] = 4] = "unknownError";
HttpResponseStatus[HttpResponseStatus["timeout"] = 5] = "timeout";
})(Http.HttpResponseStatus || (Http.HttpResponseStatus = {}));
var HttpResponseStatus = Http.HttpResponseStatus;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Http) {
var HttpUrl = (function () {
function HttpUrl(domain, protocol, path, queryString, port) {
if (typeof protocol === "undefined") { protocol = 0 /* http */; }
if (typeof path === "undefined") { path = "/"; }
if (typeof queryString === "undefined") { queryString = {}; }
if (typeof port === "undefined") { port = 0; }
if (port < 1 || port > 65535 || isNaN(port)) {
2014-02-26 19:12:37 +00:00
port = HttpUrl.DefaultPort(protocol);
}
if (path.indexOf("/") != 0) {
path = "/" + path;
}
2014-02-28 17:45:37 +00:00
this.domain = domain;
this.protocol = protocol;
this.path = path;
this.queryString = queryString;
this.port = port;
2014-02-26 19:12:37 +00:00
}
HttpUrl.DefaultPort = function (protocol) {
switch (protocol) {
case 0 /* http */:
return 80;
case 1 /* https */:
return 443;
default:
return -1;
}
2014-02-26 19:12:37 +00:00
};
2014-02-26 21:21:43 +00:00
HttpUrl.FromUrl = function (location) {
var l = document.createElement("a");
l.href = location;
2014-04-18 04:34:22 +00:00
if (!l.hostname || !l.protocol || !l.pathname || !l.search || !l.port) {
l.href = l.href;
}
return new HttpUrl(l.hostname, Typertext.Http.HttpProtocol[l.protocol.slice(0, -1)], l.pathname, HttpUrl.DecodeQueryString(l.search), parseInt(l.port));
2014-02-26 21:21:43 +00:00
};
HttpUrl.DecodeQueryString = function (queryString) {
if (queryString.indexOf("?") == 0) {
queryString = queryString.substring(1);
2014-02-26 21:21:43 +00:00
}
2014-02-26 21:58:23 +00:00
return HttpUrl.UrlDecodeString(queryString);
2014-02-26 21:21:43 +00:00
};
2014-02-26 19:12:37 +00:00
HttpUrl.EncodeQueryString = function (query) {
2014-02-26 21:58:23 +00:00
var rs = "?" + HttpUrl.UrlEncodeObject(query);
2014-02-26 19:12:37 +00:00
return ((rs.length == 1) ? "" : rs);
};
2014-02-26 21:58:23 +00:00
HttpUrl.UrlEncodeObject = function (data) {
2014-02-26 19:12:37 +00:00
var rs = "";
var temp;
for (temp in data) {
2014-02-28 23:46:03 +00:00
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(data[temp]) + "&";
2014-02-26 19:12:37 +00:00
}
return rs.slice(0, -1);
};
2014-02-26 21:58:23 +00:00
HttpUrl.UrlDecodeString = function (queryString) {
var returnValue = {}, params = HttpUrl.splitString(queryString, "&");
for (var i = 0; i < params.length; i++) {
if (params[i] == "") {
continue;
}
2014-02-26 21:58:23 +00:00
var param = HttpUrl.splitString(params[i], "=", 2);
var key = decodeURIComponent(param[0]);
2014-02-26 21:58:23 +00:00
if (param.length == 1) {
returnValue[key] = "";
2014-02-26 21:58:23 +00:00
continue;
}
returnValue[key] = decodeURIComponent(param[1]);
2014-02-26 21:58:23 +00:00
}
return returnValue;
};
2014-02-26 21:21:43 +00:00
HttpUrl.splitString = function (input, separator, limit) {
if (typeof limit === "undefined") { limit = -1; }
2014-02-26 21:21:43 +00:00
limit++;
var chunks = input.split(separator);
if (limit > 0 && chunks.length > limit) {
var ret = chunks.splice(0, limit);
ret.push(chunks.join(separator));
return ret;
}
return chunks;
};
2014-02-26 19:12:37 +00:00
HttpUrl.prototype.ToString = function () {
2014-02-28 17:45:37 +00:00
return Typertext.Http.HttpProtocol[this.protocol] + "://" + this.domain + ((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path + HttpUrl.EncodeQueryString(this.queryString);
2014-02-26 19:12:37 +00:00
};
2014-04-15 16:25:52 +00:00
HttpUrl.prototype.GetPort = function () {
return this.port;
};
HttpUrl.prototype.GetDomain = function () {
return this.domain;
};
HttpUrl.prototype.GetProtocol = function () {
return this.protocol;
};
2014-04-18 04:17:33 +00:00
HttpUrl.prototype.SameOriginCheck = function (url) {
2014-04-15 16:25:52 +00:00
return (this.domain === url.GetDomain() && this.port === url.GetPort() && this.protocol === url.GetProtocol());
};
2014-02-26 19:12:37 +00:00
return HttpUrl;
})();
Http.HttpUrl = HttpUrl;
})(Typertext.Http || (Typertext.Http = {}));
var Http = Typertext.Http;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Json) {
var JsonException = (function (_super) {
__extends(JsonException, _super);
function JsonException(message, code) {
_super.call(this, message, code, null);
}
return JsonException;
})(Typertext.BaseException);
Json.JsonException = JsonException;
})(Typertext.Json || (Typertext.Json = {}));
var Json = Typertext.Json;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Json) {
var HttpRequest = Typertext.Http.HttpRequest;
var HttpMethod = Typertext.Http.HttpMethod;
var JsonRequest = (function () {
function JsonRequest(jsonContentType) {
if (typeof jsonContentType === "undefined") { jsonContentType = "application/json"; }
this.request = new HttpRequest();
this.jsonType = jsonContentType;
}
JsonRequest.prototype.Delete = function (request, callback) {
this.RawRequest(0 /* DELETE */, request, {}, callback);
};
2014-02-26 19:12:37 +00:00
JsonRequest.prototype.Get = function (request, callback) {
this.RawRequest(1 /* GET */, request, {}, callback);
2014-02-26 19:12:37 +00:00
};
JsonRequest.prototype.Post = function (request, postData, callback) {
this.RawRequest(4 /* POST */, request, postData, callback);
};
JsonRequest.prototype.Put = function (request, putData, callback) {
this.RawRequest(5 /* PUT */, request, putData, callback);
2014-02-26 19:12:37 +00:00
};
JsonRequest.prototype.RawRequest = function (method, request, postData, callback, transport) {
2014-02-26 19:12:37 +00:00
var _this = this;
if (typeof postData === "undefined") { postData = {}; }
2014-03-12 18:56:11 +00:00
if (typeof callback != "function") {
this.request.RawRequest(method, request, postData, function () {
});
return;
}
2014-02-26 19:12:37 +00:00
this.request.RawRequest(method, request, postData, function (response) {
if (response.GetContentType() != _this.jsonType) {
2014-03-12 18:56:11 +00:00
callback(Typertext.Json.JsonResponse.fromInvalidHttpResponse(response));
return;
2014-02-26 19:12:37 +00:00
}
2014-03-12 18:56:11 +00:00
callback(Typertext.Json.JsonResponse.fromHttpResponse(response));
}, transport);
2014-02-26 19:12:37 +00:00
};
return JsonRequest;
})();
Json.JsonRequest = JsonRequest;
})(Typertext.Json || (Typertext.Json = {}));
var Json = Typertext.Json;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Json) {
2014-03-12 18:56:11 +00:00
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
2014-02-26 19:12:37 +00:00
var JsonResponse = (function (_super) {
__extends(JsonResponse, _super);
2014-03-12 18:56:11 +00:00
function JsonResponse(status, responseHeaderGetter, httpResponseCode, responseBody, parseSuccess) {
_super.call(this, status, responseHeaderGetter, httpResponseCode, responseBody);
2014-03-12 18:56:11 +00:00
parseSuccess = !!parseSuccess || false;
this.parseSuccess = parseSuccess;
2014-02-26 19:12:37 +00:00
}
JsonResponse.fromHttpResponse = function (httpResponse) {
2014-03-12 18:56:11 +00:00
try {
return new JsonResponse(httpResponse.GetStatus(), httpResponse.GetHeader, httpResponse.GetHttpStatus(), window["JSON"].parse(httpResponse.GetContent()), true);
} catch (e) {
return new JsonResponse(httpResponse.GetStatus(), httpResponse.GetHeader, httpResponse.GetHttpStatus(), null);
}
};
JsonResponse.fromInvalidHttpResponse = function (httpResponse) {
return new JsonResponse(3 /* responseError */, httpResponse.GetHeader, httpResponse.GetHttpStatus());
};
JsonResponse.prototype.GetParseStatus = function () {
return this.parseSuccess;
2014-02-26 19:12:37 +00:00
};
return JsonResponse;
})(Typertext.GenericResponse);
Json.JsonResponse = JsonResponse;
})(Typertext.Json || (Typertext.Json = {}));
var Json = Typertext.Json;
})(Typertext || (Typertext = {}));
2014-04-11 18:51:54 +00:00
var Typertext;
(function (Typertext) {
(function (Transport) {
2014-04-15 16:25:52 +00:00
var HttpUrl = Typertext.Http.HttpUrl;
2014-04-11 18:51:54 +00:00
var TransportChooser = (function () {
function TransportChooser() {
}
2014-04-14 17:01:17 +00:00
TransportChooser.Transport = function (method, request, postData, callback) {
2014-04-15 16:25:52 +00:00
var ieTestDiv = document.createElement("div");
ieTestDiv.innerHTML = "<!--[if lte IE 7]><i></i><![endif]-->";
2014-04-11 18:51:54 +00:00
2014-04-15 16:25:52 +00:00
if (ieTestDiv.getElementsByTagName("i").length === 1) {
throw {};
}
ieTestDiv.innerHTML = "<!--[if lte IE 9]><i></i><![endif]-->";
var ieLte9 = (ieTestDiv.getElementsByTagName("i").length === 1);
var origin = HttpUrl.FromUrl(window.location.href);
2014-04-18 04:17:33 +00:00
if (origin.SameOriginCheck(origin) || !ieLte9) {
return Typertext.Transport.XHR;
2014-04-15 16:25:52 +00:00
}
if (origin.GetProtocol() === request.GetProtocol()) {
return Typertext.Transport.XDR;
2014-04-11 18:51:54 +00:00
}
throw {};
};
return TransportChooser;
})();
Transport.TransportChooser = TransportChooser;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Transport) {
2014-04-14 15:04:46 +00:00
var HttpMethod = Typertext.Http.HttpMethod;
var HttpUrl = Typertext.Http.HttpUrl;
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
var HttpResponse = Typertext.Http.HttpResponse;
var XDR = (function () {
2014-04-14 15:04:46 +00:00
function XDR(method, request, postData, callback) {
2014-04-11 18:51:54 +00:00
if (typeof postData === "undefined") { postData = {}; }
if (typeof callback === "undefined") { callback = function (c) {
2014-04-14 15:04:46 +00:00
return null;
2014-04-11 18:51:54 +00:00
}; }
this.postData = postData;
this.method = method;
this.request = request;
this.callback = callback;
2014-04-14 15:04:46 +00:00
this.xdr = new XDomainRequest();
}
XDR.prototype.Send = function () {
var _this = this;
2014-04-14 15:04:46 +00:00
var getHeader = function (name) {
if (name.toLowerCase() === "content-type") {
return _this.xdr.contentType;
2014-04-14 15:04:46 +00:00
}
return undefined;
};
this.xdr.ontimeout = function () {
_this.callback(new HttpResponse(5 /* timeout */, function (i) {
2014-04-14 15:04:46 +00:00
return "";
}, -1, ""));
};
this.xdr.onerror = function () {
_this.callback(new HttpResponse(4 /* unknownError */, getHeader, -1, _this.xdr.responseText));
2014-04-14 15:04:46 +00:00
};
this.xdr.onload = function () {
_this.callback(new HttpResponse(0 /* success */, getHeader, 200, _this.xdr.responseText));
2014-04-14 15:04:46 +00:00
};
this.xdr.onprogress = function () {
2014-04-14 17:01:17 +00:00
return null;
};
this.xdr.open(HttpMethod[this.method], this.request.ToString());
2014-04-14 15:04:46 +00:00
if (this.method == 1 /* GET */) {
this.xdr.send();
2014-04-14 15:04:46 +00:00
return;
}
this.xdr.send(HttpUrl.UrlEncodeObject(this.postData));
};
XDR.prototype.Destroy = function () {
this.xdr.ontimeout = this.xdr.onerror = this.xdr.onload = this.xdr.onprogress = null;
this.xdr = null;
};
2014-04-11 18:51:54 +00:00
return XDR;
})();
2014-04-11 18:51:54 +00:00
Transport.XDR = XDR;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
})(Typertext || (Typertext = {}));
var Typertext;
(function (Typertext) {
(function (Transport) {
var HttpMethod = Typertext.Http.HttpMethod;
var HttpUrl = Typertext.Http.HttpUrl;
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
var HttpResponse = Typertext.Http.HttpResponse;
var XHR = (function () {
2014-04-14 15:04:46 +00:00
function XHR(method, request, postData, callback) {
2014-04-11 18:51:54 +00:00
if (typeof postData === "undefined") { postData = {}; }
if (typeof callback === "undefined") { callback = function (c) {
2014-04-14 15:04:46 +00:00
return null;
2014-04-11 18:51:54 +00:00
}; }
var _this = this;
this.postData = postData;
this.method = method;
this.request = request;
this.callback = callback;
2014-04-11 18:51:54 +00:00
this.xhr = new XMLHttpRequest();
2014-04-11 18:51:54 +00:00
this.xhr.onreadystatechange = function () {
if (_this.xhr.readyState == 4) {
2014-04-11 18:51:54 +00:00
var getHeader = function (name) {
return _this.xhr.getResponseHeader(name);
2014-04-11 18:51:54 +00:00
};
if (_this.xhr.status == 200) {
_this.callback(new HttpResponse(0 /* success */, getHeader, _this.xhr.status, _this.xhr.responseText));
} else if (_this.xhr.status >= 400 && _this.xhr.status < 500) {
_this.callback(new HttpResponse(2 /* clientError */, getHeader, _this.xhr.status, _this.xhr.responseText));
} else if (_this.xhr.status >= 500 && _this.xhr.status < 600) {
_this.callback(new HttpResponse(1 /* serverError */, getHeader, _this.xhr.status, _this.xhr.responseText));
2014-04-11 18:51:54 +00:00
} else {
_this.callback(new HttpResponse(4 /* unknownError */, getHeader, _this.xhr.status, _this.xhr.responseText));
2014-04-11 18:51:54 +00:00
}
}
};
this.xhr.ontimeout = function () {
_this.callback(new HttpResponse(5 /* timeout */, function (i) {
2014-04-14 15:04:46 +00:00
return "";
}, -1, ""));
2014-04-11 18:51:54 +00:00
};
}
XHR.prototype.Send = function () {
this.xhr.open(HttpMethod[this.method], this.request.ToString(), true);
2014-04-11 18:51:54 +00:00
if (this.method == 1 /* GET */) {
this.xhr.send();
2014-04-11 18:51:54 +00:00
return;
}
this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
2014-04-11 18:51:54 +00:00
this.xhr.send(HttpUrl.UrlEncodeObject(this.postData));
};
XHR.prototype.Destroy = function () {
this.xhr.onreadystatechange = this.xhr.ontimeout = null;
this.xhr = null;
};
2014-04-11 18:51:54 +00:00
return XHR;
})();
2014-04-11 18:51:54 +00:00
Transport.XHR = XHR;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
})(Typertext || (Typertext = {}));
2014-02-26 19:12:37 +00:00
//# sourceMappingURL=typertext.js.map