Skip to content

Commit

Permalink
feat(retry): accept shorthand retry settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 9, 2017
1 parent 57b7dc2 commit dfed69d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function cachingFetch (uri, _opts) {
if (opts.integrity && !ssri) {
ssri = require('ssri')
}
if (opts.retry && typeof opts.retry === 'number') {
opts.retry = {retries: opts.retry}
} else if (opts.retry === false) {
opts.retry = {retries: 0}
}
opts.cacheManager = opts.cacheManager && (
typeof opts.cacheManager === 'string'
? new Cache(opts.cacheManager, opts.cacheOpts)
Expand Down Expand Up @@ -326,7 +331,7 @@ function remoteFetch (uri, opts) {
throw err
}
})
}, opts.retry === false ? { retries: 0 } : opts.retry).catch(err => {
}, opts.retry).catch(err => {
if (err.status >= 400) {
return err
} else {
Expand Down
25 changes: 25 additions & 0 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,29 @@ test('retries non-POST requests on 500 errors', t => {
})
})

test('accepts opts.retry shorthands', t => {
const srv = tnock(t, HOST)
srv.get('/test').reply(500, '')
return fetch(`${HOST}/test`, {
retry: false
}).then(res => {
t.equal(res.status, 500, 'did not retry')
srv.get('/test').reply(500, () => {
srv.get('/test').reply(200, CONTENT)
return ''
})
return fetch(`${HOST}/test`, {
retry: 1
})
}).then(res => {
t.equal(res.status, 200, 'retried once')
srv.get('/test').twice().reply(500)
return fetch(`${HOST}/test`, {
retry: 1
})
}).then(res => {
t.equal(res.status, 500, 'failed on second retry')
})
})

test('retries non-POST requests on ECONNRESET')

0 comments on commit dfed69d

Please sign in to comment.