合并路由器
在同一文件中编写所有 API 代码并不是一个好主意。可以轻松地将路由器与其他路由器合并。
定义内联子路由器
定义内联子路由器时,可以将路由器表示为一个普通对象。
在以下示例中,nested1
和 nested2
相等
server/_app.tsts
import * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';constappRouter =router ({// Shorthand plain object for creating a sub-routernested1 : {proc :publicProcedure .query (() => '...'),},// Equivalent of:nested2 :router ({proc :publicProcedure .query (() => '...'),}),});
server/_app.tsts
import * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';constappRouter =router ({// Shorthand plain object for creating a sub-routernested1 : {proc :publicProcedure .query (() => '...'),},// Equivalent of:nested2 :router ({proc :publicProcedure .query (() => '...'),}),});
与子路由器合并
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
合并
如果您希望将所有过程平铺在一个命名空间中,则可以使用 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 [];}),});