diff --git a/examples/tanstack/.gitignore b/examples/tanstack/.gitignore index a14702c..00df2e9 100644 --- a/examples/tanstack/.gitignore +++ b/examples/tanstack/.gitignore @@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json # Finder (MacOS) folder config .DS_Store + +tmp diff --git a/examples/tanstack/app/routes/index.tsx b/examples/tanstack/app/routes/index.tsx index ce8ff9e..fd363d5 100644 --- a/examples/tanstack/app/routes/index.tsx +++ b/examples/tanstack/app/routes/index.tsx @@ -1,12 +1,13 @@ import * as fs from "node:fs"; +import * as path from "node:path"; import { createFileRoute, useRouter } from "@tanstack/react-router"; import { createServerFn } from "@tanstack/react-start"; -const filePath = "count.txt"; +const countFilePath = "count.txt"; async function readCount() { return parseInt( - await fs.promises.readFile(filePath, "utf-8").catch(() => "0") + await fs.promises.readFile(countFilePath, "utf-8").catch(() => "0") ); } @@ -16,16 +17,36 @@ const getCount = createServerFn({ return readCount(); }); +// Used to post a file input to the server +const postFile = createServerFn({ + method: "POST", +}) + .validator((data: FormData) => { + const file = data.get("file"); + if (!(file instanceof File)) { + throw new Error("Expected a file input"); + } + return file; + }) + .handler(async ({ data }) => { + const file = data; + const content = await file.text(); + const filePath = path.join(import.meta.dirname, "..", "..", "tmp", file.name); + + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); + await fs.promises.writeFile(filePath, content); + }); + const updateCount = createServerFn({ method: "POST" }) .validator((d: number) => d) .handler(async ({ data }) => { const count = await readCount(); - await fs.promises.writeFile(filePath, `${count + data}`); + await fs.promises.writeFile(countFilePath, `${count + data}`); }); // n.b. we can't create a `DELETE` server fn const resetCount = createServerFn({ method: "POST" }).handler(async () => { - await fs.promises.writeFile(filePath, "0"); + await fs.promises.writeFile(countFilePath, "0"); }); export const Route = createFileRoute("/")({ @@ -66,29 +87,16 @@ function Home() {