import {z} from 'zod' export type CreateApiAutomationsAutomationIdEnableArgs = {automationId: string, body: {expectedRevision: number, projectId?: string | undefined}}; export type CreateApiAutomationsAutomationIdEnableOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAutomationsAutomationIdEnableResponse = z.object({automation: z.object({id: z.string(), configurationRevision: z.number().int().gt(0).lte(9007199254740991), templateProvenance: z.object({templateId: z.string().min(1), templateVersion: z.number().int().gt(0).lte(9007199254740991), resolvedInputs: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))}).nullable(), projectId: z.string(), name: z.string(), description: z.string().nullable(), kind: z.literal("script"), trigger: z.enum(["schedule", "webhook"]), webhook: z.object({hookId: z.string(), url: z.string().nullable(), lastDeliveryAt: z.string().nullable(), secretRotatedAt: z.string().nullable()}).nullable(), scriptId: z.string().nullable(), scriptName: z.string().nullable(), enabled: z.boolean(), cronExpression: z.string().nullable(), timezone: z.string(), timeoutSeconds: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), health: z.enum(["healthy", "failing", "never_run"]), lastRun: z.object({id: z.string(), automationRunKey: z.string(), status: z.enum(["running", "succeeded", "failed", "timed_out", "cancelled"]), startedAt: z.string(), completedAt: z.string().nullable(), durationMs: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), exitCode: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable()}).nullable(), successRate: z.number().nullable(), runCount: z.number().int().gte(-9007199254740991).lte(9007199254740991), failedRunCount: z.number().int().gte(-9007199254740991).lte(9007199254740991), createdAt: z.string(), updatedAt: z.string(), recordedHistoryStartsAt: z.string(), scriptIdentifier: z.string().nullable(), canToggle: z.boolean()})}); 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 }; /** * Enable an automation * * POST /automations/{automationId}/enable */ export const createApiAutomationsAutomationIdEnable = async (args: CreateApiAutomationsAutomationIdEnableArgs, options?: CreateApiAutomationsAutomationIdEnableOptions) => { const {baseUrl = "https://api.sazabi.com/v1", 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(/\/+$/, '')}/automations/${encodeURIComponent(String(args.automationId))}/enable${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(createApiAutomationsAutomationIdEnableResponse, await res.json()) };