Created
November 21, 2023 10:03
-
-
Save martinhj/30ce35b4bc762fe36a5d612d66b19807 to your computer and use it in GitHub Desktop.
Convert NodeJS' ParseUrlQuery to browser API URLSearchParams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { ParsedUrl } from 'next/dist/shared/lib/router/utils/parse-url' | |
export function convertUrlQueryToUrlSearchParams( | |
queryArgs: ParsedUrl['query'], | |
) { | |
const searchparams = new URLSearchParams() | |
Object.entries(queryArgs) | |
.filter(([key, value]) => Boolean(value)) | |
.forEach( | |
([key, value]) => | |
value !== undefined && | |
(Array.isArray(value) | |
? value.forEach(_value => searchparams.append(key, _value)) | |
: searchparams.append(key, value)), | |
) | |
return searchparams | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment