合并路由
将代码中的所有 API 代码写入同一个文件中并不是一个好主意。将路由与其他路由合并很容易。
¥Writing all API-code in your code in the same file is not a great idea. It's easy to merge routers with other routers.
与子路由合并
¥Merging with child routers
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;// @filename: routers/_app.tsimport {router } from '../trpc';import {z } from 'zod';import {userRouter } from './user';import {postRouter } from './post';constappRouter =router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});// You can then access the merged route with// http://localhost:3000/trpc/<NAMESPACE>.<PROCEDURE>export typeAppRouter = typeofappRouter ;// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({create :publicProcedure .input (z .object ({title :z .string (),}),).mutation ((opts ) => {const {input } =opts ;// [...]}),list :publicProcedure .query (() => {// ...return [];}),});// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({list :publicProcedure .query (() => {// [..]return [];}),});
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;// @filename: routers/_app.tsimport {router } from '../trpc';import {z } from 'zod';import {userRouter } from './user';import {postRouter } from './post';constappRouter =router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});// You can then access the merged route with// http://localhost:3000/trpc/<NAMESPACE>.<PROCEDURE>export typeAppRouter = typeofappRouter ;// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({create :publicProcedure .input (z .object ({title :z .string (),}),).mutation ((opts ) => {const {input } =opts ;// [...]}),list :publicProcedure .query (() => {// ...return [];}),});// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({list :publicProcedure .query (() => {// [..]return [];}),});
与 t.mergeRouters
合并
¥Merging with t.mergeRouters
如果你希望将所有过程放在一个名称空间中,则可以使用 t.mergeRouters
¥If you prefer having all procedures flat in one single namespace, you can instead use t.mergeRouters
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;export constmergeRouters =t .mergeRouters ;// @filename: routers/_app.tsimport {router ,publicProcedure ,mergeRouters } from '../trpc';import {z } from 'zod';import {userRouter } from './user';import {postRouter } from './post';constappRouter =mergeRouters (userRouter ,postRouter )export typeAppRouter = typeofappRouter ;// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({postCreate :publicProcedure .input (z .object ({title :z .string (),}),).mutation ((opts ) => {const {input } =opts ;// [...]}),postList :publicProcedure .query (() => {// ...return [];}),});// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({userList :publicProcedure .query (() => {// [..]return [];}),});
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;export constmergeRouters =t .mergeRouters ;// @filename: routers/_app.tsimport {router ,publicProcedure ,mergeRouters } from '../trpc';import {z } from 'zod';import {userRouter } from './user';import {postRouter } from './post';constappRouter =mergeRouters (userRouter ,postRouter )export typeAppRouter = typeofappRouter ;// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({postCreate :publicProcedure .input (z .object ({title :z .string (),}),).mutation ((opts ) => {const {input } =opts ;// [...]}),postList :publicProcedure .query (() => {// ...return [];}),});// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({userList :publicProcedure .query (() => {// [..]return [];}),});