-
First of all, thank you for this amazing project! My question is as follows: I want to know is there's a way to use trpc-sveltekit to expose trpc functions to external apps. My guess is that it has to be attached to an endpoint /trpc/+server.ts, but I can't find an example of how to achieve this nor my way around it. Thank you in advanced for your valuable help! |
Beta Was this translation helpful? Give feedback.
Answered by
anatolzak
Jan 23, 2024
Replies: 1 comment
-
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 type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch';
export async function createContext({ req, resHeaders }: FetchCreateContextFnOptions) {
return {};
}
export type Context = inferAsyncReturnType<typeof createContext>; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
icflorescu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.