1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Url = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("../common");
|
6 | var util = require("./util");
|
7 | var Url = (function () {
|
8 | function Url(args) {
|
9 | this._query = {};
|
10 | this._querystring = '';
|
11 | this.origin = Url.parse(args.origin).origin;
|
12 | this.path = "/" + (args.path || '').trim().replace(/^\/*/, '');
|
13 | this._query = args.query || {};
|
14 | this._querystring = typeof args.querystring === 'string' ? args.querystring.trim() : '';
|
15 | }
|
16 | Url.parse = function (input) {
|
17 | input = common_1.value.isNumeric(input) ? "localhost:" + input : input === null || input === void 0 ? void 0 : input.toString();
|
18 | var text = (input || '').trim();
|
19 | text = text || 'localhost';
|
20 | text = util.stripHttp(text);
|
21 | var index = text.indexOf('/');
|
22 | var path = index > -1 ? text.substring(index) : '';
|
23 | path = "/" + path.replace(/^\/*/, '');
|
24 | text = index > -1 ? text.substring(0, text.indexOf('/')) : text;
|
25 | var hostname = common_1.R.pipe(util.stripSlashEnd, util.stripPort)(text).split('/')[0];
|
26 | hostname = hostname || 'localhost';
|
27 | var protocol = util.toProtocol(hostname);
|
28 | var port = util.toPort(text) || 80;
|
29 | var host = port === 80 ? hostname : hostname + ":" + port;
|
30 | var origin = {
|
31 | protocol: protocol,
|
32 | hostname: hostname,
|
33 | host: host,
|
34 | port: port,
|
35 | toString: function () { return protocol + "://" + host; },
|
36 | };
|
37 | return { origin: origin, path: path };
|
38 | };
|
39 | Object.defineProperty(Url.prototype, "querystring", {
|
40 | get: function () {
|
41 | var text = (this._querystring || '').replace(/^\?*/, '');
|
42 | var query = this._query || {};
|
43 | var res = '';
|
44 | var format = function (value) {
|
45 | value = typeof value === 'string' ? value.trim() : value;
|
46 | return value;
|
47 | };
|
48 | var append = function (key, value) {
|
49 | res = res ? res + "&" : res;
|
50 | res = value === undefined ? "" + res + key : "" + res + key + "=" + value;
|
51 | };
|
52 | Object.keys(query).forEach(function (key) {
|
53 | var value = query[key];
|
54 | if (typeof value !== 'function' && value !== undefined && value !== '') {
|
55 | if (Array.isArray(value)) {
|
56 | var values = value.map(function (value) { return format(value); });
|
57 | common_1.R.uniq(values).forEach(function (value) { return append(key, value); });
|
58 | }
|
59 | else {
|
60 | append(key, format(value));
|
61 | }
|
62 | }
|
63 | });
|
64 | if (text) {
|
65 | res = res ? res + "&" + text : text;
|
66 | }
|
67 | return res ? "?" + res : '';
|
68 | },
|
69 | enumerable: false,
|
70 | configurable: true
|
71 | });
|
72 | Url.prototype.query = function (input) {
|
73 | var querystring = this._querystring || '';
|
74 | var query = this._query || {};
|
75 | if (typeof input === 'object') {
|
76 | query = tslib_1.__assign(tslib_1.__assign({}, query), input);
|
77 | }
|
78 | return new Url({
|
79 | origin: this.origin.toString(),
|
80 | path: this.path,
|
81 | query: query,
|
82 | querystring: querystring,
|
83 | });
|
84 | };
|
85 | Url.prototype.toString = function (options) {
|
86 | if (options === void 0) { options = {}; }
|
87 | var origin = common_1.defaultValue(options.origin, true);
|
88 | var path = "" + this.path + this.querystring;
|
89 | return origin ? "" + this.origin + path : path;
|
90 | };
|
91 | Url.isLocal = util.isLocal;
|
92 | return Url;
|
93 | }());
|
94 | exports.Url = Url;
|