UNPKG

2.58 kBMarkdownView Raw
1# plugin-retry.js
2
3> Retries requests for server 4xx/5xx responses except `400`, `401`, `403` and `404`.
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**Note**: If you use it with `@octokit/rest` v16, install `@octokit/core` as a devDependency. This is only temporary and will no longer be necessary with `@octokit/rest` v17.
42
43```js
44const MyOctokit = Octokit.plugin(retry);
45const octokit = new MyOctokit({ auth: "secret123" });
46
47// retries request up to 3 times in case of a 500 response
48octokit.request("/").catch((error) => {
49 if (error.request.request.retryCount) {
50 console.log(
51 `request failed after ${error.request.request.retryCount} retries`
52 );
53 }
54
55 console.error(error);
56});
57```
58
59To override the default `doNotRetry` list:
60
61```js
62const octokit = new MyOctokit({
63 auth: "secret123",
64 retry: {
65 doNotRetry: [
66 /* List of HTTP 4xx/5xx status codes */
67 ],
68 },
69});
70```
71
72To override the number of retries:
73
74```js
75const octokit = new MyOctokit({
76 auth: "secret123",
77 request: { retries: 1 },
78});
79```
80
81You can manually ask for retries for any request by passing `{ request: { retries: numRetries, retryAfter: delayInSeconds }}`
82
83```js
84octokit
85 .request("/", { request: { retries: 1, retryAfter: 1 } })
86 .catch((error) => {
87 if (error.request.request.retryCount) {
88 console.log(
89 `request failed after ${error.request.request.retryCount} retries`
90 );
91 }
92
93 console.error(error);
94 });
95```
96
97Pass `{ retry: { enabled: false } }` to disable this plugin.
98
99## Contributing
100
101See [CONTRIBUTING.md](CONTRIBUTING.md)
102
103## License
104
105[MIT](LICENSE)