Open History to see the last commit
1import { decode, encode } from './qss'
2import type { AnySchema } from './validators'
3β
4/** Default `parseSearch` that strips leading '?' and JSON-parses values. */
5/** Default `parseSearch` that strips leading '?' and JSON-parses values. */
6/** Default `parseSearch` that strips leading '?' and JSON-parses values. */
7export const defaultParseSearch = parseSearchWith(JSON.parse)
8/** Default `stringifySearch` using JSON.stringify for complex values. */
9export const defaultStringifySearch = stringifySearchWith(
10 JSON.stringify,
11 JSON.parse,
12)
13β
14/**
15 * Build a `parseSearch` function using a provided JSON-like parser.
16 *
17 * The returned function strips a leading `?`, decodes values, and attempts to
18 * JSON-parse string values using the given `parser`.
19 *
20 * @param parser Function to parse a string value (e.g. `JSON.parse`).
21 * @returns A `parseSearch` function compatible with `Router` options.
22 * @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
23 */
24/** Build a parseSearch function using a provided JSON-like parser. */
25export function parseSearchWith(parser: (str: string) => any) {
26 return (searchStr: string): AnySchema => {
27 if (searchStr[0] === '?') {
28 searchStr = searchStr.substring(1)
29 }
30β
31 const query: Record<string, unknown> = decode(searchStr)
32β
33 // Try to parse any query params that might be json
34 for (const key in query) {
35 const value = query[key]
36 if (typeof value === 'string') {
37 try {
38 query[key] = parser(value)
39 } catch (_err) {
40 // silent
41 }
42 }
43 }
44β
45 return query
46 }
47}
48β
49/**
50 * Build a `stringifySearch` function using a provided serializer.
51 *
52 * Non-primitive values are serialized with `stringify`. If a `parser` is
53 * supplied, string values that are parseable are re-serialized to ensure
54 * symmetry with `parseSearch`.
55 *
56 * @param stringify Function to serialize a value (e.g. `JSON.stringify`).
57 * @param parser Optional parser to detect parseable strings.
58 * @returns A `stringifySearch` function compatible with `Router` options.
59 * @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
60 */
61/** Build a stringifySearch function using a provided serializer/parser. */
62export function stringifySearchWith(
63 stringify: (search: any) => string,
64 parser?: (str: string) => any,
65) {
66 const hasParser = typeof parser === 'function'
67 function stringifyValue(val: any) {
68 if (typeof val === 'object' && val !== null) {
69 try {
70 return stringify(val)
71 } catch (_err) {
72 // silent
73 }
74 } else if (hasParser && typeof val === 'string') {
75 try {
76 // Check if it's a valid parseable string.
77 // If it is, then stringify it again.
78 parser(val)
79 return stringify(val)
80 } catch (_err) {
81 // silent
82 }
83 }
84 return val
85 }
86β
87 return (search: Record<string, any>) => {
88 const searchStr = encode(search, stringifyValue)
89 return searchStr ? `?${searchStr}` : ''
90 }
91}
92β
93export type SearchSerializer = (searchObj: Record<string, any>) => string
94export type SearchParser = (searchStr: string) => Record<string, any>