Ready for testing

This commit is contained in:
Kegan Myers 2014-04-14 10:04:46 -05:00
parent 4df82a0507
commit dc98b01eba
10 changed files with 129 additions and 50 deletions

14
build/typertext.d.ts vendored
View file

@ -140,22 +140,22 @@ declare module Typertext.Json {
}
}
declare module Typertext.Transport {
interface GenericTransport {
RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): void;
class GenericTransport {
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler);
}
}
declare module Typertext.Transport {
class TransportChooser {
static GetTransport(method: Http.HttpMethod, request: Http.HttpUrl): GenericTransport;
static GetTransport(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): GenericTransport;
}
}
declare module Typertext.Transport {
class XDR implements GenericTransport {
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
class XDR extends GenericTransport {
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
}
}
declare module Typertext.Transport {
class XHR implements GenericTransport {
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
class XHR extends GenericTransport {
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
}
}

View file

@ -117,7 +117,7 @@ var Typertext;
if (typeof postData === "undefined") { postData = {}; }
if (typeof callback === "undefined") { callback = function (c) {
}; }
Typertext.Transport.TransportChooser.GetTransport(method, request).RawRequest(method, request, postData, callback);
Typertext.Transport.TransportChooser.GetTransport(method, request, postData, callback);
};
return HttpRequest;
})();
@ -354,6 +354,12 @@ var Typertext;
var Typertext;
(function (Typertext) {
(function (Transport) {
var GenericTransport = (function () {
function GenericTransport(method, request, postData, callback) {
}
return GenericTransport;
})();
Transport.GenericTransport = GenericTransport;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
})(Typertext || (Typertext = {}));
@ -363,15 +369,15 @@ var Typertext;
var TransportChooser = (function () {
function TransportChooser() {
}
TransportChooser.GetTransport = function (method, request) {
TransportChooser.GetTransport = function (method, request, postData, callback) {
var ieLte9 = false;
var isXdomain = false;
var isXprotocol = false;
if (!ieLte9) {
return new Typertext.Transport.XDR();
return new Typertext.Transport.XDR(method, request, postData, callback);
} else if (isXdomain && !isXprotocol) {
return new Typertext.Transport.XHR();
return new Typertext.Transport.XHR(method, request, postData, callback);
}
throw {};
@ -385,16 +391,55 @@ var Typertext;
var Typertext;
(function (Typertext) {
(function (Transport) {
var XDR = (function () {
function XDR() {
}
XDR.prototype.RawRequest = function (method, request, postData, callback) {
var HttpMethod = Typertext.Http.HttpMethod;
var HttpUrl = Typertext.Http.HttpUrl;
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
var HttpResponse = Typertext.Http.HttpResponse;
var XDR = (function (_super) {
__extends(XDR, _super);
function XDR(method, request, postData, callback) {
if (typeof postData === "undefined") { postData = {}; }
if (typeof callback === "undefined") { callback = function (c) {
return null;
}; }
};
_super.call(this, method, request, postData, callback);
var xdr = new XDomainRequest();
var getHeader = function (name) {
if (name.toLowerCase() === "content-type") {
return xdr.contentType;
}
return undefined;
};
xdr.ontimeout = function () {
callback(new HttpResponse(5 /* timeout */, function (i) {
return "";
}, -1, ""));
};
xdr.onerror = function () {
callback(new HttpResponse(4 /* unknownError */, getHeader, -1, xdr.responseText));
};
xdr.onload = function () {
callback(new HttpResponse(0 /* success */, getHeader, 200, xdr.responseText));
};
xdr.open(HttpMethod[method], request.ToString());
if (method == 0 /* GET */) {
xdr.send();
return;
}
xdr.send(HttpUrl.UrlEncodeObject(postData));
}
return XDR;
})();
})(Typertext.Transport.GenericTransport);
Transport.XDR = XDR;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;
@ -408,16 +453,14 @@ var Typertext;
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
var HttpResponse = Typertext.Http.HttpResponse;
var XHR = (function () {
function XHR() {
}
XHR.prototype.RawRequest = function (method, request, postData, callback) {
var XHR = (function (_super) {
__extends(XHR, _super);
function XHR(method, request, postData, callback) {
if (typeof postData === "undefined") { postData = {}; }
if (typeof callback === "undefined") { callback = function (c) {
return null;
}; }
var noop = function (i) {
return "";
};
_super.call(this, method, request, postData, callback);
var xhr = new XMLHttpRequest();
@ -440,7 +483,9 @@ var Typertext;
};
xhr.ontimeout = function () {
callback(new HttpResponse(5 /* timeout */, noop, -1, ""));
callback(new HttpResponse(5 /* timeout */, function (i) {
return "";
}, -1, ""));
};
xhr.open(HttpMethod[method], request.ToString(), true);
@ -453,9 +498,9 @@ var Typertext;
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(HttpUrl.UrlEncodeObject(postData));
};
}
return XHR;
})();
})(Typertext.Transport.GenericTransport);
Transport.XHR = XHR;
})(Typertext.Transport || (Typertext.Transport = {}));
var Transport = Typertext.Transport;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -52,7 +52,7 @@ module Typertext.Http {
*/
public RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
}):void {
Typertext.Transport.TransportChooser.GetTransport(method, request).RawRequest(method, request, postData, callback);
Typertext.Transport.TransportChooser.Transport(method, request, postData, callback);
}
}
}

View file

@ -1,10 +1,7 @@
module Typertext.Transport {
import HttpMethod = Typertext.Http.HttpMethod;
import HttpUrl = Typertext.Http.HttpUrl;
import HttpPostData = Typertext.Http.HttpPostData;
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
export class GenericTransport {
constructor(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler) {
export interface GenericTransport {
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):void;
}
}
}

View file

@ -1,14 +1,14 @@
module Typertext.Transport {
export class TransportChooser {
static GetTransport(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl):GenericTransport {
static Transport(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler):GenericTransport {
var ieLte9 = false;
var isXdomain = false;
var isXprotocol = false;
if (!ieLte9) {
return new XDR();
return new XDR(method, request, postData, callback);
} else if (isXdomain && !isXprotocol) {
return new XHR();
return new XHR(method, request, postData, callback);
}
throw {};

View file

@ -3,11 +3,51 @@ module Typertext.Transport {
import HttpUrl = Typertext.Http.HttpUrl;
import HttpPostData = Typertext.Http.HttpPostData;
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
import HttpResponse = Typertext.Http.HttpResponse;
export class XDR implements GenericTransport {
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
}):void {
export class XDR extends GenericTransport {
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
super(method, request, postData, callback);
//Create a XDR
var xdr = new XDomainRequest();
//and an interface to get the content type of the response
var getHeader = (name:string):string => {
if (name.toLowerCase() === "content-type") {
return xdr.contentType;
}
return undefined;
};
//No handle timeouts,
xdr.ontimeout = () => {
callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
};
//all errors (because XDR sucks)
xdr.onerror = () => {
callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, -1, xdr.responseText));
};
//and success.
xdr.onload = () => {
callback(new HttpResponse(HttpResponseStatus.success, getHeader, 200, xdr.responseText));
};
//Finally, open the request
xdr.open(HttpMethod[method], request.ToString());
//and either send
if (method == HttpMethod.GET) {
//a get request without data,
xdr.send();
return;
}
//or send the post-data to the server (as text/plain, because XDR sucks)
xdr.send(HttpUrl.UrlEncodeObject(postData));
}
}
}

View file

@ -6,12 +6,9 @@ module Typertext.Transport {
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
import HttpResponse = Typertext.Http.HttpResponse;
export class XHR implements GenericTransport {
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
}):void {
var noop = (i:string)=>{
return "";
};
export class XHR extends GenericTransport {
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
super(method, request, postData, callback);
//Create a XHR
var xhr = new XMLHttpRequest();
@ -45,7 +42,7 @@ module Typertext.Transport {
//Or if it times out
xhr.ontimeout = () => {
//And make a big deal of the failing
callback(new HttpResponse(HttpResponseStatus.timeout, noop, -1, ""));
callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
};
//Now connect