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: Allow the serialize function in parsers to remove their key from the URL #770

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/nuqs/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export type Parser<T> = {

/**
* Render the state value into a query string value.
*
* Return `null` to remove the query parameter from the URL.
*/
serialize?: (value: T) => string
serialize?: (value: T) => string | null

/**
* Check if two state values are equal.
Expand Down Expand Up @@ -94,7 +96,7 @@ export type ParserBuilder<T> = Required<Parser<T>> &
* you can pass to one of the hooks, making its default value type safe.
*/
export function createParser<T>(
parser: Require<Parser<T>, 'parse' | 'serialize'>
parser: Require<Parser<T>, 'parse'>
): ParserBuilder<T> {
function parseServerSideNullable(value: string | string[] | undefined) {
if (typeof value === 'undefined') {
Expand All @@ -117,6 +119,7 @@ export function createParser<T>(

return {
eq: (a, b) => a === b,
serialize: String,
...parser,
parseServerSide: parseServerSideNullable,
withDefault(defaultValue) {
Expand Down Expand Up @@ -401,12 +404,11 @@ export function parseAsArrayOf<ItemType>(
},
serialize: values =>
values
.map<string>(value => {
const str = itemParser.serialize
? itemParser.serialize(value)
: String(value)
return str.replaceAll(separator, encodedSeparator)
.map<string | null>(value => {
const str = (itemParser.serialize ?? String)(value)
return str?.replaceAll(separator, encodedSeparator) ?? null
})
.filter(value => value !== null)
.join(separator),
eq(a, b) {
if (a === b) {
Expand Down
7 changes: 6 additions & 1 deletion packages/nuqs/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ export function createSerializer<
) {
search.delete(urlKey)
} else {
search.set(urlKey, parser.serialize(value))
const serialized = parser.serialize(value)
if (serialized === null) {
search.delete(urlKey)
} else {
search.set(urlKey, serialized)
}
}
}
return base + renderQueryString(search)
Expand Down
2 changes: 1 addition & 1 deletion packages/nuqs/src/update-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function resetQueue() {
export function enqueueQueryStringUpdate<Value>(
key: string,
value: Value | null,
serialize: (value: Value) => string,
serialize: (value: Value) => string | null,
options: Pick<
Options,
'history' | 'scroll' | 'shallow' | 'startTransition' | 'throttleMs'
Expand Down
Loading