The query object to encode
Encoding options
Optional Readonlymode?: "form" | "json" | "base64"The output format:
"json" (default) — Percent-encoded JSON; human-readable
but verbose; see JSON Serialization"base64" — Base64-encoded JSON; compact and URL-safe"form" — Form-encoded key=value pairs;
most compatible with standard tooling; see Form SerializationThe encoded query string, with internalized IRIs if base is provided
If query is not a valid Query, opts.mode is not a supported format,
or opts.base is not an absolute hierarchical IRI
The "form" format always encodes to canonical form:
>=price=100)name="widget")null remain unquoted (JSON literals)^price=1, ^name=-2)This ensures consistent, predictable output. The decoder accepts both canonical and shorthand forms (e.g., postfix
operators like price>=100, unquoted strings like name=widget).
encodeQuery(
{ "~name": "widget", ">=price": 50, "^price": 1, "#": 25 },
{ mode: "form" }
);
// → '~name=%22widget%22&%3E%3Dprice=50&%5Eprice=1&%23=25'
Encodes a query as a URL-safe string.
Serializes a Query object into a string representation suitable for transmission as a URL query string in GET requests. The output format can be selected based on readability, compactness, and compatibility requirements.
If
baseis provided, converts absolute IRIs (matchingisIRI(value, "absolute")) to root-relative IRIs using internalize, recursively throughout the query structure. Otherwise, performs plain serialization.