alex
alex
TtRPC
Created by alex on 6/7/2024 in #❓-help
nodeHTTPHandler with Nitro
I'm trying to setup trpc with nitro.unjs.io using the node-httpadapter.
import { nodeHTTPRequestHandler } from "@trpc/server/adapters/node-http";
import { appRouter } from "~/lib/trpc/router";

export default defineEventHandler(async (event) => {
console.log('trpc route handler');
const res = await nodeHTTPRequestHandler({
req: event.node.req,
res: event.node.res,
router: appRouter,
path: '/api/trpc',
});
console.log(res) // returns undefined
});
import { nodeHTTPRequestHandler } from "@trpc/server/adapters/node-http";
import { appRouter } from "~/lib/trpc/router";

export default defineEventHandler(async (event) => {
console.log('trpc route handler');
const res = await nodeHTTPRequestHandler({
req: event.node.req,
res: event.node.res,
router: appRouter,
path: '/api/trpc',
});
console.log(res) // returns undefined
});
Frontend setup:
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() => trpc.createClient({
links: [
httpBatchLink({
url: 'http://localhost:3000/api/trpc',
}),
],
}));
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() => trpc.createClient({
links: [
httpBatchLink({
url: 'http://localhost:3000/api/trpc',
}),
],
}));
const { data, isLoading } = trpc.formList.useQuery();
const { data, isLoading } = trpc.formList.useQuery();
There are no TS errors anywhere so the router setup should be correct. However the nodeHTTPRequestHandler function always returns undefined. I've also tried the trpc-nuxt package because apparently that's supposed to work with Nitro but I couldn't get it to work because I'm using trpc v11.
3 replies
TtRPC
Created by alex on 12/21/2022 in #❓-help
Type safety with enabled option
https://tkdodo.eu/blog/react-query-and-type-script#type-safety-with-the-enabled-option What is the best way to solve this with trpc?
function useSessions(userId: number | null) {
trpc.sessions.useQuery({ userId }, { enabled: userId !== null });
...
}
function useSessions(userId: number | null) {
trpc.sessions.useQuery({ userId }, { enabled: userId !== null });
...
}
typescript: Type 'number | null' is not assignable to type 'number'.
19 replies