import {z} from 'zod' export type GetApiBackupAndDrSnapshotsArgs = {page?: number | undefined, page_size?: number | undefined, policy_id?: Array | undefined, status?: (Array<'Pending' | 'In Progress' | 'Success' | 'Failed'>) | undefined, integration_id?: Array | undefined, region?: Array | undefined, from_date?: string | undefined, to_date?: string | undefined, sort_by?: ('status' | 'snapshot_time' | 'created_at') | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiBackupAndDrSnapshotsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scopeConfig = z.object({type: z.enum(["tags", "resource_group", "asset_types", "exclude_asset_types", "selected_resources"]), value: z.array(z.string())}); export const applicationScope = z.array(scopeConfig); export const snapshotStatus = z.enum(["Pending", "In Progress", "Success", "Failed"]); export const changedATR = z.object({integration_id: z.string(), asset_type: z.string(), regions: z.array(z.string())}); export const snapshot = z.object({account_id: z.string(), snapshot_id: z.string(), snapshot_time: z.string().optional(), policy_id: z.string(), provider_type: z.string().max(50).optional(), integration_id: z.string().optional(), region: z.string().optional(), trigger: z.object({type: z.enum(["scheduled", "on-demand"]).optional(), initiated_by: z.string().optional()}).optional(), scope: applicationScope.optional(), status: snapshotStatus.optional(), is_latest: z.boolean().optional(), created_at: z.string().optional(), asset_types: z.array(z.string()).optional(), changed_atrs: z.array(changedATR).optional(), error_code: z.enum(["SCOPE_EMPTY", "INTERNAL_ERROR"]).nullable().optional()}); export const pagination = z.object({page: z.number().int(), page_size: z.number().int(), total: z.number().int(), has_next: z.boolean(), has_prev: z.boolean()}); export const facetValue = z.object({value: z.string().optional(), count: z.number().int().optional()}); export const facet = z.object({field: z.string(), size: z.number().int(), pagination: pagination, values: z.array(facetValue)}); export const snapshotListResponse = z.object({data: z.array(snapshot).optional(), pagination: pagination.optional(), summary: z.object({total_snapshots: z.number().int().optional(), successful_snapshots: z.number().int().optional(), failed_snapshots: z.number().int().optional(), in_progress_snapshots: z.number().int().optional()}).optional(), facets: z.array(facet).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 snapshots * * GET /backup-and-dr/snapshots */ export const getApiBackupAndDrSnapshots = async (args: GetApiBackupAndDrSnapshotsArgs, options?: GetApiBackupAndDrSnapshotsOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) for (const item of args.policy_id ?? []) query.append("policy_id", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.integration_id ?? []) query.append("integration_id", String(item)) for (const item of args.region ?? []) query.append("region", String(item)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) const url = `${baseUrl.replace(/\/+$/, '')}/backup-and-dr/snapshots${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(snapshotListResponse, await res.json()) };