Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
api-client-limiter
Advanced tools
Simple function wrapper queuing and limiting the number of calls to a function. Ideal for Rest API call limiting compliance.
Readme
Some web API limits the number of calls you can make in a certain period of time. After looking for a node module that just do that, I was frustrated and created mine. All modules I saw were either very complex to use or were actually "debouncing" or throttling calls, which means that all calls within a certain time frame get the same results instead of doing the full round trip to the API.
This simple module does only one thing: it wraps a function following the standard nodejs callback pattern, queue calls and execute the real method every X seconds.
Just install the module in your node project using the following call :
npm install api-client-limiter
Here's a quick sample on how to transform a tight loop performing API calls into a limited one. This sample use async to control the loop, because of the provided callback parameter. This is the ideal way of using this module.
var Limiter = require('api-client-limiter');
// Create a limiter for 1 call every 2 seconds
var limiter = new Limiter(1, 2000);
// Wrap a callback that will be called for a team collection
var callback = limiter(function(team, done) {
console.log("This will be queued and called every 2 seconds");
// An API call is performed by this callback
request.get({
url: 'http://api.sample.com',
json:true,
agent:false
}, function(err, resp, body) {
// Let's do anything with the received data...
db.save(body);
done();
});
});
async.forEach(teams, callback, function(err) {
console.log("Execution is now complete. All teams have been processed");
});
Like your can see in this sample, calling limiter with the function your want to limit is very easy. Internally, we create a queue allowing a certain number of parallel calls every x milliseconds. Each call to limiter will enqueue a call to your original callback, wrapped in a timer of the given duration.
For now, only functions with a callback as the last parameter are supported. I may add support for promises in the future, but for now, that was what I needed for my use case.
FAQs
Simple function wrapper queuing and limiting the number of calls to a function. Ideal for Rest API call limiting compliance.
The npm package api-client-limiter receives a total of 0 weekly downloads. As such, api-client-limiter popularity was classified as not popular.
We found that api-client-limiter demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.