Popsicle Status Middleware

3.0.0 · active · verified Wed Apr 22

Popsicle Status is a middleware for the `popsicle` HTTP client library, designed to automatically reject HTTP responses with undesirable status codes. It provides a simple function, `status(min?: number, max?: number)`, which returns a middleware that validates a response's status code against a specified inclusive `min` and exclusive `max` range. By default, it rejects any status outside the 200-399 range (i.e., anything not a 2xx or 3xx success/redirection code). The current stable version is `3.0.0`. This library is actively maintained, with releases primarily aligning with major updates to its peer dependencies, `popsicle` and `servie`, to ensure compatibility. Its key differentiator is its seamless integration into the `popsicle` middleware chain, simplifying error handling by transforming invalid HTTP responses into thrown errors, making control flow clearer than manual status checks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to apply the `status` middleware to a `popsicle` request, configuring it to only allow 2xx responses, and handling both successful and rejected outcomes.

import { status } from 'popsicle-status';
import { Request, Response } from 'popsicle'; // Assuming popsicle types are available

async function main() {
  const statusMiddleware = status(200, 300); // Configure to only allow 2xx responses

  // Mock popsicle's internal `send` function for demonstration
  const mockSend = async (req: Request): Promise<Response> => {
    if (req.url === 'https://example.com/success') {
      return { url: req.url, status: 200, statusText: 'OK', headers: {}, body: 'Success!', clone: () => ({ ...this }) } as Response;
    }
    if (req.url === 'https://example.com/bad-request') {
      return { url: req.url, status: 400, statusText: 'Bad Request', headers: {}, body: 'Bad', clone: () => ({ ...this }) } as Response;
    }
    throw new Error('Unexpected request URL');
  };

  // Create a dummy request object
  const createRequest = (url: string): Request => ({
    url,
    method: 'GET',
    headers: {}, 
    body: null, 
    clone: () => ({ ...this }) 
  } as Request);

  console.log('--- Testing a successful request (200 OK) ---');
  try {
    const reqSuccess = createRequest('https://example.com/success');
    const resSuccess = await statusMiddleware(reqSuccess, mockSend);
    console.log(`Success! Status: ${resSuccess.status}, Body: ${await resSuccess.body}`);
  } catch (err: any) {
    console.error(`Unexpected error for success: ${err.message}`);
  }

  console.log('\n--- Testing a failing request (400 Bad Request) ---');
  try {
    const reqFail = createRequest('https://example.com/bad-request');
    const resFail = await statusMiddleware(reqFail, mockSend); // This should throw due to status filter
    console.log(`Unexpected success for failure: Status: ${resFail.status}`);
  } catch (err: any) {
    console.error(`Caught expected error: ${err.message}`);
    if (err.res) {
      console.log(`Error includes response object. Status: ${err.res.status}`);
    }
  }
}

main();

view raw JSON →