tRPC-SvelteKit v3.6.2

End-to-end typesafe APIs for your SvelteKit applications

Suggested structure

Here's how you could structure the tRPC-related files in a non-trivial application in order to avoid cyclic dependencies:

--------------
|  lib/trpc  |         👈 place your tRPC-related stuff here
--------------
  |
  |-- context.ts       👈 router context
  |-- t.ts             👈 tRPC definition helper (see below)
  |-- router.ts        👈 main router definition (imports files defined in routes folder)
  |-- middleware.ts    👈 tRPC middleware (this could be split into multiple files if necessary)
  |-- client.ts        👈 define a client helper function here
  |
  |  ------------
  |--|  routes  |      👈 split your routes into multiple files and place them here
  |  ------------
  |    |
  |    |-- route-1.ts
  |    |-- route-2.ts
  |    |-- route-3.ts
  |    |  ...
  |    |-- route-n.ts
  |
  |  ----------------
  |--|  middleware  |  👈 split your middleware into multiple files and place them here
     ----------------
       |
       |-- middleware-1.ts
       |  ...
       |-- middleware-n.ts

The t.ts file contains a simple helper that allows you to build routes and middleware:

lib/trpc/t.ts
import type { Context } from '$lib/trpc/context';
import { initTRPC } from '@trpc/server';
import transformer from 'trpc-transformer';

export const t = initTRPC.context<Context>().create({ transformer });

You'll have to import t.ts in multiple other files such as router.ts, middleware.ts, routes/route-1.ts, routes/route-2.ts, etc.