From 26055d3f9aa21ea5d8712e83a28d97a7ee2b0c92 Mon Sep 17 00:00:00 2001 From: Jiu Liancheng Date: Wed, 12 Apr 2023 20:24:00 +0800 Subject: [PATCH 1/3] feat: add support for nested inputs --- packages/dev-app/src/router.ts | 17 +++++++++++++++++ packages/trpc-panel/src/parse/parseProcedure.ts | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/dev-app/src/router.ts b/packages/dev-app/src/router.ts index b7e8a8d..6f63b2f 100644 --- a/packages/dev-app/src/router.ts +++ b/packages/dev-app/src/router.ts @@ -31,6 +31,23 @@ const postsRouter = createTRPCRouter({ text: input.text, }; }), + createNestedPost: procedure + .input( + z.object({ + text: z.string(), + }) + ) + .input( + z.object({ + title: z.string(), + }) + ) + .mutation(({ input }) => { + return { + id: "aoisdjfoasidjfasodf", + text: input.text, + }; + }), }); export const appRouter = createTRPCRouter({ diff --git a/packages/trpc-panel/src/parse/parseProcedure.ts b/packages/trpc-panel/src/parse/parseProcedure.ts index 87748e9..2f41798 100644 --- a/packages/trpc-panel/src/parse/parseProcedure.ts +++ b/packages/trpc-panel/src/parse/parseProcedure.ts @@ -73,10 +73,11 @@ function nodeAndInputSchemaFromInputs( }), }; } + + let input = inputs[0]; if (inputs.length !== 1) { - return { parseInputResult: "failure" }; + input = inputs.reduce((acc, input) => (acc as any).merge(input), emptyZodObject); } - const input = inputs[0]; const iType = inputType(input); if (iType == "unsupported") { return { parseInputResult: "failure" }; From 0d4e1d26e8e942aa49334dff95d03661361d8d8a Mon Sep 17 00:00:00 2001 From: Devinda Senanayaka <3579142+dca123@users.noreply.github.com> Date: Tue, 6 Jun 2023 22:36:06 +0800 Subject: [PATCH 2/3] check if all inputs are zodobjects and thus mergable --- packages/trpc-panel/package.json | 1 - .../trpc-panel/src/parse/parseProcedure.ts | 20 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/trpc-panel/package.json b/packages/trpc-panel/package.json index 38d684d..e655fe0 100644 --- a/packages/trpc-panel/package.json +++ b/packages/trpc-panel/package.json @@ -84,7 +84,6 @@ "tslib": "^2.4.1", "typescript": "^4.9.3", "url": "^0.11.0", - "zod": "^3.19.1", "zustand": "^4.1.5" }, "dependencies": { diff --git a/packages/trpc-panel/src/parse/parseProcedure.ts b/packages/trpc-panel/src/parse/parseProcedure.ts index 2f41798..92b4ec5 100644 --- a/packages/trpc-panel/src/parse/parseProcedure.ts +++ b/packages/trpc-panel/src/parse/parseProcedure.ts @@ -75,8 +75,24 @@ function nodeAndInputSchemaFromInputs( } let input = inputs[0]; - if (inputs.length !== 1) { - input = inputs.reduce((acc, input) => (acc as any).merge(input), emptyZodObject); + if (inputs.length < 1) { + return { parseInputResult: "failure" }; + } + + let input = inputs[0]; + + if (inputs.length > 1) { + const allInputsAreZodObjects = inputs.every( + (input) => input instanceof z.ZodObject + ); + if (!allInputsAreZodObjects) { + return { parseInputResult: "failure" }; + } + + input = inputs.reduce( + (acc, input: z.AnyZodObject) => (acc as z.AnyZodObject).merge(input), + emptyZodObject + ); } const iType = inputType(input); if (iType == "unsupported") { From fff864a23d5e3b7c42213dd1125fe8c621c4455f Mon Sep 17 00:00:00 2001 From: Devinda Senanayaka <3579142+dca123@users.noreply.github.com> Date: Tue, 6 Jun 2023 22:44:34 +0800 Subject: [PATCH 3/3] remove redeclared variable --- packages/trpc-panel/src/parse/parseProcedure.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/trpc-panel/src/parse/parseProcedure.ts b/packages/trpc-panel/src/parse/parseProcedure.ts index a82a918..028c4a0 100644 --- a/packages/trpc-panel/src/parse/parseProcedure.ts +++ b/packages/trpc-panel/src/parse/parseProcedure.ts @@ -82,8 +82,6 @@ function nodeAndInputSchemaFromInputs( return { parseInputResult: "failure" }; } - let input = inputs[0]; - if (inputs.length > 1) { const allInputsAreZodObjects = inputs.every( (input) => input instanceof z.ZodObject @@ -97,6 +95,7 @@ function nodeAndInputSchemaFromInputs( emptyZodObject ); } + const iType = inputType(input); if (iType == "unsupported") { return { parseInputResult: "failure" };