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-1/

Conditional API Request Manager

Build a request manager that efficiently handles conditional API requests using deferred execution. The system should only execute HTTP requests when their results are actually needed, avoiding unnecessary network calls.

Requirements

Your implementation should provide a function createConditionalRequest that wraps an HTTP request configuration and returns a deferred promise. The HTTP request should not execute immediately upon creation, but only when the promise is consumed (via .then(), .catch(), etc.).

The function should:

  • Accept a request configuration object containing path, method, headers, and entity properties
  • Return a promise-like object that defers HTTP request execution
  • Execute the HTTP request only when the promise is accessed via .then() or similar methods
  • Support standard promise chaining

Request configuration properties:

  • path: The API endpoint URL (required)
  • method: HTTP method like GET, POST, etc. (optional, defaults to GET)
  • headers: HTTP headers object (optional)
  • entity: Request body for POST/PUT requests (optional)

Usage Example

// Create a conditional request that won't execute immediately
const userRequest = createConditionalRequest({
  path: '/api/users/123',
  method: 'GET'
});

// Request executes only when result is needed
userRequest.then(response => {
  console.log('User data:', response.entity);
});

// Chain multiple conditional requests
const orderRequest = createConditionalRequest({
  path: '/api/orders',
  method: 'GET'
});

// Both requests only execute when final result is accessed
userRequest
  .then(user => orderRequest)
  .then(orders => {
    console.log('Orders:', orders.entity);
  });

Test Cases

  • Create a conditional request for path '/api/test' and verify it returns a promise-like object @test
  • Create a conditional request and verify the HTTP request is not executed until .then() is called @test
  • Chain two conditional requests and verify both execute in sequence when the final result is accessed @test
  • Create a conditional POST request with entity data and verify it sends the correct payload @test

Implementation

@generates

API

/**
 * Creates a conditional request that defers execution until accessed
 * @param {object} config - Request configuration
 * @param {string} config.path - API endpoint path
 * @param {string} [config.method='GET'] - HTTP method
 * @param {object} [config.headers] - HTTP headers
 * @param {any} [config.entity] - Request body for POST/PUT
 * @returns {Promise} A promise-like object that defers HTTP request execution
 */
function createConditionalRequest(config);

module.exports = { createConditionalRequest };

Dependencies { .dependencies }

rest { .dependency }

Provides RESTful HTTP client functionality with lazy promise evaluation support.

@satisfied-by

Version

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