Begin adding support for XDR requests
This commit is contained in:
parent
19fbcfff3a
commit
4df82a0507
20
build/typertext.d.ts
vendored
20
build/typertext.d.ts
vendored
|
@ -139,3 +139,23 @@ declare module Typertext.Json {
|
||||||
interface JsonResponseHandler extends GenericResponseHandler<JsonResponse> {
|
interface JsonResponseHandler extends GenericResponseHandler<JsonResponse> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
declare module Typertext.Transport {
|
||||||
|
interface GenericTransport {
|
||||||
|
RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): void;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
declare module Typertext.Transport {
|
||||||
|
class TransportChooser {
|
||||||
|
static GetTransport(method: Http.HttpMethod, request: Http.HttpUrl): GenericTransport;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
declare module Typertext.Transport {
|
||||||
|
class XDR implements GenericTransport {
|
||||||
|
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
declare module Typertext.Transport {
|
||||||
|
class XHR implements GenericTransport {
|
||||||
|
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): void;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -117,44 +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) {
|
||||||
}; }
|
}; }
|
||||||
var noop = function (i) {
|
Typertext.Transport.TransportChooser.GetTransport(method, request).RawRequest(method, request, postData, callback);
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function () {
|
|
||||||
if (xhr.readyState == 4) {
|
|
||||||
var getHeader = function (name) {
|
|
||||||
return xhr.getResponseHeader(name);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (xhr.status == 200) {
|
|
||||||
callback(new Typertext.Http.HttpResponse(0 /* success */, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else if (xhr.status >= 400 && xhr.status < 500) {
|
|
||||||
callback(new Typertext.Http.HttpResponse(2 /* clientError */, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else if (xhr.status >= 500 && xhr.status < 600) {
|
|
||||||
callback(new Typertext.Http.HttpResponse(1 /* serverError */, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else {
|
|
||||||
callback(new Typertext.Http.HttpResponse(4 /* unknownError */, getHeader, xhr.status, xhr.responseText));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.ontimeout = function () {
|
|
||||||
callback(new Typertext.Http.HttpResponse(5 /* timeout */, noop, -1, ""));
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.open(Typertext.Http.HttpMethod[method], request.ToString(), true);
|
|
||||||
|
|
||||||
if (method == 0 /* GET */) {
|
|
||||||
xhr.send();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
|
|
||||||
xhr.send(Typertext.Http.HttpUrl.UrlEncodeObject(postData));
|
|
||||||
};
|
};
|
||||||
return HttpRequest;
|
return HttpRequest;
|
||||||
})();
|
})();
|
||||||
|
@ -388,4 +351,113 @@ var Typertext;
|
||||||
})(Typertext.Json || (Typertext.Json = {}));
|
})(Typertext.Json || (Typertext.Json = {}));
|
||||||
var Json = Typertext.Json;
|
var Json = Typertext.Json;
|
||||||
})(Typertext || (Typertext = {}));
|
})(Typertext || (Typertext = {}));
|
||||||
|
var Typertext;
|
||||||
|
(function (Typertext) {
|
||||||
|
(function (Transport) {
|
||||||
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
|
var Transport = Typertext.Transport;
|
||||||
|
})(Typertext || (Typertext = {}));
|
||||||
|
var Typertext;
|
||||||
|
(function (Typertext) {
|
||||||
|
(function (Transport) {
|
||||||
|
var TransportChooser = (function () {
|
||||||
|
function TransportChooser() {
|
||||||
|
}
|
||||||
|
TransportChooser.GetTransport = function (method, request) {
|
||||||
|
var ieLte9 = false;
|
||||||
|
var isXdomain = false;
|
||||||
|
var isXprotocol = false;
|
||||||
|
|
||||||
|
if (!ieLte9) {
|
||||||
|
return new Typertext.Transport.XDR();
|
||||||
|
} else if (isXdomain && !isXprotocol) {
|
||||||
|
return new Typertext.Transport.XHR();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw {};
|
||||||
|
};
|
||||||
|
return TransportChooser;
|
||||||
|
})();
|
||||||
|
Transport.TransportChooser = TransportChooser;
|
||||||
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
|
var Transport = Typertext.Transport;
|
||||||
|
})(Typertext || (Typertext = {}));
|
||||||
|
var Typertext;
|
||||||
|
(function (Typertext) {
|
||||||
|
(function (Transport) {
|
||||||
|
var XDR = (function () {
|
||||||
|
function XDR() {
|
||||||
|
}
|
||||||
|
XDR.prototype.RawRequest = function (method, request, postData, callback) {
|
||||||
|
if (typeof postData === "undefined") { postData = {}; }
|
||||||
|
if (typeof callback === "undefined") { callback = function (c) {
|
||||||
|
}; }
|
||||||
|
};
|
||||||
|
return XDR;
|
||||||
|
})();
|
||||||
|
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 () {
|
||||||
|
function XHR() {
|
||||||
|
}
|
||||||
|
XHR.prototype.RawRequest = function (method, request, postData, callback) {
|
||||||
|
if (typeof postData === "undefined") { postData = {}; }
|
||||||
|
if (typeof callback === "undefined") { callback = function (c) {
|
||||||
|
}; }
|
||||||
|
var noop = function (i) {
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
xhr.onreadystatechange = function () {
|
||||||
|
if (xhr.readyState == 4) {
|
||||||
|
var getHeader = function (name) {
|
||||||
|
return xhr.getResponseHeader(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (xhr.status == 200) {
|
||||||
|
callback(new HttpResponse(0 /* success */, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else if (xhr.status >= 400 && xhr.status < 500) {
|
||||||
|
callback(new HttpResponse(2 /* clientError */, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else if (xhr.status >= 500 && xhr.status < 600) {
|
||||||
|
callback(new HttpResponse(1 /* serverError */, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else {
|
||||||
|
callback(new HttpResponse(4 /* unknownError */, getHeader, xhr.status, xhr.responseText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.ontimeout = function () {
|
||||||
|
callback(new HttpResponse(5 /* timeout */, noop, -1, ""));
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.open(HttpMethod[method], request.ToString(), true);
|
||||||
|
|
||||||
|
if (method == 0 /* GET */) {
|
||||||
|
xhr.send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
||||||
|
};
|
||||||
|
return XHR;
|
||||||
|
})();
|
||||||
|
Transport.XHR = XHR;
|
||||||
|
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||||
|
var Transport = Typertext.Transport;
|
||||||
|
})(Typertext || (Typertext = {}));
|
||||||
//# sourceMappingURL=typertext.js.map
|
//# sourceMappingURL=typertext.js.map
|
||||||
|
|
File diff suppressed because one or more lines are too long
2
build/typertext.min.js
vendored
2
build/typertext.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -52,59 +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 {
|
||||||
var noop = (i:string)=>{
|
Typertext.Transport.TransportChooser.GetTransport(method, request).RawRequest(method, request, postData, callback);
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
//Create a XHR
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
//And let us know when it does something
|
|
||||||
xhr.onreadystatechange = ()=> {
|
|
||||||
//If the request is complete
|
|
||||||
if (xhr.readyState == 4) {
|
|
||||||
//Prepare a getter for the header
|
|
||||||
var getHeader = (name:string):string => {
|
|
||||||
return xhr.getResponseHeader(name);
|
|
||||||
};
|
|
||||||
|
|
||||||
//Check the status
|
|
||||||
if (xhr.status == 200) {
|
|
||||||
//And either succeed
|
|
||||||
callback(new HttpResponse(HttpResponseStatus.success, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else if (xhr.status >= 400 && xhr.status < 500) {
|
|
||||||
//Or fail miserably
|
|
||||||
callback(new HttpResponse(HttpResponseStatus.clientError, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else if (xhr.status >= 500 && xhr.status < 600) {
|
|
||||||
//Again
|
|
||||||
callback(new HttpResponse(HttpResponseStatus.serverError, getHeader, xhr.status, xhr.responseText));
|
|
||||||
} else {
|
|
||||||
//And again
|
|
||||||
callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, xhr.status, xhr.responseText));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//Or if it times out
|
|
||||||
xhr.ontimeout = () => {
|
|
||||||
//And make a big deal of the failing
|
|
||||||
callback(new HttpResponse(HttpResponseStatus.timeout, noop, -1, ""));
|
|
||||||
};
|
|
||||||
|
|
||||||
//Now connect
|
|
||||||
xhr.open(HttpMethod[method], request.ToString(), true);
|
|
||||||
|
|
||||||
//And either send
|
|
||||||
if (method == HttpMethod.GET) {
|
|
||||||
//A get request
|
|
||||||
xhr.send();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Or set the content-type
|
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
//And send the post-data to the server
|
|
||||||
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
10
lib/Typertext/Transport/GenericTransport.ts
Normal file
10
lib/Typertext/Transport/GenericTransport.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
module Typertext.Transport {
|
||||||
|
import HttpMethod = Typertext.Http.HttpMethod;
|
||||||
|
import HttpUrl = Typertext.Http.HttpUrl;
|
||||||
|
import HttpPostData = Typertext.Http.HttpPostData;
|
||||||
|
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
||||||
|
|
||||||
|
export interface GenericTransport {
|
||||||
|
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):void;
|
||||||
|
}
|
||||||
|
}
|
17
lib/Typertext/Transport/TransportChooser.ts
Normal file
17
lib/Typertext/Transport/TransportChooser.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module Typertext.Transport {
|
||||||
|
export class TransportChooser {
|
||||||
|
static GetTransport(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl):GenericTransport {
|
||||||
|
var ieLte9 = false;
|
||||||
|
var isXdomain = false;
|
||||||
|
var isXprotocol = false;
|
||||||
|
|
||||||
|
if (!ieLte9) {
|
||||||
|
return new XDR();
|
||||||
|
} else if (isXdomain && !isXprotocol) {
|
||||||
|
return new XHR();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
lib/Typertext/Transport/XDR.ts
Normal file
13
lib/Typertext/Transport/XDR.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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 XDR implements GenericTransport {
|
||||||
|
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
||||||
|
}):void {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
67
lib/Typertext/Transport/XHR.ts
Normal file
67
lib/Typertext/Transport/XHR.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
module Typertext.Transport {
|
||||||
|
import HttpMethod = Typertext.Http.HttpMethod;
|
||||||
|
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 XHR implements GenericTransport {
|
||||||
|
RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
||||||
|
}):void {
|
||||||
|
var noop = (i:string)=>{
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
|
//Create a XHR
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
//And let us know when it does something
|
||||||
|
xhr.onreadystatechange = ()=> {
|
||||||
|
//If the request is complete
|
||||||
|
if (xhr.readyState == 4) {
|
||||||
|
//Prepare a getter for the header
|
||||||
|
var getHeader = (name:string):string => {
|
||||||
|
return xhr.getResponseHeader(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
//Check the status
|
||||||
|
if (xhr.status == 200) {
|
||||||
|
//And either succeed
|
||||||
|
callback(new HttpResponse(HttpResponseStatus.success, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else if (xhr.status >= 400 && xhr.status < 500) {
|
||||||
|
//Or fail miserably
|
||||||
|
callback(new HttpResponse(HttpResponseStatus.clientError, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else if (xhr.status >= 500 && xhr.status < 600) {
|
||||||
|
//Again
|
||||||
|
callback(new HttpResponse(HttpResponseStatus.serverError, getHeader, xhr.status, xhr.responseText));
|
||||||
|
} else {
|
||||||
|
//And again
|
||||||
|
callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, xhr.status, xhr.responseText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Or if it times out
|
||||||
|
xhr.ontimeout = () => {
|
||||||
|
//And make a big deal of the failing
|
||||||
|
callback(new HttpResponse(HttpResponseStatus.timeout, noop, -1, ""));
|
||||||
|
};
|
||||||
|
|
||||||
|
//Now connect
|
||||||
|
xhr.open(HttpMethod[method], request.ToString(), true);
|
||||||
|
|
||||||
|
//And either send
|
||||||
|
if (method == HttpMethod.GET) {
|
||||||
|
//A get request
|
||||||
|
xhr.send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Or set the content-type
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
//And send the post-data to the server
|
||||||
|
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue