Document HttpUrl and rename privates
This commit is contained in:
parent
6aa6cb8673
commit
f5731d9776
|
@ -1,23 +1,45 @@
|
||||||
|
/**
|
||||||
|
* @namespace Typertext
|
||||||
|
* @module Http
|
||||||
|
*/
|
||||||
module Typertext.Http {
|
module Typertext.Http {
|
||||||
export class HttpUrl {
|
export class HttpUrl {
|
||||||
private _Domain:string;
|
private domain:string;
|
||||||
private _Path:string;
|
private path:string;
|
||||||
private _Port:number;
|
private port:number;
|
||||||
private _Protocol:HttpProtocol;
|
private protocol:HttpProtocol;
|
||||||
private _QueryString:{
|
private queryString:{
|
||||||
[index:string]:string
|
[index:string]:string
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param protocol
|
||||||
|
* @returns {number}
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
public static DefaultPort(protocol:HttpProtocol) {
|
public static DefaultPort(protocol:HttpProtocol) {
|
||||||
return ((protocol == HttpProtocol.http) ? 80 : 443)
|
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 {
|
public static FromUrl(location:string):HttpUrl {
|
||||||
var l = document.createElement("a");
|
var l = document.createElement("a");
|
||||||
l.href = location;
|
l.href = location;
|
||||||
return new HttpUrl(l.hostname, HttpProtocol[l.protocol], l.pathname, HttpUrl.DecodeQueryString(l.search))
|
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 {
|
public static DecodeQueryString(queryString:string):HttpQueryString {
|
||||||
if (queryString.length == 0 || queryString == "?") {
|
if (queryString.length == 0 || queryString == "?") {
|
||||||
return {};
|
return {};
|
||||||
|
@ -26,11 +48,23 @@ module Typertext.Http {
|
||||||
return HttpUrl.UrlDecodeString(queryString);
|
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) {
|
public static EncodeQueryString(query:HttpQueryString) {
|
||||||
var rs = "?" + HttpUrl.UrlEncodeObject(query);
|
var rs = "?" + HttpUrl.UrlEncodeObject(query);
|
||||||
return ((rs.length == 1) ? "" : rs);
|
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 {
|
public static UrlEncodeObject(data:HttpQueryString):string {
|
||||||
var rs:string = "";
|
var rs:string = "";
|
||||||
var temp:string;
|
var temp:string;
|
||||||
|
@ -42,6 +76,12 @@ module Typertext.Http {
|
||||||
return rs.slice(0, -1);
|
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 {
|
public static UrlDecodeString(queryString:string):HttpQueryString {
|
||||||
var returnValue:HttpQueryString = {}, params:string[] = HttpUrl.splitString(queryString, "&");
|
var returnValue:HttpQueryString = {}, params:string[] = HttpUrl.splitString(queryString, "&");
|
||||||
for (var i:number = 0; i < params.length; i++) {
|
for (var i:number = 0; i < params.length; i++) {
|
||||||
|
@ -57,6 +97,15 @@ module Typertext.Http {
|
||||||
return returnValue;
|
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[] {
|
private static splitString(input:string, separator:string, limit:number = 0):string[] {
|
||||||
limit++;
|
limit++;
|
||||||
var chunks:string[] = input.split(separator);
|
var chunks:string[] = input.split(separator);
|
||||||
|
@ -68,6 +117,18 @@ module Typertext.Http {
|
||||||
return chunks;
|
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) {
|
constructor(domain:string, protocol:HttpProtocol = HttpProtocol.http, path:string = "/", queryString:HttpQueryString = {}, port:number = 0) {
|
||||||
if (port < 1 || port > 65535) {
|
if (port < 1 || port > 65535) {
|
||||||
port = HttpUrl.DefaultPort(protocol);
|
port = HttpUrl.DefaultPort(protocol);
|
||||||
|
@ -77,18 +138,22 @@ module Typertext.Http {
|
||||||
path = "/" + path;
|
path = "/" + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._Domain = domain;
|
this.domain = domain;
|
||||||
this._Protocol = protocol;
|
this.protocol = protocol;
|
||||||
this._Path = path;
|
this.path = path;
|
||||||
this._QueryString = queryString;
|
this.queryString = queryString;
|
||||||
this._Port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format the current HttpUrl into a standard href/url
|
||||||
|
*
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
public ToString():string {
|
public ToString():string {
|
||||||
return HttpProtocol[this._Protocol] + "://" + this._Domain +
|
return HttpProtocol[this.protocol] + "://" + this.domain +
|
||||||
((this._Port == HttpUrl.DefaultPort(this._Protocol)) ? "" : ":" + this._Port) + this._Path +
|
((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path +
|
||||||
HttpUrl.EncodeQueryString(this._QueryString);
|
HttpUrl.EncodeQueryString(this.queryString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue