Planck HTTP Fetch

1.7.4 · active · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import { Fetch } from "planck-http-fetch";

async function makeRequest() {
  // Using process.env for sensitive data in a real application
  const apiKey = process.env.API_KEY ?? 'your-default-api-key'; // Replace with a real API key or secure retrieval method
  const username = process.env.BASIC_AUTH_USER ?? 'testuser';
  const password = process.env.BASIC_AUTH_PASS ?? 'testpass';
  const targetUrl = "https://jsonplaceholder.typicode.com/posts"; // Using a public API for demonstration

  try {
    console.log("Making a POST request...");
    // Example POST request with JSON data
    const postData = {
      title: 'foo',
      body: 'bar',
      userId: 1,
    };
    const postResponse = await new Fetch(targetUrl)
      .timeout(8000) // Set an 8-second timeout for the request
      .head("Authorization", `Bearer ${apiKey}`) // Example of setting an Authorization header
      .head("X-Custom-App", "PlanckExample")
      .fetch(JSON.stringify(postData), "application/json");

    console.log("POST Response Status:", postResponse.status);
    console.log("POST Response Body (first 100 chars):");
    console.log(postResponse.data.toString().substring(0, 100));

    console.log("\nMaking a GET request...");
    // Example GET request (no data provided to .fetch(), so it defaults to GET)
    const getResponse = await new Fetch(`${targetUrl}/1`)
      .basicAuth(username, password) // Apply basic authentication
      .fetch();

    console.log("GET Response Status:", getResponse.status);
    console.log("GET Response Body (first 100 chars):");
    console.log(getResponse.data.toString().substring(0, 100));

    // Example of skipping SSL cert check (CAUTION: generally not recommended for production)
    // const insecureResponse = await new Fetch("https://self-signed.example.com")
    //   .unauthorized() // Skips certificate validation
    //   .fetch();
    // console.log("Insecure Fetch Response:", insecureResponse.data.toString());

  } catch (error) {
    console.error("An error occurred during fetch:", error instanceof Error ? error.message : String(error));
  }
}

makeRequest();

view raw JSON →