Popsicle HTTP Client

12.1.2 · active · verified Wed Apr 22

Popsicle is an advanced HTTP client library designed for both Node.js and browser environments, currently stable at version 12.1.2. It provides a fetch-like API, built upon the `Servie` request and response interfaces, offering a universal solution without requiring environment-specific configuration by default. Releases typically involve patch updates for bug fixes and dependency management, with major versions introducing significant architectural changes, such as the `Servie 4` migration in v12.0.0. Key differentiators include its modular middleware architecture, which allows for extensive customization, and its optimized bundles for different environments. Node.js environments benefit from built-in features like User-Agent handling, content encoding decoding, redirect following, and an in-memory cookie cache, while browser builds are lighter, focusing solely on the `XMLHttpRequest` transport layer. This design allows developers to compose functionality and create highly tailored HTTP clients.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates making an HTTP GET request using `fetch` and gracefully handling request abortion via `AbortController`.

import { fetch, AbortController } from "popsicle";

const controller = new AbortController();
const signal = controller.signal;

// Simulate aborting the request after 500ms
setTimeout(() => {
  console.log('Aborting request...');
  controller.abort();
}, 500);

async function makeRequest() {
  try {
    // Replace with a valid API endpoint for testing
    const res = await fetch("https://jsonplaceholder.typicode.com/todos/1", { signal });
    if (!res.ok) {
      throw new Error(`HTTP error! Status: ${res.status}`);
    }
    const data = await res.json();
    console.log("Response data:", data);
  } catch (error: any) {
    if (signal.aborted) {
      console.log(`Request was aborted: ${error.name}`);
    } else {
      console.error(`Request failed unexpectedly: ${error.message}`);
    }
  }
}

makeRequest();

view raw JSON →