Skip to content

Commit

Permalink
ref: Use loader in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Dec 27, 2024
1 parent 0c272cd commit 41c63b6
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/nuqs/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { cache } from 'react'
import type { SearchParams, UrlKeys } from './defs'
import { error } from './errors'
import { createLoader } from './loader'
import type { inferParserType, ParserMap } from './parsers'

const $input: unique symbol = Symbol('Input')
Expand All @@ -10,6 +11,7 @@ export function createSearchParamsCache<Parsers extends ParserMap>(
parsers: Parsers,
{ urlKeys = {} }: { urlKeys?: UrlKeys<Parsers> } = {}
) {
const load = createLoader(parsers, { urlKeys })
type Keys = keyof Parsers
type ParsedSearchParams = {
readonly [K in Keys]: inferParserType<Parsers[K]>
Expand Down Expand Up @@ -43,11 +45,7 @@ export function createSearchParamsCache<Parsers extends ParserMap>(
// Different inputs in the same request - fail
throw new Error(error(501))
}
for (const key in parsers) {
const parser = parsers[key]!
const urlKey = urlKeys[key] ?? key
c.searchParams[key] = parser.parseServerSide(searchParams[urlKey])
}
c.searchParams = load(searchParams)
c[$input] = searchParams
return Object.freeze(c.searchParams) as ParsedSearchParams
}
Expand Down

0 comments on commit 41c63b6

Please sign in to comment.