yurl
Version:
A URL manipulation library that offers support for daisy chaining, pathname resolution and query args manipulation.
166 lines (165 loc) • 4.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YURL = void 0;
const URL_1 = require("./URL");
const utils_1 = require("./utils");
const resolvePathname_1 = require("./resolvePathname");
class YURL {
parts;
constructor(str, base) {
this.parts = str instanceof YURL
? this.parts = new URL_1.URL(str.parts)
: new URL_1.URL(str, base instanceof YURL ? base.parts : base);
}
get(key) {
switch (key) {
case 'path':
return `${this.parts.pathname}${this.parts.search}`;
case 'query':
case 'searchParams':
return (0, utils_1.searchParamsToObj)(this.parts.searchParams);
case 'hash':
case 'host':
case 'hostname':
case 'href':
case 'origin':
case 'password':
case 'pathname':
case 'port':
case 'protocol':
case 'search':
case 'username':
return this.parts[key];
default:
throw new Error(`Invalid URL part: ${key}`);
}
}
clone() {
return new YURL(this);
}
toString() {
return this.parts.toString();
}
toJSON() {
return this.parts.toJSON();
}
format() {
return this.parts.toString();
}
static parse(str, base) {
return new YURL(str, base);
}
host(host) {
this.parts.host = host;
return this;
}
hostname(hostname) {
this.parts.hostname = (0, utils_1.nilToEmptyString)(hostname);
return this;
}
port(port) {
this.parts.port = (0, utils_1.nilToEmptyString)(port) + '';
return this;
}
pathname(to) {
if (!to) {
this.parts.pathname = '/';
}
else {
this.parts.pathname = (0, resolvePathname_1.resolvePathname)(to, this.parts.pathname || '/');
}
return this;
}
path(path) {
const match = path.match(/((?:\/)[^?]*)(?:\?([^#]*))?(?:\#(.*))?/);
if (!match) {
throw new Error('Invalid pathname');
}
return this
.pathname(match[1])
.search(match[2]);
}
search(search) {
if (search) {
if (search.charAt(0) !== '?') {
this.parts.search = `?${search}`;
}
else {
this.parts.search = search;
}
}
else {
this.parts.search = '';
}
return this;
}
hash(hash) {
if (hash) {
if (hash.charAt(0) !== '#') {
this.parts.hash = `#${hash}`;
}
else {
this.parts.hash = hash;
}
}
else {
this.parts.hash = '';
}
return this;
}
auth(auth) {
return this
.password(null)
.username(auth);
}
username(username) {
this.parts.username = (0, utils_1.nilToEmptyString)(username);
return this;
}
password(password) {
this.parts.password = (0, utils_1.nilToEmptyString)(password);
return this;
}
searchParam(key, value) {
if (value === null || value === undefined || value === '') {
this.parts.searchParams.delete(key);
}
else {
if (Array.isArray(value)) {
for (let i = 0, l = value.length; i < l; i += 1) {
this.parts.searchParams.append(key, value[i]);
}
}
else {
this.parts.searchParams.append(key, value);
}
}
}
searchParams(paramsOrKey, value) {
if (paramsOrKey) {
if (typeof (paramsOrKey) === 'string') {
this.searchParam(paramsOrKey, value);
}
if (typeof paramsOrKey === 'object') {
for (const key in paramsOrKey) {
this.searchParam(key, paramsOrKey[key]);
}
}
}
else {
const { searchParams } = this.parts;
for (const key of Array.from(searchParams.keys())) {
searchParams.delete(key);
}
}
return this;
}
query(paramsOrKey, value) {
return this.searchParams(paramsOrKey, value);
}
protocol(protocol) {
this.parts.protocol = (0, utils_1.nilToEmptyString)(protocol);
return this;
}
}
exports.YURL = YURL;