import {z} from 'zod' export type ScopeConfig = {type: 'tags' | 'resource_group' | 'asset_types' | 'exclude_asset_types' | 'selected_resources', value: Array}; export type SnapshotQueryParams = {filters?: (Array<{key: 'policy_id' | 'status' | 'integration_id' | 'region' | 'is_latest', values: Array}>) | undefined, scopes?: 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 SnapshotSearchRequest = {query?: SnapshotQueryParams | undefined, page?: number | undefined, page_size?: number | undefined, facet_page?: number | undefined, facet_page_size?: number | undefined, facet_fields?: Array | undefined}; export type CreateApiBackupAndDrSnapshotsSearchArgs = {body: SnapshotSearchRequest}; export type CreateApiBackupAndDrSnapshotsSearchOptions = {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 }; /** * Search snapshots with filters * * POST /backup-and-dr/snapshots/search */ export const createApiBackupAndDrSnapshotsSearch = async (args: CreateApiBackupAndDrSnapshotsSearchArgs, options?: CreateApiBackupAndDrSnapshotsSearchOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/backup-and-dr/snapshots/search${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(snapshotListResponse, await res.json()) };