Skip to content

Commit 6737473

Browse files
authored
Add retry logic to fetch (withastro#1303)
1 parent e8910fe commit 6737473

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"htmlparser2": "^7.2.0",
4949
"kleur": "^4.1.5",
5050
"node-fetch": "^3.2.10",
51+
"p-retry": "^5.1.1",
5152
"parse-numeric-range": "^1.3.0",
5253
"preact": "^10.10.1",
5354
"prettier": "^2.7.1",

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/lib/github-get.mjs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import fetch from 'node-fetch';
2+
import pRetry, { AbortError } from 'p-retry';
23

34
/**
45
* Fetch a URL from the GitHub API and resolve its JSON response.
56
* @param {{ url: string; githubToken?: string }} options
67
* @returns {Promise<any>}
78
*/
89
export async function githubGet({ url, githubToken = undefined }) {
9-
const headers = {
10-
Accept: 'application/vnd.github.v3+json',
11-
};
12-
if (githubToken) {
13-
headers.Authorization = `token ${githubToken}`;
14-
}
15-
const response = await fetch(url, {
16-
headers,
17-
});
18-
const json = await response.json();
10+
return await pRetry(
11+
async () => {
12+
const headers = {
13+
Accept: 'application/vnd.github.v3+json',
14+
};
15+
if (githubToken) {
16+
headers.Authorization = `token ${githubToken}`;
17+
}
18+
const response = await fetch(url, { headers });
19+
const json = await response.json();
1920

20-
if (!response.ok) {
21-
throw new Error(`GitHub API call failed: GET "${url}" returned status ${response.status}: ${JSON.stringify(json)}`);
22-
}
21+
if (!response.ok) {
22+
throw new AbortError(`GitHub API call failed: GET "${url}" returned status ${response.status}: ${JSON.stringify(json)}`);
23+
}
2324

24-
return json;
25+
return json;
26+
},
27+
{ retries: 5 }
28+
);
2529
}

0 commit comments

Comments
 (0)