RESTful HTTP client library with composable interceptor architecture for Node.js and browsers
76
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.
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:
.then() or similar methodsRequest 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)// 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);
});.then() is called @test@generates
/**
* 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 };Provides RESTful HTTP client functionality with lazy promise evaluation support.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-restevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10