import {z} from 'zod' export type GetApiApiV3AutomationsAutomationIdArgs = {automation_id: string}; export type GetApiApiV3AutomationsAutomationIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const automationType = z.enum(["ranked_institutions", "vendor_contract_enrichment", "outreach_sequence", "deep_research", "contacts_export"]); export const automationStatus = z.enum(["active", "archived", "deleted"]); export const automationStatItem = z.object({label: z.string(), value: z.string()}); export const automationStats = z.object({items: z.array(automationStatItem)}); export const automationExecutionStatus = z.enum(["pending", "executing", "completed", "failed", "cancelled"]); export const latestRunSummary = z.object({id: z.string(), status: automationExecutionStatus, created_at: z.string(), completed_at: z.string().nullable().optional(), result_status_counts: z.record(z.string(), z.number().int())}); export const automationWithStats = z.object({id: z.string(), name: z.string(), type: automationType, status: automationStatus, config: z.record(z.string(), z.unknown()), created_at: z.string(), updated_at: z.string(), stats: automationStats, latest_run: latestRunSummary.nullable().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 }; /** * Get Automation * * GET /api/v3/automations/{automation_id} */ export const getApiApiV3AutomationsAutomationId = async (args: GetApiApiV3AutomationsAutomationIdArgs, options: GetApiApiV3AutomationsAutomationIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/automations/${encodeURIComponent(String(args.automation_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(automationWithStats, await res.json()) };