feat: add trpc logger

This commit is contained in:
moonrailgun 2023-10-09 00:29:37 +08:00
parent 3bd7266dcf
commit 6723b9f3da
2 changed files with 8 additions and 1 deletions

View File

@ -1,9 +1,10 @@
import { createTRPCReact } from '@trpc/react-query'; import { createTRPCReact } from '@trpc/react-query';
import type { AppRouter } from '../../server/trpc/routers'; import type { AppRouter } from '../../server/trpc/routers';
import { httpBatchLink, TRPCClientErrorLike } from '@trpc/client'; import { httpBatchLink, loggerLink, TRPCClientErrorLike } from '@trpc/client';
import { getJWT } from './auth'; import { getJWT } from './auth';
import { message } from 'antd'; import { message } from 'antd';
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server'; import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
import { isDev } from '../utils/env';
export const trpc = createTRPCReact<AppRouter>(); export const trpc = createTRPCReact<AppRouter>();
@ -12,6 +13,11 @@ export type RouterOutput = inferRouterOutputs<AppRouter>;
export const trpcClient = trpc.createClient({ export const trpcClient = trpc.createClient({
links: [ links: [
loggerLink({
enabled: (opts) =>
(isDev && typeof window !== 'undefined') ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({ httpBatchLink({
url: '/trpc', url: '/trpc',
async headers() { async headers() {

1
src/client/utils/env.ts Normal file
View File

@ -0,0 +1 @@
export const isDev = import.meta.env.MODE === 'development';