import {z} from 'zod' export type CallDocuSignDraftUpdate = {values?: {[key: string]: unknown} | undefined, recipient_name?: (string | null) | undefined, recipient_email?: (string | null) | undefined}; export type PatchApiCallsCallIdDocusignDraftsDraftIdArgs = {call_id: string, draft_id: string, body: CallDocuSignDraftUpdate}; export type PatchApiCallsCallIdDocusignDraftsDraftIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const docuSignDraftField = z.object({tab_label: z.string(), display_name: z.string(), description: z.string().nullable().optional(), input_kind: z.string(), section: z.string().nullable().optional(), address_group: z.string().nullable().optional(), required: z.boolean().optional(), max_length: z.number().int().nullable().optional(), value: z.unknown(), ai_value: z.unknown()}); export const callDocuSignDraftResponse = z.object({docusign_form_draft_id: z.string(), call_id: z.string(), docusign_form_id: z.string().nullable().optional(), template_name: z.string().nullable().optional(), status: z.string(), error: z.string().nullable().optional(), recipient_name: z.string().nullable().optional(), recipient_email: z.string().nullable().optional(), fields: z.array(docuSignDraftField).optional(), placeholder_recipient_email: z.string().nullable().optional(), docusign_envelope_id: z.string().nullable().optional(), envelope_status: z.string().nullable().optional(), envelope_created_at: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().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 }; /** * Update Call Docusign Draft * * PATCH /calls/{call_id}/docusign-drafts/{draft_id} */ export const patchApiCallsCallIdDocusignDraftsDraftId = async (args: PatchApiCallsCallIdDocusignDraftsDraftIdArgs, options: PatchApiCallsCallIdDocusignDraftsDraftIdOptions) => { const {baseUrl, 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(/\/+$/, '')}/calls/${encodeURIComponent(String(args.call_id))}/docusign-drafts/${encodeURIComponent(String(args.draft_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(callDocuSignDraftResponse, await res.json()) };