or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-3/

Streaming API Client

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.

Requirements

Your client should:

  1. Make HTTP Requests: Fetch JSON data from a given API URL
  2. Progressive Processing: Process individual product items as they arrive in the stream
  3. Collect Results: Accumulate processed results and provide them when the request completes
  4. Error Handling: Properly handle HTTP errors and network failures
  5. Support POST Requests: Allow sending search queries to the API using POST method with custom headers

API

/**
 * 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.

Test Cases

  • Given a URL that returns {"products": [{"id": 1, "name": "Laptop"}]}, calling onProduct processes the laptop object, and onComplete receives an array with one product @test
  • Given a URL that returns {"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 @test
  • Given a URL that returns a 404 error, the onError callback is invoked with error information including the status code @test
  • Given a POST request with body {"query": "laptop"} and header {"Content-Type": "application/json"}, the client sends the POST request with the specified body and headers @test

Implementation

@generates

Dependencies { .dependencies }

oboe { .dependency }

Provides progressive JSON parsing and HTTP request capabilities.