元数据
过程元数据允许您添加一个可选的过程特定 meta
属性,该属性将在所有 中间件 函数参数中可用。
提示
如果您想为您的应用程序公开 REST 兼容的端点,请将元数据与 trpc-openapi
结合使用。
使用类型化元数据创建路由器
tsx
import { initTRPC } from '@trpc/server';// [...]interface Meta {authRequired: boolean;}export const t = initTRPC.context<Context>().meta<Meta>().create();export const appRouter = t.router({// [...]});
tsx
import { initTRPC } from '@trpc/server';// [...]interface Meta {authRequired: boolean;}export const t = initTRPC.context<Context>().meta<Meta>().create();export const appRouter = t.router({// [...]});
每个路由身份验证设置的示例
server.tstsx
import { initTRPC } from '@trpc/server';// [...]interface Meta {authRequired: boolean;}export const t = initTRPC.context<Context>().meta<Meta>().create();export const authedProcedure = t.procedure.use(async (opts) => {const { meta, next, ctx } = opts;// only check authorization if enabledif (meta?.authRequired && !ctx.user) {throw new TRPCError({ code: 'UNAUTHORIZED' });}return next();});export const appRouter = t.router({hello: authedProcedure.meta({ authRequired: false }).query(() => {return {greeting: 'hello world',};}),protectedHello: authedProcedure.meta({ authRequired: true }).query(() => {return {greeting: 'hello-world',};}),});
server.tstsx
import { initTRPC } from '@trpc/server';// [...]interface Meta {authRequired: boolean;}export const t = initTRPC.context<Context>().meta<Meta>().create();export const authedProcedure = t.procedure.use(async (opts) => {const { meta, next, ctx } = opts;// only check authorization if enabledif (meta?.authRequired && !ctx.user) {throw new TRPCError({ code: 'UNAUTHORIZED' });}return next();});export const appRouter = t.router({hello: authedProcedure.meta({ authRequired: false }).query(() => {return {greeting: 'hello world',};}),protectedHello: authedProcedure.meta({ authRequired: true }).query(() => {return {greeting: 'hello-world',};}),});
默认元数据、链接和浅层合并
您可以为您的元数据类型设置默认值,如果您在基本过程之上链接元数据,它将被浅层合并。
tsx
import { initTRPC } from '@trpc/server';interface Meta {authRequired: boolean;role?: 'user' | 'admin'}export const t = initTRPC.context<Context>().meta<Meta>().create({// Set a default valuedefaultMeta: { authRequired: false }});const publicProcedure = t.procedure// ^ Default Meta: { authRequired: false }const authProcedure = publicProcedure.use(authMiddleware).meta({authRequired: true;role: 'user'});// ^ Meta: { authRequired: true, role: 'user' }const adminProcedure = authProcedure.meta({role: 'admin'});// ^ Meta: { authRequired: true, role: 'admin' }
tsx
import { initTRPC } from '@trpc/server';interface Meta {authRequired: boolean;role?: 'user' | 'admin'}export const t = initTRPC.context<Context>().meta<Meta>().create({// Set a default valuedefaultMeta: { authRequired: false }});const publicProcedure = t.procedure// ^ Default Meta: { authRequired: false }const authProcedure = publicProcedure.use(authMiddleware).meta({authRequired: true;role: 'user'});// ^ Meta: { authRequired: true, role: 'user' }const adminProcedure = authProcedure.meta({role: 'admin'});// ^ Meta: { authRequired: true, role: 'admin' }