-
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.
Replies: 2 comments
-
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.
-
HOw to distingush GET POST PUT DELETE routes? We use the same declaration: |
Beta Was this translation helpful? Give feedback.
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.