ロガーリンク
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
loggerLinkは、tRPCクライアント用のロガーを実装できるリンクです。このリンクを使用すると、クエリ・ミューテーション・サブスクリプションといった操作と、それらのリクエスト・レスポンスをより明確に確認できます。デフォルトではブラウザのコンソールに整形されたログを出力しますが、ログの動作やコンソールへの出力方法を独自実装でカスタマイズ可能です。
使用方法
loggerLinkは次のようにインポートしてlinks配列に追加できます:
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',}),],});
loggerLinkのオプション
loggerLink関数は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;};
参考
このリンクのソースコードはGitHubで確認できます。