From 2490e9a21f7cccf3c51ccb06604ac2bb1bdfe368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Afra=20=E2=80=8C?= Date: Tue, 25 Aug 2026 12:21:03 +0330 Subject: [PATCH] feat: logger added --- src/lib/api/winofy.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lib/api/winofy.ts b/src/lib/api/winofy.ts index 942e336..7ccda3c 100644 --- a/src/lib/api/winofy.ts +++ b/src/lib/api/winofy.ts @@ -3,6 +3,7 @@ import { getValidSession } from "@/lib/auth/session"; import { ApiError, type WinofyErrorDetails } from "./errors"; const BASE_URL = process.env.WINOFY_API_BASE_URL!; +const DEBUG = process.env.NODE_ENV !== "production"; interface WinofyFetchOptions extends Omit { /** Attach the caller's bearer token. Defaults to true — set false for public endpoints. */ @@ -18,8 +19,28 @@ export interface PaginatedResponse { results: T[]; } +/** + * Dev-only request/response logging — this is the terminal-equivalent of Chrome's + * Network tab for these calls, since they run server-side and the browser never + * sees them (no CORS on the Winofy API). Prints method/URL/body going out and + * status/body coming back. Never runs in production (checked once via DEBUG, + * and nothing here writes to a file or leaves the process). + */ +function logRequest(method: string, url: string, body: unknown) { + if (!DEBUG) return; + console.log(`\n→ [winofy] ${method} ${url}`); + if (body !== undefined) console.log(" body:", JSON.stringify(body)); +} + +function logResponse(method: string, url: string, status: number, data: unknown) { + if (!DEBUG) return; + console.log(`← [winofy] ${status} ${method} ${url}`); + console.log(" data:", JSON.stringify(data)); +} + export async function winofyFetch(path: string, options: WinofyFetchOptions = {}): Promise { const { auth = true, params, headers, body, ...rest } = options; + const method = rest.method ?? "GET"; const url = new URL(BASE_URL.replace(/\/$/, "") + path); if (params) { @@ -39,6 +60,8 @@ export async function winofyFetch(path: string, options: WinofyFetchOptions = if (session) finalHeaders.set("Authorization", `Bearer ${session.accessToken}`); } + logRequest(method, url.toString(), body); + const res = await fetch(url.toString(), { ...rest, headers: finalHeaders, @@ -46,9 +69,13 @@ export async function winofyFetch(path: string, options: WinofyFetchOptions = cache: "no-store", }); - if (res.status === 204) return undefined as T; + if (res.status === 204) { + logResponse(method, url.toString(), res.status, null); + return undefined as T; + } const data = await res.json().catch(() => null); + logResponse(method, url.toString(), res.status, data); if (!res.ok) { const envelope = data as { status_message?: string; details?: WinofyErrorDetails } | null;