import {z} from 'zod' export type DriftDetectionSchedule = {attributes: {'run-mode'?: ('refresh-only' | 'plan') | undefined, schedule: 'daily' | 'weekly', 'time-window'?: ('00:00-04:00' | '04:00-08:00' | '08:00-12:00' | '12:00-16:00' | '16:00-20:00' | '20:00-24:00' | null) | undefined, 'workspace-filters'?: ({'environment-types'?: (Array<'production' | 'staging' | 'testing' | 'development' | 'unmapped'>) | undefined, 'name-patterns'?: Array | undefined, tags?: Array | undefined}) | undefined}, id?: string | undefined, links?: {[key: string]: string} | undefined, relationships: {environment: {data: {id: string, type: 'environments' | ''}}}, type: 'drift-detection-schedule'}; export type DriftDetectionScheduleDocument = {data?: DriftDetectionSchedule | undefined, included?: Array> | undefined, meta?: Record | undefined}; export type CreateApiDriftDetectionSchedulesArgs = {fields?: ({'drift-detection-schedule'?: string | undefined}) | undefined, body: DriftDetectionScheduleDocument}; export type CreateApiDriftDetectionSchedulesOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const driftDetectionSchedule = z.object({attributes: z.object({"run-mode": z.enum(["refresh-only", "plan"]).optional(), schedule: z.enum(["daily", "weekly"]), "time-window": z.enum(["00:00-04:00", "04:00-08:00", "08:00-12:00", "12:00-16:00", "16:00-20:00", "20:00-24:00"]).nullable().optional(), "workspace-filters": z.object({"environment-types": z.array(z.enum(["production", "staging", "testing", "development", "unmapped"])).optional(), "name-patterns": z.array(z.string()).optional(), tags: z.array(z.string()).optional()}).optional()}), id: z.string().optional(), links: z.record(z.string(), z.string()).optional(), relationships: z.object({environment: z.object({data: z.object({id: z.string(), type: z.enum(["environments", ""])})})}), type: z.literal("drift-detection-schedule")}); export const driftDetectionScheduleDocument = z.object({data: driftDetectionSchedule.optional(), included: z.array(z.object({})).optional(), meta: z.object({}).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 }; /** * Create Drift Detection Schedule * * POST /drift-detection-schedules */ export const createApiDriftDetectionSchedules = async (args: CreateApiDriftDetectionSchedulesArgs, options?: CreateApiDriftDetectionSchedulesOptions) => { const {baseUrl = "https://example.scalr.io/api/iacp/v3", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args.fields != null) query.append("fields", JSON.stringify(args.fields)) if (APIKey !== undefined) headers.set('authorization', `Bearer ${APIKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/drift-detection-schedules${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(driftDetectionScheduleDocument, await res.json()) };