{"library":"popsicle-user-agent","title":"Popsicle User Agent Middleware","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install popsicle-user-agent"],"cli":null},"imports":["import { userAgent } from 'popsicle-user-agent';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { userAgent } from 'popsicle-user-agent';\nimport { compose } from 'servie'; // 'servie' is the peer dep and typically provides 'compose'\n\n// Mock transport function for demonstration.\n// In a real application, you would typically import a transport like 'popsicle-transport-http'\n// and use it with the actual 'popsicle' client.\nconst mockTransport = () => async (req: Request): Promise<Response> => {\n  console.log(`[Mock Transport] Sending request to: ${req.url}`);\n  console.log(`[Mock Transport] User-Agent header: ${req.headers.get('User-Agent')}`);\n  // Simulate network delay and return a simple response\n  await new Promise(resolve => setTimeout(resolve, 50));\n  return new Response('{\"message\": \"Request processed\"}', {\n    status: 200,\n    headers: { 'Content-Type': 'application/json' }\n  });\n};\n\nasync function runExample() {\n  // 1. Create a middleware stack with the default User-Agent\n  const defaultStack = compose([\n    userAgent(), // Defaults to \"Popsicle\"\n    mockTransport()\n  ]);\n\n  console.log('--- Request with default User-Agent ---');\n  const defaultRequest = new Request('https://api.example.com/data');\n  await defaultStack(defaultRequest);\n\n  // 2. Create a middleware stack with a custom User-Agent\n  const customStack = compose([\n    userAgent('MyAwesomeApp/1.2.3 (Node.js)'),\n    mockTransport()\n  ]);\n\n  console.log('\\n--- Request with custom User-Agent ---');\n  const customRequest = new Request('https://api.example.com/status');\n  await customStack(customRequest);\n\n  // To integrate with an actual popsicle client (conceptual):\n  // import * as popsicle from 'popsicle';\n  // const client = popsicle.create({ baseUrl: 'https://api.example.com' }).use(customStack);\n  // const response = await client.get('/some-path');\n  // console.log('Actual Popsicle Client Response Status:', response.status);\n}\n\nrunExample().catch(console.error);\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}