Skip to content

Commit 1812c73

Browse files
committed
📝 Warn to use a custom until function to avoid retrying on 4xx errors
Should solve #176
1 parent b696b1c commit 1812c73

File tree

72 files changed

+158
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+158
-127
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ const w = wretch().middlewares([retry(), dedupe()])
653653

654654
**Retries a request multiple times in case of an error (or until a custom condition is true).**
655655

656+
> **💡 By default, the request will be retried if the response status is not in the 2xx range.**
657+
>
658+
> ```js
659+
> // Replace the default condition with a custom one to avoid retrying on 4xx errors:
660+
> until: (response, error) => response && (response.ok || (response.status >= 400 && response.status < 500))
661+
> ```
662+
656663
```js
657664
import wretch from 'wretch'
658665
import { retry } from 'wretch/middlewares'

docs/api/assets/highlight.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
--dark-hl-9: #D4D4D4;
2222
--light-hl-10: #267F99;
2323
--dark-hl-10: #4EC9B0;
24+
--light-hl-11: #000000;
25+
--dark-hl-11: #C8C8C8;
2426
--light-code-background: #FFFFFF;
2527
--dark-code-background: #1E1E1E;
2628
}
@@ -37,6 +39,7 @@
3739
--hl-8: var(--light-hl-8);
3840
--hl-9: var(--light-hl-9);
3941
--hl-10: var(--light-hl-10);
42+
--hl-11: var(--light-hl-11);
4043
--code-background: var(--light-code-background);
4144
} }
4245

@@ -52,6 +55,7 @@
5255
--hl-8: var(--dark-hl-8);
5356
--hl-9: var(--dark-hl-9);
5457
--hl-10: var(--dark-hl-10);
58+
--hl-11: var(--dark-hl-11);
5559
--code-background: var(--dark-code-background);
5660
} }
5761

@@ -67,6 +71,7 @@
6771
--hl-8: var(--light-hl-8);
6872
--hl-9: var(--light-hl-9);
6973
--hl-10: var(--light-hl-10);
74+
--hl-11: var(--light-hl-11);
7075
--code-background: var(--light-code-background);
7176
}
7277

@@ -82,6 +87,7 @@
8287
--hl-8: var(--dark-hl-8);
8388
--hl-9: var(--dark-hl-9);
8489
--hl-10: var(--dark-hl-10);
90+
--hl-11: var(--dark-hl-11);
8591
--code-background: var(--dark-code-background);
8692
}
8793

@@ -96,4 +102,5 @@
96102
.hl-8 { color: var(--hl-8); }
97103
.hl-9 { color: var(--hl-9); }
98104
.hl-10 { color: var(--hl-10); }
105+
.hl-11 { color: var(--hl-11); }
99106
pre, code { background: var(--code-background); }

docs/api/functions/addons_abort.default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h1>Function default</h1></div>
3030
</div>
3131
<h4 class="tsd-returns-title">Returns <a href="../types/index.WretchAddon.html" class="tsd-signature-type" data-tsd-kind="Type alias">WretchAddon</a><span class="tsd-signature-symbol">&lt;</span><a href="../interfaces/addons_abort.AbortWretch.html" class="tsd-signature-type" data-tsd-kind="Interface">AbortWretch</a><span class="tsd-signature-symbol">, </span><a href="../interfaces/addons_abort.AbortResolver.html" class="tsd-signature-type" data-tsd-kind="Interface">AbortResolver</a><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
3232
<ul>
33-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/addons/abort.ts#L110">src/addons/abort.ts:110</a></li></ul></aside></li></ul></section></div>
33+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/addons/abort.ts#L110">src/addons/abort.ts:110</a></li></ul></aside></li></ul></section></div>
3434
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
3535
<div class="tsd-navigation settings">
3636
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/addons_perfs.default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>Function default</h1></div>
3434
</div>
3535
<h4 class="tsd-returns-title">Returns <a href="../types/index.WretchAddon.html" class="tsd-signature-type" data-tsd-kind="Type alias">WretchAddon</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">unknown</span><span class="tsd-signature-symbol">, </span><a href="../interfaces/addons_perfs.PerfsAddon.html" class="tsd-signature-type" data-tsd-kind="Interface">PerfsAddon</a><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
3636
<ul>
37-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/addons/perfs.ts#L58">src/addons/perfs.ts:58</a></li></ul></aside></li></ul></section></div>
37+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/addons/perfs.ts#L58">src/addons/perfs.ts:58</a></li></ul></aside></li></ul></section></div>
3838
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
3939
<div class="tsd-navigation settings">
4040
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/addons_progress.default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h1>Function default</h1></div>
2727
</div>
2828
<h4 class="tsd-returns-title">Returns <a href="../types/index.WretchAddon.html" class="tsd-signature-type" data-tsd-kind="Type alias">WretchAddon</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">unknown</span><span class="tsd-signature-symbol">, </span><a href="../interfaces/addons_progress.ProgressResolver.html" class="tsd-signature-type" data-tsd-kind="Interface">ProgressResolver</a><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
2929
<ul>
30-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/addons/progress.ts#L45">src/addons/progress.ts:45</a></li></ul></aside></li></ul></section></div>
30+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/addons/progress.ts#L45">src/addons/progress.ts:45</a></li></ul></aside></li></ul></section></div>
3131
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
3232
<div class="tsd-navigation settings">
3333
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/index.default-1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h5>_options: <span class="tsd-signature-symbol">{}</span><span class="tsd-signa
4141
<ul class="tsd-parameters"></ul></li></ul></div>
4242
<h4 class="tsd-returns-title">Returns <a href="../interfaces/index.Wretch.html" class="tsd-signature-type" data-tsd-kind="Interface">Wretch</a></h4><aside class="tsd-sources">
4343
<ul>
44-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/index.ts#L36">src/index.ts:36</a></li></ul></aside></li></ul></section></div>
44+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/index.ts#L36">src/index.ts:36</a></li></ul></aside></li></ul></section></div>
4545
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
4646
<div class="tsd-navigation settings">
4747
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/middlewares_dedupe.dedupe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
5656
<h5><code class="tsd-tag ts-flagOptional">Optional</code> options: <a href="../types/middlewares_dedupe.DedupeOptions.html" class="tsd-signature-type" data-tsd-kind="Type alias">DedupeOptions</a></h5></li></ul></div>
5757
<h4 class="tsd-returns-title">Returns <a href="../types/index.ConfiguredMiddleware.html" class="tsd-signature-type" data-tsd-kind="Type alias">ConfiguredMiddleware</a></h4><aside class="tsd-sources">
5858
<ul>
59-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/middlewares/dedupe.ts#L33">src/middlewares/dedupe.ts:33</a></li></ul></aside></li></ul></section></div>
59+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/middlewares/dedupe.ts#L33">src/middlewares/dedupe.ts:33</a></li></ul></aside></li></ul></section></div>
6060
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
6161
<div class="tsd-navigation settings">
6262
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/middlewares_delay.delay.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
4343
<h5>time: <span class="tsd-signature-type">number</span></h5></li></ul></div>
4444
<h4 class="tsd-returns-title">Returns <a href="../types/index.ConfiguredMiddleware.html" class="tsd-signature-type" data-tsd-kind="Type alias">ConfiguredMiddleware</a></h4><aside class="tsd-sources">
4545
<ul>
46-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/middlewares/delay.ts#L16">src/middlewares/delay.ts:16</a></li></ul></aside></li></ul></section></div>
46+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/middlewares/delay.ts#L16">src/middlewares/delay.ts:16</a></li></ul></aside></li></ul></section></div>
4747
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
4848
<div class="tsd-navigation settings">
4949
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/middlewares_retry.retry.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ <h2>Retry middleware</h2>
2828
<a href="#retries-a-request-multiple-times-in-case-of-an-error-or-until-a-custom-condition-is-true" id="retries-a-request-multiple-times-in-case-of-an-error-or-until-a-custom-condition-is-true" style="color: inherit; text-decoration: none;">
2929
<h4>Retries a request multiple times in case of an error (or until a custom condition is true).</h4>
3030
</a>
31+
<blockquote>
32+
<p><strong>💡 By default, the request will be retried if the response status is not in the 2xx range.</strong></p>
33+
<pre><code class="language-js"><span class="hl-0">// Replace the default condition with a custom one to avoid retrying on 4xx errors:</span><br/><span class="hl-11">until</span><span class="hl-2">: (</span><span class="hl-6">response</span><span class="hl-2">, </span><span class="hl-6">error</span><span class="hl-2">) </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-6">response</span><span class="hl-2"> &amp;&amp; (</span><span class="hl-6">response</span><span class="hl-2">.</span><span class="hl-6">ok</span><span class="hl-2"> || (</span><span class="hl-6">response</span><span class="hl-2">.</span><span class="hl-6">status</span><span class="hl-2"> &gt;= </span><span class="hl-7">400</span><span class="hl-2"> &amp;&amp; </span><span class="hl-6">response</span><span class="hl-2">.</span><span class="hl-6">status</span><span class="hl-2"> &lt; </span><span class="hl-7">500</span><span class="hl-2">))</span>
34+
</code></pre>
35+
</blockquote>
3136
<pre><code class="language-ts"><span class="hl-8">import</span><span class="hl-2"> </span><span class="hl-6">wretch</span><span class="hl-2"> </span><span class="hl-8">from</span><span class="hl-2"> </span><span class="hl-5">&#39;wretch&#39;</span><br/><span class="hl-8">import</span><span class="hl-2"> { </span><span class="hl-6">retry</span><span class="hl-2"> } </span><span class="hl-8">from</span><span class="hl-2"> </span><span class="hl-5">&#39;wretch/middlewares&#39;</span><br/><br/><span class="hl-4">wretch</span><span class="hl-2">().</span><span class="hl-4">middlewares</span><span class="hl-2">([</span><br/><span class="hl-2"> </span><span class="hl-4">retry</span><span class="hl-2">({</span><br/><span class="hl-2"> </span><span class="hl-0">// Options - defaults below</span><br/><span class="hl-2"> </span><span class="hl-6">delayTimer:</span><span class="hl-2"> </span><span class="hl-7">500</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-4">delayRamp</span><span class="hl-6">:</span><span class="hl-2"> (</span><span class="hl-6">delay</span><span class="hl-2">, </span><span class="hl-6">nbOfAttempts</span><span class="hl-2">) </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-6">delay</span><span class="hl-2"> * </span><span class="hl-6">nbOfAttempts</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-6">maxAttempts:</span><span class="hl-2"> </span><span class="hl-7">10</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-4">until</span><span class="hl-6">:</span><span class="hl-2"> (</span><span class="hl-6">response</span><span class="hl-2">, </span><span class="hl-6">error</span><span class="hl-2">) </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-6">response</span><span class="hl-2"> &amp;&amp; </span><span class="hl-6">response</span><span class="hl-2">.</span><span class="hl-6">ok</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-6">onRetry:</span><span class="hl-2"> </span><span class="hl-1">null</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-6">retryOnNetworkError:</span><span class="hl-2"> </span><span class="hl-1">false</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-6">resolveWithLatestResponse:</span><span class="hl-2"> </span><span class="hl-1">false</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-6">skip:</span><span class="hl-2"> </span><span class="hl-1">undefined</span><br/><span class="hl-2"> })</span><br/><span class="hl-2">])</span><br/><br/><span class="hl-0">// You can also return a Promise, which is useful if you want to inspect the body:</span><br/><span class="hl-4">wretch</span><span class="hl-2">().</span><span class="hl-4">middlewares</span><span class="hl-2">([</span><br/><span class="hl-2"> </span><span class="hl-4">retry</span><span class="hl-2">({</span><br/><span class="hl-2"> </span><span class="hl-4">until</span><span class="hl-6">:</span><span class="hl-2"> </span><span class="hl-6">response</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><br/><span class="hl-2"> </span><span class="hl-6">response</span><span class="hl-2">.</span><span class="hl-4">clone</span><span class="hl-2">().</span><span class="hl-4">json</span><span class="hl-2">().</span><span class="hl-4">then</span><span class="hl-2">(</span><span class="hl-6">body</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><br/><span class="hl-2"> </span><span class="hl-6">body</span><span class="hl-2">.</span><span class="hl-6">field</span><span class="hl-2"> === </span><span class="hl-5">&#39;something&#39;</span><br/><span class="hl-2"> )</span><br/><span class="hl-2"> })</span><br/><span class="hl-2">])</span>
3237
</code></pre>
3338
</div>
@@ -38,7 +43,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
3843
<h5><code class="tsd-tag ts-flagOptional">Optional</code> options: <a href="../types/middlewares_retry.RetryOptions.html" class="tsd-signature-type" data-tsd-kind="Type alias">RetryOptions</a></h5></li></ul></div>
3944
<h4 class="tsd-returns-title">Returns <a href="../types/index.ConfiguredMiddleware.html" class="tsd-signature-type" data-tsd-kind="Type alias">ConfiguredMiddleware</a></h4><aside class="tsd-sources">
4045
<ul>
41-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/middlewares/retry.ts#L112">src/middlewares/retry.ts:112</a></li></ul></aside></li></ul></section></div>
46+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/middlewares/retry.ts#L119">src/middlewares/retry.ts:119</a></li></ul></aside></li></ul></section></div>
4247
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
4348
<div class="tsd-navigation settings">
4449
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

docs/api/functions/middlewares_throttlingCache.throttlingCache.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
7979
<h5><code class="tsd-tag ts-flagOptional">Optional</code> options: <a href="../types/middlewares_throttlingCache.ThrottlingCacheOptions.html" class="tsd-signature-type" data-tsd-kind="Type alias">ThrottlingCacheOptions</a></h5></li></ul></div>
8080
<h4 class="tsd-returns-title">Returns <a href="../types/index.ConfiguredMiddleware.html" class="tsd-signature-type" data-tsd-kind="Type alias">ConfiguredMiddleware</a><span class="tsd-signature-symbol"> &amp; </span><span class="tsd-signature-symbol">{ </span><br/><span>    </span>cache<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Map</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>inflight<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Map</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>throttling<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Set</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">unknown</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>cacheResponse<span class="tsd-signature-symbol">(</span>key<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span>, response<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span></h4><aside class="tsd-sources">
8181
<ul>
82-
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/a68a524/src/middlewares/throttlingCache.ts#L56">src/middlewares/throttlingCache.ts:56</a></li></ul></aside></li></ul></section></div>
82+
<li>Defined in <a href="https://github.com/elbywan/wretch/blob/7d7b9b5/src/middlewares/throttlingCache.ts#L56">src/middlewares/throttlingCache.ts:56</a></li></ul></aside></li></ul></section></div>
8383
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
8484
<div class="tsd-navigation settings">
8585
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

0 commit comments

Comments
 (0)