s5-js/src/utils/url.ts

98 lines
2.4 KiB
TypeScript

import urljoin from "url-join";
import parse from "url-parse";
import { trimSuffix } from "./string.js";
import { throwValidationError } from "./validation.js";
export const URI_S5_PREFIX = "s5://";
/**
* Adds a subdomain to the given URL.
*
* @param url - The URL.
* @param subdomain - The subdomain to add.
* @returns - The final URL.
*/
export function addUrlSubdomain(url: string, subdomain: string): string {
const urlObj = new URL(url);
urlObj.hostname = `${subdomain}.${urlObj.hostname}`;
const str = urlObj.toString();
return trimSuffix(str, "/");
}
/**
* Adds a query to the given URL.
*
* @param url - The URL.
* @param query - The query parameters.
* @returns - The final URL.
*/
export function addUrlQuery(
url: string,
query: { [key: string]: string | undefined },
): string {
const parsed = parse(url, true);
// Combine the desired query params with the already existing ones.
query = { ...parsed.query, ...query };
parsed.set("query", query);
return parsed.toString();
}
/**
* Prepends the prefix to the given string only if the string does not already start with the prefix.
*
* @param str - The string.
* @param prefix - The prefix.
* @returns - The prefixed string.
*/
export function ensurePrefix(str: string, prefix: string): string {
if (!str.startsWith(prefix)) {
str = `${prefix}${str}`;
}
return str;
}
/**
* Ensures that the given string is a URL.
*
* @param url - The given string.
* @returns - The URL.
*/
export function ensureUrl(url: string): string {
if (url.startsWith("http://")) {
return url;
}
return ensurePrefix(url, "https://");
}
/**
* Ensures that the given string is a URL with a protocol prefix.
*
* @param url - The given string.
* @returns - The URL.
*/
export function ensureUrlPrefix(url: string): string {
if (url === "localhost") {
return "http://localhost/";
}
if (!/^https?:(\/\/)?/i.test(url)) {
return `https://${url}`;
}
return url;
}
/**
* Properly joins paths together to create a URL. Takes a variable number of
* arguments.
*
* @param args - Array of URL parts to join.
* @returns - Final URL constructed from the input parts.
*/
export function makeUrl(...args: string[]): string {
if (args.length === 0) {
throwValidationError("args", args, "parameter", "non-empty");
}
return ensureUrl(args.reduce((acc, cur) => urljoin(acc, cur)));
}