2014-02-26 19:12:37 +00:00
//TODO add support for IE8-9 CORS via XDomain
//TODO better error handling, ala exceptions
2014-02-28 16:57:21 +00:00
/ * *
* @namespace Typertext
* @module Http
* /
2014-02-26 19:12:37 +00:00
module Typertext.Http {
export class HttpRequest implements Typertext . GenericRequest < HttpResponseHandler > {
2014-02-28 16:57:21 +00:00
/ * *
* The class that everything that calls an http ( s ) server should use and build on top of using callbacks
*
* @class HttpRequest
* @extends GenericRequest
*
* @author Kegan Myers < kegan @ keganmyers.com >
* @version 0.3 . 0
* @constructor
* /
2014-03-04 22:51:55 +00:00
constructor ( ) {
2014-02-26 19:12:37 +00:00
}
2014-02-28 16:57:21 +00:00
/ * *
* A convenience method for simply calling a GET
*
* @param { HttpUrl } request
* @param { HttpResponseHandler } callback
* /
2014-02-26 19:12:37 +00:00
public Get ( request :HttpUrl , callback :HttpResponseHandler ) : void {
this . RawRequest ( HttpMethod . GET , request , { } , callback ) ;
}
2014-02-28 16:57:21 +00:00
/ * *
* A convenience method for simply calling a POST
*
* @param { HttpUrl } request
* @param { HttpPostData } postData
* @param { HttpResponseHandler } callback
* /
2014-02-26 19:12:37 +00:00
public Post ( request :HttpUrl , postData :HttpPostData , callback :HttpResponseHandler ) : void {
2014-03-04 01:04:49 +00:00
this . RawRequest ( HttpMethod . POST , request , postData , callback ) ;
2014-02-26 19:12:37 +00:00
}
2014-02-28 16:57:21 +00:00
/ * *
* This is a method that calls against a specified HTTP server and does basic handling of responses , with no
* data manipulation
*
* @param { HttpMethod } method
* @param { HttpUrl } request
* @param { HttpPostData } postData
* @param { HttpResponseHandler } callback
* /
2014-02-26 19:12:37 +00:00
public RawRequest ( method :HttpMethod , request :HttpUrl , postData :HttpPostData = { } , callback :HttpResponseHandler = ( c ) = > {
} ) : void {
2014-03-04 22:51:55 +00:00
var noop = ( i :string ) = > {
return "" ;
} ;
//Create a XHR
2014-02-26 19:12:37 +00:00
var xhr = new XMLHttpRequest ( ) ;
2014-03-04 22:51:55 +00:00
//And let us know when it does something
2014-02-26 19:12:37 +00:00
xhr . onreadystatechange = ( ) = > {
2014-03-04 22:51:55 +00:00
//If the request is complete
2014-02-26 19:12:37 +00:00
if ( xhr . readyState == 4 ) {
2014-03-04 22:51:55 +00:00
//Prepare a getter for the header
2014-03-03 18:35:46 +00:00
var getHeader = ( name :string ) : string = > {
return xhr . getResponseHeader ( name ) ;
} ;
2014-03-04 22:51:55 +00:00
//Check the status
2014-02-26 19:12:37 +00:00
if ( xhr . status == 200 ) {
2014-03-04 22:51:55 +00:00
//And either succeed
2014-03-03 18:35:46 +00:00
callback ( new HttpResponse ( HttpResponseStatus . success , getHeader , xhr . status , xhr . responseText ) ) ;
2014-02-26 19:12:37 +00:00
} else if ( xhr . status >= 400 && xhr . status < 500 ) {
2014-03-04 22:51:55 +00:00
//Or fail miserably
throw new HttpException ( "The server returned an error response state" , xhr . status , new HttpResponse ( HttpResponseStatus . clientError , getHeader , xhr . status , xhr . responseText ) ) ;
2014-02-26 19:12:37 +00:00
} else if ( xhr . status >= 500 && xhr . status < 600 ) {
2014-03-04 22:51:55 +00:00
//Again
throw new HttpException ( "The server returned an error response state" , xhr . status , new HttpResponse ( HttpResponseStatus . serverError , getHeader , xhr . status , xhr . responseText ) ) ;
2014-02-26 19:12:37 +00:00
}
2014-03-04 22:51:55 +00:00
//And again
throw new HttpException ( "An unknown error has occurred" , - 2 , new HttpResponse ( HttpResponseStatus . unknownError , getHeader , xhr . status , xhr . responseText ) ) ;
2014-02-26 19:12:37 +00:00
}
} ;
2014-03-04 22:51:55 +00:00
//Or if it times out
2014-02-26 19:12:37 +00:00
xhr . ontimeout = ( ) = > {
2014-03-04 22:51:55 +00:00
//And make a big deal of the failing
throw new HttpException ( "The server took too long to respond to our request" , - 1 , new HttpResponse ( HttpResponseStatus . timeout , noop , - 1 , "" ) ) ;
2014-02-26 19:12:37 +00:00
} ;
2014-03-04 22:51:55 +00:00
//Now connect
2014-02-26 19:12:37 +00:00
xhr . open ( HttpMethod [ method ] , request . ToString ( ) , true ) ;
2014-03-04 22:51:55 +00:00
//And either send
2014-02-26 19:12:37 +00:00
if ( method == HttpMethod . GET ) {
2014-03-04 22:51:55 +00:00
//A get request
2014-02-26 19:12:37 +00:00
xhr . send ( ) ;
return ;
}
2014-03-04 22:51:55 +00:00
//Or set the content-type
2014-02-26 19:12:37 +00:00
xhr . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
2014-03-04 22:51:55 +00:00
//And send the post-data to the server
2014-02-26 21:58:23 +00:00
xhr . send ( HttpUrl . UrlEncodeObject ( postData ) ) ;
2014-02-26 19:12:37 +00:00
}
}
}