riseup-squad20/susconecta/lib/debug-utils.ts

43 lines
1.0 KiB
TypeScript

/**
* Utilitário de debug para requisições HTTP (apenas em desenvolvimento)
*/
export function debugRequest(
method: string,
url: string,
headers: Record<string, string>,
body?: any,
) {
if (process.env.NODE_ENV !== "development") return;
const headersWithoutSensitive = Object.keys(headers).reduce(
(accumulator, key) => {
// Não logar valores sensíveis, apenas nomes
if (
key.toLowerCase().includes("apikey") ||
key.toLowerCase().includes("authorization")
) {
accumulator[key] = "[REDACTED]";
} else {
accumulator[key] = headers[key];
}
return accumulator;
},
{} as Record<string, string>,
);
const bodyShape = body
? Object.keys(typeof body === "string" ? JSON.parse(body) : body)
: [];
console.log("[DEBUG] Request Preview:", {
method,
path: new URL(url).pathname,
query: new URL(url).search,
headerNames: Object.keys(headers),
headers: headersWithoutSensitive,
bodyShape,
timestamp: new Date().toISOString(),
});
}