throttle-ky
v1.0.1
Published
Throttles HTTP requests made by a ky instance. Useful for enforcing rate-limits.
Downloads
9
Readme
throttle-ky
Throttles HTTP requests made by a ky instance.
Intro
Very useful for enforcing rate-limits. Intended to be used with ky (an excellent fetch wrapper) and p-throttle, though p-throttle
can be swapped for any rate-limiting implementation.
Install
npm install throttle-ky ky
Usage
import throttleKy from 'throttle-ky'
import pThrottle from 'p-throttle'
import ky from 'ky'
// Allow up to 600 requests per minute.
export const throttle = pThrottle({
limit: 600,
interval: 60 * 1000
})
const rateLimitedKy = throttleKy(ky, throttle)
// `rateLimitedKy` is just a normal `ky` instance that is guaranteed to respect
// the given throttle before making new HTTP requests.
const result = await rateLimitedKy.get('https://example.com').text()
This package is very simple, but I find myself using it all the time for making more robust HTTP requests, especially when used alongside other async primitives like p-map or when creating HTTP API clients which are configurable via ky.extend
while also respecting server-side rate limits.
License
MIT © Travis Fischer