Composable HTTP Client
request-compose is a lightweight, dependency-free HTTP client for Node.js, built around the paradigm of function composition. It empowers developers to construct highly customized HTTP request and response pipelines by chaining together small, single-purpose middleware functions. The library's core philosophy emphasizes zero external dependencies, minimal abstraction, and stateless operation, offering a flexible and performant foundation for various HTTP interaction patterns. Its current stable version is 2.1.7. While a specific release cadence is not explicitly defined, the package appears actively maintained given its npm version and GitHub activity. Key differentiators include its extreme modularity, functional programming approach, and a footprint that includes no external runtime dependencies, contrasting with more opinionated HTTP clients by providing granular control over every step of the request-response lifecycle without imposing a rigid structure.
Common errors
-
TypeError: (0 , _requestCompose.Request) is not a function
cause Attempting to import `Request` or `Response` as named exports from 'request-compose' when they are properties of the default export.fixUse `import compose from 'request-compose'; const { Request, Response } = compose;` (ESM) or `const compose = require('request-compose'); const { Request, Response } = compose;` (CommonJS). -
TypeError: compose is not a function
cause Trying to import the `compose` function as a named export when it is the default export of the package.fixUse `import compose from 'request-compose';` (ESM) or `const compose = require('request-compose');` (CommonJS). -
Error: connect ECONNREFUSED
cause The underlying HTTP request failed, typically due to an incorrect URL, an unreachable host, or a server that is not running or not listening on the specified port.fixVerify the URL, hostname, and port provided to `Request.url()` or within your custom request options. Ensure the target server is operational and network access is permitted. -
SyntaxError: Unexpected token 'export' (when using Node.js)
cause Attempting to use ESM `import`/`export` syntax in a Node.js environment configured for CommonJS, or in an older Node.js version without explicitly declaring `"type": "module"` in `package.json`.fixFor CommonJS environments, use `require()` syntax (e.g., `const compose = require('request-compose');`). For ESM in Node.js, ensure your `package.json` contains `"type": "module"` and use `import` statements.
Warnings
- gotcha The `Request` and `Response` middleware factories are properties of the default `compose` export, not named exports. Attempting to destructure them directly from the module like `import { Request } from 'request-compose'` will result in `undefined`.
- gotcha `request-compose` offers a composition mechanism and basic middlewares, but it does not bundle a full HTTP client implementation by default. For protocols like HTTPS, you often need to supply your own initial request function that wraps Node.js's native `http` or `https` modules within your composition pipeline.
- gotcha Since `request-compose` operations are promise-based, robust error handling with `try...catch` blocks or `.catch()` handlers is critical. Neglecting to handle rejected promises can lead to unhandled promise rejections, potentially crashing Node.js processes in environments prior to Node.js 15 or causing silent failures.
Install
-
npm install request-compose -
yarn add request-compose -
pnpm add request-compose
Imports
- compose
import { compose } from 'request-compose';import compose from 'request-compose';
- Request
import { Request } from 'request-compose';import compose from 'request-compose'; const { Request } = compose; - Response
import { Response } from 'request-compose';import compose from 'request-compose'; const { Response } = compose;
Quickstart
import compose from 'request-compose';
const Request = compose.Request;
const Response = compose.Response;
(async () => {
try {
const { res, body } = await compose(
Request.defaults({headers: {'user-agent': 'request-compose'}}),
Request.url('https://api.github.com/users/simov'),
Request.send(),
Response.buffer(),
Response.string(),
Response.parse()
)();
console.log(res.statusCode, res.statusMessage);
console.log(res.headers['x-ratelimit-remaining']);
console.log(body);
}
catch (err) {
console.error(err);
}
})();