|
1 | 1 | export type QueryKey = readonly unknown[] |
2 | 2 |
|
3 | 3 | export type QueryNamespace = string | readonly string[] |
| 4 | +export type NormalizeQueryKeyParamsOptions = { |
| 5 | + omitKeys?: readonly string[] |
| 6 | +} |
4 | 7 |
|
5 | 8 | const isPlainObject = (value: unknown): value is Record<string, unknown> => { |
6 | 9 | if (!value || typeof value !== "object") { |
@@ -45,6 +48,94 @@ const stableValue = (value: unknown, visited: WeakSet<object>): unknown => { |
45 | 48 | return value |
46 | 49 | } |
47 | 50 |
|
| 51 | +const normalizeValue = ( |
| 52 | + value: unknown, |
| 53 | + visited: WeakSet<object>, |
| 54 | + omitKeys: ReadonlySet<string> |
| 55 | +): unknown => { |
| 56 | + if (Array.isArray(value)) { |
| 57 | + if (visited.has(value)) { |
| 58 | + throw new Error("QueryKey contains a circular reference") |
| 59 | + } |
| 60 | + visited.add(value) |
| 61 | + const result = value |
| 62 | + .map((entry) => normalizeValue(entry, visited, omitKeys)) |
| 63 | + .filter((entry) => entry !== undefined) |
| 64 | + visited.delete(value) |
| 65 | + return result |
| 66 | + } |
| 67 | + |
| 68 | + if (isPlainObject(value)) { |
| 69 | + if (visited.has(value)) { |
| 70 | + throw new Error("QueryKey contains a circular reference") |
| 71 | + } |
| 72 | + visited.add(value) |
| 73 | + const entries = Object.entries(value).sort(([a], [b]) => |
| 74 | + a.localeCompare(b) |
| 75 | + ) |
| 76 | + const result: Record<string, unknown> = {} |
| 77 | + for (const [key, entryValue] of entries) { |
| 78 | + if (omitKeys.has(key) || entryValue === undefined) { |
| 79 | + continue |
| 80 | + } |
| 81 | + const normalizedValue = normalizeValue(entryValue, visited, omitKeys) |
| 82 | + if (normalizedValue === undefined) { |
| 83 | + continue |
| 84 | + } |
| 85 | + result[key] = normalizedValue |
| 86 | + } |
| 87 | + visited.delete(value) |
| 88 | + return result |
| 89 | + } |
| 90 | + |
| 91 | + return value |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Normalizes object-like query params before putting them into query keys. |
| 96 | + * |
| 97 | + * - Removes `undefined` values recursively |
| 98 | + * - Removes keys listed in `omitKeys` (for non-cache-affecting flags like `enabled`) |
| 99 | + * - Sorts object keys for stable hashing |
| 100 | + */ |
| 101 | +export function normalizeQueryKeyParams<TParams extends Record<string, unknown>>( |
| 102 | + params: TParams, |
| 103 | + options?: NormalizeQueryKeyParamsOptions |
| 104 | +): Record<string, unknown> { |
| 105 | + if (!isPlainObject(params)) { |
| 106 | + throw new Error( |
| 107 | + "QueryKey params must be a plain object. Use a serializer before normalizeQueryKeyParams." |
| 108 | + ) |
| 109 | + } |
| 110 | + const visited = new WeakSet<object>() |
| 111 | + const omitKeys = new Set(options?.omitKeys ?? []) |
| 112 | + const normalized = normalizeValue(params, visited, omitKeys) |
| 113 | + if (isPlainObject(normalized)) { |
| 114 | + return normalized |
| 115 | + } |
| 116 | + return {} |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Safe normalization for query-key parts used by hook factories. |
| 121 | + * |
| 122 | + * - Plain objects are normalized via `normalizeQueryKeyParams` |
| 123 | + * - `undefined` maps to `{}` for stable optional key parts |
| 124 | + * - Other values are passed through the stable serializer |
| 125 | + */ |
| 126 | +export function normalizeQueryKeyPart( |
| 127 | + value: unknown, |
| 128 | + options?: NormalizeQueryKeyParamsOptions |
| 129 | +): unknown { |
| 130 | + if (value === undefined) { |
| 131 | + return {} |
| 132 | + } |
| 133 | + if (isPlainObject(value)) { |
| 134 | + return normalizeQueryKeyParams(value, options) |
| 135 | + } |
| 136 | + return stableValue(value, new WeakSet<object>()) |
| 137 | +} |
| 138 | + |
48 | 139 | export function createQueryKey( |
49 | 140 | namespace: QueryNamespace, |
50 | 141 | ...parts: readonly unknown[] |
|
0 commit comments