Popsicle HTTP Redirects Middleware

1.1.1 · active · verified Wed Apr 22

Popsicle Redirects is a middleware package for the Popsicle HTTP client library, designed to automatically follow HTTP redirects (e.g., 301, 302, 307, 308). It abstracts away the complexity of handling redirect chains, allowing developers to configure the maximum number of redirects and define custom confirmation logic for non-idempotent redirects (307/308). The current stable version is 1.1.1. The project appears to follow an as-needed release cadence for bug fixes and minor improvements, typically tied to the `servie` ecosystem. Its primary differentiator is its integration into the `popsicle` middleware pattern, providing a specific solution for redirect handling within that client.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `popsicle-redirects` into a `popsicle` client to automatically follow HTTP redirects, including basic error handling.

import { request } from 'popsicle';
import { redirects } from 'popsicle-redirects';
import { transport } from 'popsicle';

async function fetchDataWithRedirects() {
  const httpClient = request
    .use(redirects(transport())) // Integrate the redirects middleware
    .use(async (request, next) => {
      // Example of custom middleware or error handling
      try {
        const response = await next(request);
        if (!response.ok) {
          console.error(`HTTP Error: ${response.status} ${response.statusText}`);
        }
        return response;
      } catch (error) {
        console.error('Request failed:', error.message);
        throw error;
      }
    });

  try {
    const response = await httpClient.get('http://example.com/redirect-to-target');
    const data = await response.text();
    console.log('Response data:', data);
  } catch (error) {
    console.error('Failed to fetch:', error.message);
  }
}

fetchDataWithRedirects();

view raw JSON →