uribuilder
Version:
URI builder and parser
140 lines (138 loc) • 4.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var uriQueryBuilder_1 = require("./uriQueryBuilder");
var uriSchemaPortList_1 = require("./uriSchemaPortList");
// #region Uri Format Regex
var uriRegExp = /^(([^:/?#]+):)\/\/([^/?#]+)(\?([^#]*))?(#(.*))?/;
var uriRegExp_schema = /^(([^:/?#]+):)/;
var uriRegExp_hostPort = /\/\/[^/?#]+/;
var uriRegExp_pathSegments = /(\/[^/?#]*)+/;
var uriRegExp_query = /\?[^#]*/;
var uriRegExp_fragment = /#(.*)/;
var UriBuilder = (function () {
function UriBuilder() {
this.query = {};
}
Object.defineProperty(UriBuilder.prototype, "authority", {
get: function () {
return this._authority;
},
enumerable: true,
configurable: true
});
Object.defineProperty(UriBuilder.prototype, "port", {
get: function () {
return this._port || uriSchemaPortList_1.UriSchemaPortList.getPort(this.schema);
},
set: function (value) {
this._port = value;
},
enumerable: true,
configurable: true
});
UriBuilder.isUriFormat = function (str) {
return uriRegExp.test(str);
};
UriBuilder.updateQuery = function (uri, model) {
var builder = UriBuilder.parse(uri);
for (var key in model) {
if (!model.hasOwnProperty(key)) {
continue;
}
builder.query[key] = model[key];
}
return builder.toString();
};
UriBuilder.parse = function (uri) {
if (!this.isUriFormat(uri)) {
uri = UriBuilder.relative + "://" + uri;
if (!this.isUriFormat(uri)) {
throw new Error('URI Format ERROR');
}
}
var result = new UriBuilder();
result.schema = uri.match(uriRegExp_schema)[0];
result.schema = result.schema.substring(0, result.schema.length - 1);
result.host = uri.match(uriRegExp_hostPort)[0].substring(2);
if (result.host.indexOf('@') > -1) {
// has authority
var authorityTemp = result.host.split('@', 2);
result.host = authorityTemp[1];
authorityTemp = authorityTemp[0].split(':');
result._authority = {
user: authorityTemp[0],
password: authorityTemp[1]
};
}
var hostPortTemp = result.host.split(':', 2);
result.host = hostPortTemp[0];
result.port = +hostPortTemp[1];
result.pathSegments = uri
.match(uriRegExp_pathSegments)[0]
.substring(2)
.split('/')
.slice(1);
var queryTemp = uri.match(uriRegExp_query);
if (queryTemp &&
uri.substring(0, queryTemp.index + 1).indexOf('#') === -1) {
result.query = uriQueryBuilder_1.UriQueryBuilder.parse(queryTemp[0]).model;
}
var hashTemp = uri.match(uriRegExp_fragment);
if (hashTemp) {
result.fragment = hashTemp[0].substring(1);
}
return result;
};
UriBuilder.prototype.setPath = function (path) {
if (path.indexOf('/') === 0)
path = path.substring(1);
this.pathSegments = path.split('/');
};
UriBuilder.prototype.setAuthority = function (user, password) {
if (!this._authority) {
this._authority = {
user: undefined
};
}
this._authority.user = user;
this._authority.password = password;
};
UriBuilder.prototype.isRelative = function () {
return this.schema === UriBuilder.relative;
};
UriBuilder.prototype.toString = function () {
var result = this.schema + "://";
if (this.schema === UriBuilder.relative) {
result = '';
}
if (this.authority && this.authority.user) {
result += this.authority.user;
if (this.authority.password) {
result += ':' + this.authority.password;
}
result += '@';
}
result += this.host;
if (!uriSchemaPortList_1.UriSchemaPortList.isDefaultPort(this.schema, this.port)) {
result += ':' + this.port;
}
result += '/';
if (this.pathSegments && this.pathSegments.length) {
result += this.pathSegments.join('/');
}
if (this.query) {
var queryTemp = new uriQueryBuilder_1.UriQueryBuilder(this.query).toString();
if (queryTemp.length) {
result += '?' + queryTemp;
}
}
if (this.fragment && this.fragment.length) {
result += '#' + this.fragment;
}
return result;
};
UriBuilder.relative = 'relative';
return UriBuilder;
}());
exports.UriBuilder = UriBuilder;
//# sourceMappingURL=uriBuilder.js.map