Skip to main content

Import

import { UrlHelper } from "bytekit/url-helper";

What it does

UrlHelper is a collection of static methods for working with URLs: parsing, building, joining, normalizing, encoding, and query-string manipulation. All methods are pure functions with no side effects.

Methods

Parsing and building

MethodSignatureDescription
parseparse(url: string): ParsedUrlDecompose a URL into its components.
buildbuild(parts: ParsedUrl): stringBuild a URL string from parts.
resolveresolve(base: string, relative: string): stringResolve a relative URL against a base.
joinjoin(...parts: string[]): stringJoin path segments into a single URL path.

Query-string helpers

MethodSignatureDescription
getQueryParamsgetQueryParams(url: string): Record<string, string>Extract query parameters as a key-value map.
setQueryParamssetQueryParams(url: string, params: Record<string, string>): stringReplace all query parameters on a URL.
addQueryParamaddQueryParam(url: string, key: string, value: string): stringAppend a single query parameter.
removeQueryParamremoveQueryParam(url: string, key: string): stringRemove a query parameter by key.
stringifystringify(params: Record<string, string>): stringSerialize a params object to a query string.

Validation and normalization

MethodSignatureDescription
isAbsoluteisAbsolute(url: string): booleanCheck whether a URL is absolute.
isValidisValid(url: string): booleanCheck whether a string is a valid URL.
normalizenormalize(url: string): stringNormalize a URL (trailing slashes, encoding).

Encoding

MethodSignatureDescription
encodeencode(str: string): stringPercent-encode a string for use in a URL.
decodedecode(str: string): stringDecode a percent-encoded string.

Path extraction

MethodSignatureDescription
getExtensiongetExtension(url: string): stringExtract the file extension from a URL path.
getFilenamegetFilename(url: string): stringExtract the filename from a URL path.

Text utilities

MethodSignatureDescription
slugifyslugify(text: string): stringConvert text into a URL-safe slug.

Examples

Parse and rebuild

import { UrlHelper } from "bytekit/url-helper";

const parts = UrlHelper.parse("https://example.com/api/users?page=2");
// { protocol: 'https', host: 'example.com', path: '/api/users', query: 'page=2', ... }

const url = UrlHelper.build(parts);
// "https://example.com/api/users?page=2"

Query-string manipulation

const url = "https://example.com/search?q=hello";

const withLimit = UrlHelper.addQueryParam(url, "limit", "10");
// "https://example.com/search?q=hello&limit=10"

const params = UrlHelper.getQueryParams(withLimit);
// { q: "hello", limit: "10" }

const clean = UrlHelper.removeQueryParam(withLimit, "limit");
// "https://example.com/search?q=hello"

Join and resolve

const endpoint = UrlHelper.join("https://api.example.com", "v2", "users");
// "https://api.example.com/v2/users"

const full = UrlHelper.resolve("https://example.com/docs/", "../api");
// "https://example.com/api"

Slugify

const slug = UrlHelper.slugify("Hello World — Special Characters!");
// "hello-world-special-characters"

Validation

UrlHelper.isValid("https://example.com"); // true
UrlHelper.isValid("not a url");           // false
UrlHelper.isAbsolute("/relative/path");   // false
All methods are static — there is no need to instantiate UrlHelper.