react-router-typesafe-routes
Version:
Enhanced type safety via validation for all route params in React Router v7.
34 lines (33 loc) • 1.59 kB
JavaScript
import { type, parser } from "../lib/index.mjs";
import { ZodType, ZodOptional } from "zod/v4";
import { ZodOptional as ZodOptionalV3, ZodString as ZodStringV3, ZodDate as ZodDateV3, ZodNumber as ZodNumberV3, ZodBoolean as ZodBooleanV3, } from "zod/v3";
function configure({ parserFactory }) {
function zod(zodType, parser) {
const unwrappedZodType = zodType instanceof ZodOptional
? zodType.unwrap()
: zodType instanceof ZodOptionalV3
? zodType.unwrap()
: zodType;
let typeHint = "unknown";
if ((unwrappedZodType instanceof ZodType && unwrappedZodType.def.type === "string") ||
unwrappedZodType instanceof ZodStringV3) {
typeHint = "string";
}
else if ((unwrappedZodType instanceof ZodType && unwrappedZodType.def.type === "number") ||
unwrappedZodType instanceof ZodNumberV3) {
typeHint = "number";
}
else if ((unwrappedZodType instanceof ZodType && unwrappedZodType.def.type === "boolean") ||
unwrappedZodType instanceof ZodBooleanV3) {
typeHint = "boolean";
}
else if ((unwrappedZodType instanceof ZodType && unwrappedZodType.def.type === "date") ||
unwrappedZodType instanceof ZodDateV3) {
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 };