From faae6ac0f67469219ebf0258d9856abf53e08207 Mon Sep 17 00:00:00 2001 From: Michael Freno Date: Fri, 19 Dec 2025 17:06:08 -0500 Subject: [PATCH] remove console logging in prod --- src/lib/api.ts | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index aec4088..71675bc 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -12,11 +12,40 @@ const getBaseUrl = () => { return `http://localhost:${process.env.PORT ?? 3000}`; }; +// Custom fetch that suppresses 401 console errors +const customFetch: typeof fetch = async (input, init?) => { + try { + const response = await fetch(input, init); + // Suppress logging for 401 errors by cloning and not throwing + if (response.status === 401) { + // Return the response without logging to console + return response; + } + return response; + } catch (error) { + // Only re-throw non-401 errors + throw error; + } +}; + export const api = createTRPCProxyClient({ links: [ - // will print out helpful logs when using client - loggerLink(), + // will print out helpful logs when using client (suppress 401 errors) + loggerLink({ + enabled: (opts) => { + // Only log in development, and suppress 401 errors + const isDev = process.env.NODE_ENV === "development"; + const is401 = + opts.direction === "down" && + opts.result instanceof Error && + opts.result.message?.includes("UNAUTHORIZED"); + return isDev && !is401; + } + }), // identifies what url will handle trpc requests - httpBatchLink({ url: `${getBaseUrl()}/api/trpc` }) + httpBatchLink({ + url: `${getBaseUrl()}/api/trpc`, + fetch: customFetch + }) ] });