40 lines
1013 B
TypeScript
40 lines
1013 B
TypeScript
type Tag = {
|
|
name: string;
|
|
digest: string;
|
|
};
|
|
|
|
class DockerHub {
|
|
getLatestVersions = async (imageName: string): Promise<string[]> => {
|
|
const imagePath = imageName.includes("/")
|
|
? imageName
|
|
: `library/${imageName}`;
|
|
|
|
const tagsResponse = await this.fetch(
|
|
`/${imagePath}/tags?page_size=10&ordering=last_updated`
|
|
).then((res) => res.json());
|
|
|
|
const tags: Tag[] = tagsResponse.results;
|
|
if (tags.length === 0) {
|
|
throw new Error(`No tags found for ${imageName}`);
|
|
}
|
|
|
|
const latestTag = tags.find((tag) => tag.name === "latest");
|
|
if (!latestTag) {
|
|
return [];
|
|
}
|
|
|
|
const latestTags = tags
|
|
.filter((tag) => tag.digest === latestTag.digest)
|
|
.map((tag) => tag.name);
|
|
return latestTags;
|
|
};
|
|
|
|
private fetch = async (url: string): Promise<Response> => {
|
|
const fullUrl = `https://hub.docker.com/v2/repositories${url}`;
|
|
console.debug("-> Docker: Fetching", fullUrl);
|
|
return fetch(fullUrl);
|
|
};
|
|
}
|
|
|
|
export default DockerHub;
|