keycloak-scim/js/apps/admin-ui/src/utils/generateEncodedPath.ts
Erik Jan de Wit 13207aabac
Encode parameters for React Router links in URL-safe manner (#23667)
Closes #22600

Co-authored-by: Jon Koops <jonkoops@gmail.com>
2023-10-17 07:36:26 +00:00

17 lines
504 B
TypeScript

import { generatePath } from "react-router-dom";
export type PathParam = { [key: string]: string };
export function generateEncodedPath<Path extends string>(
originalPath: Path,
params: PathParam,
): string {
const encodedParams: PathParam = {};
Object.entries(params).forEach(
([k, v]) => (encodedParams[k] = encodeURIComponent(v)),
);
//TODO: Fix type once https://github.com/remix-run/react-router/pull/10719 is merged.
return generatePath(originalPath, encodedParams as any);
}