Skip to content

Commit

Permalink
fix(ssr): Ensure trpc-swr/ssr caller can execute on mutations in TS
Browse files Browse the repository at this point in the history
  • Loading branch information
sannajammeh committed Dec 6, 2023
1 parent f0712c9 commit 4d101c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-boxes-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trpc-swr/ssr": patch
---

Ensure trpc-swr/ssr caller can execute on mutations
5 changes: 2 additions & 3 deletions packages/ssr/createProxySSGHelpers.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AnyProcedure,
AnyQueryProcedure,
AnyRouter,
ClientDataTransformerOptions,
inferProcedureInput,
Expand Down Expand Up @@ -33,7 +32,7 @@ type CallableProcedure<TProcedure extends AnyProcedure> = (
type DecorateProcedure<
TProcedure extends AnyProcedure,
TPath extends string,
> = TProcedure extends AnyQueryProcedure
> = TProcedure extends AnyProcedure
? {
getKey: GetKey<TProcedure, TPath>;
} & CallableProcedure<TProcedure>
Expand Down Expand Up @@ -67,7 +66,7 @@ function createSSRProxyDecoration(
state: Map<string, any>,
caller: Caller,
serialize: (obj: unknown) => unknown = (obj) => obj,
defaultTransform: boolean = false
defaultTransform = false
) {
return createRecursiveProxy((opts) => {
const args = opts.args;
Expand Down
3 changes: 3 additions & 0 deletions test/unit/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down
12 changes: 11 additions & 1 deletion test/unit/tests/ssg.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 4d101c7

Please sign in to comment.