UNPKG

2.55 kBMarkdownView Raw
1# plugin-retry.js
2
3> Retries requests for server 4xx/5xx responses except `400`, `401`, `403`, `404`, and `422`.
4
5[![@latest](https://img.shields.io/npm/v/@octokit/plugin-retry.svg)](https://www.npmjs.com/package/@octokit/plugin-retry)
6[![Build Status](https://github.com/octokit/plugin-retry.js/workflows/Test/badge.svg)](https://github.com/octokit/plugin-retry.js/actions?workflow=Test)
7
8## Usage
9
10<table>
11<tbody valign=top align=left>
12<tr><th>
13Browsers
14</th><td width=100%>
15
16Load `@octokit/plugin-retry` and [`@octokit/core`](https://github.com/octokit/core.js) (or core-compatible module) directly from [cdn.skypack.dev](https://cdn.skypack.dev)
17
18```html
19<script type="module">
20 import { Octokit } from "https://cdn.skypack.dev/@octokit/core";
21 import { retry } from "https://cdn.skypack.dev/@octokit/plugin-retry";
22</script>
23```
24
25</td></tr>
26<tr><th>
27Node
28</th><td>
29
30Install with `npm install @octokit/core @octokit/plugin-retry`. Optionally replace `@octokit/core` with a core-compatible module
31
32```js
33const { Octokit } = require("@octokit/core");
34const { retry } = require("@octokit/plugin-retry");
35```
36
37</td></tr>
38</tbody>
39</table>
40
41```js
42const MyOctokit = Octokit.plugin(retry);
43const octokit = new MyOctokit({ auth: "secret123" });
44
45// retries request up to 3 times in case of a 500 response
46octokit.request("/").catch((error) => {
47 if (error.request.request.retryCount) {
48 console.log(
49 `request failed after ${error.request.request.retryCount} retries`
50 );
51 }
52
53 console.error(error);
54});
55```
56
57To override the default `doNotRetry` list:
58
59```js
60const octokit = new MyOctokit({
61 auth: "secret123",
62 retry: {
63 doNotRetry: [
64 /* List of HTTP 4xx/5xx status codes */
65 ],
66 },
67});
68```
69
70To override the number of retries:
71
72```js
73const octokit = new MyOctokit({
74 auth: "secret123",
75 request: { retries: 1 },
76});
77```
78
79You can manually ask for retries for any request by passing `{ request: { retries: numRetries, retryAfter: delayInSeconds }}`. Note that the `doNotRetry` option from the constructor is ignored in this case, requests will be retried no matter their response code.
80
81```js
82octokit
83 .request("/", { request: { retries: 1, retryAfter: 1 } })
84 .catch((error) => {
85 if (error.request.request.retryCount) {
86 console.log(
87 `request failed after ${error.request.request.retryCount} retries`
88 );
89 }
90
91 console.error(error);
92 });
93```
94
95Pass `{ retry: { enabled: false } }` to disable this plugin.
96
97## Contributing
98
99See [CONTRIBUTING.md](CONTRIBUTING.md)
100
101## License
102
103[MIT](LICENSE)