Request-Promise Middleware Framework

3.0.6 · active · verified Sun Apr 19

This package provides a framework for intercepting and modifying HTTP requests and responses made using the `request-promise` HTTP client. It enables developers to define middleware functions that can execute custom logic before an HTTP call, modify request options, or process responses. The current stable version is 3.0.6. The release cadence appears to be irregular, driven primarily by dependency updates and security fixes rather than new feature additions. A key differentiator is its explicit handling of `resolveWithFullResponse`, defaulting it to `true` within the middleware pipeline to ensure full response access for all middleware components, diverging slightly from `request-promise`'s default. This framework is specifically designed for `request-promise` and does not support other HTTP clients, serving as a dedicated extensibility layer for that ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and register two types of middleware: one for logging request/response details and another for completely short-circuiting an HTTP call based on the URL. It then shows how to obtain and use the middleware-enabled `request-promise` instance.

import RequestPromiseMiddlewareFramework from 'request-promise-middleware-framework';
import rpBase from 'request-promise';

// Define a simple logging middleware
function loggingMiddleware(options, callback, next) {
  console.log(`[Middleware] Requesting: ${options.uri || options.url}`);
  const _callback = (err, response, body) => {
    if (err) {
      console.error(`[Middleware] Error for ${options.uri || options.url}:`, err.message);
    } else {
      console.log(`[Middleware] Received response for ${options.uri || options.url} with status: ${response ? response.statusCode : 'N/A'}`);
    }
    callback(err, response, body);
  };
  next(options, _callback);
}

// Define a short-circuiting middleware example
function shortCircuitMiddleware(options, callback, next) {
  if (options.uri === 'http://example.com/short-circuit') {
    console.log('[Middleware] Short-circuiting request to http://example.com/short-circuit');
    const mockBody = { message: 'Short-circuited by middleware' };
    const mockResponse = { statusCode: 200, body: mockBody, headers: { 'content-type': 'application/json' } };
    callback(null, mockResponse, mockBody);
  } else {
    next(options, callback);
  }
}

// Initialize the framework with request-promise and our middleware
const rpMiddlewareFramework = new RequestPromiseMiddlewareFramework(
  { rp: rpBase },
  [loggingMiddleware, shortCircuitMiddleware]
);

// Get the middleware-enabled request-promise instance
const rp = rpMiddlewareFramework.getMiddlewareEnabledRequestPromise();

// Use the new rp instance
async function makeRequests() {
  try {
    // Request that goes through the network
    const result1 = await rp('http://httpbin.org/get');
    console.log('Result 1 (full response body snippet):', result1.slice(0, 100), '...');

    // Request that is short-circuited
    const result2 = await rp('http://example.com/short-circuit');
    console.log('Result 2 (short-circuited):', result2);
  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

makeRequests();

view raw JSON →