From ee422a6f6e16a5aa8975b620bec90a0871b7d459 Mon Sep 17 00:00:00 2001 From: Thiha Date: Wed, 24 Jan 2024 21:16:32 +0700 Subject: [PATCH] fix: utility: url: filter undefined query parameter values --- utility/url.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utility/url.ts b/utility/url.ts index 25ef7c5..6019b65 100644 --- a/utility/url.ts +++ b/utility/url.ts @@ -4,8 +4,8 @@ type Value = string | number | bigint | boolean | undefined type Data = { [key: string]: Value } - const baseURL = self.location.origin +const excludedQueryParams: Value[] = [undefined] export type URLBuilderOptions = { params?: Data @@ -40,9 +40,11 @@ export const buildURL = ( const url = new URL(path, baseURL) - Object.keys(queryParams).map(name => { - url.searchParams.set(name, `${queryParams[name]}`) - }) + for (const [name, value] of Object.entries(queryParams)) { + if (excludedQueryParams.includes(value)) continue + + url.searchParams.set(name, `${value}`) + } if (hash) { url.hash = hash