Metadatos de Ruta
Traducción Beta No Oficial
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Los metadatos de ruta te permiten agregar una propiedad meta opcional específica para cada ruta, que estará disponible en todos los parámetros de las funciones middleware.
Crear un enrutador con metadatos tipados
jsximport * as trpc from '@trpc/server';// [...]interface Meta {hasAuth: boolean}export const appRouter = trpc.router<Context, Meta>();
jsximport * as trpc from '@trpc/server';// [...]interface Meta {hasAuth: boolean}export const appRouter = trpc.router<Context, Meta>();
Ejemplo con configuraciones de autenticación por ruta
server.tstsximport * as trpc from '@trpc/server';// [...]interface Meta {hasAuth: boolean;}export const appRouter = trpc.router<Context, Meta>().middleware(async ({ meta, next, ctx }) => {// only check authorization if enabledif (meta?.hasAuth && !ctx.user) {throw new TRPCError({ code: 'UNAUTHORIZED' });}return next();}).query('hello', {meta: {hasAuth: false,},resolve({ ctx }) {return {greeting: `hello world`,};},}).query('protected-hello', {meta: {hasAuth: true,},resolve({ ctx }) {return {greeting: `hello world`,};},});
server.tstsximport * as trpc from '@trpc/server';// [...]interface Meta {hasAuth: boolean;}export const appRouter = trpc.router<Context, Meta>().middleware(async ({ meta, next, ctx }) => {// only check authorization if enabledif (meta?.hasAuth && !ctx.user) {throw new TRPCError({ code: 'UNAUTHORIZED' });}return next();}).query('hello', {meta: {hasAuth: false,},resolve({ ctx }) {return {greeting: `hello world`,};},}).query('protected-hello', {meta: {hasAuth: true,},resolve({ ctx }) {return {greeting: `hello world`,};},});