Skip to content

How to attach tRPC to an endpoint (+server.ts file)? #105

Answered by anatolzak
camiloux asked this question in Q&A
Discussion options

You must be logged in to vote

Here is how you can use a SvelteKit server endpoint for your tRPC instead of hooks. This actually avoids the use of this package all together.

// routes/trpc/[...trpc]/+server.ts

import { createContext } from '$lib/trpc/context';
import { router } from '$lib/trpc/router';
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import type { RequestHandler } from './$types';

const handler: RequestHandler = (event) =>
	fetchRequestHandler({
		req: event.request,
		router,
		endpoint: '/trpc',
		createContext
	});

export const GET: RequestHandler = handler;
export const POST: RequestHandler = handler;
// context.ts

import type { inferAsyncReturnType } from '@trpc/server';
import 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by icflorescu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #99 on January 23, 2024 09:12.