Skip to content

Commit

Permalink
📝 Warn to use a custom until function to avoid retrying on 4xx errors
Browse files Browse the repository at this point in the history
Should solve #176
  • Loading branch information
elbywan committed Mar 12, 2023
1 parent b696b1c commit 1812c73
Show file tree
Hide file tree
Showing 72 changed files with 158 additions and 127 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,13 @@ const w = wretch().middlewares([retry(), dedupe()])

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

> **💡 By default, the request will be retried if the response status is not in the 2xx range.**
>
> ```js
> // Replace the default condition with a custom one to avoid retrying on 4xx errors:
> until: (response, error) => response && (response.ok || (response.status >= 400 && response.status < 500))
> ```
```js
import wretch from 'wretch'
import { retry } from 'wretch/middlewares'
Expand Down
7 changes: 7 additions & 0 deletions docs/api/assets/highlight.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
--dark-hl-9: #D4D4D4;
--light-hl-10: #267F99;
--dark-hl-10: #4EC9B0;
--light-hl-11: #000000;
--dark-hl-11: #C8C8C8;
--light-code-background: #FFFFFF;
--dark-code-background: #1E1E1E;
}
Expand All @@ -37,6 +39,7 @@
--hl-8: var(--light-hl-8);
--hl-9: var(--light-hl-9);
--hl-10: var(--light-hl-10);
--hl-11: var(--light-hl-11);
--code-background: var(--light-code-background);
} }

Expand All @@ -52,6 +55,7 @@
--hl-8: var(--dark-hl-8);
--hl-9: var(--dark-hl-9);
--hl-10: var(--dark-hl-10);
--hl-11: var(--dark-hl-11);
--code-background: var(--dark-code-background);
} }

Expand All @@ -67,6 +71,7 @@
--hl-8: var(--light-hl-8);
--hl-9: var(--light-hl-9);
--hl-10: var(--light-hl-10);
--hl-11: var(--light-hl-11);
--code-background: var(--light-code-background);
}

Expand All @@ -82,6 +87,7 @@
--hl-8: var(--dark-hl-8);
--hl-9: var(--dark-hl-9);
--hl-10: var(--dark-hl-10);
--hl-11: var(--dark-hl-11);
--code-background: var(--dark-code-background);
}

Expand All @@ -96,4 +102,5 @@
.hl-8 { color: var(--hl-8); }
.hl-9 { color: var(--hl-9); }
.hl-10 { color: var(--hl-10); }
.hl-11 { color: var(--hl-11); }
pre, code { background: var(--code-background); }
2 changes: 1 addition & 1 deletion docs/api/functions/addons_abort.default.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1>Function default</h1></div>
</div>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/functions/addons_perfs.default.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>Function default</h1></div>
</div>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/functions/addons_progress.default.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>Function default</h1></div>
</div>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/functions/index.default-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h5>_options: <span class="tsd-signature-symbol">{}</span><span class="tsd-signa
<ul class="tsd-parameters"></ul></li></ul></div>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/functions/middlewares_dedupe.dedupe.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
<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>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/functions/middlewares_delay.delay.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
<h5>time: <span class="tsd-signature-type">number</span></h5></li></ul></div>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
7 changes: 6 additions & 1 deletion docs/api/functions/middlewares_retry.retry.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ <h2>Retry middleware</h2>
<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;">
<h4>Retries a request multiple times in case of an error (or until a custom condition is true).</h4>
</a>
<blockquote>
<p><strong>💡 By default, the request will be retried if the response status is not in the 2xx range.</strong></p>
<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>
</code></pre>
</blockquote>
<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>
</code></pre>
</div>
Expand All @@ -38,7 +43,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
<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>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
<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>
<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">
<ul>
<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>
<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>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
Loading

0 comments on commit 1812c73

Please sign in to comment.