import {z} from 'zod' export type AutomationOnJobEndEventTriggerConfig = {type?: 'on_job_end' | undefined, job_id?: (string | string | null) | undefined}; export type AutomationOnSyncEndEventTriggerConfig = {type?: 'on_sync_end' | undefined, src_integration_id?: (string | string | null) | undefined}; export type AutomationOnWebhookEventTriggerConfig = {type?: 'webhook' | undefined}; export type AutomationEventTriggerDetailsSchema = {type?: 'event' | undefined, config: AutomationOnJobEndEventTriggerConfig | AutomationOnSyncEndEventTriggerConfig | AutomationOnWebhookEventTriggerConfig}; export type AutomationCronScheduledTriggerConfig = {type?: 'cron' | undefined, cron?: string | undefined}; export type AutomationScheduledTriggerDetailsSchema = {type?: 'schedule' | undefined, config: AutomationCronScheduledTriggerConfig}; export type AutomationNoneTriggerDetailsSchema = {type?: 'none' | undefined}; export type AutomationNoneDestinationSchema = {skipIfEmpty?: boolean | undefined, type?: 'none' | undefined}; export type AutomationSlackDestinationSchema = {integrationId?: (string | string | null) | undefined, format?: ('json' | 'parquet' | 'txt' | 'csv' | 'png' | 'pdf' | 'auto') | undefined, skipIfEmpty?: boolean | undefined, type?: 'slack' | undefined, channelId?: (string | null) | undefined}; export type AutomationEmailDestinationSchema = {format?: ('json' | 'parquet' | 'txt' | 'csv' | 'png' | 'pdf' | 'auto') | undefined, skipIfEmpty?: boolean | undefined, type?: 'email' | undefined, address?: (string | null) | undefined}; export type AutomationSheetsDestinationSchema = {integrationId?: (string | string | null) | undefined, skipIfEmpty?: boolean | undefined, type?: 'sheets' | undefined, spreadsheetId?: (string | null) | undefined, sheet?: (string | null) | undefined, header?: boolean | undefined, range?: (string | null) | undefined, overwriteSheet?: (boolean | null) | undefined, overwriteRange?: (boolean | null) | undefined}; export type AutomationExcelDestinationSchema = {integrationId?: (string | string | null) | undefined, skipIfEmpty?: boolean | undefined, type?: 'excel' | undefined, workbookId?: (string | null) | undefined, worksheet?: (string | null) | undefined, header?: boolean | undefined, range?: (string | null) | undefined, overwriteSheet?: (boolean | null) | undefined, overwriteRange?: (boolean | null) | undefined}; export type AutomationGcsDestinationSchema = {format?: ('json' | 'parquet' | 'txt' | 'csv' | 'png' | 'pdf' | 'auto') | undefined, integrationId?: (string | string | null) | undefined, skipIfEmpty?: boolean | undefined, type?: 'gcs' | undefined, bucket?: (string | null) | undefined, blobName?: (string | null) | undefined}; export type AutomationWebhookDestinationSchema = {skipIfEmpty?: boolean | undefined, type?: 'webhook' | undefined, url: string}; export type AutomationFiThreadDestinationSchema = {skipIfEmpty?: boolean | undefined, type?: 'fi_thread' | undefined, thread_id?: (string | null) | undefined, row_cap?: number | undefined, slack_integration_id?: (string | null) | undefined, slack_channel_id?: (string | null) | undefined, slack_template?: ({[key: string]: unknown} | null) | undefined}; export type UpdateAutomationJobRequest = {name?: (string | null) | undefined, trigger?: (AutomationEventTriggerDetailsSchema | AutomationScheduledTriggerDetailsSchema | AutomationNoneTriggerDetailsSchema | null) | undefined, destination?: (AutomationNoneDestinationSchema | AutomationSlackDestinationSchema | AutomationEmailDestinationSchema | AutomationSheetsDestinationSchema | AutomationExcelDestinationSchema | AutomationGcsDestinationSchema | AutomationWebhookDestinationSchema | AutomationFiThreadDestinationSchema | null) | undefined, enabled?: (boolean | null) | undefined, maxAttempts?: (number | null) | undefined, details?: ({[key: string]: unknown} | null) | undefined}; export type PatchApiV4AutomationsJobsJobIdArgs = {job_id: string | string, body: UpdateAutomationJobRequest}; export type PatchApiV4AutomationsJobsJobIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const automationOnJobEndEventTriggerConfig = z.object({type: z.literal("on_job_end").optional(), job_id: z.union([z.string(), z.string()]).nullable().optional()}); export const automationOnSyncEndEventTriggerConfig = z.object({type: z.literal("on_sync_end").optional(), src_integration_id: z.union([z.string(), z.string()]).nullable().optional()}); export const automationOnWebhookEventTriggerConfig = z.object({type: z.literal("webhook").optional()}); export const automationEventTriggerDetailsSchema = z.object({type: z.literal("event").optional(), config: z.discriminatedUnion("type", [automationOnJobEndEventTriggerConfig, automationOnSyncEndEventTriggerConfig, automationOnWebhookEventTriggerConfig])}); export const automationCronScheduledTriggerConfig = z.object({type: z.literal("cron").optional(), cron: z.string().optional()}); export const automationScheduledTriggerDetailsSchema = z.object({type: z.literal("schedule").optional(), config: automationCronScheduledTriggerConfig}); export const automationNoneTriggerDetailsSchema = z.object({type: z.literal("none").optional()}); export const automationNoneDestinationSchema = z.object({skipIfEmpty: z.boolean().optional(), type: z.literal("none").optional()}); export const automationSlackDestinationSchema = z.object({integrationId: z.union([z.string(), z.string()]).nullable().optional(), format: z.enum(["json", "parquet", "txt", "csv", "png", "pdf", "auto"]).optional(), skipIfEmpty: z.boolean().optional(), type: z.literal("slack").optional(), channelId: z.string().nullable().optional()}); export const automationEmailDestinationSchema = z.object({format: z.enum(["json", "parquet", "txt", "csv", "png", "pdf", "auto"]).optional(), skipIfEmpty: z.boolean().optional(), type: z.literal("email").optional(), address: z.string().nullable().optional()}); export const automationSheetsDestinationSchema = z.object({integrationId: z.union([z.string(), z.string()]).nullable().optional(), skipIfEmpty: z.boolean().optional(), type: z.literal("sheets").optional(), spreadsheetId: z.string().nullable().optional(), sheet: z.string().nullable().optional(), header: z.boolean().optional(), range: z.string().nullable().optional(), overwriteSheet: z.boolean().nullable().optional(), overwriteRange: z.boolean().nullable().optional()}); export const automationExcelDestinationSchema = z.object({integrationId: z.union([z.string(), z.string()]).nullable().optional(), skipIfEmpty: z.boolean().optional(), type: z.literal("excel").optional(), workbookId: z.string().nullable().optional(), worksheet: z.string().nullable().optional(), header: z.boolean().optional(), range: z.string().nullable().optional(), overwriteSheet: z.boolean().nullable().optional(), overwriteRange: z.boolean().nullable().optional()}); export const automationGcsDestinationSchema = z.object({format: z.enum(["json", "parquet", "txt", "csv", "png", "pdf", "auto"]).optional(), integrationId: z.union([z.string(), z.string()]).nullable().optional(), skipIfEmpty: z.boolean().optional(), type: z.literal("gcs").optional(), bucket: z.string().nullable().optional(), blobName: z.string().nullable().optional()}); export const automationWebhookDestinationSchema = z.object({skipIfEmpty: z.boolean().optional(), type: z.literal("webhook").optional(), url: z.string()}); export const automationFiThreadDestinationSchema = z.object({skipIfEmpty: z.boolean().optional(), type: z.literal("fi_thread").optional(), thread_id: z.string().nullable().optional(), row_cap: z.number().int().optional(), slack_integration_id: z.string().nullable().optional(), slack_channel_id: z.string().nullable().optional(), slack_template: z.record(z.string(), z.unknown()).nullable().optional()}); export const automationJobsSchema = z.object({id: z.union([z.string(), z.string()]).optional(), team_id: z.union([z.string(), z.string()]), name: z.string(), resource_id: z.union([z.string(), z.string()]), resource_type: z.enum(["doc", "ducklake_integration_maintenance"]), trigger: z.discriminatedUnion("type", [automationEventTriggerDetailsSchema, automationScheduledTriggerDetailsSchema, automationNoneTriggerDetailsSchema]), destination: z.union([automationNoneDestinationSchema, automationSlackDestinationSchema, automationEmailDestinationSchema, automationSheetsDestinationSchema, automationExcelDestinationSchema, automationGcsDestinationSchema, automationWebhookDestinationSchema, automationFiThreadDestinationSchema]).nullable().optional(), enabled: z.boolean().optional(), disabled_by: z.enum(["user", "definite-admin"]).nullable().optional(), max_attempts: z.number().int().optional(), details: z.record(z.string(), z.unknown()).optional(), created_at: z.string().optional()}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseAutomationJobsSchema = z.object({success: z.literal(true), data: automationJobsSchema, 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 }; /** * Update job * * PATCH /v4/automations/jobs/{job_id} */ export const patchApiV4AutomationsJobsJobId = async (args: PatchApiV4AutomationsJobsJobIdArgs, options: PatchApiV4AutomationsJobsJobIdOptions) => { 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/automations/jobs/${encodeURIComponent(String(args.job_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(successResponseAutomationJobsSchema, await res.json()) };