{"library":"request-promise-middleware-framework","title":"Request-Promise Middleware Framework","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install request-promise-middleware-framework"],"cli":null},"imports":["import RequestPromiseMiddlewareFramework from 'request-promise-middleware-framework';","const RequestPromiseMiddlewareFramework = require('request-promise-middleware-framework');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import RequestPromiseMiddlewareFramework from 'request-promise-middleware-framework';\nimport rpBase from 'request-promise';\n\n// Define a simple logging middleware\nfunction loggingMiddleware(options, callback, next) {\n  console.log(`[Middleware] Requesting: ${options.uri || options.url}`);\n  const _callback = (err, response, body) => {\n    if (err) {\n      console.error(`[Middleware] Error for ${options.uri || options.url}:`, err.message);\n    } else {\n      console.log(`[Middleware] Received response for ${options.uri || options.url} with status: ${response ? response.statusCode : 'N/A'}`);\n    }\n    callback(err, response, body);\n  };\n  next(options, _callback);\n}\n\n// Define a short-circuiting middleware example\nfunction shortCircuitMiddleware(options, callback, next) {\n  if (options.uri === 'http://example.com/short-circuit') {\n    console.log('[Middleware] Short-circuiting request to http://example.com/short-circuit');\n    const mockBody = { message: 'Short-circuited by middleware' };\n    const mockResponse = { statusCode: 200, body: mockBody, headers: { 'content-type': 'application/json' } };\n    callback(null, mockResponse, mockBody);\n  } else {\n    next(options, callback);\n  }\n}\n\n// Initialize the framework with request-promise and our middleware\nconst rpMiddlewareFramework = new RequestPromiseMiddlewareFramework(\n  { rp: rpBase },\n  [loggingMiddleware, shortCircuitMiddleware]\n);\n\n// Get the middleware-enabled request-promise instance\nconst rp = rpMiddlewareFramework.getMiddlewareEnabledRequestPromise();\n\n// Use the new rp instance\nasync function makeRequests() {\n  try {\n    // Request that goes through the network\n    const result1 = await rp('http://httpbin.org/get');\n    console.log('Result 1 (full response body snippet):', result1.slice(0, 100), '...');\n\n    // Request that is short-circuited\n    const result2 = await rp('http://example.com/short-circuit');\n    console.log('Result 2 (short-circuited):', result2);\n  } catch (error) {\n    console.error('An error occurred:', error.message);\n  }\n}\n\nmakeRequests();","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}