@tdb/util
Version:
Shared helpers and utilities.
273 lines (272 loc) • 9.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var url_1 = require("url");
var queryString_1 = require("../queryString");
var value_1 = require("../value");
var libs_1 = require("./libs");
var Route = (function () {
function Route(path, options) {
if (options === void 0) { options = {}; }
path = (path || '').trim();
if (!path) {
throw new Error("A route pattern is required.");
}
var sensitive = value_1.value.defaultValue(options.caseSensitive, true);
var strict = value_1.value.defaultValue(options.strict, true);
var end = value_1.value.defaultValue(options.end, true);
var start = value_1.value.defaultValue(options.start, true);
var delimiter = value_1.value.defaultValue(options.delimiter, '/');
var endsWith = value_1.value.defaultValue(options.endsWith, undefined);
this.path = path;
this._options = options;
this._regex = libs_1.pathToRegex(path, [], {
sensitive: sensitive,
strict: strict,
end: end,
start: start,
delimiter: delimiter,
endsWith: endsWith,
});
this._tokens = libs_1.pathToRegex.parse(path);
this._toPath = libs_1.pathToRegex.compile(path);
}
Route.prototype.toString = function () {
return this.path;
};
Object.defineProperty(Route.prototype, "schema", {
get: function () {
return this._options.schema || {};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "method", {
get: function () {
return this._options.method || 'GET';
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "title", {
get: function () {
return this._options.title;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "description", {
get: function () {
return this._options.description;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "docs", {
get: function () {
return this._options.docs;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "meta", {
get: function () {
return this._options.meta || {};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "tokens", {
get: function () {
var toObj = function (token) {
var name = token.name, prefix = token.prefix, delimiter = token.delimiter, optional = token.optional, repeat = token.repeat;
var res = {
name: name,
prefix: prefix,
delimiter: delimiter,
optional: optional,
repeat: repeat,
};
return res;
};
return (this._tokens || []).map(function (token) {
return typeof token === 'string' ? token : toObj(token);
});
},
enumerable: true,
configurable: true
});
Route.prototype.isMatch = function (url) {
return Boolean(this._regex.exec(url || ''));
};
Route.prototype.params = function (url) {
return Route.params(this, url);
};
Route.params = function (route, url) {
var params = {};
if (!url) {
return params;
}
var tokens = route.tokens;
var matches = route._regex.exec(url);
if (!matches) {
return params;
}
matches.forEach(function (value, i) {
value = value || '';
var token = tokens[i];
if (typeof token === 'object') {
value = value.split('?')[0];
params[token.name] = value_1.value.toType(value);
}
});
return params;
};
Route.prototype.query = function (url) {
return Route.query(url);
};
Route.query = function (url) {
var query = url ? url_1.parse(url, true).query : {};
Object.keys(query).forEach(function (key) {
var value = query[key];
value = value === '' ? queryString_1.queryString.valueAsFlag(value) : value;
value = value_1.value.toType(value);
query[key] = value;
});
return query;
};
Route.prototype.url = function (url, options) {
if (url === void 0) { url = ''; }
if (options === void 0) { options = {}; }
var origin = options.origin;
return new RouteUrl({ url: url, route: this, origin: origin });
};
Route.prototype.toUrl = function (args) {
var _a = args.params, params = _a === void 0 ? {} : _a, _b = args.query, query = _b === void 0 ? {} : _b, origin = args.origin;
var url = this._toPath(params);
var q = Route.toQueryString(query);
if (q) {
url = url.includes('?') ? url + "&" + q : url + "?" + q;
}
return this.url(url, { origin: origin });
};
Route.toQueryString = function (query) {
var toQuery = function (key, value) {
value = value ? encodeURI(value.toString()) : value;
return value ? key + "=" + value : key;
};
var res = query
? Object.keys(query || {})
.filter(function (key) { return Boolean(query[key]); })
.map(function (key) { return toQuery(key, query[key]); })
: [];
return res.join('&');
};
Route.prototype.clone = function (options) {
if (options === void 0) { options = {}; }
return Route.create(options.path || this.path, tslib_1.__assign({}, this._options, options));
};
Route.prototype.toObject = function () {
return {
path: this.path,
method: this.method,
title: this.title,
description: this.description,
docs: this.docs,
schema: this.schema,
tokens: this.tokens,
};
};
Route.walk = function (tree, fn) {
if (!tree) {
return;
}
var stopped = false;
for (var _i = 0, _a = Object.keys(tree); _i < _a.length; _i++) {
var key = _a[_i];
var value = tree[key];
if (tree[key] instanceof Route) {
fn(value, { stop: function () { return (stopped = true); } });
}
else {
if (typeof value === 'object') {
this.walk(value, fn);
}
}
if (stopped) {
return;
}
}
};
Route.find = function (tree, match) {
if (!tree) {
return;
}
var result;
Route.walk(tree, function (route, e) {
if (match(route) === true) {
result = route;
e.stop();
}
});
return result;
};
Route.map = function (tree, fn) {
var result = [];
Route.walk(tree, function (route) { return (result = result.concat([fn(route, result.length - 1)])); });
return result;
};
Route.toString = function (path, options) {
if (options === void 0) { options = {}; }
var res = path;
if (options.origin) {
res = options.origin.replace(/\/*$/, '') + "/" + res.replace(/^\//, '');
}
return res;
};
Route.create = function (path, args) { return new Route(path, args); };
Route.get = function (path, args) {
return Route.create(path, tslib_1.__assign({}, args, { method: 'GET' }));
};
Route.put = function (path, args) {
return Route.create(path, tslib_1.__assign({}, args, { method: 'PUT' }));
};
Route.post = function (path, args) {
return Route.create(path, tslib_1.__assign({}, args, { method: 'POST' }));
};
Route.delete = function (path, args) {
return Route.create(path, tslib_1.__assign({}, args, { method: 'DELETE' }));
};
Route.patch = function (path, args) {
return Route.create(path, tslib_1.__assign({}, args, { method: 'PATCH' }));
};
return Route;
}());
exports.Route = Route;
var RouteUrl = (function () {
function RouteUrl(args) {
var url = args.url, route = args.route, _a = args.origin, origin = _a === void 0 ? '' : _a;
this.path = url;
this.route = route;
this.params = Route.params(route, url);
this.query = Route.query(url);
this.origin = origin.trim() ? origin.replace(/\/*$/, '') : undefined;
}
Object.defineProperty(RouteUrl.prototype, "s", {
get: function () {
return this.toString();
},
enumerable: true,
configurable: true
});
RouteUrl.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var origin = options.origin || this.origin;
return Route.toString(this.path, tslib_1.__assign({}, options, { origin: origin }));
};
RouteUrl.prototype.hasFlag = function (key) {
return queryString_1.queryString.isFlag(key, this.query);
};
return RouteUrl;
}());
exports.RouteUrl = RouteUrl;