Add ability to specify a transport to avoid autodetection, 0.7.0
This commit is contained in:
parent
5e7f079b43
commit
fb91130952
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "Typertext",
|
||||
"version": "0.6.1",
|
||||
"version": "0.7.0",
|
||||
"homepage": "https://github.com/terribleplan/Typertext",
|
||||
"authors": [
|
||||
"Kegan Myers <kegan@keganmyers.com>"
|
||||
|
|
36
build/typertext.d.ts
vendored
36
build/typertext.d.ts
vendored
|
@ -9,11 +9,16 @@ declare module Typertext {
|
|||
public GetCustom(): T;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Transport {
|
||||
interface TransportConstructor {
|
||||
new(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler): GenericTransport;
|
||||
}
|
||||
}
|
||||
declare module Typertext {
|
||||
interface GenericRequest<T extends GenericResponseHandler<GenericResponse<any>>> {
|
||||
Get(request: Http.HttpUrl, callback: T): void;
|
||||
Post(request: Http.HttpUrl, postData: Http.HttpPostData, callback: T): void;
|
||||
RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: T): void;
|
||||
RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: T, transport?: Transport.TransportConstructor): void;
|
||||
}
|
||||
}
|
||||
declare module Typertext {
|
||||
|
@ -66,7 +71,7 @@ declare module Typertext.Http {
|
|||
constructor();
|
||||
public Get(request: HttpUrl, callback: HttpResponseHandler): void;
|
||||
public Post(request: HttpUrl, postData: HttpPostData, callback: HttpResponseHandler): void;
|
||||
public RawRequest(method: HttpMethod, request: HttpUrl, postData?: HttpPostData, callback?: HttpResponseHandler): void;
|
||||
public RawRequest(method: HttpMethod, request: HttpUrl, postData?: HttpPostData, callback?: HttpResponseHandler, transport?: Transport.TransportConstructor): void;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Http {
|
||||
|
@ -127,7 +132,7 @@ declare module Typertext.Json {
|
|||
constructor(jsonContentType?: string);
|
||||
public Get(request: Http.HttpUrl, callback: JsonResponseHandler): void;
|
||||
public Post(request: Http.HttpUrl, postData: Http.HttpPostData, callback: JsonResponseHandler): void;
|
||||
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: JsonResponseHandler): void;
|
||||
public RawRequest(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: JsonResponseHandler, transport?: Transport.TransportConstructor): void;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Json {
|
||||
|
@ -144,22 +149,37 @@ declare module Typertext.Json {
|
|||
}
|
||||
}
|
||||
declare module Typertext.Transport {
|
||||
class GenericTransport {
|
||||
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler);
|
||||
interface GenericTransport {
|
||||
Send(): void;
|
||||
Destroy(): void;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Transport {
|
||||
class TransportChooser {
|
||||
static Transport(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): GenericTransport;
|
||||
static Transport(method: Http.HttpMethod, request: Http.HttpUrl, postData: Http.HttpPostData, callback: Http.HttpResponseHandler): TransportConstructor;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Transport {
|
||||
class XDR extends GenericTransport {
|
||||
class XDR implements GenericTransport {
|
||||
private xdr;
|
||||
private postData;
|
||||
private method;
|
||||
private request;
|
||||
private callback;
|
||||
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
|
||||
public Send(): void;
|
||||
public Destroy(): void;
|
||||
}
|
||||
}
|
||||
declare module Typertext.Transport {
|
||||
class XHR extends GenericTransport {
|
||||
class XHR implements GenericTransport {
|
||||
private xhr;
|
||||
private postData;
|
||||
private method;
|
||||
private request;
|
||||
private callback;
|
||||
constructor(method: Http.HttpMethod, request: Http.HttpUrl, postData?: Http.HttpPostData, callback?: Http.HttpResponseHandler);
|
||||
public Send(): void;
|
||||
public Destroy(): void;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,12 @@ var Typertext;
|
|||
Typertext.BaseException = BaseException;
|
||||
})(Typertext || (Typertext = {}));
|
||||
var Typertext;
|
||||
(function (Typertext) {
|
||||
(function (Transport) {
|
||||
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||
var Transport = Typertext.Transport;
|
||||
})(Typertext || (Typertext = {}));
|
||||
var Typertext;
|
||||
(function (Typertext) {
|
||||
|
||||
})(Typertext || (Typertext = {}));
|
||||
|
@ -102,6 +108,8 @@ var Typertext;
|
|||
var Typertext;
|
||||
(function (Typertext) {
|
||||
(function (Http) {
|
||||
var TransportChooser = Typertext.Transport.TransportChooser;
|
||||
|
||||
var HttpRequest = (function () {
|
||||
function HttpRequest() {
|
||||
}
|
||||
|
@ -113,11 +121,18 @@ var Typertext;
|
|||
this.RawRequest(1 /* POST */, request, postData, callback);
|
||||
};
|
||||
|
||||
HttpRequest.prototype.RawRequest = function (method, request, postData, callback) {
|
||||
HttpRequest.prototype.RawRequest = function (method, request, postData, callback, transport) {
|
||||
if (typeof postData === "undefined") { postData = {}; }
|
||||
if (typeof callback === "undefined") { callback = function (c) {
|
||||
}; }
|
||||
Typertext.Transport.TransportChooser.Transport(method, request, postData, callback);
|
||||
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();
|
||||
};
|
||||
return HttpRequest;
|
||||
})();
|
||||
|
@ -310,7 +325,7 @@ var Typertext;
|
|||
this.RawRequest(1 /* POST */, request, postData, callback);
|
||||
};
|
||||
|
||||
JsonRequest.prototype.RawRequest = function (method, request, postData, callback) {
|
||||
JsonRequest.prototype.RawRequest = function (method, request, postData, callback, transport) {
|
||||
var _this = this;
|
||||
if (typeof postData === "undefined") { postData = {}; }
|
||||
if (typeof callback != "function") {
|
||||
|
@ -326,7 +341,7 @@ var Typertext;
|
|||
}
|
||||
|
||||
callback(Typertext.Json.JsonResponse.fromHttpResponse(response));
|
||||
});
|
||||
}, transport);
|
||||
};
|
||||
return JsonRequest;
|
||||
})();
|
||||
|
@ -368,18 +383,6 @@ var Typertext;
|
|||
var Json = Typertext.Json;
|
||||
})(Typertext || (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 = {}));
|
||||
var Typertext;
|
||||
(function (Typertext) {
|
||||
(function (Transport) {
|
||||
var HttpUrl = Typertext.Http.HttpUrl;
|
||||
|
@ -400,11 +403,11 @@ var Typertext;
|
|||
var origin = HttpUrl.FromUrl(window.location.href);
|
||||
|
||||
if (!origin.CrossOriginCheck(origin) || !ieLte9) {
|
||||
return new Typertext.Transport.XHR(method, request, postData, callback);
|
||||
return Typertext.Transport.XHR;
|
||||
}
|
||||
|
||||
if (origin.GetProtocol() === request.GetProtocol()) {
|
||||
return new Typertext.Transport.XDR(method, request, postData, callback);
|
||||
return Typertext.Transport.XDR;
|
||||
}
|
||||
|
||||
throw {};
|
||||
|
@ -424,53 +427,62 @@ var Typertext;
|
|||
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||
var HttpResponse = Typertext.Http.HttpResponse;
|
||||
|
||||
var XDR = (function (_super) {
|
||||
__extends(XDR, _super);
|
||||
var XDR = (function () {
|
||||
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();
|
||||
this.postData = postData;
|
||||
this.method = method;
|
||||
this.request = request;
|
||||
this.callback = callback;
|
||||
|
||||
this.xdr = new XDomainRequest();
|
||||
}
|
||||
XDR.prototype.Send = function () {
|
||||
var _this = this;
|
||||
var getHeader = function (name) {
|
||||
if (name.toLowerCase() === "content-type") {
|
||||
return xdr.contentType;
|
||||
return _this.xdr.contentType;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
xdr.ontimeout = function () {
|
||||
callback(new HttpResponse(5 /* timeout */, function (i) {
|
||||
this.xdr.ontimeout = function () {
|
||||
_this.callback(new HttpResponse(5 /* timeout */, function (i) {
|
||||
return "";
|
||||
}, -1, ""));
|
||||
};
|
||||
|
||||
xdr.onerror = function () {
|
||||
callback(new HttpResponse(4 /* unknownError */, getHeader, -1, xdr.responseText));
|
||||
this.xdr.onerror = function () {
|
||||
_this.callback(new HttpResponse(4 /* unknownError */, getHeader, -1, _this.xdr.responseText));
|
||||
};
|
||||
|
||||
xdr.onload = function () {
|
||||
callback(new HttpResponse(0 /* success */, getHeader, 200, xdr.responseText));
|
||||
this.xdr.onload = function () {
|
||||
_this.callback(new HttpResponse(0 /* success */, getHeader, 200, _this.xdr.responseText));
|
||||
};
|
||||
|
||||
xdr.onprogress = function () {
|
||||
this.xdr.onprogress = function () {
|
||||
return null;
|
||||
};
|
||||
|
||||
xdr.open(HttpMethod[method], request.ToString());
|
||||
this.xdr.open(HttpMethod[this.method], this.request.ToString());
|
||||
|
||||
if (method == 0 /* GET */) {
|
||||
xdr.send();
|
||||
if (this.method == 0 /* GET */) {
|
||||
this.xdr.send();
|
||||
return;
|
||||
}
|
||||
|
||||
xdr.send(HttpUrl.UrlEncodeObject(postData));
|
||||
}
|
||||
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;
|
||||
};
|
||||
return XDR;
|
||||
})(Typertext.Transport.GenericTransport);
|
||||
})();
|
||||
Transport.XDR = XDR;
|
||||
})(Typertext.Transport || (Typertext.Transport = {}));
|
||||
var Transport = Typertext.Transport;
|
||||
|
@ -484,54 +496,63 @@ var Typertext;
|
|||
var HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||
var HttpResponse = Typertext.Http.HttpResponse;
|
||||
|
||||
var XHR = (function (_super) {
|
||||
__extends(XHR, _super);
|
||||
var XHR = (function () {
|
||||
function XHR(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 _this = this;
|
||||
this.postData = postData;
|
||||
this.method = method;
|
||||
this.request = request;
|
||||
this.callback = callback;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
this.xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4) {
|
||||
this.xhr.onreadystatechange = function () {
|
||||
if (_this.xhr.readyState == 4) {
|
||||
var getHeader = function (name) {
|
||||
return xhr.getResponseHeader(name);
|
||||
return _this.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));
|
||||
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));
|
||||
} else {
|
||||
callback(new HttpResponse(4 /* unknownError */, getHeader, xhr.status, xhr.responseText));
|
||||
_this.callback(new HttpResponse(4 /* unknownError */, getHeader, _this.xhr.status, _this.xhr.responseText));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.ontimeout = function () {
|
||||
callback(new HttpResponse(5 /* timeout */, function (i) {
|
||||
this.xhr.ontimeout = function () {
|
||||
_this.callback(new HttpResponse(5 /* timeout */, function (i) {
|
||||
return "";
|
||||
}, -1, ""));
|
||||
};
|
||||
}
|
||||
XHR.prototype.Send = function () {
|
||||
this.xhr.open(HttpMethod[this.method], this.request.ToString(), true);
|
||||
|
||||
xhr.open(HttpMethod[method], request.ToString(), true);
|
||||
|
||||
if (method == 0 /* GET */) {
|
||||
xhr.send();
|
||||
if (this.method == 0 /* GET */) {
|
||||
this.xhr.send();
|
||||
return;
|
||||
}
|
||||
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
||||
}
|
||||
this.xhr.send(HttpUrl.UrlEncodeObject(this.postData));
|
||||
};
|
||||
|
||||
XHR.prototype.Destroy = function () {
|
||||
this.xhr.onreadystatechange = this.xhr.ontimeout = null;
|
||||
this.xhr = null;
|
||||
};
|
||||
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
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
|
@ -1,3 +1,5 @@
|
|||
/// <reference path="Transport/TransportConstructor.ts" />
|
||||
|
||||
/**
|
||||
* @module Typertext
|
||||
* @submodule Http
|
||||
|
@ -7,6 +9,7 @@ module Typertext {
|
|||
import HttpMethod = Typertext.Http.HttpMethod;
|
||||
import HttpPostData = Typertext.Http.HttpPostData;
|
||||
import HttpUrl = Typertext.Http.HttpUrl;
|
||||
import TransportConstructor = Typertext.Transport.TransportConstructor;
|
||||
|
||||
/**
|
||||
* A class to simplify passing both the status and data of a completed proxy request
|
||||
|
@ -45,7 +48,8 @@ module Typertext {
|
|||
* @param {HttpUrl} request
|
||||
* @param {HttpPostData} postData
|
||||
* @param {GenericResponseHandler} callback
|
||||
* @param {GenericTransport} transport
|
||||
*/
|
||||
RawRequest(method:HttpMethod, request:HttpUrl, postData?:HttpPostData, callback?:T):void;
|
||||
RawRequest(method:HttpMethod, request:HttpUrl, postData?:HttpPostData, callback?:T, transport?:TransportConstructor):void;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
|
||||
|
||||
//TODO add support for IE8-9 CORS via XDomain
|
||||
//TODO better error handling, ala exceptions
|
||||
|
||||
|
@ -6,6 +8,10 @@
|
|||
* @module Http
|
||||
*/
|
||||
module Typertext.Http {
|
||||
import GenericTransport = Typertext.Transport.GenericTransport;
|
||||
import TransportChooser = Typertext.Transport.TransportChooser;
|
||||
import TransportConstructor = Typertext.Transport.TransportConstructor;
|
||||
|
||||
export class HttpRequest implements Typertext.GenericRequest<HttpResponseHandler> {
|
||||
/**
|
||||
* The class that everything that calls an http(s) server should use and build on top of using callbacks
|
||||
|
@ -49,10 +55,18 @@ module Typertext.Http {
|
|||
* @param {HttpUrl} request
|
||||
* @param {HttpPostData} postData
|
||||
* @param {HttpResponseHandler} callback
|
||||
* @param {GenericTransport} transport
|
||||
*/
|
||||
public RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> {
|
||||
}):void {
|
||||
Typertext.Transport.TransportChooser.Transport(method, request, postData, callback);
|
||||
public RawRequest(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback?:HttpResponseHandler, transport?:TransportConstructor):void {
|
||||
if (!callback)
|
||||
callback = (c)=> null;
|
||||
|
||||
if (!transport)
|
||||
transport = TransportChooser.Transport(method, request, postData, callback);
|
||||
|
||||
//This is guaranteed to return a GenericTransport, but PhpStorm isn't so sure
|
||||
var transportInstance:GenericTransport = <GenericTransport> new transport(method, request, postData, callback);
|
||||
transportInstance.Send();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ module Typertext.Json {
|
|||
import HttpUrl = Typertext.Http.HttpUrl;
|
||||
import HttpPostData = Typertext.Http.HttpPostData;
|
||||
import HttpMethod = Typertext.Http.HttpMethod;
|
||||
import TransportConstructor = Typertext.Transport.TransportConstructor;
|
||||
|
||||
export class JsonRequest implements Typertext.GenericRequest<JsonResponseHandler> {
|
||||
private jsonType:string;
|
||||
|
@ -62,8 +63,9 @@ module Typertext.Json {
|
|||
* @param {HttpUrl} request
|
||||
* @param {HttpPostData} postData
|
||||
* @param {JsonResponseHandler} callback
|
||||
* @param {TransportConstructor} transport
|
||||
*/
|
||||
public RawRequest(method:HttpMethod, request:HttpUrl, postData:Typertext.Http.HttpPostData = {}, callback?:JsonResponseHandler) {
|
||||
public RawRequest(method:HttpMethod, request:HttpUrl, postData:Typertext.Http.HttpPostData = {}, callback?:JsonResponseHandler, transport?:TransportConstructor) {
|
||||
//Ensure we have an executable callback
|
||||
if (typeof callback != "function") {
|
||||
//Make a request and ignore the response, throwing exceptions in async code can be weird
|
||||
|
@ -82,7 +84,7 @@ module Typertext.Json {
|
|||
|
||||
//If it is then we can just pass it straight through to the JSON response
|
||||
callback(JsonResponse.fromHttpResponse(response));
|
||||
});
|
||||
}, transport);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
module Typertext.Transport {
|
||||
export class GenericTransport {
|
||||
constructor(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler) {
|
||||
|
||||
}
|
||||
export interface GenericTransport {
|
||||
Send(): void;
|
||||
Destroy(): void;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
/**
|
||||
* @namespace Typertext
|
||||
* @module Transport
|
||||
*/
|
||||
module Typertext.Transport {
|
||||
import HttpMethod = Typertext.Http.HttpMethod;
|
||||
import HttpUrl = Typertext.Http.HttpUrl
|
||||
|
@ -13,7 +17,7 @@ module Typertext.Transport {
|
|||
* @param {HttpResponseHandler} callback
|
||||
* @returns {GenericTransport}
|
||||
*/
|
||||
static Transport(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):GenericTransport {
|
||||
static Transport(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):TransportConstructor {
|
||||
//Prepare to test if we are in IE
|
||||
var ieTestDiv = document.createElement("div");
|
||||
ieTestDiv.innerHTML = "<!--[if lte IE 7]><i></i><![endif]-->";
|
||||
|
@ -31,13 +35,13 @@ module Typertext.Transport {
|
|||
//If this is a CORS request in a modern browser
|
||||
if (!origin.CrossOriginCheck(origin) || !ieLte9) {
|
||||
//Just use a standard XHR request
|
||||
return new XHR(method, request, postData, callback);
|
||||
return XHR;
|
||||
}
|
||||
|
||||
//Otherwise if we aren't cross protocol
|
||||
if (origin.GetProtocol() === request.GetProtocol()) {
|
||||
//Use IE's silly XDomainRequest
|
||||
return new XDR(method, request, postData, callback);
|
||||
return XDR;
|
||||
}
|
||||
|
||||
//Otherwise there is no supported transport
|
||||
|
|
10
lib/Typertext/Transport/TransportConstructor.ts
Normal file
10
lib/Typertext/Transport/TransportConstructor.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 TransportConstructor {
|
||||
new (method:HttpMethod, request:HttpUrl, postData?:HttpPostData, callback?:HttpResponseHandler): GenericTransport;
|
||||
}
|
||||
}
|
|
@ -6,51 +6,67 @@ module Typertext.Transport {
|
|||
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||
import HttpResponse = Typertext.Http.HttpResponse;
|
||||
|
||||
export class XDR extends GenericTransport {
|
||||
export class XDR implements GenericTransport {
|
||||
private xdr:XDomainRequest;
|
||||
private postData: HttpPostData;
|
||||
private method:HttpMethod;
|
||||
private request:HttpUrl;
|
||||
private callback:HttpResponseHandler;
|
||||
|
||||
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
|
||||
super(method, request, postData, callback);
|
||||
|
||||
//Store the request information
|
||||
this.postData = postData;
|
||||
this.method = method;
|
||||
this.request = request;
|
||||
this.callback = callback;
|
||||
//Create a XDR
|
||||
var xdr = new XDomainRequest();
|
||||
this.xdr = new XDomainRequest();
|
||||
}
|
||||
|
||||
Send():void {
|
||||
//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 this.xdr.contentType;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
//Now to handle timeouts,
|
||||
xdr.ontimeout = () => {
|
||||
callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
|
||||
this.xdr.ontimeout = () => {
|
||||
this.callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
|
||||
};
|
||||
|
||||
//all errors (because XDR sucks),
|
||||
xdr.onerror = () => {
|
||||
callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, -1, xdr.responseText));
|
||||
this.xdr.onerror = () => {
|
||||
this.callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, -1, this.xdr.responseText));
|
||||
};
|
||||
|
||||
//successes,
|
||||
xdr.onload = () => {
|
||||
callback(new HttpResponse(HttpResponseStatus.success, getHeader, 200, xdr.responseText));
|
||||
this.xdr.onload = () => {
|
||||
this.callback(new HttpResponse(HttpResponseStatus.success, getHeader, 200, this.xdr.responseText));
|
||||
};
|
||||
|
||||
//and even more stupidity (because XDR REALLY sucks).
|
||||
xdr.onprogress = () => null;
|
||||
this.xdr.onprogress = () => null;
|
||||
|
||||
//Finally, open the request
|
||||
xdr.open(HttpMethod[method], request.ToString());
|
||||
this.xdr.open(HttpMethod[this.method], this.request.ToString());
|
||||
|
||||
//and either send
|
||||
if (method == HttpMethod.GET) {
|
||||
if (this.method == HttpMethod.GET) {
|
||||
//a get request without data,
|
||||
xdr.send();
|
||||
this.xdr.send();
|
||||
return;
|
||||
}
|
||||
|
||||
//or send the post-data to the server (as text/plain, because XDR sucks)
|
||||
xdr.send(HttpUrl.UrlEncodeObject(postData));
|
||||
this.xdr.send(HttpUrl.UrlEncodeObject(this.postData));
|
||||
}
|
||||
|
||||
Destroy():void {
|
||||
this.xdr.ontimeout = this.xdr.onerror = this.xdr.onload = this.xdr.onprogress = null;
|
||||
this.xdr = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,59 +6,76 @@ module Typertext.Transport {
|
|||
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
||||
import HttpResponse = Typertext.Http.HttpResponse;
|
||||
|
||||
export class XHR extends GenericTransport {
|
||||
export class XHR implements GenericTransport {
|
||||
private xhr:XMLHttpRequest;
|
||||
private postData: HttpPostData;
|
||||
private method:HttpMethod;
|
||||
private request:HttpUrl;
|
||||
private callback:HttpResponseHandler;
|
||||
|
||||
constructor(method:HttpMethod, request:HttpUrl, postData:HttpPostData = {}, callback:HttpResponseHandler = (c)=> null) {
|
||||
super(method, request, postData, callback);
|
||||
//Store the request information
|
||||
this.postData = postData;
|
||||
this.method = method;
|
||||
this.request = request;
|
||||
this.callback = callback;
|
||||
|
||||
//Create a XHR
|
||||
var xhr = new XMLHttpRequest();
|
||||
this.xhr = new XMLHttpRequest();
|
||||
|
||||
//And let us know when it does something
|
||||
xhr.onreadystatechange = ()=> {
|
||||
this.xhr.onreadystatechange = ()=> {
|
||||
//If the request is complete
|
||||
if (xhr.readyState == 4) {
|
||||
if (this.xhr.readyState == 4) {
|
||||
//Prepare a getter for the header
|
||||
var getHeader = (name:string):string => {
|
||||
return xhr.getResponseHeader(name);
|
||||
return this.xhr.getResponseHeader(name);
|
||||
};
|
||||
|
||||
//Check the status
|
||||
if (xhr.status == 200) {
|
||||
if (this.xhr.status == 200) {
|
||||
//And either succeed
|
||||
callback(new HttpResponse(HttpResponseStatus.success, getHeader, xhr.status, xhr.responseText));
|
||||
} else if (xhr.status >= 400 && xhr.status < 500) {
|
||||
this.callback(new HttpResponse(HttpResponseStatus.success, getHeader, this.xhr.status, this.xhr.responseText));
|
||||
} else if (this.xhr.status >= 400 && this.xhr.status < 500) {
|
||||
//Or fail miserably
|
||||
callback(new HttpResponse(HttpResponseStatus.clientError, getHeader, xhr.status, xhr.responseText));
|
||||
} else if (xhr.status >= 500 && xhr.status < 600) {
|
||||
this.callback(new HttpResponse(HttpResponseStatus.clientError, getHeader, this.xhr.status, this.xhr.responseText));
|
||||
} else if (this.xhr.status >= 500 && this.xhr.status < 600) {
|
||||
//Again
|
||||
callback(new HttpResponse(HttpResponseStatus.serverError, getHeader, xhr.status, xhr.responseText));
|
||||
this.callback(new HttpResponse(HttpResponseStatus.serverError, getHeader, this.xhr.status, this.xhr.responseText));
|
||||
} else {
|
||||
//And again
|
||||
callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, xhr.status, xhr.responseText));
|
||||
this.callback(new HttpResponse(HttpResponseStatus.unknownError, getHeader, this.xhr.status, this.xhr.responseText));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//Or if it times out
|
||||
xhr.ontimeout = () => {
|
||||
this.xhr.ontimeout = () => {
|
||||
//And make a big deal of the failing
|
||||
callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
|
||||
this.callback(new HttpResponse(HttpResponseStatus.timeout, (i:string)=>"", -1, ""));
|
||||
};
|
||||
}
|
||||
|
||||
public Send() {
|
||||
//Now connect
|
||||
xhr.open(HttpMethod[method], request.ToString(), true);
|
||||
this.xhr.open(HttpMethod[this.method], this.request.ToString(), true);
|
||||
|
||||
//And either send
|
||||
if (method == HttpMethod.GET) {
|
||||
if (this.method == HttpMethod.GET) {
|
||||
//A get request
|
||||
xhr.send();
|
||||
this.xhr.send();
|
||||
return;
|
||||
}
|
||||
|
||||
//Or set the content-type
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
//And send the post-data to the server
|
||||
xhr.send(HttpUrl.UrlEncodeObject(postData));
|
||||
this.xhr.send(HttpUrl.UrlEncodeObject(this.postData));
|
||||
}
|
||||
|
||||
public Destroy():void {
|
||||
this.xhr.onreadystatechange = this.xhr.ontimeout = null;
|
||||
this.xhr = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
"type": "git",
|
||||
"url": "https://github.com/terribleplan/Typertext.git"
|
||||
},
|
||||
"version": "0.6.1",
|
||||
"version": "0.7.0",
|
||||
"devDependencies": {
|
||||
"grunt": "~0.4.2",
|
||||
"grunt-cli": "~0.1.13",
|
||||
|
|
Reference in a new issue