web/src/utils/fetch-docker-instructions.ts
85 lines · 1947 bytes
1import { fetchEnrich } from './fetch-enrich';
2
3export const fetchDockerData = (
4 serviceName: string,
5): Promise<TemplateResponse | null> =>
6 fetchEnrich<TemplateResponse>(
7 'Docker',
8 `/v1/enrich/docker/${encodeURIComponent(serviceName)}`,
9 serviceName,
10 { missingIsExpected: true },
11 );
12
13export const dockerHubUrl = (image: string): string | null => {
14 const segments = image.split('/');
15 if (segments.length > 1 && /[.:]/.test(segments[0])) return null;
16 const path = image.replace(/:[^/]*$/, '');
17 const [namespace, repo] = path.includes('/')
18 ? path.split('/')
19 : ['library', path];
20 if (!repo) return null;
21 return namespace === 'library'
22 ? `https://hub.docker.com/_/${repo}`
23 : `https://hub.docker.com/r/${namespace}/${repo}`;
24};
25
26interface DockerTemplateEnvironmentVariable {
27 name: string;
28 label?: string;
29 default?: string;
30 description?: string;
31}
32
33interface DockerTemplateVolume {
34 container: string;
35 bind?: string;
36 readonly?: boolean;
37}
38
39interface DockerTemplate {
40 name?: string;
41 title?: string;
42 description?: string;
43 logo?: string;
44 image?: string;
45 categories?: string[];
46 ports?: string[];
47 env?: DockerTemplateEnvironmentVariable[];
48 volumes?: DockerTemplateVolume[];
49 restart_policy?: string;
50}
51
52interface DockerHubData {
53 user: string;
54 name: string;
55 namespace: string;
56 repository_type: string;
57 status: number;
58 description: string;
59 is_private: boolean;
60 is_automated: boolean;
61 can_edit: boolean;
62 star_count: number;
63 pull_count: number;
64 last_updated: string;
65 date_registered: string;
66 build_status: string;
67 permissions: {
68 read: boolean;
69 write: boolean;
70 admin: boolean;
71 };
72}
73
74interface DockerUsage {
75 dockerRunCommand: string;
76 dockerComposeFile: string;
77}
78
79export interface TemplateResponse {
80 found: boolean;
81 error: string | null;
82 template?: DockerTemplate;
83 dockerHubData?: DockerHubData | null;
84 usage?: DockerUsage;
85}