Skip to content

Commit

Permalink
fix: Make addQueryToUrl return the same type it was given
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard committed Oct 7, 2023
1 parent 37c60ae commit c5403bc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { JSONValue, PathParams, SearchParams } from './types'
* @param searchParams the query parameters
* @returns the url with the query parameters added with the same type as the url
*/
function addQueryToURL(
url: string | URL,
function addQueryToURL<T extends string | URL>(
url: T,
searchParams?: SearchParams,
): string | URL {
): T {
if (!searchParams) return url

if (typeof url === 'string') {
const separator = url.includes('?') ? '&' : '?'
return `${url}${separator}${new URLSearchParams(searchParams)}`
return `${url}${separator}${new URLSearchParams(searchParams)}` as T
}
if (searchParams && url instanceof URL) {
for (const [key, value] of new URLSearchParams(searchParams).entries()) {
Expand Down

0 comments on commit c5403bc

Please sign in to comment.