{"library":"popsicle-status","title":"Popsicle Status Middleware","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install popsicle-status"],"cli":null},"imports":["import { status } from 'popsicle-status';","import type { Middleware } from 'popsicle';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { status } from 'popsicle-status';\nimport { Request, Response } from 'popsicle'; // Assuming popsicle types are available\n\nasync function main() {\n  const statusMiddleware = status(200, 300); // Configure to only allow 2xx responses\n\n  // Mock popsicle's internal `send` function for demonstration\n  const mockSend = async (req: Request): Promise<Response> => {\n    if (req.url === 'https://example.com/success') {\n      return { url: req.url, status: 200, statusText: 'OK', headers: {}, body: 'Success!', clone: () => ({ ...this }) } as Response;\n    }\n    if (req.url === 'https://example.com/bad-request') {\n      return { url: req.url, status: 400, statusText: 'Bad Request', headers: {}, body: 'Bad', clone: () => ({ ...this }) } as Response;\n    }\n    throw new Error('Unexpected request URL');\n  };\n\n  // Create a dummy request object\n  const createRequest = (url: string): Request => ({\n    url,\n    method: 'GET',\n    headers: {}, \n    body: null, \n    clone: () => ({ ...this }) \n  } as Request);\n\n  console.log('--- Testing a successful request (200 OK) ---');\n  try {\n    const reqSuccess = createRequest('https://example.com/success');\n    const resSuccess = await statusMiddleware(reqSuccess, mockSend);\n    console.log(`Success! Status: ${resSuccess.status}, Body: ${await resSuccess.body}`);\n  } catch (err: any) {\n    console.error(`Unexpected error for success: ${err.message}`);\n  }\n\n  console.log('\\n--- Testing a failing request (400 Bad Request) ---');\n  try {\n    const reqFail = createRequest('https://example.com/bad-request');\n    const resFail = await statusMiddleware(reqFail, mockSend); // This should throw due to status filter\n    console.log(`Unexpected success for failure: Status: ${resFail.status}`);\n  } catch (err: any) {\n    console.error(`Caught expected error: ${err.message}`);\n    if (err.res) {\n      console.log(`Error includes response object. Status: ${err.res.status}`);\n    }\n  }\n}\n\nmain();","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}