Skip to content

Commit

Permalink
feat(enabled): Add 'enabled' option
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Grondin authored and gr2m committed Jan 12, 2019
1 parent 0b0360b commit fa606db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const errorRequest = require('./error-request')

function retryPlugin (octokit, octokitOptions = {}) {
const state = Object.assign({
enabled: true,
retryAfterBaseValue: 1000,
doNotRetry: [ 400, 401, 403, 404 ],
retries: 3
Expand All @@ -18,6 +19,10 @@ function retryPlugin (octokit, octokitOptions = {}) {
}
}

if (!state.enabled) {
return
}

octokit.hook.error('request', errorRequest.bind(null, octokit, state))
octokit.hook.wrap('request', wrapRequest.bind(null, state))
}
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ const expect = require('chai').expect
const Octokit = require('./octokit')

describe('Automatic Retries', function () {
it('Should be possible to disable the plugin', async function () {
const octokit = new Octokit({ retry: { enabled: false } })

try {
await octokit.request('GET /route', {
request: {
responses: [
{ status: 403, headers: {}, data: { message: 'Did not retry' } },
{ status: 200, headers: {}, data: { message: 'Success!' } }
],
retries: 1
}
})
throw new Error('Should not reach this point')
} catch (error) {
expect(error.status).to.equal(403)
expect(error.message).to.equal('Did not retry')
}

expect(octokit.__requestLog).to.deep.equal([
'START GET /route'
])
})

it('Should retry once and pass', async function () {
const octokit = new Octokit()

Expand Down

0 comments on commit fa606db

Please sign in to comment.