-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.ts
32 lines (28 loc) · 846 Bytes
/
paths.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { buildSearchParams } from "./searchParams";
export type RoutePath = MarkoRun.Route["path"];
export type RouteParams<Path extends RoutePath> = (MarkoRun.Route & {
path: Path;
})["params"];
export type BuildPathArgs<Path extends RoutePath> =
keyof RouteParams<Path> extends never
? {
path: Path;
params?: RouteParams<Path>;
query?: Record<string, unknown>;
}
: {
path: Path;
params: RouteParams<Path>;
query?: Record<string, unknown>;
};
export const buildPath = <Path extends RoutePath>({
params,
path,
query,
}: BuildPathArgs<Path>) => {
const base = Object.entries(params || {}).reduce<string>(
(prev, [param, value]) => prev.replaceAll(`:${param}`, value as string),
path,
);
return query ? `${base}?${buildSearchParams(query)}` : base;
};