Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add UrlKeys type helper #824

Merged
merged 1 commit into from
Dec 26, 2024
Merged

feat: Add UrlKeys type helper #824

merged 1 commit into from
Dec 26, 2024

Conversation

franky47
Copy link
Member

This helps defining the values for urlKeys in a type-safe manner by tying it to a parsers object.

Usage

import { type UrlKeys } from 'nuqs' // or 'nuqs/server'

export const coordinatesSearchParams = {
  latitude: parseAsFloat.withDefault(0),
  longitude: parseAsFloat.withDefault(0),
}
export const coordinatesUrlKeys: UrlKeys<typeof coordinatesSearchParams> = {
  latitude: 'lat',
  longitude: 'lng',
}

// Later in the code:
useQueryStates(coordinatesSearchParams, {
  urlKeys: coordinatesUrlKeys
})
createSerializer(coordinatesSearchParams, {
  urlKeys: coordinatesUrlKeys
})
createSearchParamsCache(coordinatesSearchParams, {
  urlKeys: coordinatesUrlKeys
})

Note: the UrlKeys type is intentionally wider to work in useQueryStates, which does not (yet) use the ParsersMap also introduced in this PR.

[email protected] will deprecate some of the internal types exported in useQueryStates.ts, and remap them to be based on ParsersMap. This will mark the serialize and eq functions of parsers required, possibly breaking userland code, so it will be the subject of another PR.

Copy link

vercel bot commented Dec 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 26, 2024 10:15am

Copy link

pkg-pr-new bot commented Dec 26, 2024

Open in Stackblitz

npm i https://pkg.pr.new/nuqs@824

commit: 07234e3

@franky47 franky47 merged commit 479d5ea into next Dec 26, 2024
26 checks passed
@franky47 franky47 deleted the feat/urlkeys-type branch December 26, 2024 10:29
@franky47 franky47 added this to the 🚀 Shipping next milestone Dec 26, 2024
Copy link

🎉 This PR is included in version 2.3.0-beta.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented Jan 1, 2025

🎉 This PR is included in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@franky47 franky47 mentioned this pull request Jan 3, 2025
@franky47 franky47 removed this from the 🚀 Shipping next milestone Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant