Enlace de Registro
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
loggerLink es un enlace que te permite implementar un sistema de registro para tu cliente tRPC. Te ayuda a identificar claramente qué operaciones son queries, mutations o subscriptions, mostrando tanto las solicitudes como las respuestas. Por defecto, este enlace imprime registros formateados en la consola del navegador. Sin embargo, puedes personalizar completamente el comportamiento del registro y cómo se muestra en consola con tus propias implementaciones.
Uso
Puedes importar y agregar el loggerLink al array de links de esta manera:
client/index.tstsimport {createTRPCClient ,httpBatchLink ,loggerLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [/*** The function passed to enabled is an example in case you want to the link to* log to your console in development and only log errors in production*/loggerLink ({enabled : (opts ) =>(process .env .NODE_ENV === 'development' &&typeofwindow !== 'undefined') ||(opts .direction === 'down' &&opts .result instanceofError ),}),httpBatchLink ({url : 'http://localhost:3000',}),],});
client/index.tstsimport {createTRPCClient ,httpBatchLink ,loggerLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [/*** The function passed to enabled is an example in case you want to the link to* log to your console in development and only log errors in production*/loggerLink ({enabled : (opts ) =>(process .env .NODE_ENV === 'development' &&typeofwindow !== 'undefined') ||(opts .direction === 'down' &&opts .result instanceofError ),}),httpBatchLink ({url : 'http://localhost:3000',}),],});
Opciones de loggerLink
La función loggerLink recibe un objeto de opciones con la estructura de LoggerLinkOptions:
tstypeLoggerLinkOptions <TRouter extendsAnyRouter > = {logger ?:LogFn <TRouter >;/*** It is a function that returns a condition that determines whether to enable the logger.* It is true by default.*/enabled ?:EnabledFn <TRouter >;/*** Used in the built-in defaultLogger*/console ?:ConsoleEsque ;/*** Color mode used in the default logger.* @default typeof window === 'undefined' ? 'ansi' : 'css'*/colorMode ?: 'ansi' | 'css' | 'none';/*** Include context in the log - defaults to false unless colorMode is 'css'*/withContext ?: boolean;};
tstypeLoggerLinkOptions <TRouter extendsAnyRouter > = {logger ?:LogFn <TRouter >;/*** It is a function that returns a condition that determines whether to enable the logger.* It is true by default.*/enabled ?:EnabledFn <TRouter >;/*** Used in the built-in defaultLogger*/console ?:ConsoleEsque ;/*** Color mode used in the default logger.* @default typeof window === 'undefined' ? 'ansi' : 'css'*/colorMode ?: 'ansi' | 'css' | 'none';/*** Include context in the log - defaults to false unless colorMode is 'css'*/withContext ?: boolean;};
Referencia
Puedes consultar el código fuente de este enlace en GitHub.