Skip to content

Commit

Permalink
Merge pull request #24 from iway1/bugfix/void-inputs-and-auto-refetch
Browse files Browse the repository at this point in the history
allows void / undefined inputs to send requests, no longer resends re…
  • Loading branch information
iway1 authored Dec 22, 2022
2 parents 0c3d64a + 7e01df0 commit 4d537f7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/test-app/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const testRouter = t.router({
]),
})
)
.query(() => {
.query(({ input }) => {
return "It's an input";
}),
emailTextInput: t.procedure
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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,
};
}
4 changes: 4 additions & 0 deletions packages/trpc-panel/src/parse/input-mappers/zod/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ZodPromiseDef,
ZodStringDef,
ZodUndefinedDef,
ZodVoidDef,
} from "zod";
import { parseZodStringDef } from "./parsers/parseZodStringDef";
import { ParserSelectorFunction } from "../../parseNodeTypes";
Expand All @@ -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<ZodDefWithType> = (
def,
Expand Down Expand Up @@ -87,6 +89,8 @@ export const zodSelectorFunction: ParserSelectorFunction<ZodDefWithType> = (
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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function ProcedureForm({
// null => request was never sent
// undefined => request successful but nothing returned from procedure
const [mutationResponse, setMutationResponse] = useState<any>(null);
const [queryEnabled, setQueryEnabled] = useState<boolean>(false);
const [queryInput, setQueryInput] = useState<any>(null);
const formRef = useRef<HTMLFormElement | null>(null);
const context = trpc.useContext();
Expand All @@ -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<any>;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +136,7 @@ export function ProcedureForm({
}, [shouldReset]);
function reset() {
setShouldReset(true);
setQueryEnabled(false);
}

const data =
Expand Down Expand Up @@ -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#",
};
Expand Down

0 comments on commit 4d537f7

Please sign in to comment.