| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame^] | 1 | import axios from "axios"; |
| 2 | import { z } from "zod"; |
| 3 | |
| 4 | export const DeployResponseSchema = z.object({ |
| 5 | id: z.string(), |
| 6 | deployKey: z.string(), |
| 7 | }); |
| 8 | |
| 9 | export type DeployResponse = z.infer<typeof DeployResponseSchema>; |
| 10 | |
| 11 | export class AppManager { |
| 12 | private baseUrl: string; |
| 13 | |
| 14 | constructor(baseUrl: string = "http://appmanager.hgrz-appmanager.svc.cluster.local") { |
| 15 | this.baseUrl = baseUrl; |
| 16 | } |
| 17 | |
| 18 | async deploy(config: unknown): Promise<DeployResponse> { |
| 19 | const response = await axios.request({ |
| 20 | url: `${this.baseUrl}/api/dodo-app`, |
| 21 | method: "post", |
| 22 | data: { config }, |
| 23 | }); |
| 24 | if (response.status !== 200) { |
| 25 | throw new Error(`Failed to deploy application: ${response.statusText}`); |
| 26 | } |
| 27 | const result = DeployResponseSchema.safeParse(response.data); |
| 28 | if (!result.success) { |
| 29 | throw new Error(`Invalid deploy response format: ${result.error.message}`); |
| 30 | } |
| 31 | return result.data; |
| 32 | } |
| 33 | |
| 34 | async update(instanceId: string, config: unknown): Promise<boolean> { |
| 35 | const response = await axios.request({ |
| 36 | url: `${this.baseUrl}/api/dodo-app/${instanceId}`, |
| 37 | method: "put", |
| 38 | data: { config }, |
| 39 | }); |
| 40 | return response.status === 200; |
| 41 | } |
| 42 | |
| 43 | async getStatus(instanceId: string): Promise<unknown> { |
| 44 | const response = await axios.request({ |
| 45 | url: `${this.baseUrl}/api/instance/${instanceId}/status`, |
| 46 | method: "get", |
| 47 | }); |
| 48 | if (response.status !== 200) { |
| 49 | throw new Error(`Failed to get application status: ${response.statusText}`); |
| 50 | } |
| 51 | return response.data; |
| 52 | } |
| 53 | |
| 54 | async removeInstance(instanceId: string): Promise<boolean> { |
| 55 | const response = await axios.request({ |
| 56 | url: `${this.baseUrl}/api/instance/${instanceId}/remove`, |
| 57 | method: "post", |
| 58 | }); |
| 59 | return response.status === 200; |
| 60 | } |
| 61 | } |