{"id":17330,"library":"planck-http-fetch","title":"Planck HTTP Fetch","description":"Planck HTTP Fetch (current version 1.7.4) is a lightweight, promise-based library for making HTTP and HTTPS requests within Node.js environments. It offers a fluent, chainable API that simplifies the configuration of requests, allowing developers to easily set parameters such as timeouts, custom headers, and basic authentication details. A notable feature is its automatic handling of HTTP 307 temporary redirects, which was introduced in version 1.7.2, enhancing its robustness for web interactions. The library implicitly determines the HTTP method (GET or POST) based on the presence of a request body, streamlining its usage for common data exchange patterns. It ships with comprehensive TypeScript type definitions, providing strong type checking and IDE support. While a specific release cadence isn't detailed, the package appears to be actively maintained, focusing on a straightforward and developer-friendly approach to network requests, differentiating itself through its minimalist API and direct control over core fetch functionalities.","status":"active","version":"1.7.4","language":"javascript","source_language":"en","source_url":"https://github.com/pankleks/planck-http-fetch","tags":["javascript","http","https","fetch","typescript"],"install":[{"cmd":"npm install planck-http-fetch","lang":"bash","label":"npm"},{"cmd":"yarn add planck-http-fetch","lang":"bash","label":"yarn"},{"cmd":"pnpm add planck-http-fetch","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Planck HTTP Fetch is primarily designed for ES Modules. Using CommonJS 'require' syntax may lead to runtime errors, especially in newer Node.js environments or when using bundlers that expect ESM.","wrong":"const { Fetch } = require('planck-http-fetch');","symbol":"Fetch","correct":"import { Fetch } from 'planck-http-fetch';"}],"quickstart":{"code":"import { Fetch } from \"planck-http-fetch\";\n\nasync function makeRequest() {\n  // Using process.env for sensitive data in a real application\n  const apiKey = process.env.API_KEY ?? 'your-default-api-key'; // Replace with a real API key or secure retrieval method\n  const username = process.env.BASIC_AUTH_USER ?? 'testuser';\n  const password = process.env.BASIC_AUTH_PASS ?? 'testpass';\n  const targetUrl = \"https://jsonplaceholder.typicode.com/posts\"; // Using a public API for demonstration\n\n  try {\n    console.log(\"Making a POST request...\");\n    // Example POST request with JSON data\n    const postData = {\n      title: 'foo',\n      body: 'bar',\n      userId: 1,\n    };\n    const postResponse = await new Fetch(targetUrl)\n      .timeout(8000) // Set an 8-second timeout for the request\n      .head(\"Authorization\", `Bearer ${apiKey}`) // Example of setting an Authorization header\n      .head(\"X-Custom-App\", \"PlanckExample\")\n      .fetch(JSON.stringify(postData), \"application/json\");\n\n    console.log(\"POST Response Status:\", postResponse.status);\n    console.log(\"POST Response Body (first 100 chars):\");\n    console.log(postResponse.data.toString().substring(0, 100));\n\n    console.log(\"\\nMaking a GET request...\");\n    // Example GET request (no data provided to .fetch(), so it defaults to GET)\n    const getResponse = await new Fetch(`${targetUrl}/1`)\n      .basicAuth(username, password) // Apply basic authentication\n      .fetch();\n\n    console.log(\"GET Response Status:\", getResponse.status);\n    console.log(\"GET Response Body (first 100 chars):\");\n    console.log(getResponse.data.toString().substring(0, 100));\n\n    // Example of skipping SSL cert check (CAUTION: generally not recommended for production)\n    // const insecureResponse = await new Fetch(\"https://self-signed.example.com\")\n    //   .unauthorized() // Skips certificate validation\n    //   .fetch();\n    // console.log(\"Insecure Fetch Response:\", insecureResponse.data.toString());\n\n  } catch (error) {\n    console.error(\"An error occurred during fetch:\", error instanceof Error ? error.message : String(error));\n  }\n}\n\nmakeRequest();","lang":"typescript","description":"This quickstart demonstrates how to perform both POST and GET requests using `planck-http-fetch`. It shows how to chain methods for setting timeouts, custom headers, and basic authentication, and how the library implicitly determines the HTTP method based on the presence of a request body."},"warnings":[{"fix":"Always explicitly specify the `contentType` parameter in `.fetch(data, contentType)` when sending non-JSON data, or ensure your server is configured to handle `application/json` by default.","message":"When sending data, if no `contentType` is explicitly provided to the `.fetch()` method, it defaults to `application/json;charset=utf-8`. This might lead to unexpected server responses if your API expects a different default.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Avoid using `.unauthorized()` in production environments. Ensure your HTTPS certificates are properly issued and trusted by your system's root CAs. If custom CAs are needed, use Node.js environment variables like `NODE_EXTRA_CA_CERTS`.","message":"The `.unauthorized()` method disables SSL certificate validation for HTTPS requests. While this can be useful for testing against self-signed certificates or specific proxy setups, using it in production with untrusted endpoints exposes your application to Man-in-the-Middle attacks and is a significant security risk.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be mindful of the `data` parameter. If you explicitly need a GET request, ensure `data` is omitted or `undefined`. For POST, always provide `data`. The library does not currently offer an explicit method to force GET or POST if data is present.","message":"The HTTP method (GET or POST) is implicitly determined by the presence of the `data` argument in the `.fetch(data, contentType)` call. If `data` is provided, the request will be POST; otherwise, it will be GET. This implicit behavior can sometimes lead to confusion or unintended request types.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured for ES Modules (e.g., `\"type\": \"module\"` in `package.json`) and use `import { Fetch } from 'planck-http-fetch';`. If you must use CommonJS, consider using dynamic import `import('planck-http-fetch').then(m => new m.Fetch())` (requires Node.js >=13).","cause":"Attempting to use CommonJS `require` syntax or incorrect destructuring with an ES Module package. This library exports named `Fetch`.","error":"TypeError: Fetch is not a constructor"},{"fix":"Increase the timeout duration using `.timeout(milliseconds)` in your chain (e.g., `.timeout(15000)` for 15 seconds). Verify the target URL is correct, the server is running, and there are no network issues (e.g., firewall, proxy, DNS resolution) blocking the connection from your environment.","cause":"The HTTP request could not establish a connection within the specified timeout duration, or the remote server is unreachable/unresponsive.","error":"Error: connect ETIMEDOUT"}],"ecosystem":"npm","meta_description":null}