Skip to content

Commit

Permalink
Merge pull request #9 from iway1/zod-3.20-stability-and-refactor
Browse files Browse the repository at this point in the history
Zod 3.20 stability and refactor
  • Loading branch information
iway1 authored Dec 14, 2022
2 parents b16088a + 9d3e7af commit 26fab7c
Show file tree
Hide file tree
Showing 31 changed files with 577 additions and 292 deletions.
4 changes: 2 additions & 2 deletions packages/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"trpc-panel": "^1.0.0",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0",
"zod": "^3.19.1"
"zod": "3.20.2"
},
"scripts": {
"dev": "SERVER_URL=\"http://localhost\" TRPC_PATH=\"trpc\" DEV_PORT=\"4000\" SIMULATE_DELAY=\"true\" nodemon ./src/server.ts",
"dev": "LIVE_RELOAD=\"true\" SERVER_URL=\"http://localhost\" TRPC_PATH=\"trpc\" DEV_PORT=\"4000\" SIMULATE_DELAY=\"true\" nodemon ./src/server.ts",
"build": "npx tsc --project tsconfig.json",
"start": "node ./lib/server"
},
Expand Down
1 change: 0 additions & 1 deletion packages/trpc-panel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"peerDependencies": {
"@trpc/server": "^10.0.0",
"react": "^18.2.0",
"zod": "^3.19.1"
},
"devDependencies": {
Expand Down
11 changes: 9 additions & 2 deletions packages/trpc-panel/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ export default [
typescript(),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
preventAssignment: false,
}),
babel({
presets: ["@babel/preset-react"],
presets: [
[
"@babel/preset-react",
{
development: isWatching,
},
],
],
}),
commonjs(),
copy({
Expand All @@ -62,7 +70,6 @@ export default [
],
}),
!isWatching && terser(),
visualizer(),
],
},
];
2 changes: 0 additions & 2 deletions packages/trpc-panel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export { parseNode as parseRouter } from "./parse/parse-router";
export { mapZodObjectToNode as mapZodObject } from "./parse/input-mappers/zod";
export { renderTrpcPanel as renderTrpcPanel } from "./render";
125 changes: 0 additions & 125 deletions packages/trpc-panel/src/parse/input-mappers/zod.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ZodArrayDef } from "zod";
import { ArrayNode, ParseFunction } from "../../../parsed-node-types";
import { zodSelectorFunction } from "../selector";

export const parseZodArrayDef: ParseFunction<ZodArrayDef, ArrayNode> = (def, refs) => {
const {type} = def
const childType = zodSelectorFunction(type._def, refs)
return {
type: "array",
childType,
...refs
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ZodBooleanDef } from "zod";
import { BooleanNode, ParseFunction } from "../../../parsed-node-types";

export const parseZodBooleanFieldDef: ParseFunction<ZodBooleanDef, BooleanNode> = (_, ref)=>{
return { type: "boolean", ...ref };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { AnyZodObject, ZodFirstPartyTypeKind } from "zod";
import {
DiscriminatedUnionNode,
ParseFunction,
} from "../../../parsed-node-types";
import { zodSelectorFunction } from "../selector";

type OptionsMap = Map<string, AnyZodObject>;

type ZodDiscriminatedUnionThreePointTwenty = {
optionsMap: OptionsMap;
discriminator: string;
};

type ZodDiscriminatedUnionPreThreePointTwenty = {
options: OptionsMap;
discriminator: string;
};

export type ZodDiscriminatedUnionDefUnversioned =
| ZodDiscriminatedUnionPreThreePointTwenty
| ZodDiscriminatedUnionThreePointTwenty;

function isZodThreePointTwenty(
def: ZodDiscriminatedUnionDefUnversioned
): def is ZodDiscriminatedUnionThreePointTwenty {
return "optionsMap" in def;
}

function makeDefConsistent(def: ZodDiscriminatedUnionDefUnversioned): {
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion;
discriminator: string;
options: Map<string, AnyZodObject>;
} {
const optionsMap = isZodThreePointTwenty(def)
? def.optionsMap
: def.options;
return {
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
discriminator: def.discriminator,
options: optionsMap,
};
}

export const parseZodDiscriminatedUnionDef: ParseFunction<
ZodDiscriminatedUnionDefUnversioned,
DiscriminatedUnionNode
> = (def, refs) => {
const defConsistent = makeDefConsistent(def);
const entries = Array.from(defConsistent.options.entries());
const nodeEntries = entries.map(([discriminatorValue, zodObj]) => [
discriminatorValue,
zodSelectorFunction(zodObj._def, refs),
]);
// Not sure why this is here but seems important
// if (nodeEntries.some((e) => e[1] === null)) return null;

const nodesMap = Object.fromEntries(nodeEntries);

return {
type: "discriminated-union",
discriminatedUnionValues: entries.map(([n]) => n),
discriminatedUnionChildrenMap: nodesMap,
discriminatorName: def.discriminator,
...refs,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ZodEnumDef } from "zod";
import { EnumNode, ParseFunction } from "../../../parsed-node-types";

export const parseZodEnumDef: ParseFunction<ZodEnumDef, EnumNode> = (
def,
refs
) => {
const values = def.values as string[];
return { type: "enum", enumValues: values, ...refs };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ZodLiteralDef } from "zod";
import { LiteralNode, ParseFunction } from "../../../parsed-node-types";

export const parseZodLiteralDef: ParseFunction<ZodLiteralDef, LiteralNode> = (def, refs)=>{
return {
type :'literal',
value: def.value,
...refs,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NumberNode, ParseFunction } from "../../../parsed-node-types";
import { ZodNumberDef } from "zod";

export const parseZodNumberDef: ParseFunction<ZodNumberDef, NumberNode> = (
_,
references
) => {
return {
type: "number",
path: references.path,
optional: references.optional,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ZodObjectDef } from "zod";
import {
ObjectNode,
ParsedInputNode,
ParseFunction,
UnsupportedNode,
} from "../../../parsed-node-types";
import { zodSelectorFunction } from "../selector";

export const parseZodObjectDef: ParseFunction<
ZodObjectDef,
ObjectNode | UnsupportedNode
> = (def, refs) => {
const shape = def.shape();
const children: { [propertyName: string]: ParsedInputNode } = {};
for (var propertyName of Object.keys(shape)) {
const node = zodSelectorFunction(shape[propertyName]!._def, {
...refs,
path: refs.path.concat([propertyName]),
});
children[propertyName] = node;
}
return {
type: "object",
children,
path: refs.path,
optional: refs.optional,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ZodOptionalDef } from "zod";
import { ParsedInputNode, ParseFunction } from "../../../parsed-node-types";
import { zodSelectorFunction } from "../selector";


export const parseZodOptionalDef: ParseFunction<ZodOptionalDef, ParsedInputNode & {optional: true}> = (def, refs)=>{
const parsedInner = zodSelectorFunction(def.innerType._def, refs)
return {
...parsedInner,
optional: true,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ParseFunction, StringNode } from "../../../parsed-node-types";
import { ZodStringDef } from "zod";

export const parseZodStringDef: ParseFunction<ZodStringDef, StringNode> = (_, refs)=>{
return {
type: 'string',
path: refs.path,
optional: refs.optional
}
}
Loading

0 comments on commit 26fab7c

Please sign in to comment.