Ready for testing
This commit is contained in:
parent
4df82a0507
commit
dc98b01eba
|
@ -140,22 +140,22 @@ declare module Typertext.Json {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module Typertext.Transport {
|
declare module Typertext.Transport {
|
||||||
interface GenericTransport {
|
class GenericTransport {
|
||||||
RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): void;
|
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module Typertext.Transport {
|
declare module Typertext.Transport {
|
||||||
class TransportChooser {
|
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 {
|
declare module Typertext.Transport {
|
||||||
class XDR implements GenericTransport {
|
class XDR extends GenericTransport {
|
||||||
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
|
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module Typertext.Transport {
|
declare module Typertext.Transport {
|
||||||
class XHR implements GenericTransport {
|
class XHR extends GenericTransport {
|
||||||
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
|
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,7 @@ var Typertext;
|
||||||
if (typeof postData === "undefined") { postData = {}; }
|
if (typeof postData === "undefined") { postData = {}; }
|
||||||
if (typeof callback === "undefined") { callback = function (c) {
|
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;
|
return HttpRequest;
|
||||||
})();
|
})();
|
||||||
|
@ -354,6 +354,12 @@ var Typertext;
|
||||||
var Typertext;
|
var Typertext;
|
||||||
(function (Typertext) {
|
(function (Typertext) {
|
||||||
(function (Transport) {
|
(function (Transport) {
|
||||||
|
var GenericTransport = (function () {
|
||||||
|
function GenericTransport(method, request, postData, callback) {
|
||||||
|
}
|
||||||
|
return GenericTransport;
|
||||||
|
})();
|
||||||
|
Transport.GenericTransport = GenericTransport;
|
||||||
})(Typertext.Transport || (Typertext.Transport = {}));
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
var Transport = Typertext.Transport;
|
var Transport = Typertext.Transport;
|
||||||
})(Typertext || (Typertext = {}));
|
})(Typertext || (Typertext = {}));
|
||||||
|
@ -363,15 +369,15 @@ var Typertext;
|
||||||
var TransportChooser = (function () {
|
var TransportChooser = (function () {
|
||||||
function TransportChooser() {
|
function TransportChooser() {
|
||||||
}
|
}
|
||||||
TransportChooser.GetTransport = function (method, request) {
|
TransportChooser.GetTransport = function (method, request, postData, callback) {
|
||||||
var ieLte9 = false;
|
var ieLte9 = false;
|
||||||
var isXdomain = false;
|
var isXdomain = false;
|
||||||
var isXprotocol = false;
|
var isXprotocol = false;
|
||||||
|
|
||||||
if (!ieLte9) {
|
if (!ieLte9) {
|
||||||
return new Typertext.Transport.XDR();
|
return new Typertext.Transport.XDR(method, request, postData, callback);
|
||||||
} else if (isXdomain && !isXprotocol) {
|
} else if (isXdomain && !isXprotocol) {
|
||||||
return new Typertext.Transport.XHR();
|
return new Typertext.Transport.XHR(method, request, postData, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw {};
|
throw {};
|
||||||
|
@ -385,16 +391,55 @@ var Typertext;
|
||||||
var Typertext;
|
var Typertext;
|
||||||
(function (Typertext) {
|
(function (Typertext) {
|
||||||
(function (Transport) {
|
(function (Transport) {
|
||||||
var XDR = (function () {
|
var HttpMethod = Typertext.Http.HttpMethod;
|
||||||
function XDR() {
|
var HttpUrl = Typertext.Http.HttpUrl;
|
||||||
}
|
|
||||||
XDR.prototype.RawRequest = function (method, request, postData, callback) {
|
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 postData === "undefined") { postData = {}; }
|
||||||
if (typeof callback === "undefined") { callback = function (c) {
|
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;
|
return XDR;
|
||||||
})();
|
})(Typertext.Transport.GenericTransport);
|
||||||
Transport.XDR = XDR;
|
Transport.XDR = XDR;
|
||||||
})(Typertext.Transport || (Typertext.Transport = {}));
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
var Transport = Typertext.Transport;
|
var Transport = Typertext.Transport;
|
||||||
|
@ -408,16 +453,14 @@ var Typertext;
|
||||||
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||||
var HttpResponse = Typertext.Http.HttpResponse;
|
var HttpResponse = Typertext.Http.HttpResponse;
|
||||||
|
|
||||||
var XHR = (function () {
|
var XHR = (function (_super) {
|
||||||
function XHR() {
|
__extends(XHR, _super);
|
||||||
}
|
function XHR(method, request, postData, callback) {
|
||||||
XHR.prototype.RawRequest = function (method, request, postData, callback) {
|
|
||||||
if (typeof postData === "undefined") { postData = {}; }
|
if (typeof postData === "undefined") { postData = {}; }
|
||||||
if (typeof callback === "undefined") { callback = function (c) {
|
if (typeof callback === "undefined") { callback = function (c) {
|
||||||
|
return null;
|
||||||
}; }
|
}; }
|
||||||
var noop = function (i) {
|
_super.call(this, method, request, postData, callback);
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
@ -440,7 +483,9 @@ var Typertext;
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.ontimeout = function () {
|
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);
|
xhr.open(HttpMethod[method], request.ToString(), true);
|
||||||
|
@ -453,9 +498,9 @@ var Typertext;
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
||||||
};
|
}
|
||||||
return XHR;
|
return XHR;
|
||||||
})();
|
})(Typertext.Transport.GenericTransport);
|
||||||
Transport.XHR = XHR;
|
Transport.XHR = XHR;
|
||||||
})(Typertext.Transport || (Typertext.Transport = {}));
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
var 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
|
@ -52,7 +52,7 @@ module Typertext.Http {
|
||||||
*/
|
*/
|
||||||
public RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
public RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
||||||
}):void {
|
}):void {
|
||||||
Typertext.Transport.TransportChooser.GetTransport(method, request).RawRequest(method, request, postData, callback);
|
Typertext.Transport.TransportChooser.Transport(method, request, postData, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,7 @@
|
||||||
module Typertext.Transport {
|
module Typertext.Transport {
|
||||||
import HttpMethod = Typertext.Http.HttpMethod;
|
export class GenericTransport {
|
||||||
import HttpUrl = Typertext.Http.HttpUrl;
|
constructor(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler) {
|
||||||
import HttpPostData = Typertext.Http.HttpPostData;
|
|
||||||
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
|
||||||
|
|
||||||
export interface GenericTransport {
|
}
|
||||||
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):void;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
module Typertext.Transport {
|
module Typertext.Transport {
|
||||||
export class TransportChooser {
|
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 ieLte9 = false;
|
||||||
var isXdomain = false;
|
var isXdomain = false;
|
||||||
var isXprotocol = false;
|
var isXprotocol = false;
|
||||||
|
|
||||||
if (!ieLte9) {
|
if (!ieLte9) {
|
||||||
return new XDR();
|
return new XDR(method, request, postData, callback);
|
||||||
} else if (isXdomain && !isXprotocol) {
|
} else if (isXdomain && !isXprotocol) {
|
||||||
return new XHR();
|
return new XHR(method, request, postData, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw {};
|
throw {};
|
||||||
|
|
|
@ -3,11 +3,51 @@ module Typertext.Transport {
|
||||||
import HttpUrl = Typertext.Http.HttpUrl;
|
import HttpUrl = Typertext.Http.HttpUrl;
|
||||||
import HttpPostData = Typertext.Http.HttpPostData;
|
import HttpPostData = Typertext.Http.HttpPostData;
|
||||||
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
||||||
|
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||||
|
import HttpResponse = Typertext.Http.HttpResponse;
|
||||||
|
|
||||||
export class XDR implements GenericTransport {
|
export class XDR extends GenericTransport {
|
||||||
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
|
||||||
}):void {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,12 +6,9 @@ module Typertext.Transport {
|
||||||
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||||
import HttpResponse = Typertext.Http.HttpResponse;
|
import HttpResponse = Typertext.Http.HttpResponse;
|
||||||
|
|
||||||
export class XHR implements GenericTransport {
|
export class XHR extends GenericTransport {
|
||||||
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
|
||||||
}):void {
|
super(method, request, postData, callback);
|
||||||
var noop = (i:string)=>{
|
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
//Create a XHR
|
//Create a XHR
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
@ -45,7 +42,7 @@ module Typertext.Transport {
|
||||||
//Or if it times out
|
//Or if it times out
|
||||||
xhr.ontimeout = () => {
|
xhr.ontimeout = () => {
|
||||||
//And make a big deal of the failing
|
//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
|
//Now connect
|
||||||
|
|
Reference in New Issue