Skip to content

Commit

Permalink
Add simple trpc api calling
Browse files Browse the repository at this point in the history
  • Loading branch information
monshinawatra committed Feb 20, 2024
1 parent 588bb58 commit 217b830
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
22 changes: 11 additions & 11 deletions src/app/_components/testHook.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use client"
import { trpc } from "../_trpc/client"
'use client';
import { trpc } from '../_trpc/client';

export default function TestHook() {
const test = trpc.test.useQuery()
const testMutation = trpc.hello.useMutation()
const test = trpc.test.useQuery();
const hellothere = trpc.hellothere.useQuery();

return (
<div>
<p>{JSON.stringify(test.data)}</p>
<p>{JSON.stringify(testMutation.data)}</p>
</div>
)
}
return (
<div>
<p>{JSON.stringify(test.data)}</p>
<p>{JSON.stringify(hellothere.data)}</p>
</div>
);
}
20 changes: 10 additions & 10 deletions src/app/api/trpc/[trpc]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { appRouter } from "@/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { appRouter } from '@/server';
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';

const handler = (req: Request) =>
fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext: () => ({})
})
const handler = (req: Request) =>
fetchRequestHandler({
endpoint: '/api/trpc',
req,
router: appRouter,
createContext: () => ({}),
});

export { handler as GET, handler as POST };
export { handler as DELETE, handler as GET, handler as POST, handler as PUT };
11 changes: 11 additions & 0 deletions src/server/api/secondTestTRPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { publicProcedure } from '../trpc';

export default function apiSecondTest() {
return {
hellothere: publicProcedure.query(async () => {
return {
data: { status: 200, message: 'Hello world!' },
};
}),
};
}
11 changes: 11 additions & 0 deletions src/server/api/testTRPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { publicProcedure } from '../trpc';

export default function apiTest() {
return {
test: publicProcedure.query(async () => {
return {
message: 'Hello world! if you see this, it means that trpc is working!',
};
}),
};
}
22 changes: 8 additions & 14 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { z } from 'zod';
import { publicProcedure, router } from "./trpc";
import apiSecondTest from './api/secondTestTRPC';
import apiTest from './api/testTRPC';
import { router } from './trpc';

export const appRouter = router({
test: publicProcedure.query(async () => {
return {
message: 'Hello world! if you see this, it means that trpc is working!'
}
}),
hello: publicProcedure.input(z.string()).mutation(async (input) => {
return {
message: `Hello, ${input}!`
}
})
})
...apiTest(),
...apiSecondTest(),
});

export type AppRouter = typeof appRouter;
export type AppRouter = typeof appRouter;

0 comments on commit 217b830

Please sign in to comment.