import {z} from 'zod' export type DuckLakeMaintenanceStepName = 'flush_inlined_data' | 'thin_snapshots' | 'expire_snapshots' | 'merge_adjacent_files' | 'rewrite_data_files' | 'cleanup_old_files' | 'delete_orphaned_files' | 'vacuum_catalog'; export type DuckLakeMaintenanceRunThreshold = 'any' | 'warning' | 'critical'; export type DuckLakeMaintenanceRequest = {steps?: (Array | null) | undefined, deleteThreshold?: (number | null) | undefined, expireSnapshotsOlderThan?: (string | null) | undefined, cleanupAll?: boolean | undefined, cpu?: string | undefined, memory?: string | undefined, timeoutSeconds?: number | undefined, thinKeepLast?: number | undefined, thinKeepDailyForDays?: number | undefined, vacuumFull?: boolean | undefined, mergeConcurrency?: number | undefined, runThreshold?: DuckLakeMaintenanceRunThreshold | undefined}; export type CreateApiV4AdminTeamsTeamIdMaintenanceArgs = {team_id: string | string, body: DuckLakeMaintenanceRequest | null}; export type CreateApiV4AdminTeamsTeamIdMaintenanceOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const duckDBJobStatus = z.enum(["pending", "running", "succeeded", "failed", "unknown"]); export const duckLakeMaintenanceJobResponse = z.object({jobId: z.string(), maintenanceId: z.string(), integrationId: z.union([z.string(), z.string()]), status: duckDBJobStatus}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseDuckLakeMaintenanceJobResponse = z.object({success: z.literal(true), data: duckLakeMaintenanceJobResponse, meta: responseMeta.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 }; /** * Submit a DuckLake maintenance job for a team * * POST /v4/admin/teams/{team_id}/maintenance */ export const createApiV4AdminTeamsTeamIdMaintenance = async (args: CreateApiV4AdminTeamsTeamIdMaintenanceArgs, options: CreateApiV4AdminTeamsTeamIdMaintenanceOptions) => { const {baseUrl, 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(/\/+$/, '')}/v4/admin/teams/${encodeURIComponent(String(args.team_id))}/maintenance${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(successResponseDuckLakeMaintenanceJobResponse, await res.json()) };