Popsicle User Agent Middleware

1.0.0 · active · verified Wed Apr 22

popsicle-user-agent is a specialized middleware designed for the Popsicle HTTP client library, facilitating the automatic setting of a `User-Agent` header for outgoing requests. Its current stable version is 1.0.0, an initial release that extracts this functionality from the Popsicle core. This signifies a modular approach to Popsicle's feature set, where specific concerns like user agent management are handled by dedicated packages. This package provides a simple, composable function, `userAgent()`, that can be seamlessly integrated into Popsicle's middleware chain. This allows developers to easily standardize the `User-Agent` string for all requests made through a specific Popsicle instance or a composed middleware stack. The primary differentiator is its tight integration within the Popsicle ecosystem, offering a lightweight and focused solution without introducing external dependencies beyond Popsicle itself and its peer dependency, Servie. Given its highly specific utility, the package is expected to maintain a stable API with infrequent, targeted releases, focusing on reliability rather than rapid feature expansion.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply the `userAgent` middleware to a `Popsicle` middleware stack, showing both the default and a custom User-Agent string being set on outgoing requests before they reach a mock transport layer.

import { userAgent } from 'popsicle-user-agent';
import { compose } from 'servie'; // 'servie' is the peer dep and typically provides 'compose'

// Mock transport function for demonstration.
// In a real application, you would typically import a transport like 'popsicle-transport-http'
// and use it with the actual 'popsicle' client.
const mockTransport = () => async (req: Request): Promise<Response> => {
  console.log(`[Mock Transport] Sending request to: ${req.url}`);
  console.log(`[Mock Transport] User-Agent header: ${req.headers.get('User-Agent')}`);
  // Simulate network delay and return a simple response
  await new Promise(resolve => setTimeout(resolve, 50));
  return new Response('{"message": "Request processed"}', {
    status: 200,
    headers: { 'Content-Type': 'application/json' }
  });
};

async function runExample() {
  // 1. Create a middleware stack with the default User-Agent
  const defaultStack = compose([
    userAgent(), // Defaults to "Popsicle"
    mockTransport()
  ]);

  console.log('--- Request with default User-Agent ---');
  const defaultRequest = new Request('https://api.example.com/data');
  await defaultStack(defaultRequest);

  // 2. Create a middleware stack with a custom User-Agent
  const customStack = compose([
    userAgent('MyAwesomeApp/1.2.3 (Node.js)'),
    mockTransport()
  ]);

  console.log('\n--- Request with custom User-Agent ---');
  const customRequest = new Request('https://api.example.com/status');
  await customStack(customRequest);

  // To integrate with an actual popsicle client (conceptual):
  // import * as popsicle from 'popsicle';
  // const client = popsicle.create({ baseUrl: 'https://api.example.com' }).use(customStack);
  // const response = await client.get('/some-path');
  // console.log('Actual Popsicle Client Response Status:', response.status);
}

runExample().catch(console.error);

view raw JSON →