Skip to content

Commit c4d9715

Browse files
committed
refactor(search-params): create custom type for SearchParams
Regular parameters support only single values, while search parameters support arrays
1 parent b4f7544 commit c4d9715

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/routing.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import type {
3232
RouterContext,
3333
RouterIntegration,
3434
SetParams,
35-
Submission
35+
Submission,
36+
SearchParams,
37+
SetSearchParams
3638
} from "./types.js";
3739
import {
3840
mockBase,
@@ -96,13 +98,13 @@ export const useCurrentMatches = () => useRouter().matches;
9698

9799
export const useParams = <T extends Params>() => useRouter().params as T;
98100

99-
export const useSearchParams = <T extends Params>(): [
101+
export const useSearchParams = <T extends SearchParams>(): [
100102
Partial<T>,
101-
(params: SetParams, options?: Partial<NavigateOptions>) => void
103+
(params: SetSearchParams, options?: Partial<NavigateOptions>) => void
102104
] => {
103105
const location = useLocation();
104106
const navigate = useNavigate();
105-
const setSearchParams = (params: SetParams, options?: Partial<NavigateOptions>) => {
107+
const setSearchParams = (params: SetSearchParams, options?: Partial<NavigateOptions>) => {
106108
const searchString = untrack(() => mergeSearchString(location.search, params) + location.hash);
107109
navigate(searchString, {
108110
scroll: false,

src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ declare module "solid-js/web" {
2323
}
2424
}
2525

26-
export type Params = Record<string, string | string[]>;
26+
export type Params = Record<string, string>;
27+
export type SearchParams = Record<string, string | string[]>;
2728

2829
export type SetParams = Record<
30+
string,
31+
string | number | boolean | null | undefined
32+
>;
33+
export type SetSearchParams = Record<
2934
string,
3035
string | string[] | number | number[] | boolean | boolean[] | null | undefined
3136
>;
@@ -37,7 +42,7 @@ export interface Path {
3742
}
3843

3944
export interface Location<S = unknown> extends Path {
40-
query: Params;
45+
query: SearchParams;
4146
state: Readonly<Partial<S>> | null;
4247
key: string;
4348
}

src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import type {
55
Params,
66
PathMatch,
77
RouteDescription,
8-
SetParams
8+
SearchParams,
9+
SetParams,
10+
SetSearchParams
911
} from "./types.ts";
1012

1113
const hasSchemeRegex = /^(?:[a-z0-9]+:)?\/\//i;
@@ -45,8 +47,8 @@ export function joinPaths(from: string, to: string): string {
4547
return normalizePath(from).replace(/\/*(\*.*)?$/g, "") + normalizePath(to);
4648
}
4749

48-
export function extractSearchParams(url: URL): Params {
49-
const params: Params = {};
50+
export function extractSearchParams(url: URL): SearchParams {
51+
const params: SearchParams = {};
5052
url.searchParams.forEach((value, key) => {
5153
if (key in params) {
5254
params[key] = Array.isArray(params[key])
@@ -163,7 +165,7 @@ export function createMemoObject<T extends Record<string | symbol, unknown>>(fn:
163165
});
164166
}
165167

166-
export function mergeSearchString(search: string, params: SetParams) {
168+
export function mergeSearchString(search: string, params: SetSearchParams) {
167169
const merged = new URLSearchParams(search);
168170
Object.entries(params).forEach(([key, value]) => {
169171
if (value == null || value === "" || (value instanceof Array && !value.length)) {

0 commit comments

Comments
 (0)