Eat Something New
+
+
{{ data?.greeting }}
Generate a random recipe.
Random Recipe Now
diff --git a/plugins/client.ts b/plugins/client.ts
new file mode 100644
index 0000000..4ff7015
--- /dev/null
+++ b/plugins/client.ts
@@ -0,0 +1,22 @@
+import { createTRPCNuxtClient, httpBatchLink } from "trpc-nuxt/client";
+import type { AppRouter } from "~/server/trpc/routers";
+
+export default defineNuxtPlugin(() => {
+ /**
+ * createTRPCNuxtClient adds a `useQuery` composable
+ * built on top of `useAsyncData`.
+ */
+ const client = createTRPCNuxtClient({
+ links: [
+ httpBatchLink({
+ url: "/api/trpc",
+ }),
+ ],
+ });
+
+ return {
+ provide: {
+ client,
+ },
+ };
+});
diff --git a/server/api/trpc/[trpc].ts b/server/api/trpc/[trpc].ts
new file mode 100644
index 0000000..70d2eca
--- /dev/null
+++ b/server/api/trpc/[trpc].ts
@@ -0,0 +1,9 @@
+import { createNuxtApiHandler } from "trpc-nuxt";
+import { appRouter } from "~/server/trpc/routers";
+import { createContext } from "~/server/trpc/context";
+
+// export API handler
+export default createNuxtApiHandler({
+ router: appRouter,
+ createContext,
+});
diff --git a/server/trpc/context.ts b/server/trpc/context.ts
new file mode 100644
index 0000000..a143234
--- /dev/null
+++ b/server/trpc/context.ts
@@ -0,0 +1,9 @@
+import type { inferAsyncReturnType } from "@trpc/server";
+
+/**
+ * Creates context for an incoming request
+ * @link https://trpc.io/docs/context
+ */
+export const createContext = () => ({});
+
+export type Context = inferAsyncReturnType;
diff --git a/server/trpc/routers/index.ts b/server/trpc/routers/index.ts
new file mode 100644
index 0000000..f68f912
--- /dev/null
+++ b/server/trpc/routers/index.ts
@@ -0,0 +1,19 @@
+import { z } from "zod";
+import { publicProcedure, router } from "../trpc";
+
+export const appRouter = router({
+ hello: publicProcedure
+ .input(
+ z.object({
+ text: z.string().nullish(),
+ }),
+ )
+ .query(({ input }) => {
+ return {
+ greeting: `hello ${input?.text ?? "world"}`,
+ };
+ }),
+});
+
+// export type definition of API
+export type AppRouter = typeof appRouter;
diff --git a/server/trpc/trpc.ts b/server/trpc/trpc.ts
new file mode 100644
index 0000000..314ec1a
--- /dev/null
+++ b/server/trpc/trpc.ts
@@ -0,0 +1,11 @@
+import { initTRPC } from "@trpc/server";
+import type { Context } from "~/server/trpc/context";
+
+const t = initTRPC.context().create();
+
+/**
+ * Unprotected procedure
+ **/
+export const publicProcedure = t.procedure;
+export const router = t.router;
+export const middleware = t.middleware;