Document HttpUrl and rename privates

This commit is contained in:
Kegan Myers 2014-02-28 11:16:04 -06:00
parent 6aa6cb8673
commit f5731d9776
1 changed files with 79 additions and 14 deletions

View File

@ -1,23 +1,45 @@
/**
* @namespace Typertext
* @module Http
*/
module Typertext.Http {
export class HttpUrl {
private _Domain:string;
private _Path:string;
private _Port:number;
private _Protocol:HttpProtocol;
private _QueryString:{
private domain:string;
private path:string;
private port:number;
private protocol:HttpProtocol;
private queryString:{
[index:string]:string
};
/**
*
* @param protocol
* @returns {number}
* @constructor
*/
public static DefaultPort(protocol:HttpProtocol) {
return ((protocol == HttpProtocol.http) ? 80 : 443)
}
/**
* This method takes a URL as a string and decomposes it into a Typertext HttpUrl
*
* @param {string} location
* @returns {HttpUrl}
*/
public static FromUrl(location:string):HttpUrl {
var l = document.createElement("a");
l.href = location;
return new HttpUrl(l.hostname, HttpProtocol[l.protocol], l.pathname, HttpUrl.DecodeQueryString(l.search))
}
/**
* Reads a standard query string (or "search") component and returns it as an object
*
* @param {string} queryString
* @returns {HttpQueryString}
*/
public static DecodeQueryString(queryString:string):HttpQueryString {
if (queryString.length == 0 || queryString == "?") {
return {};
@ -26,11 +48,23 @@ module Typertext.Http {
return HttpUrl.UrlDecodeString(queryString);
}
/**
* Reads a Typertext HttpQueryString and converts it to the proper string representations
*
* @param {HttpQueryString} query
* @returns {string}
*/
public static EncodeQueryString(query:HttpQueryString) {
var rs = "?" + HttpUrl.UrlEncodeObject(query);
return ((rs.length == 1) ? "" : rs);
}
/**
* Reads any object with string key/value pairs and converts it to a urlencoded string
*
* @param data
* @returns {string}
*/
public static UrlEncodeObject(data:HttpQueryString):string {
var rs:string = "";
var temp:string;
@ -42,6 +76,12 @@ module Typertext.Http {
return rs.slice(0, -1);
}
/**
* Reads any urlencoded string and converts it to a object with string key/value pairs
*
* @param {string} queryString
* @returns {HttpQueryString}
*/
public static UrlDecodeString(queryString:string):HttpQueryString {
var returnValue:HttpQueryString = {}, params:string[] = HttpUrl.splitString(queryString, "&");
for (var i:number = 0; i < params.length; i++) {
@ -57,6 +97,15 @@ module Typertext.Http {
return returnValue;
}
/**
* Split a string by a string, returning the remainder in the (limit-1)'th index, with no guarantee of object
* length
*
* @param {string} input
* @param {string} separator
* @param {number} limit
* @returns {string[]}
*/
private static splitString(input:string, separator:string, limit:number = 0):string[] {
limit++;
var chunks:string[] = input.split(separator);
@ -68,6 +117,18 @@ module Typertext.Http {
return chunks;
}
/**
*
* @param {string} domain
* @param {HttpProtocol} protocol
* @param {string} path
* @param {HttpQueryString} queryString
* @param {number} port
*
* @author Kegan Myers <kegan@keganmyers.com>
* @version 0.3.0
* @constructor
*/
constructor(domain:string, protocol:HttpProtocol = HttpProtocol.http, path:string = "/", queryString:HttpQueryString = {}, port:number = 0) {
if (port < 1 || port > 65535) {
port = HttpUrl.DefaultPort(protocol);
@ -77,18 +138,22 @@ module Typertext.Http {
path = "/" + path;
}
this._Domain = domain;
this._Protocol = protocol;
this._Path = path;
this._QueryString = queryString;
this._Port = port;
this.domain = domain;
this.protocol = protocol;
this.path = path;
this.queryString = queryString;
this.port = port;
}
/**
* Format the current HttpUrl into a standard href/url
*
* @returns {string}
*/
public ToString():string {
return HttpProtocol[this._Protocol] + "://" + this._Domain +
((this._Port == HttpUrl.DefaultPort(this._Protocol)) ? "" : ":" + this._Port) + this._Path +
HttpUrl.EncodeQueryString(this._QueryString);
return HttpProtocol[this.protocol] + "://" + this.domain +
((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path +
HttpUrl.EncodeQueryString(this.queryString);
}
}
}