Skip to content

Commit

Permalink
feat: add generics to addQueryToURL type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
garusis committed Oct 17, 2023
1 parent 37c60ae commit 92c0a0c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
npm
tsc/
.DS_Store
.idea
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 92c0a0c

Please sign in to comment.