web/src/utils/fetch-docker-instructions.ts
87 lines · 2013 bytes
1import { error } from './logger';
2import { safeFetch } from './safe-fetch';
3import { apiBase, enrichHeaders } from './api-config';
4
5export const fetchDockerData = async (
6 serviceName: string,
7): Promise<TemplateResponse | null> => {
8 const endpoint = `${apiBase}/v1/enrich/docker/${encodeURIComponent(serviceName)}`;
9 try {
10 const res = await safeFetch(endpoint, { headers: enrichHeaders() });
11 if (!res.ok) {
12 error('Docker', `HTTP ${res.status} for ${serviceName} (${endpoint})`);
13 return null;
14 }
15 return await res.json();
16 } catch (err) {
17 error('Docker', `Network error for ${serviceName}: ${err}`);
18 return null;
19 }
20};
21
22interface DockerTemplatePort {
23 privatePort: number;
24 publicPort: number;
25 type: string; // Typically TCP/UDP
26}
27
28interface DockerTemplateEnvironmentVariable {
29 name: string;
30 label?: string;
31 default?: string;
32 description?: string;
33}
34
35interface DockerTemplateVolume {
36 bind: string;
37 container: string;
38 readonly?: boolean;
39}
40
41interface DockerTemplate {
42 name?: string;
43 title: string;
44 description?: string;
45 logo?: string;
46 image: string;
47 categories?: string[];
48 ports?: DockerTemplatePort[];
49 env?: DockerTemplateEnvironmentVariable[];
50 volumes?: DockerTemplateVolume[];
51 restart_policy?: string; // Typically "no", "always", "unless-stopped", "on-failure"
52}
53
54interface DockerHubData {
55 user: string;
56 name: string;
57 namespace: string;
58 repository_type: string;
59 status: number;
60 description: string;
61 is_private: boolean;
62 is_automated: boolean;
63 can_edit: boolean;
64 star_count: number;
65 pull_count: number;
66 last_updated: string;
67 date_registered: string;
68 build_status: string;
69 permissions: {
70 read: boolean;
71 write: boolean;
72 admin: boolean;
73 };
74}
75
76interface DockerUsage {
77 dockerRunCommand: string;
78 dockerComposeFile: string;
79}
80
81export interface TemplateResponse {
82 found: boolean;
83 error: string | null;
84 template?: DockerTemplate;
85 dockerHubData?: DockerHubData;
86 usage?: DockerUsage;
87}