UNPKG

1.88 kBMarkdownView Raw
1# plugin-throttling.js
2
3> Octokit plugin for GitHub’s recommended request throttling
4
5[![npm](https://img.shields.io/npm/v/@octokit/plugin-throttling.svg)][https://www.npmjs.com/package/@octokit/plugin-throttling]
6[![Build Status](https://travis-ci.org/octokit/plugin-throttling.js.svg?branch=beta)][https://travis-ci.org/octokit/plugin-throttling.js]
7[![Coverage Status](https://img.shields.io/coveralls/github/octokit/plugin-throttling.js/beta.svg)][https://coveralls.io/github/octokit/plugin-throttling.js]
8[![Greenkeeper](https://badges.greenkeeper.io/octokit/plugin-throttling.js.svg)](https://greenkeeper.io/)
9
10Implements all [recommended best practises](https://developer.github.com/v3/guides/best-practices-for-integrators/) to prevent hitting abuse rate limits.
11
12## Usage
13
14The code below creates a "Hello, world!" issue on every repository in a given organization. Without the throttling plugin it would send many requests in parallel and would hit rate limits very quickly. But the `@octokit/plugin-throttling` makes sure that no requests using the same authentication token are throttled correctly.
15
16```js
17const Octokit = require('@ocotkit/rest')
18 .plugin(require('@octokit/plugin-throttling'))
19
20const octokit = new Octokit()
21octokit.authenticate({
22 type: 'token',
23 token: process.env.TOKEN
24})
25
26async function createIssueOnAllRepos (org) {
27 const repos = await octokit.paginate(octokit.repos.listForOrg.endpoint({ org }))
28 return Promise.all(repos.forEach(({ name } => {
29 octokit.issues.create({
30 owner,
31 repo: name,
32 title: 'Hello, world!'
33 })
34 })))
35}
36```
37
38Handle events
39
40```js
41octokit.throttle.on('rate-limit', (retryAfter) => console.warn(`Rate-limit hit, retrying after ${retryAfter}s`))
42octokit.throttle.on('abuse-limit', (retryAfter) => console.warn(`Abuse-limit hit, retrying after ${retryAfter}s`))
43```
44
45## LICENSE
46
47[MIT](LICENSE)