Skip to content

Commit

Permalink
Support null in generatePath params
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jan 11, 2023
1 parent 41eaf60 commit 7e7e3ab
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export type ParamParseKey<Segment extends string> =
* The parameters that were parsed from the URL path.
*/
export type Params<Key extends string = string> = {
readonly [key in Key]: string | null | undefined;
readonly [key in Key]: string | undefined;
};

/**
Expand Down Expand Up @@ -616,7 +616,7 @@ function matchRouteBranch<
export function generatePath<Path extends string>(
originalPath: Path,
params: {
[key in PathParam<Path>]: string;
[key in PathParam<Path>]: string | null;
} = {} as any
): string {
let path = originalPath;
Expand All @@ -636,23 +636,27 @@ export function generatePath<Path extends string>(
.replace(
/^:(\w+)(\??)/g,
(_, key: PathParam<Path>, optional: string | undefined) => {
let hasParam = params[key] != null;
let param = params[key];
if (optional === "?") {
return hasParam ? params[key] : "";
return param == null ? "" : param;
}
invariant(hasParam, `Missing ":${key}" param`);
return params[key]!;
if (param == null) {
invariant(false, `Missing ":${key}" param`);
}
return param;
}
)
.replace(
/\/:(\w+)(\??)/g,
(_, key: PathParam<Path>, optional: string | undefined) => {
let hasParam = params[key] != null;
let param = params[key];
if (optional === "?") {
return hasParam ? `/${params[key]}` : "";
return param == null ? "" : `/${param}`;
}
if (param == null) {
invariant(false, `Missing ":${key}" param`);
}
invariant(hasParam, `Missing ":${key}" param`);
return `/${params[key]!}`;
return `/${param}`;
}
)
// Remove any optional markers from optional static segments
Expand Down

0 comments on commit 7e7e3ab

Please sign in to comment.