import {z} from 'zod' export type DataDump = {Hosts?: (Array<{arch?: string | undefined, bonds?: (Array<{bmc?: boolean | undefined, fqdn?: string | undefined, id?: (number | null) | undefined, ifname?: string | undefined, ip?: string | undefined, mac?: string | undefined, mtu?: number | undefined, peers?: Array | undefined, vlan?: string | undefined} | null>) | undefined, boot_image?: string | undefined, firmware?: string | undefined, id?: (number | null) | undefined, interfaces?: (Array<{bmc?: boolean | undefined, fqdn?: string | undefined, id?: (number | null) | undefined, ifname?: string | undefined, ip?: string | undefined, mac?: string | undefined, mtu?: number | undefined, vlan?: string | undefined} | null>) | undefined, name?: string | undefined, provision?: boolean | undefined, tags?: (Array | null) | undefined, uid?: (string | null) | undefined} | null>) | undefined, Images?: (Array<{cmdline?: string | undefined, id?: (number | null) | undefined, initrd?: Array | undefined, kernel?: string | undefined, liveimg?: string | undefined, name?: string | undefined, provision_templates?: ({[key: string]: string | null} | null) | undefined, uid?: (string | null) | undefined, verify?: boolean | undefined, version?: string | undefined} | null>) | undefined, Roles?: (Array<{name?: string | undefined, permission_list?: (Array<{method?: string | undefined, path?: string | undefined}>) | undefined, unassigned_permission_list?: (Array<{method?: string | undefined, path?: string | undefined}>) | undefined}>) | undefined, Users?: (Array<{created_at?: string | undefined, enabled?: boolean | undefined, hash?: string | undefined, id?: number | undefined, modified_at?: string | undefined, role?: string | undefined, username?: string | undefined}>) | undefined}; export type CreateApiV1DbRestoreArgs = {Accept?: string | undefined, body: DataDump}; export type CreateApiV1DbRestoreOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const genericResponse = z.object({changed: z.number().int().optional(), detail: z.string().optional(), title: z.string().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 }; /** * restore * * POST /v1/db/restore * * Also accepts auth: cookieAuth */ export const createApiV1DbRestore = async (args: CreateApiV1DbRestoreArgs, options?: CreateApiV1DbRestoreOptions) => { const {baseUrl = "https://127.0.0.1:8080", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (headerAuth !== undefined) headers.set('authorization', `Bearer ${headerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/db/restore${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(genericResponse, await res.json()) };