From 4d101c72c0bd2aa50f8a54621818099a684bdcaf Mon Sep 17 00:00:00 2001 From: Sanna Jammeh <50969683+sannajammeh@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:25:56 +0100 Subject: [PATCH] fix(ssr): Ensure trpc-swr/ssr caller can execute on mutations in TS --- .changeset/new-boxes-sniff.md | 5 +++++ packages/ssr/createProxySSGHelpers.tsx | 5 ++--- test/unit/server.ts | 3 +++ test/unit/tests/ssg.spec.ts | 12 +++++++++++- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/new-boxes-sniff.md diff --git a/.changeset/new-boxes-sniff.md b/.changeset/new-boxes-sniff.md new file mode 100644 index 0000000..413034c --- /dev/null +++ b/.changeset/new-boxes-sniff.md @@ -0,0 +1,5 @@ +--- +"@trpc-swr/ssr": patch +--- + +Ensure trpc-swr/ssr caller can execute on mutations diff --git a/packages/ssr/createProxySSGHelpers.tsx b/packages/ssr/createProxySSGHelpers.tsx index 76d3492..62b275c 100644 --- a/packages/ssr/createProxySSGHelpers.tsx +++ b/packages/ssr/createProxySSGHelpers.tsx @@ -1,6 +1,5 @@ import { AnyProcedure, - AnyQueryProcedure, AnyRouter, ClientDataTransformerOptions, inferProcedureInput, @@ -33,7 +32,7 @@ type CallableProcedure = ( type DecorateProcedure< TProcedure extends AnyProcedure, TPath extends string, -> = TProcedure extends AnyQueryProcedure +> = TProcedure extends AnyProcedure ? { getKey: GetKey; } & CallableProcedure @@ -67,7 +66,7 @@ function createSSRProxyDecoration( state: Map, caller: Caller, serialize: (obj: unknown) => unknown = (obj) => obj, - defaultTransform: boolean = false + defaultTransform = false ) { return createRecursiveProxy((opts) => { const args = opts.args; diff --git a/test/unit/server.ts b/test/unit/server.ts index 974b1c3..05251cc 100644 --- a/test/unit/server.ts +++ b/test/unit/server.ts @@ -143,6 +143,9 @@ export const appRouter = t.router({ hello: t.procedure.query(() => { return "world"; }), + testMutation: t.procedure.mutation(() => { + return "testMutation"; + }), xTest: t.procedure.query(({ ctx }) => { const xTestHeader = ctx.req.headers["X-Test"]; diff --git a/test/unit/tests/ssg.spec.ts b/test/unit/tests/ssg.spec.ts index 8ccffd2..0938a85 100644 --- a/test/unit/tests/ssg.spec.ts +++ b/test/unit/tests/ssg.spec.ts @@ -2,7 +2,7 @@ import assert from "node:assert"; import { unstable_serialize, createSSRHelpers } from "@trpc-swr/ssr"; import { unstable_serialize as swr_unstable_serialize } from "swr"; import { appRouter } from "../utils"; -import { test, describe } from "vitest"; +import { test, describe, expect } from "vitest"; describe("unstable_serialize", async () => { await test("Should serialize a string key", () => { @@ -73,6 +73,16 @@ const createSSG = () => { }; describe("createSSRHelpers", async () => { + await test("Should access mutation", async () => { + const ssg = createSSG(); + + assert.equal(typeof ssg.testMutation, "function"); + + const data = await ssg.testMutation(); + + expect(data).toEqual("testMutation"); + }); + await test("Should return a proxy object", () => { const ssg = createSSG();