TanStackT
TanStack16mo ago
29 replies
verbal-lime

Is there correct way to create abstraction around createServerFn?

Hello, I'm trying to create abstraction on top of createServerFn to validate payload using zod. But I'm running into strange errors. Here is very simplified example:

import { createServerFn } from "@tanstack/start";

const createServerFnWithConsoleLog: typeof createServerFn = (...args) => {
  "use server"; // The error is same whether this directive is here or not.
  console.log("Hello from wrapper");

  return createServerFn(...args);
};

export const testFn = createServerFnWithConsoleLog("GET", async () => {
  return "Hello world!";
});


With this code I'm getting:
Error: Invariant failed: createServerFn must be called with a function that is marked with the 'use server' pragma. Are you using the @tanstack/router-plugin/vite ?


Is there correct way to do this?
Was this page helpful?