UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

95 lines (94 loc) 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Url = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var util = require("./util"); var Url = (function () { function Url(args) { this._query = {}; this._querystring = ''; this.origin = Url.parse(args.origin).origin; this.path = "/".concat((args.path || '').trim().replace(/^\/*/, '')); this._query = args.query || {}; this._querystring = typeof args.querystring === 'string' ? args.querystring.trim() : ''; } Url.parse = function (input) { input = common_1.value.isNumeric(input) ? "localhost:".concat(input) : input === null || input === void 0 ? void 0 : input.toString(); var text = (input || '').trim(); text = text || 'localhost'; text = util.stripHttp(text); var index = text.indexOf('/'); var path = index > -1 ? text.substring(index) : ''; path = "/".concat(path.replace(/^\/*/, '')); text = index > -1 ? text.substring(0, text.indexOf('/')) : text; var hostname = common_1.R.pipe(util.stripSlashEnd, util.stripPort)(text).split('/')[0]; hostname = hostname || 'localhost'; var protocol = util.toProtocol(hostname); var port = util.toPort(text) || 80; var host = port === 80 ? hostname : "".concat(hostname, ":").concat(port); var origin = { protocol: protocol, hostname: hostname, host: host, port: port, toString: function () { return "".concat(protocol, "://").concat(host); }, }; return { origin: origin, path: path }; }; Object.defineProperty(Url.prototype, "querystring", { get: function () { var text = (this._querystring || '').replace(/^\?*/, ''); var query = this._query || {}; var res = ''; var format = function (value) { value = typeof value === 'string' ? value.trim() : value; return value; }; var append = function (key, value) { res = res ? "".concat(res, "&") : res; res = value === undefined ? "".concat(res).concat(key) : "".concat(res).concat(key, "=").concat(value); }; Object.keys(query).forEach(function (key) { var value = query[key]; if (typeof value !== 'function' && value !== undefined && value !== '') { if (Array.isArray(value)) { var values = value.map(function (value) { return format(value); }); common_1.R.uniq(values).forEach(function (value) { return append(key, value); }); } else { append(key, format(value)); } } }); if (text) { res = res ? "".concat(res, "&").concat(text) : text; } return res ? "?".concat(res) : ''; }, enumerable: false, configurable: true }); Url.prototype.query = function (input) { var querystring = this._querystring || ''; var query = this._query || {}; if (typeof input === 'object') { query = tslib_1.__assign(tslib_1.__assign({}, query), input); } return new Url({ origin: this.origin.toString(), path: this.path, query: query, querystring: querystring, }); }; Url.prototype.toString = function (options) { if (options === void 0) { options = {}; } var origin = (0, common_1.defaultValue)(options.origin, true); var path = "".concat(this.path).concat(this.querystring); return origin ? "".concat(this.origin).concat(path) : path; }; Url.isLocal = util.isLocal; return Url; }()); exports.Url = Url;