{"library":"node-fetch-har","title":"HAR Logging for Node.js Fetch","description":"node-fetch-har is a Node.js library that provides a wrapper around the popular `node-fetch` package, enabling the automatic generation of HTTP Archive (HAR) logs for server-side HTTP requests. This functionality is crucial for debugging, performance analysis, and capturing detailed network activity on the backend, analogous to a browser's network tab. Currently at version 1.0.1, the library maintains a stable release cycle, having solidified its API after a series of release candidates for its 1.0.0 major version. A key differentiator is its seamless integration with existing `node-fetch` usage, offering options to attach HAR entries directly to response objects, use an `onHarEntry` callback, or collect multiple entries into a single HAR log via `createHarLog`. It supports custom `node-fetch` agents, tracks various request timings (DNS, connect, SSL, send), and handles redirects and request body data, making it a comprehensive tool for server-side request introspection. Unlike browser-based HAR generation, this library is strictly for server environments due to its reliance on Node.js built-in modules and the sensitive nature of data it can capture.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-fetch-har"],"cli":null},"imports":["import { withHar } from 'node-fetch-har';","import { createHarLog } from 'node-fetch-har';","response.harEntry"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { withHar, createHarLog } from 'node-fetch-har';\nimport nodeFetch from 'node-fetch';\n\nasync function recordHarLogs() {\n  const har = createHarLog();\n  const fetch = withHar(nodeFetch, { har });\n\n  try {\n    console.log('Making concurrent fetch requests...');\n    await Promise.all([\n      fetch('https://httpstat.us/200'),\n      fetch('https://httpstat.us/500'),\n      fetch('https://httpstat.us/200?sleep=100')\n    ]);\n    console.log('Requests completed. Generated HAR log:');\n    console.log(JSON.stringify(har, null, 2));\n\n    // Example: Accessing harEntry on a single response\n    const singleResponse = await fetch('https://httpstat.us/200');\n    console.log('\\nSingle response HAR entry:');\n    console.log(JSON.stringify(singleResponse.harEntry, null, 2));\n\n  } catch (error) {\n    console.error('An error occurred during fetch operations:', error);\n  }\n}\n\nrecordHarLogs();","lang":"javascript","description":"Demonstrates wrapping `node-fetch` with `withHar`, creating a HAR log with `createHarLog`, and collecting multiple request entries into it concurrently.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}