web/src/utils/fetch-docker-instructions.ts

86 lines · 1917 bytes

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