From 7e01df03efa7a0c68f94f57782c654d30289360d Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Thu, 22 Dec 2022 11:02:45 -0600 Subject: [PATCH] allows void / undefined inputs to send requests, no longer resends request on window focus. --- packages/test-app/src/router.ts | 5 +++- .../input-mappers/__tests__/zod/void.test.ts | 29 +++++++++++++++++++ .../zod/parsers/parseZodVoidDef.ts | 13 +++++++++ .../src/parse/input-mappers/zod/selector.ts | 4 +++ .../components/form/ProcedureForm/index.tsx | 8 +++-- 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 packages/trpc-panel/src/parse/input-mappers/__tests__/zod/void.test.ts create mode 100644 packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodVoidDef.ts diff --git a/packages/test-app/src/router.ts b/packages/test-app/src/router.ts index c9b5ed6..a279618 100644 --- a/packages/test-app/src/router.ts +++ b/packages/test-app/src/router.ts @@ -184,7 +184,7 @@ export const testRouter = t.router({ ]), }) ) - .query(() => { + .query(({ input }) => { return "It's an input"; }), emailTextInput: t.procedure @@ -196,6 +196,9 @@ export const testRouter = t.router({ .query(({ input }) => { return "It's good"; }), + voidInput: t.procedure.input(z.void()).query(() => { + return "yep"; + }), }), anErrorThrowingRoute: t.procedure diff --git a/packages/trpc-panel/src/parse/input-mappers/__tests__/zod/void.test.ts b/packages/trpc-panel/src/parse/input-mappers/__tests__/zod/void.test.ts new file mode 100644 index 0000000..1a60e54 --- /dev/null +++ b/packages/trpc-panel/src/parse/input-mappers/__tests__/zod/void.test.ts @@ -0,0 +1,29 @@ +import { defaultReferences } from "../../defaultReferences"; +import { parseZodVoidDef } from "../../zod/parsers/parseZodVoidDef"; +import { zodSelectorFunction } from "../../zod/selector"; +import { LiteralNode } from "../../../parseNodeTypes"; +import { z } from "zod"; + +describe("Parse ZodVoid", () => { + it("should parse a void def as a literal node with undefined value", () => { + const expected: LiteralNode = { + type: "literal", + path: [], + value: undefined, + }; + const zodSchema = z.void(); + const parsed = parseZodVoidDef(zodSchema._def, defaultReferences()); + expect(parsed).toStrictEqual(expected); + }); + + it("should be mapped correctly via the selector and parsed as a literal node", () => { + const expected: LiteralNode = { + type: "literal", + path: [], + value: undefined, + }; + const zodSchema = z.void(); + const parsed = zodSelectorFunction(zodSchema._def, defaultReferences()); + expect(parsed).toStrictEqual(expected); + }); +}); diff --git a/packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodVoidDef.ts b/packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodVoidDef.ts new file mode 100644 index 0000000..c87dfb4 --- /dev/null +++ b/packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodVoidDef.ts @@ -0,0 +1,13 @@ +import { LiteralNode, ParseReferences } from "@src/parse/parseNodeTypes"; +import { ZodVoidDef } from "zod"; + +export function parseZodVoidDef( + _: ZodVoidDef, + refs: ParseReferences +): LiteralNode { + return { + type: "literal", + value: undefined, + path: refs.path, + }; +} diff --git a/packages/trpc-panel/src/parse/input-mappers/zod/selector.ts b/packages/trpc-panel/src/parse/input-mappers/zod/selector.ts index eab2581..d61c854 100644 --- a/packages/trpc-panel/src/parse/input-mappers/zod/selector.ts +++ b/packages/trpc-panel/src/parse/input-mappers/zod/selector.ts @@ -16,6 +16,7 @@ import { ZodPromiseDef, ZodStringDef, ZodUndefinedDef, + ZodVoidDef, } from "zod"; import { parseZodStringDef } from "./parsers/parseZodStringDef"; import { ParserSelectorFunction } from "../../parseNodeTypes"; @@ -39,6 +40,7 @@ import { parseZodEffectsDef } from "@src/parse/input-mappers/zod/parsers/parseZo import { parseZodNullDef } from "@src/parse/input-mappers/zod/parsers/parseZodNullDef"; import { parseZodPromiseDef } from "@src/parse/input-mappers/zod/parsers/parseZodPromiseDef"; import { parseZodUndefinedDef } from "@src/parse/input-mappers/zod/parsers/parseZodUndefinedDef"; +import { parseZodVoidDef } from "./parsers/parseZodVoidDef"; export const zodSelectorFunction: ParserSelectorFunction = ( def, @@ -87,6 +89,8 @@ export const zodSelectorFunction: ParserSelectorFunction = ( return parseZodPromiseDef(def as ZodPromiseDef, references); case ZodFirstPartyTypeKind.ZodUndefined: return parseZodUndefinedDef(def as ZodUndefinedDef, references); + case ZodFirstPartyTypeKind.ZodVoid: + return parseZodVoidDef(def as ZodVoidDef, references); } return { type: "unsupported", path: references.path }; }; diff --git a/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx index 59a9bae..15b3f3c 100644 --- a/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx +++ b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx @@ -49,6 +49,7 @@ export function ProcedureForm({ // null => request was never sent // undefined => request successful but nothing returned from procedure const [mutationResponse, setMutationResponse] = useState(null); + const [queryEnabled, setQueryEnabled] = useState(false); const [queryInput, setQueryInput] = useState(null); const formRef = useRef(null); const context = trpc.useContext(); @@ -67,9 +68,10 @@ export function ProcedureForm({ const router = getProcedure(); //@ts-ignore return router.useQuery(queryInput, { - enabled: !!queryInput, + enabled: queryEnabled, initialData: null, retry: false, + refetchOnWindowFocus: false, }); })() as UseQueryResult; @@ -106,6 +108,7 @@ export function ProcedureForm({ if (procedure.procedureType === "query") { const newData = { ...data }; setQueryInput(newData[ROOT_VALS_PROPERTY_NAME]); + setQueryEnabled(true); invalidateQuery(data.vals); } else { mutation @@ -133,6 +136,7 @@ export function ProcedureForm({ }, [shouldReset]); function reset() { setShouldReset(true); + setQueryEnabled(false); } const data = @@ -238,7 +242,7 @@ function wrapJsonSchema(jsonSchema: any) { properties: { [ROOT_VALS_PROPERTY_NAME]: jsonSchema, }, - required: [ROOT_VALS_PROPERTY_NAME], + required: [], additionalProperties: false, $schema: "http://json-schema.org/draft-07/schema#", };