-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
github.icon-repository.ts
69 lines (60 loc) · 2.08 KB
/
github.icon-repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { parse } from "path";
import { fetchWithTimeout } from "@homarr/common";
import type { IconRepositoryLicense } from "../types/icon-repository-license";
import type { RepositoryIconGroup } from "../types/repository-icon-group";
import { IconRepository } from "./icon-repository";
export class GitHubIconRepository extends IconRepository {
constructor(
public readonly name: string,
public readonly slug: string,
public readonly license: IconRepositoryLicense,
public readonly repositoryUrl?: URL,
public readonly repositoryIndexingUrl?: URL,
public readonly repositoryBlobUrlTemplate?: string,
) {
super(name, slug, license, repositoryUrl, repositoryIndexingUrl, repositoryBlobUrlTemplate);
}
protected async getAllIconsInternalAsync(): Promise<RepositoryIconGroup> {
if (!this.repositoryIndexingUrl || !this.repositoryBlobUrlTemplate) {
throw new Error("Repository URLs are required for this repository");
}
const response = await fetchWithTimeout(this.repositoryIndexingUrl);
const listOfFiles = (await response.json()) as GitHubApiResponse;
return {
success: true,
icons: listOfFiles.tree
.filter(({ path }) =>
this.allowedImageFileTypes.some((allowedImageFileType) => parse(path).ext === allowedImageFileType),
)
.map(({ path, size: sizeInBytes, sha: checksum }) => {
const file = parse(path);
const fileNameWithExtension = file.base;
const imageUrl = new URL(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.repositoryBlobUrlTemplate!.replace("{0}", path).replace("{1}", file.name),
);
return {
imageUrl,
fileNameWithExtension,
local: false,
sizeInBytes,
checksum,
};
}),
slug: this.slug,
};
}
}
interface GitHubApiResponse {
sha: string;
url: string;
tree: TreeItem[];
truncated: boolean;
}
export interface TreeItem {
path: string;
mode: string;
sha: string;
url: string;
size?: number;
}