import { TransitionStartFunction } from "react"; import { StandardSchemaV1 } from "@standard-schema/spec"; //#region src/defs.d.ts type SearchParams = Record; type HistoryOptions = "replace" | "push"; type LimitUrlUpdates = { method: "debounce"; timeMs: number; } | { method: "throttle"; timeMs: number; }; type Options = { /** * How the query update affects page history * * `push` will create a new history entry, allowing to use the back/forward * buttons to navigate state updates. * `replace` (default) will keep the current history point and only replace * the query string. */ history?: HistoryOptions; /** * Scroll to top after a query state update * * Defaults to `false`, unlike the Next.js router page navigation methods. */ scroll?: boolean; /** * Shallow mode (true by default) keeps query states update client-side only, * meaning there won't be calls to the server. * * Setting it to `false` will trigger a network request to the server with * the updated querystring. */ shallow?: boolean; /** * Maximum amount of time (ms) to wait between updates of the URL query string. * * This is to alleviate rate-limiting of the Web History API in browsers, * and defaults to 50ms. Safari requires a higher value of around 120ms. * * Note: the value will be limited to a minimum of 50ms, anything lower * will not have any effect. * * @deprecated use `limitUrlUpdates: { 'method': 'throttle', timeMs: number }` * or use the shorthand: * ```ts * import { throttle } from 'nuqs' * * limitUrlUpdates: throttle(100) // time in ms * ``` */ throttleMs?: number; /** * Limit the rate of URL updates to prevent spamming the browser history, * and the server if `shallow: false`. * * This is to alleviate rate-limiting of the Web History API in browsers, * and defaults to 50ms. Safari requires a higher value of around 120ms. * * Note: the value will be limited to a minimum of 50ms, anything lower * will not have any effect. * * If both `throttleMs` and `limitUrlUpdates` are set, `limitUrlUpdates` will * take precedence. */ limitUrlUpdates?: LimitUrlUpdates; /** * In RSC frameworks, opt-in to observing Server Component loading states when * doing non-shallow updates by passing a `startTransition` from the * `React.useTransition()` hook. * * In other frameworks, navigation events triggered by a query update can also * be wrapped in a transition this way (e.g. `React.startTransition`). */ startTransition?: TransitionStartFunction; /** * Clear the key-value pair from the URL query string when setting the state * to the default value. * * Defaults to `true` to keep URLs clean. * * Set it to `false` to keep backwards-compatiblity when the default value * changes (prefer explicit URLs whose meaning don't change). */ clearOnDefault?: boolean; }; type Nullable = { [K in keyof T]: T[K] | null } & {}; /** * Helper type to define and reuse urlKey options to rename search params keys * * Usage: * ```ts * import { type UrlKeys } from 'nuqs' // or 'nuqs/server' * * export const coordinatesSearchParams = { * latitude: parseAsFloat.withDefault(0), * longitude: parseAsFloat.withDefault(0), * } * export const coordinatesUrlKeys: UrlKeys = { * latitude: 'lat', * longitude: 'lng', * } * * // Later in the code: * useQueryStates(coordinatesSearchParams, { * urlKeys: coordinatesUrlKeys * }) * createSerializer(coordinatesSearchParams, { * urlKeys: coordinatesUrlKeys * }) * createSearchParamsCache(coordinatesSearchParams, { * urlKeys: coordinatesUrlKeys * }) * ``` */ type UrlKeys> = Partial>; //#endregion //#region src/parsers.d.ts type Require = Pick, Keys> & Omit; type SingleParser = { type?: "single"; /** * Convert a query string value into a state value. * * If the string value does not represent a valid state value, * the parser should return `null`. Throwing an error is also supported. */ parse: (value: string) => T | null; /** * Render the state value into a query string value. */ serialize?: (value: T) => string; /** * Check if two state values are equal. * * This is used when using the `clearOnDefault` value, to compare the default * value with the set value. * * It makes sense to provide this function when the state value is an object * or an array, as the default referential equality check will not work. */ eq?: (a: T, b: T) => boolean; }; type MultiParser = { type: "multi"; parse: (value: ReadonlyArray) => T | null; serialize?: (value: T) => Array; eq?: (a: T, b: T) => boolean; }; type GenericParser = SingleParser | MultiParser; type GenericParserBuilder = SingleParserBuilder | MultiParserBuilder; /** @deprecated use SingleParser instead */ type Parser = SingleParser; /** @deprecated use SingleParserBuilder instead */ type ParserBuilder = SingleParserBuilder; type SingleParserBuilder = Required> & Options & { /** * Set history type, shallow routing and scroll restoration options * at the hook declaration level. * * Note that you can override those options in individual calls to the * state updater function. */ withOptions(this: This, options: Options): This; /** * Specifying a default value makes the hook state non-nullable when the * query is missing from the URL: the default value is returned instead * of `null`. * * Setting the state to the default value¹ will clear the query string key * from the URL, unless `clearOnDefault` is set to `false`. * * Setting the state to `null` will always clear the query string key * from the URL, and return the default value. * * ¹: Equality is checked with the parser's `eq` function, or referential * equality if not provided. * * @param defaultValue */ withDefault(this: SingleParserBuilder, defaultValue: NonNullable): Omit, "parseServerSide"> & { readonly defaultValue: NonNullable; /** * Use the parser in Server Components * * `parse` is intended to be used only by the hook, but you can use this * method to hydrate query values on server-side rendered pages. * See the `server-side-parsing` demo for an example. * * Note that when multiple queries are presented to the parser * (eg: `/?a=1&a=2`), only the **first** will be parsed, to mimic the * behaviour of URLSearchParams: * https://url.spec.whatwg.org/#dom-urlsearchparams-get * * @param value as coming from page props * * @deprecated prefer using loaders instead, as they enforce a strong * bond between the data type and the search param key. */ parseServerSide(value: string | string[] | undefined): NonNullable; }; /** * Use the parser in Server Components * * `parse` is intended to be used only by the hook, but you can use this * method to hydrate query values on server-side rendered pages. * See the `server-side-parsing` demo for an example. * * Note that when multiple queries are presented to the parser * (eg: `/?a=1&a=2`), only the **first** will be parsed, to mimic the * behaviour of URLSearchParams: * https://url.spec.whatwg.org/#dom-urlsearchparams-get * * @param value as coming from page props * * @deprecated prefer using loaders instead, as they enforce a strong * bond between the data type and the search param key. */ parseServerSide(value: string | string[] | undefined): T | null; }; type MultiParserBuilder = Required> & Options & { withOptions(this: This, options: Options): This; withDefault(this: MultiParserBuilder, defaultValue: NonNullable): Omit, "parseServerSide"> & { readonly defaultValue: NonNullable; /** * @deprecated exposed for symmetry with SingleParserBuilder only, * prefer using loaders instead. */ parseServerSide(value: string | string[] | undefined): NonNullable; }; /** * @deprecated exposed for symmetry with SingleParserBuilder only, * prefer using loaders instead. */ parseServerSide(value: string | string[] | undefined): T | null; }; /** * Wrap a set of parse/serialize functions into a builder pattern parser * you can pass to one of the hooks, making its default value type safe. */ declare function createParser(parser: Require, "parse" | "serialize">): SingleParserBuilder; declare function createMultiParser(parser: Omit, "parse" | "serialize">, "type">): MultiParserBuilder; declare const parseAsString: SingleParserBuilder; declare const parseAsInteger: SingleParserBuilder; declare const parseAsIndex: SingleParserBuilder; declare const parseAsHex: SingleParserBuilder; declare const parseAsFloat: SingleParserBuilder; declare const parseAsBoolean: SingleParserBuilder; /** * Querystring encoded as the number of milliseconds since epoch, * and returned as a Date object. */ declare const parseAsTimestamp: SingleParserBuilder; /** * Querystring encoded as an ISO-8601 string (UTC), * and returned as a Date object. */ declare const parseAsIsoDateTime: SingleParserBuilder; /** * Querystring encoded as an ISO-8601 string (UTC) * without the time zone offset, and returned as * a Date object. * * The Date is parsed without the time zone offset, * making it at 00:00:00 UTC. */ declare const parseAsIsoDate: SingleParserBuilder; /** * String-based enums provide better type-safety for known sets of values. * You will need to pass the parseAsStringEnum function a list of your enum values * in order to validate the query string. Anything else will return `null`, * or your default value if specified. * * Example: * ```ts * enum Direction { * up = 'UP', * down = 'DOWN', * left = 'LEFT', * right = 'RIGHT' * } * * const [direction, setDirection] = useQueryState( * 'direction', * parseAsStringEnum(Object.values(Direction)) // pass a list of allowed values * .withDefault(Direction.up) * ) * ``` * * Note: the query string value will be the value of the enum, not its name * (example above: `direction=UP`). * * @param validValues The values you want to accept */ declare function parseAsStringEnum(validValues: Enum[]): SingleParserBuilder; /** * String-based literals provide better type-safety for known sets of values. * You will need to pass the parseAsStringLiteral function a list of your string values * in order to validate the query string. Anything else will return `null`, * or your default value if specified. * * Example: * ```ts * const colors = ["red", "green", "blue"] as const * * const [color, setColor] = useQueryState( * 'color', * parseAsStringLiteral(colors) // pass a readonly list of allowed values * .withDefault("red") * ) * ``` * * @param validValues The values you want to accept */ declare function parseAsStringLiteral(validValues: readonly Literal[]): SingleParserBuilder; /** * Number-based literals provide better type-safety for known sets of values. * You will need to pass the parseAsNumberLiteral function a list of your number values * in order to validate the query string. Anything else will return `null`, * or your default value if specified. * * Example: * ```ts * const diceSides = [1, 2, 3, 4, 5, 6] as const * * const [side, setSide] = useQueryState( * 'side', * parseAsNumberLiteral(diceSides) // pass a readonly list of allowed values * .withDefault(4) * ) * ``` * * @param validValues The values you want to accept */ declare function parseAsNumberLiteral(validValues: readonly Literal[]): SingleParserBuilder; /** * Encode any object shape into the querystring value as JSON. * Note: you may want to use `useQueryStates` for finer control over * multiple related query keys. * * @param runtimeParser Runtime parser (eg: Zod schema or Standard Schema) to validate after JSON.parse */ declare function parseAsJson(validator: ((value: unknown) => T | null) | StandardSchemaV1): SingleParserBuilder; /** * A comma-separated list of items. * Items are URI-encoded for safety, so they may not look nice in the URL. * * @param itemParser Parser for each individual item in the array * @param separator The character to use to separate items (default ',') */ declare function parseAsArrayOf(itemParser: SingleParser, separator?: string): SingleParserBuilder; declare function parseAsNativeArrayOf(itemParser: SingleParser): ReturnType["withDefault"]>; type inferSingleParserType = Parser$1 extends GenericParserBuilder & { defaultValue: infer Value; } ? Value : Parser$1 extends GenericParserBuilder ? Value | null : never; type inferParserRecordType>> = { [Key in keyof Map]: inferSingleParserType } & {}; /** * Type helper to extract the underlying returned data type of a parser * or of an object describing multiple parsers and their associated keys. * * Usage: * * ```ts * import { type inferParserType } from 'nuqs' // or 'nuqs/server' * * const intNullable = parseAsInteger * const intNonNull = parseAsInteger.withDefault(0) * * inferParserType // number | null * inferParserType // number * * const parsers = { * a: parseAsInteger, * b: parseAsBoolean.withDefault(false) * } * * inferParserType * // { a: number | null, b: boolean } * ``` */ type inferParserType = Input extends GenericParserBuilder ? inferSingleParserType : Input extends Record> ? inferParserRecordType : never; type ParserWithOptionalDefault = GenericParserBuilder & { defaultValue?: T; }; type ParserMap = Record>; //#endregion export { LimitUrlUpdates as A, parseAsNativeArrayOf as C, parseAsStringLiteral as D, parseAsStringEnum as E, Options as M, SearchParams as N, parseAsTimestamp as O, UrlKeys as P, parseAsJson as S, parseAsString as T, parseAsHex as _, Parser as a, parseAsIsoDate as b, ParserWithOptionalDefault as c, createMultiParser as d, createParser as f, parseAsFloat as g, parseAsBoolean as h, MultiParserBuilder as i, Nullable as j, HistoryOptions as k, SingleParser as l, parseAsArrayOf as m, GenericParserBuilder as n, ParserBuilder as o, inferParserType as p, MultiParser as r, ParserMap as s, GenericParser as t, SingleParserBuilder as u, parseAsIndex as v, parseAsNumberLiteral as w, parseAsIsoDateTime as x, parseAsInteger as y }; //# sourceMappingURL=parsers-U3P6hK0x.d.ts.map