evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a streaming API client that fetches and processes product data from an e-commerce API endpoint. The client should progressively handle incoming JSON data without waiting for the complete response.
Your client should:
/**
* Create a streaming API client that fetches and processes JSON data progressively
* @param {string} url - The API endpoint URL
* @param {object} [options] - Optional configuration
* @param {string} [options.method='GET'] - HTTP method (GET, POST, etc.)
* @param {object} [options.body] - Request body for POST/PUT requests
* @param {object} [options.headers] - Custom HTTP headers
* @returns {object} Client instance with methods for handling responses
*/
function createStreamingClient(url, options) {}
module.exports = { createStreamingClient };The returned client instance supports the following methods:
onProduct(callback) - Register a callback to process each product as it arrives. Callback receives (product). Returns the client instance for chaining.onComplete(callback) - Register a callback for when all data is received. Callback receives (allProducts) array. Returns the client instance for chaining.onError(callback) - Register a callback for error handling. Callback receives (error) object. Returns the client instance for chaining.{"products": [{"id": 1, "name": "Laptop"}]}, calling onProduct processes the laptop object, and onComplete receives an array with one product @test{"products": [{"id": 1, "name": "Phone"}, {"id": 2, "name": "Tablet"}]}, calling onProduct is invoked twice (once per product), and onComplete receives an array with both products @testonError callback is invoked with error information including the status code @test{"query": "laptop"} and header {"Content-Type": "application/json"}, the client sends the POST request with the specified body and headers @testProvides progressive JSON parsing and HTTP request capabilities.