import {z} from 'zod' export type CreateApiCreateDocumentArgs = {body: {automationId: string, docId?: string | undefined, data?: {[key: string]: unknown} | undefined, documentName?: string | undefined, async?: boolean | undefined, webhookParams?: {[key: string]: unknown} | undefined, existingPdfs?: Array | undefined, docTemplateLink?: string | undefined, wordTemplateLink?: string | undefined, wordTemplateBase64?: string | undefined, pdfTemplateLink?: string | undefined, pdfTemplateBase64?: string | undefined, recipients?: (Array<{signer?: number | undefined, email: string, name?: string | undefined}>) | undefined}}; export type CreateApiCreateDocumentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentCreationResponse = z.object({message: z.string().optional(), pdfUrl: z.string().optional(), documentName: z.string().optional(), googleDocUrl: z.string().optional(), savePdfGoogleDriveUrl: z.string().optional(), savePdfGoogleDriveFileId: z.string().optional(), signingSessionId: z.string().optional(), signingStatus: z.enum(["created", "queued", "awaiting_approval"]).optional(), approval: z.object({status: z.literal("pending").optional(), rule: z.enum(["all", "any"]).optional(), condition: z.object({}).nullable().optional(), approvers: z.array(z.object({name: z.string().optional(), email: z.string().optional()})).optional()}).optional(), signingLinks: z.array(z.object({signerIndex: z.number().int().optional(), email: z.string().optional(), name: z.string().optional(), signingUrl: z.string().optional(), status: z.string().optional()})).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 a document * * POST /createDocument */ export const createApiCreateDocument = async (args: CreateApiCreateDocumentArgs, options?: CreateApiCreateDocumentOptions) => { 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(/\/+$/, '')}/createDocument${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(documentCreationResponse, await res.json()) };