Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
request-compose
Advanced tools
The request-compose npm package is a versatile tool for making HTTP requests and composing them in a functional manner. It allows for chaining requests, handling responses, and managing errors in a streamlined way.
Making HTTP Requests
This feature allows you to make HTTP requests using a simple and intuitive API. The example demonstrates how to make a GET request to an API endpoint and log the response body.
const { compose } = require('request-compose');
const { Request } = compose;
(async () => {
const { res, body } = await Request({
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: 'GET'
});
console.log(body);
})();
Chaining Requests
This feature allows you to chain multiple HTTP requests together. The example demonstrates making a GET request followed by a POST request, showing how to handle multiple requests in sequence.
const { compose } = require('request-compose');
const { Request } = compose;
(async () => {
const { res: res1, body: body1 } = await Request({
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: 'GET'
});
const { res: res2, body: body2 } = await Request({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
body: { title: 'foo', body: 'bar', userId: 1 },
json: true
});
console.log(body2);
})();
Error Handling
This feature provides robust error handling for HTTP requests. The example demonstrates how to catch and handle errors when a request fails.
const { compose } = require('request-compose');
const { Request } = compose;
(async () => {
try {
const { res, body } = await Request({
url: 'https://jsonplaceholder.typicode.com/invalid-url',
method: 'GET'
});
console.log(body);
} catch (error) {
console.error('Request failed:', error.message);
}
})();
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and supports features like interceptors, request cancellation, and automatic JSON transformation. Compared to request-compose, Axios is more widely used and has a larger community, but it may not offer the same level of composability.
Node-fetch is a lightweight module that brings the Fetch API to Node.js. It is minimalistic and focuses on providing a fetch-like interface for making HTTP requests. While it is simpler and more lightweight than request-compose, it lacks the advanced composability and chaining features.
Superagent is a small, progressive client-side HTTP request library that also works in Node.js. It provides a flexible API for making HTTP requests and supports features like query string parsing, form submissions, and file uploads. Superagent is more feature-rich than node-fetch but may not offer the same level of composability as request-compose.
Composable HTTP Client
var compose = require('request-compose')
var Request = compose.Request
var Response = compose.Response
;(async () => {
try {
var {res, body} = await compose(
Request.defaults({headers: {'user-agent': 'request-compose'}}),
Request.url('https://api.github.com/users/simov'),
Request.send(),
Response.buffer(),
Response.parse(),
)()
console.log(res.statusCode, res.statusMessage)
console.log(res.headers['x-ratelimit-remaining'])
console.log(body)
}
catch (err) {
console.error(err)
}
})()
var https = require('https')
var compose = require('request-compose')
var request = compose(
(options) => {
options.headers = options.headers || {}
options.headers['user-agent'] = 'request-compose'
return options
},
(options) => new Promise((resolve, reject) => {
https.request(options)
.on('response', resolve)
.on('error', reject)
.end()
}),
async (res) => await new Promise((resolve, reject) => {
var body = ''
res
.on('data', (chunk) => body += chunk)
.on('end', () => resolve({res, body}))
.on('error', reject)
}),
({res, body}) => ({res, body: JSON.parse(body)}),
)
;(async () => {
try {
var {res, body} = await request({
protocol: 'https:',
hostname: 'api.github.com',
path: '/users/simov',
})
console.log(res.statusCode, res.statusMessage)
console.log(res.headers['x-ratelimit-remaining'])
console.log(body)
}
catch (err) {
console.error(err)
}
})()
var compose = require('request-compose')
var Request = compose.Request
var Response = compose.Response
;(async () => {
try {
var {res, body} = await compose(
Request.defaults({headers: {'user-agent': 'request-compose'}}),
Request.url('https://api.github.com/users/simov'),
Request.send(),
Response.buffer(),
Response.parse(),
)()
console.log(res.statusCode, res.statusMessage)
console.log(res.headers['x-ratelimit-remaining'])
console.log(body)
}
catch (err) {
console.error(err)
}
})()
var request = require('request-compose').client
;(async () => {
try {
var {res, body} = await request({
url: 'https://api.github.com/users/simov',
headers: {
'user-agent': 'request-compose'
}
})
console.log(res.statusCode, res.statusMessage)
console.log(res.headers['x-ratelimit-remaining'])
console.log(body)
}
catch (err) {
console.error(err)
}
})()
FAQs
Composable HTTP Client
The npm package request-compose receives a total of 155,040 weekly downloads. As such, request-compose popularity was classified as popular.
We found that request-compose demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.