CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-rest

tessl install tessl/npm-rest@2.0.0

RESTful HTTP client library with composable interceptor architecture for Node.js and browsers

Agent Success

Agent success rate when using this tile

76%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.29x

Baseline

Agent success rate without this tile

59%

task.mdevals/scenario-7/

API Response Formatter Service

Build a service that fetches data from a backend API and converts the response to different output formats. The service should leverage automatic content type handling for both request and response processing.

Requirements

Create a function that:

  1. Accepts a resource URL and optional output format ('json', 'form', or 'text')
  2. Makes an HTTP request with appropriate Accept headers for content negotiation
  3. Automatically deserializes the response based on its Content-Type header
  4. Serializes the data to the requested output format
  5. Returns both the formatted data and its content type

Output Format Mappings:

  • 'json'application/json
  • 'form'application/x-www-form-urlencoded
  • 'text'text/plain

Test Cases

  • When requesting a JSON resource without specifying output format, it returns the data as a JSON string @test
  • When requesting a resource with outputFormat='form', it returns URL-encoded form data @test
  • When requesting a resource with outputFormat='text', it returns plain text @test
  • When the server returns JSON data, it is automatically deserialized before formatting @test

Implementation

@generates

API

/**
 * Fetches a resource and formats it according to the specified output format.
 *
 * @param {string} resourceUrl - The URL of the resource to fetch
 * @param {string} [outputFormat='json'] - The desired output format: 'json', 'form', or 'text'
 * @returns {Promise<{data: string, contentType: string}>} The formatted response
 */
async function fetchAndFormat(resourceUrl, outputFormat = 'json') {
  // IMPLEMENTATION HERE
}

module.exports = {
  fetchAndFormat
};

Dependencies { .dependencies }

rest { .dependency }

Provides HTTP client functionality with automatic content negotiation and MIME type handling.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/rest@2.0.x
tile.json