evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
A utility module that fetches user profile data from a cross-origin API endpoint and processes it progressively as it streams in.
Build a module that makes authenticated cross-origin HTTP requests to fetch JSON data containing user profiles. The system should:
Make cross-origin requests with credentials: Configure requests to send authentication cookies and credentials when communicating with the cross-origin API server.
Process streaming data: As user profile objects arrive in the JSON response array, extract and collect the username from each profile.
Handle completion: After all data has been received, return the complete list of usernames.
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"}
]
}/**
* 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 };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
Provides progressive JSON streaming and HTTP request capabilities with cross-origin support.