createSerializer
function createSerializer<M>(parsers, options?): { (values): string; (base, values?): string;};Defined in: tanstack/core/serializer.ts:58
Build a query string or URL from a parser map. Preserves unrelated params on
the base, removes keys set to null, and honors clearOnDefault/urlKeys.
Call it with just values for a query string ("?…", or "" when empty), or
with a base (path string, URL or URLSearchParams) first to merge into
it. A string base keeps its path and #hash; a URL base returns a full href.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
M extends ParserMap |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
parsers |
M |
options? |
CreateSerializerOptions<M> |
Returns
Section titled “Returns”{
(values): string;
(base, values?): string;
}
Example
Section titled “Example”import { createSerializer, parseAsIndex, parseAsString } from "@xtandard/lib/tanstack/server";
const serialize = createSerializer({ q: parseAsString, page: parseAsIndex.withDefault(0),});
serialize({ q: "x", page: 2 }); // → "?q=x&page=3"serialize({ page: 0 }); // → "" (default cleared)serialize("/users?tab=all#top", { q: "x" }); // → "/users?tab=all&q=x#top"serialize("/users?q=old&tab=all", { q: null }); // → "/users?tab=all"serialize(new URL("https://h/u?a=1"), {}); // → "https://h/u?a=1"