元数据
过程元数据允许你添加可选的过程特定 meta
属性,该属性将在所有 中间件 函数参数中可用。
¥Procedure metadata allows you to add an optional procedure specific meta
property which will be available in all middleware function parameters.
提示
如果你想为应用公开与 REST 兼容的端点,请将元数据与 trpc-openapi
一起使用。
¥Use metadata together with trpc-openapi
if you want to expose REST-compatible endpoints for your application.
使用类型化元数据创建路由
¥Create router with typed metadata
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({// [...]});
每个路由身份验证设置的示例
¥Example with per route authentication settings
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',};}),});
默认元、链接和浅合并
¥Default meta, chaining, and shallow merging
你可以为元类型设置默认值,如果你将元链接到基本过程之上,它将进行浅合并。
¥You can set default values for your meta type, and if you chain meta on top of a base procedure it will be shallow merged.
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' }