@sentrie/url
The @sentrie/url module provides URL parsing and manipulation utilities. Note: URL encoding/decoding is provided by the @sentrie/encoding module.
use { parse, join, getHost, getPath, getQuery, isValid } from @sentrie/urlParsedURL
Section titled “ParsedURL”Represents a parsed URL with its components.
interface ParsedURL { scheme: string; // URL scheme (e.g., "http", "https", "ftp") host: string; // URL host (e.g., "example.com:8080") path: string; // URL path (e.g., "/path/to/resource") query: string; // URL query string without the leading "?" (e.g., "key=value&foo=bar") fragment: string; // URL fragment without the leading "#" (e.g., "section1") user: string; // URL user info (e.g., "user:password")}Functions
Section titled “Functions”parse(urlStr: string): ParsedURL
Section titled “parse(urlStr: string): ParsedURL”Parses a URL string into its components.
Parameters:
urlStr- The URL string to parse
Returns: A ParsedURL object containing the URL components
Throws: Error if the URL string is invalid
Example:
use { parse } from @sentrie/urllet parsed = url.parse("https://example.com:8080/path/to/resource?key=value#section")// {// scheme: "https",// host: "example.com:8080",// path: "/path/to/resource",// query: "key=value",// fragment: "section",// user: ""// }join(...parts: string[]): string
Section titled “join(...parts: string[]): string”Joins multiple URL parts into a single URL. Resolves relative paths against the base URL.
Parameters:
parts- Variable number of URL part strings to join
Returns: The joined URL string
Throws: Error if any part is invalid
Example:
use { join } from @sentrie/urllet joined = url.join("https://example.com", "path", "to", "resource") // "https://example.com/path/to/resource"getHost(url: string): string
Section titled “getHost(url: string): string”Extracts the host component from a URL.
Parameters:
url- The URL string (can be full URL or just host)
Returns: The host component of the URL
Throws: Error if the URL is invalid
Example:
use { getHost } from @sentrie/urllet host = url.getHost("https://example.com:8080/path") // "example.com:8080"getPath(url: string): string
Section titled “getPath(url: string): string”Extracts the path component from a URL.
Parameters:
url- The URL string
Returns: The path component of the URL (e.g., "/path/to/resource")
Throws: Error if the URL is invalid
Example:
use { getPath } from @sentrie/urllet path = url.getPath("https://example.com/path/to/resource") // "/path/to/resource"getQuery(url: string): string
Section titled “getQuery(url: string): string”Extracts the query string component from a URL.
Parameters:
url- The URL string
Returns: The query string without the leading "?" (e.g., "key=value")
Throws: Error if the URL is invalid
Example:
use { getQuery } from @sentrie/urllet query = url.getQuery("https://example.com/path?key=value&foo=bar") // "key=value&foo=bar"isValid(urlStr: string): boolean
Section titled “isValid(urlStr: string): boolean”Validates whether a string is a valid URL. Basic validation checks for scheme and host presence (or leading "/" for relative URLs).
Parameters:
urlStr- The URL string to validate
Returns: true if the URL appears valid, false otherwise
Example:
use { isValid } from @sentrie/urllet valid = url.isValid("https://example.com") // truelet invalid = url.isValid("not-a-url") // falseComplete Example
Section titled “Complete Example”namespace com/example/validation
policy mypolicy { use { parse, getHost, getPath, isValid } from @sentrie/url fact requestUrl!: string
rule validateUrl = default false { let isValid = url.isValid(requestUrl) yield isValid }
rule checkAllowedHost = default false { let host = url.getHost(requestUrl) let allowedHosts = ["example.com", "api.example.com"] yield host in allowedHosts }
rule checkPath = default false { let path = url.getPath(requestUrl) let allowedPaths = ["/api", "/public"] yield url.startsWith(path, "/api") or url.startsWith(path, "/public") }
export decision of validateUrl export decision of checkAllowedHost export decision of checkPath}See Also
Section titled “See Also”- @sentrie/encoding - For URL encoding/decoding operations
- URL encoding/decoding (percent encoding) is provided by the
@sentrie/encodingmodule - Relative URLs (starting with
"/") are supported - The
parsefunction provides comprehensive URL parsing with all components - The
joinfunction properly resolves relative paths against base URLs