import {z} from 'zod' export type GetApiAutomationArgs = {automationId?: string | undefined, docId?: string | undefined}; export type GetApiAutomationOptions = {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 getApiAutomationResponse = z.object({automation: 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 }; /** * Get automation * * GET /automation */ export const getApiAutomation = async (args: GetApiAutomationArgs, options?: GetApiAutomationOptions) => { const {baseUrl = "https://api.docsautomator.co", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.automationId != null) query.append("automationId", String(args.automationId)) if (args.docId != null) query.append("docId", String(args.docId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/automation${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(getApiAutomationResponse, await res.json()) };