react-router-typesafe-routes
Version:
Enhanced type safety via validation for all route params in React Router v7.
25 lines (24 loc) • 964 B
JavaScript
import { type, parser } from "../lib/index.mjs";
import { ZodOptional, ZodString, ZodDate, ZodNumber, ZodBoolean } from "zod";
function configure({ parserFactory }) {
function zod(zodType, parser) {
const unwrappedZodType = zodType instanceof ZodOptional ? zodType.unwrap() : zodType;
let typeHint = "unknown";
if (unwrappedZodType instanceof ZodString) {
typeHint = "string";
}
else if (unwrappedZodType instanceof ZodNumber) {
typeHint = "number";
}
else if (unwrappedZodType instanceof ZodBoolean) {
typeHint = "boolean";
}
else if (unwrappedZodType instanceof ZodDate) {
typeHint = "date";
}
return type((value) => zodType.parse(value), parser !== null && parser !== void 0 ? parser : parserFactory(typeHint));
}
return { zod };
}
const { zod } = configure({ parserFactory: parser });
export { configure, zod };