Skip to content

Commit 0408350

Browse files
Simon Grondingr2m
authored andcommitted
feat(enabled): Add 'enabled' option
1 parent 36eb840 commit 0408350

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const triggersNotification = throttlingPlugin.triggersNotification =
1111

1212
function throttlingPlugin (octokit, octokitOptions = {}) {
1313
const state = Object.assign({
14+
enabled: true,
1415
triggersNotification,
1516
minimumAbuseRetryAfter: 5,
1617
retryAfterBaseValue: 1000,
@@ -28,6 +29,10 @@ function throttlingPlugin (octokit, octokitOptions = {}) {
2829
retryLimiter: new Bottleneck()
2930
}, octokitOptions.throttle)
3031

32+
if (!state.enabled) {
33+
return
34+
}
35+
3136
if (typeof state.onAbuseLimit !== 'function' || typeof state.onRateLimit !== 'function') {
3237
throw new Error(`octokit/plugin-throttling error:
3338
You must pass the onAbuseLimit and onRateLimit error handlers.

test/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,39 @@ const Bottleneck = require('bottleneck')
22
const expect = require('chai').expect
33
const Octokit = require('./octokit')
44

5-
describe('Github API best practices', function () {
5+
describe('General', function () {
6+
it('Should be possible to disable the plugin', async function () {
7+
const octokit = new Octokit({ throttle: { enabled: false } })
8+
9+
const req1 = octokit.request('GET /route1', {
10+
request: {
11+
responses: [{ status: 201, headers: {}, data: {} }]
12+
}
13+
})
14+
15+
const req2 = octokit.request('GET /route2', {
16+
request: {
17+
responses: [{ status: 202, headers: {}, data: {} }]
18+
}
19+
})
20+
21+
const req3 = octokit.request('GET /route3', {
22+
request: {
23+
responses: [{ status: 203, headers: {}, data: {} }]
24+
}
25+
})
26+
27+
await Promise.all([req1, req2, req3])
28+
expect(octokit.__requestLog).to.deep.equal([
29+
'START GET /route1',
30+
'START GET /route2',
31+
'START GET /route3',
32+
'END GET /route1',
33+
'END GET /route2',
34+
'END GET /route3'
35+
])
36+
})
37+
638
it('Should require the user to pass both limit handlers', function () {
739
const message = 'You must pass the onAbuseLimit and onRateLimit error handlers'
840

@@ -14,7 +46,9 @@ describe('Github API best practices', function () {
1446
expect(() => new Octokit({ throttle: { onRateLimit: () => 1 } })).to.throw(message)
1547
expect(() => new Octokit({ throttle: { onAbuseLimit: () => 1, onRateLimit: () => 1 } })).to.not.throw()
1648
})
49+
})
1750

51+
describe('Github API best practices', function () {
1852
it('Should not allow more than 1 request concurrently', async function () {
1953
const octokit = new Octokit({ throttle: { onAbuseLimit: () => 1, onRateLimit: () => 1 } })
2054
const req1 = octokit.request('GET /route1', {

0 commit comments

Comments
 (0)