LogoPear Docs
ReferencesBareModules

bare-url

WHATWG URL implementation for JavaScript

stable

bare-url — WHATWG URL implementation for JavaScript. It is a native addon.

Mirrors the Node.js url module.

npm i bare-url

Usage

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

ParameterTypeDefaultDescription
inputstringThe URL string to parse.
base?string | URLA base URL that input is resolved relative to, if provided.

Throws

  • INVALID_URLinput is not a valid URL.

URL.parse(input: string, base?: string | URL): URL | null

Source

Parse input as a URL without throwing.

Parameters

ParameterTypeDefaultDescription
inputstringThe URL string to parse.
base?string | URLA 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

ParameterTypeDefaultDescription
inputstringThe URL string to test.
base?string | URLA 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

ParameterTypeDefaultDescription
urlURL | stringThe file: URL to convert, as a URL instance or a string.

Throws

  • INVALID_URL_SCHEME — the URL does not use the file: 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

ParameterTypeDefaultDescription
pathnamestringThe 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

ParameterTypeDefaultDescription
valueunknownThe value to test.

URL.isURLSearchParams(value: unknown): value is URLSearchParams

Source

Return true if value is a URLSearchParams instance.

Parameters

ParameterTypeDefaultDescription
valueunknownThe 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

ParameterTypeDefaultDescription
initstring | 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

ParameterTypeDefaultDescription
namestringThe parameter name to look up.

getAll(name: string): string[]

Source

Return all values for name as an array.

Parameters

ParameterTypeDefaultDescription
namestringThe 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

ParameterTypeDefaultDescription
namestringThe parameter name to check.
value?stringIf provided, the pair must also match this value.

append(name: string, value: string): void

Source

Append a new name/value pair.

Parameters

ParameterTypeDefaultDescription
namestringThe parameter name.
valuestringThe parameter value.

set(name: string, value: string): void

Source

Set the value for name, replacing any existing pairs with that name.

Parameters

ParameterTypeDefaultDescription
namestringThe parameter name.
valuestringThe 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

ParameterTypeDefaultDescription
namestringThe parameter name to remove.
value?stringIf 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

ParameterTypeDefaultDescription
valueunknownThe value to test.

Errors

URLError

Source
class URLError {
  code: string
  input: string
}

bare-url/global

Types

URLConstructor

type URLConstructor = typeof url.URL
Source

URLSearchParamsConstructor

type URLSearchParamsConstructor = typeof url.URLSearchParams
Source

See also

  • bare-path — provides the platform-specific path handling used by fileURLToPath/pathToFileURL.
  • Bare modules — the full bare-* catalog.
  • Bare runtime API — the runtime these modules extend.

On this page