react-router-typesafe-routes
Version:
Enhanced type safety via validation for all route params in React Router v7.
38 lines (37 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.zod = exports.configure = void 0;
const index_js_1 = require("../lib/index.js");
const v4_1 = require("zod/v4");
const v3_1 = require("zod/v3");
function configure({ parserFactory }) {
function zod(zodType, parser) {
const unwrappedZodType = zodType instanceof v4_1.ZodOptional
? zodType.unwrap()
: zodType instanceof v3_1.ZodOptional
? zodType.unwrap()
: zodType;
let typeHint = "unknown";
if ((unwrappedZodType instanceof v4_1.ZodType && unwrappedZodType.def.type === "string") ||
unwrappedZodType instanceof v3_1.ZodString) {
typeHint = "string";
}
else if ((unwrappedZodType instanceof v4_1.ZodType && unwrappedZodType.def.type === "number") ||
unwrappedZodType instanceof v3_1.ZodNumber) {
typeHint = "number";
}
else if ((unwrappedZodType instanceof v4_1.ZodType && unwrappedZodType.def.type === "boolean") ||
unwrappedZodType instanceof v3_1.ZodBoolean) {
typeHint = "boolean";
}
else if ((unwrappedZodType instanceof v4_1.ZodType && unwrappedZodType.def.type === "date") ||
unwrappedZodType instanceof v3_1.ZodDate) {
typeHint = "date";
}
return (0, index_js_1.type)((value) => zodType.parse(value), parser !== null && parser !== void 0 ? parser : parserFactory(typeHint));
}
return { zod };
}
exports.configure = configure;
const { zod } = configure({ parserFactory: index_js_1.parser });
exports.zod = zod;