or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-1/

Cross-Origin API Data Fetcher

A utility module that fetches user profile data from a cross-origin API endpoint and processes it progressively as it streams in.

Requirements

Build a module that makes authenticated cross-origin HTTP requests to fetch JSON data containing user profiles. The system should:

  1. Make cross-origin requests with credentials: Configure requests to send authentication cookies and credentials when communicating with the cross-origin API server.

  2. Process streaming data: As user profile objects arrive in the JSON response array, extract and collect the username from each profile.

  3. Handle completion: After all data has been received, return the complete list of usernames.

  4. Handle errors: Properly catch and report any network or parsing errors that occur during the request.

The cross-origin API endpoint returns JSON in this format:

{
  "users": [
    {"id": 1, "username": "alice", "email": "alice@example.com"},
    {"id": 2, "username": "bob", "email": "bob@example.com"}
  ]
}

Implementation

@generates

API

/**
 * Fetches user profiles from a cross-origin API endpoint with credentials.
 *
 * @param {string} url - The API endpoint URL
 * @param {function} onComplete - Callback invoked with array of usernames when complete
 * @param {function} onError - Callback invoked with error if request fails
 */
function fetchUserProfiles(url, onComplete, onError) {
  // Implementation here
}

module.exports = { fetchUserProfiles };

Test Cases

  • Given a URL "https://api.example.com/profiles", when the API returns users with usernames ["alice", "bob", "charlie"], the onComplete callback receives the array ["alice", "bob", "charlie"] @test

  • Given a URL "https://api.example.com/profiles", when the API request fails with a network error, the onError callback is invoked with an error object @test

  • Given a URL "https://api.example.com/profiles", when the API returns an empty users array, the onComplete callback receives an empty array @test

Dependencies { .dependencies }

oboe { .dependency }

Provides progressive JSON streaming and HTTP request capabilities with cross-origin support.

@satisfied-by