Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/abhisekp/rate-limiter-api

:alarm_clock: :watch: A simple library to easily manage rate limits of APIs without any hassles.
https://github.com/abhisekp/rate-limiter-api

babeljs callback javascript library nodejs promise rate-limits stamp

Last synced: 4 months ago
JSON representation

:alarm_clock: :watch: A simple library to easily manage rate limits of APIs without any hassles.

Awesome Lists containing this project

README

        

# Rate Limiter API
A simple library to easily manage rate limits of APIs without any hassles.

[![Rate Limiter API image](http://i.imgur.com/JDFpgQZ.jpg "Rate Limiter API image")](#app-top)

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) [![MIT License](https://img.shields.io/badge/License-MIT-56A902.svg?style=flat-square&maxAge=2592000)](LICENSE) [![npm version](https://img.shields.io/npm/v/rate-limiter-api.svg)](https://badge.fury.io/js/rate-limiter-api) [![Join the chat at https://gitter.im/abhisekp/rate-limiter-api](https://badges.gitter.im/abhisekp/rate-limiter-api.svg)](https://gitter.im/abhisekp/rate-limiter-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fabhisekp%2Frate-limiter-api.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fabhisekp%2Frate-limiter-api?ref=badge_shield)

## Table of Contents
- [How to Use](#usage)
- [Browser](#usage-browser)
- [NodeJS](#usage-nodejs)
- [Public API](#public-api)
- [Stats](#stats)
- [Technologies Used](#techs)
- [Help & Support](#help)
- [Contributors](#contrib)
- [License](#license)
- [Contact](#contact)

## Usage
#### Browser
```html

var rateLimiter = RateLimiterAPI()

```

#### NodeJS
```sh
$ {sudo -H} npm install -S rate-limiter-api
```

```js
import RateLimiterAPI from 'rate-limiter-api'
```

## Public Interface
- `rateLimiter = RateLimiterAPI({threshold: 5})`
- `rateLimiter.limit(requestHandler) => Promise`
- `rateLimiter.updateRateLimits` on success to update rates
- `requestHandler(responseHandler)` on success to continue

----
```js
import RateLimiterAPI from 'rate-limiter-api'

const rateLimiter = RateLimiterAPI({
threshold: 5, // leave `threshold` amount of api requests intact (minimum: 1)
})
```

Pass an optional `threshold` value which would limit the total rate limits till the threshold is reached in the current session pulse till rate reset.

----
```js
const request = rateLimiter.limit(requestHandler) //=> Promise

function requestHandler(responseHandler) {
fetch('https://api.example.com/get')
.then((response) => {
rateLimiter.updateRateLimits({
rateLimit: response.headers.rateLimit,
rateRemaining: response.headers.rateRemaining,
rateReset: response.headers.rateReset,
})

responseHandler(null, response.body)
})
.catch((err) => {
responseHandler(err)
})
}
```

The `limit` method is passed a `requestHandler` which would manage the request and then call the node-style `responseHandler` callback (which gets passed by the RateLimiter library to the `requestHandler` function) after the request is successful or unsuccessful accordingly.

The `request` method returns a `Promise`.

> responseHandler(Error, response)
where the first parameter is the Error object and the seconds parameter is the `response`.

`responseHandler` callback must be called to continue getting responses.

Update the rate limits i.e. `{rateLimit, rateRemaining, rateReset}` got from the header using `updateRateLimits` method.

----
```js
request
.then((response) => {
console.dir(response, {colors:1})
})
.catch((error) => {
console.error(error.message)
})
```

When the request completes, the `Promise` resolves and can be thenable to get the response or error.

## Stats

1 text file.
1 unique file.
0 files ignored.

https://github.com/AlDanial/cloc v 1.66 T=0.04 s (22.7 files/s, 2516.9 lines/s)

Language|files|blank|comment|code
:---|:---:|:---:|:---:|:---:
JavaScript|1|12|37|62

## Technologies Used
- [**Babel**](http://babeljs.io) — Transpiles modern JS to compatible and runnable JS
- [**Stamp It**](https://github.com/stampit-org/stampit#readme) - Composable inheritance object creation libray
- [**Async JS**](https://caolan.github.io/async/index.html) - Utility for asynchronous functions
- [**Lodash**](https://lodash.com/) - Utility library

## Help & Support
- [**BabelJS**](https://babeljs.slack.com) — BabelJS Slack Chat Room
- [**nodejs/node**](https://gitter.im/nodejs/node) — NodeJS Gitter Chat Room
- [**Fun with Stamps**](https://medium.com/@koresar/fun-with-stamps-episode-1-stamp-basics-e0627d81efe0) - Learn StampIt

## Contributors

| [
Abhisek Pattnaik](http://about.me/abhisekp)
[💻](https://github.com/abhisekp/rate-limiter-api/commits?author=abhisekp) 🎨 [📖](https://github.com/abhisekp/rate-limiter-api/commits?author=abhisekp) 💡 |
| :---: |

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors#emoji-key) specification.

> **All types of Contributions are Welcome** :pray:

## License

[**MIT**](LICENSE) © [**Abhisek Patnaik**](https://github.com/abhisekp)

> ----


Tweet @abhisek

Know about/abhisekp

Chat with
@abhisekp

> ----


Table of Contents ⮭

back to top ⮭

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fabhisekp%2Frate-limiter-api.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fabhisekp%2Frate-limiter-api?ref=badge_large)