{"id":11939,"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.","status":"active","version":"3.0.6","language":"javascript","source_language":"en","source_url":"https://github.com/Cimpress-MCP/request-promise-middleware-framework","tags":["javascript","request-middleware","request-promise"],"install":[{"cmd":"npm install request-promise-middleware-framework","lang":"bash","label":"npm"},{"cmd":"yarn add request-promise-middleware-framework","lang":"bash","label":"yarn"},{"cmd":"pnpm add request-promise-middleware-framework","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This is the core HTTP client that the middleware framework intercepts.","package":"request-promise","optional":false},{"reason":"Optional dependency for using an alternate Promise library instead of native Promises.","package":"bluebird","optional":true}],"imports":[{"note":"While examples show `require()`, the `package.json` specifies `type: commonjs` but also a `main` entry that could be consumed by bundlers. For modern Node.js and TypeScript, an ES module import is preferred if transpiled or configured correctly. The package's ES6 syntax in v2.0.0 suggests it's designed for modern JS environments.","wrong":"const RequestPromiseMiddlewareFramework = require('request-promise-middleware-framework');","symbol":"RequestPromiseMiddlewareFramework","correct":"import RequestPromiseMiddlewareFramework from 'request-promise-middleware-framework';"},{"note":"This is the common CommonJS import style shown in the documentation and examples.","symbol":"RequestPromiseMiddlewareFramework (CJS)","correct":"const RequestPromiseMiddlewareFramework = require('request-promise-middleware-framework');"}],"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."},"warnings":[{"fix":"If you need `bluebird`, pass it during initialization: `new RequestPromiseMiddlewareFramework({ rp: require(\"request-promise\"), PromiseDependency: require(\"bluebird\") }, middleware);`. Otherwise, ensure your code is compatible with native Promises.","message":"Version 3.0.0 removed the dependency on `bluebird` and now utilizes native Promises by default. If your application relied on `bluebird`'s specific features or custom Promise extensions, you might need to adjust your code or explicitly provide `bluebird` to the framework initialization.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your request options explicitly set `resolveWithFullResponse: false` if you absolutely require the raw body instead of the full response object, or update your code to handle the full response object.","message":"Version 1.0.0 changed the default behavior for `resolveWithFullResponse` to `true`. While `request-promise` typically defaults this to `false`, this framework forces it to `true` internally for middleware consistency. If your code explicitly expected `resolveWithFullResponse: false` by default and was not setting it, this change would affect the shape of the returned value.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Middleware functions should always be prepared to receive a full response object (`response` and `body` parameters) regardless of the `resolveWithFullResponse` setting on the `rp` call. If you need to return only the body to the consumer, you must extract it explicitly in your final middleware or after the `rp` call.","message":"The framework's default for `resolveWithFullResponse` is `true` within the middleware pipeline, overriding `request-promise`'s default. While you can set `resolveWithFullResponse: false` on an individual `rp` invocation, the middleware pipeline itself will internally always operate with the full response object for consistency, potentially leading to unexpected behavior if middleware expects only the body.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Regularly update the package to the latest patch version (e.g., `npm install request-promise-middleware-framework@latest`) to incorporate security fixes and dependency updates.","message":"Security vulnerabilities are periodically patched (e.g., in v3.0.6, v3.0.5, v3.0.3) often related to underlying dependencies like `eslint-utils` or other core packages. Failing to update to the latest patch versions can expose applications to known CVEs.","severity":"breaking","affected_versions":"<3.0.6"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you correctly `require` or `import` the `RequestPromiseMiddlewareFramework` and instantiate it with `new RequestPromiseMiddlewareFramework(...)` before calling `getMiddlewareEnabledRequestPromise()`.","cause":"The `RequestPromiseMiddlewareFramework` constructor or instance was not correctly created or imported, or you're calling the method on a variable that isn't the framework instance.","error":"TypeError: rpMiddlewareFramework.getMiddlewareEnabledRequestPromise is not a function"},{"fix":"Install `request-promise` using npm: `npm install request-promise`.","cause":"The `request-promise` package is a required peer/runtime dependency that must be installed separately.","error":"Error: Cannot find module 'request-promise'"},{"fix":"Always add a `.catch()` block to your promise chains (e.g., `rp(...).then(...).catch(err => console.error(err))`) or use `try...catch` with `await` in `async` functions to handle potential errors.","cause":"A promise returned by `request-promise-middleware-framework` (or `request-promise`) was rejected, but the rejection was not caught by a `.catch()` block or `try...catch` in an `async` function.","error":"UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: X): Error: ..."}],"ecosystem":"npm"}