Skip to content

Commit 4c64c4c

Browse files
committed
docs: improve github-api utils
1 parent 1685007 commit 4c64c4c

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

docs/utils/github-api.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,44 @@ export const getLatestReleaseVersion = async (owner: string, repo: string): Prom
33
`https://api.github.com/repos/${owner}/${repo}/releases/latest`
44
)
55
.then((response) => response.json())
6-
.then((data) => {
7-
if (data.tag_name) {
8-
return data.tag_name.replace("v", "")
6+
.then((latestRelease) => {
7+
if (latestRelease.tag_name) {
8+
return latestRelease.tag_name.match(/(\d+\.\d+)[\-\.\da-zA-Z]+/)[0] ?? ""
99
} else {
1010
return fetch(
1111
`https://api.github.com/repos/${owner}/${repo}/tags`
1212
)
1313
.then((response) => response.json())
14-
.then((data) => {
15-
try {
16-
return data[0].name.replace("v", "")
17-
} catch {
18-
return "latest"
14+
.then((tags: any[]) => {
15+
if (tags.length) {
16+
// get latest tag version
17+
return tags[0].name.match(/(\d+\.\d+)[\-\.\da-zA-Z]+/)[0] ?? ""
1918
}
19+
return ""
20+
})
21+
}
22+
})
23+
};
24+
25+
export const getLatestRelease = async (owner: string, repo: string): Promise<string> => {
26+
return await fetch(
27+
`https://api.github.com/repos/${owner}/${repo}/releases/latest`
28+
)
29+
.then((response) => response.json())
30+
.then((latestRelease) => {
31+
if (latestRelease.prerelease === false) {
32+
return "latest"
33+
} else {
34+
return fetch(
35+
`https://api.github.com/repos/${owner}/${repo}/tags`
36+
)
37+
.then((response) => response.json())
38+
.then((tags: any[]) => {
39+
if (tags.length) {
40+
// get prerelease name (e.g. alpha) from the latest tag name
41+
return tags[0].name.match(/(?<=\.\d-)[^.]+/)[0] ?? ""
42+
}
43+
return ""
2044
})
2145
}
2246
})

0 commit comments

Comments
 (0)