From dfed69d7eddadb8a4e6cbf829cfef42c6cd4b907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sun, 9 Apr 2017 14:17:51 -0700 Subject: [PATCH] feat(retry): accept shorthand retry settings --- index.js | 7 ++++++- test/fetch.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1d60785..3bae0e8 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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 { diff --git a/test/fetch.js b/test/fetch.js index 691b7d0..e06bdce 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -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')