import {z} from 'zod' export type GetApiDbaasServiceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dbaasIntegration = z.object({description: z.string().optional(), settings: z.object({}).optional(), type: z.string().optional(), "is-enabled": z.boolean().optional(), source: z.string().optional(), "is-active": z.boolean().optional(), status: z.string().optional(), id: z.string().optional(), dest: z.string().optional()}); export const dbaasServiceName = z.string().max(63); export const dbaasServiceTypeName = z.string().max(64); export const enumServiceState = z.enum(["running", "rebuilding", "rebalancing", "poweroff"]); export const dbaasServiceNotification = z.object({level: z.enum(["warning", "notice"]), message: z.string().min(1).max(1024), type: z.enum(["service_powered_off_removal", "service_end_of_life"]), metadata: z.object({})}); export const dbaasServiceCommon = z.object({"updated-at": z.string().optional(), "node-count": z.number().int().gte(0).optional(), "node-cpu-count": z.number().int().gte(0).optional(), integrations: z.array(dbaasIntegration).optional(), zone: z.string().optional(), name: dbaasServiceName, type: dbaasServiceTypeName, state: enumServiceState.optional(), "termination-protection": z.boolean().optional(), notifications: z.array(dbaasServiceNotification).optional(), "disk-size": z.number().int().gte(0).optional(), "node-memory": z.number().int().gte(0).optional(), "created-at": z.string().optional(), plan: z.string()}); export const getApiDbaasServiceResponse = z.object({"dbaas-services": z.array(dbaasServiceCommon).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List DBaaS services * * GET /dbaas-service */ export const getApiDbaasService = async (options?: GetApiDbaasServiceOptions) => { const {baseUrl = "https://api-ch-gva-2.exoscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/dbaas-service${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiDbaasServiceResponse, await res.json()) };