bare-url
WHATWG URL implementation for JavaScript
bare-url — WHATWG URL implementation for JavaScript. It is a native addon.
Mirrors the Node.js url module.
npm i bare-urlUsage
const { URL, URLSearchParams } = require('bare-url')
const url = new URL('https://example.com/path?foo=bar#hash')
console.log(url.hostname) // 'example.com'
console.log(url.pathname) // '/path'
console.log(url.searchParams.get('foo')) // 'bar'To register URL and URLSearchParams as globals:
require('bare-url/global')API
Constructing and parsing
new URL(input: string, base?: string | URL)
Source
Parse input as a URL. If base is provided, input is resolved relative to base.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The URL string to parse. |
base? | string | URL | — | A base URL that input is resolved relative to, if provided. |
Throws
INVALID_URL—inputis not a valid URL.
URL.parse(input: string, base?: string | URL): URL | null
Source
Parse input as a URL without throwing.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The URL string to parse. |
base? | string | URL | — | A base URL that input is resolved relative to, if provided. |
Returns URL | null — A URL instance if input parses successfully, or null on failure.
URL.canParse(input: string, base?: string | URL): boolean
Source
Return true if input can be parsed as a valid URL, optionally relative to base.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The URL string to test. |
base? | string | URL | — | A base URL that input is resolved relative to, if provided. |
Components
href: string
Source
The full serialized URL string. Setting this property reparses the URL.
protocol: string
Source
The URL scheme followed by ':', e.g. 'https:'.
username: string
Source
The username portion of the URL, or an empty string.
password: string
Source
The password portion of the URL, or an empty string.
host: string
Source
The hostname and port, e.g. 'example.com:8080'.
hostname: string
Source
The hostname without the port.
port: string
Source
The port as a string, or an empty string if not present.
pathname: string
Source
The path portion of the URL.
search: string
Source
The query string including the leading '?', or an empty string.
searchParams: URLSearchParams
Source
A URLSearchParams object for the query string. Mutations to the params are reflected in the URL.
hash: string
Source
The fragment including the leading '#', or an empty string.
Converting to string
URL.toString(): string
Source
Returns the serialized string form.
URL.toJSON(): string
Source
Returns the serialized string form. Suitable for JSON serialization.
File URL conversion
URL.fileURLToPath(url: URL | string): string
Source
Convert a file: URL to a platform-specific file path. url may be a URL instance or a string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | string | — | The file: URL to convert, as a URL instance or a string. |
Throws
INVALID_URL_SCHEME— the URL does not use thefile:protocol.INVALID_FILE_URL_HOST— (non-Windows) the URL has a host other than empty or'localhost'.INVALID_FILE_URL_PATH— the URL path contains an encoded path-separator or NUL character, or, on Windows, is not an absolute drive path.
URL.pathToFileURL(pathname: string): URL
Source
Convert a platform-specific file path to a file: URL.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pathname | string | — | The platform-specific file path to convert. |
Type checks
URL.isURL(value: unknown): value is URL
Source
Return true if value is a URL instance.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | unknown | — | The value to test. |
URL.isURLSearchParams(value: unknown): value is URLSearchParams
Source
Return true if value is a URLSearchParams instance.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | unknown | — | The value to test. |
Constructing search params
new URLSearchParams(init: string | Record<string, string> | Iterable<[string, string]>)
Source
Create a new URLSearchParams instance. init may be a query string, an iterable of [name, value] pairs, or an object of key-value pairs.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
init | string | Record<string, string> | Iterable<[string, string]> | — | A query string, an iterable of [name, value] pairs, or an object of key-value pairs to initialize the params from. |
Reading and writing parameters
get(name: string): string | undefined
Source
Return the first value for name, or null if not present.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name to look up. |
getAll(name: string): string[]
Source
Return all values for name as an array.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name to look up. |
has(name: string, value?: string): boolean
Source
Return true if a pair with name exists. If value is provided, the pair must also match value.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name to check. |
value? | string | — | If provided, the pair must also match this value. |
append(name: string, value: string): void
Source
Append a new name/value pair.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name. |
value | string | — | The parameter value. |
set(name: string, value: string): void
Source
Set the value for name, replacing any existing pairs with that name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name. |
value | string | — | The value to set. |
delete(name: string, value?: string): void
Source
Remove all pairs with name. If value is provided, only pairs with both the matching name and value are removed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The parameter name to remove. |
value? | string | — | If provided, only pairs also matching this value are removed. |
size: number
Source
The total number of search parameters.
Serializing search params
URLSearchParams.toString(): string
Source
Returns the serialized string form.
URLSearchParams.toJSON(): string
Source
Returns the serialized string form. Suitable for JSON serialization.
Search params type check
URLSearchParams.isURLSearchParams(value: unknown): value is URLSearchParams
Source
Return true if value is a URLSearchParams instance.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | unknown | — | The value to test. |
Errors
URLError
Source
class URLError {
code: string
input: string
}bare-url/global
Types
URLConstructor
type URLConstructor = typeof url.URLURLSearchParamsConstructor
type URLSearchParamsConstructor = typeof url.URLSearchParamsSee also
bare-path— provides the platform-specific path handling used byfileURLToPath/pathToFileURL.- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.