import {z} from 'zod' export type CreateApiCreateAutomationArgs = {body: {title: string, dataSourceName: 'Airtable' | 'API' | 'ClickUp' | 'Glide' | 'Google Sheets' | 'Make' | 'n8n' | 'Noloco' | 'SmartSuite' | 'Zapier', docTemplateLink?: string | undefined}}; export type CreateApiCreateAutomationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const automation = z.object({_id: z.string().optional(), title: z.string().optional(), dataSource: z.object({name: z.string().optional()}).optional(), dataSourceName: z.string().optional(), docTemplateLink: z.string().optional(), newDocumentNameField: z.string().optional(), isActive: z.boolean().optional(), isPreview: z.boolean().optional(), saveGoogleDoc: z.boolean().optional(), locale: z.string().optional(), formatNumbersWithLocale: z.boolean().optional(), pdfExpiration: z.string().nullable().optional(), dateCreated: z.string().optional(), lastPreviewPdf: z.string().optional(), options: z.object({lineDeletions: z.array(z.string()).optional(), markdownPlaceholders: z.array(z.string()).optional()}).optional(), esignature: z.object({enabled: z.boolean().optional(), closeType: z.enum(["signature", "acceptance"]).optional(), recipientCount: z.number().int().optional(), signers: z.array(z.object({signer: z.number().int().optional(), role: z.string().nullable().optional(), emailSource: z.enum(["static", "placeholder", "request"]).optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional()})).optional()}).optional()}); export const createApiCreateAutomationResponse = z.object({message: z.string().optional(), newAutomation: automation.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 automation * * POST /createAutomation */ export const createApiCreateAutomation = async (args: CreateApiCreateAutomationArgs, options?: CreateApiCreateAutomationOptions) => { const {baseUrl = "https://api.docsautomator.co", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/createAutomation${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(createApiCreateAutomationResponse, await res.json()) };