import {z} from 'zod' export type PatchApiTemplatesIdArgs = {id: string, body: {name?: string | undefined, description?: string | undefined, document?: string | undefined, document_id?: string | undefined, expiration_hours?: number | undefined, settings?: ({allow_editing_before_sending?: boolean | undefined, attach_pdf_on_finish?: boolean | undefined, allow_download?: boolean | undefined, hand_drawn_only?: boolean | undefined, require_otp_verification?: (boolean | null) | undefined, disable_guided_navigation?: (boolean | null) | undefined, allow_presigning_download?: (boolean | null) | undefined, show_qr_code?: (boolean | null) | undefined, allow_decline?: (boolean | null) | undefined, show_save_for_later?: (boolean | null) | undefined}) | undefined} | {user: {id?: string | undefined, first_name: string, last_name?: string | undefined, email: string, designation: 'Signer' | 'Approver' | 'CC', order: number, phone_number?: string | undefined, street_address?: string | undefined, city?: string | undefined, state_province?: string | undefined, postal_code?: string | undefined, country?: string | undefined, title?: string | undefined, company?: string | undefined}} | {field: {id?: string | undefined, type?: ('text' | 'signature' | 'date' | 'checkbox' | 'initial' | 'initials' | 'dropdown' | 'radio_buttons' | 'textarea' | 'text_area' | 'url' | 'file' | 'stamp' | 'approval_signature' | 'approval_checkmark' | 'approval_date') | undefined, x?: number | undefined, y?: number | undefined, width?: number | undefined, height?: number | undefined, page?: number | undefined, required?: boolean | undefined, assigned_to_user_id?: (string | null) | undefined, variable_name?: string | undefined, variable_defined_name?: (string | null) | undefined, options?: Array | undefined, format_rules?: Record | undefined, multi_group_id?: string | undefined, default_to_signing_date?: boolean | undefined, read_only?: boolean | undefined, read_only_value?: string | undefined}, documents_layout_version?: number | undefined} | {seal_participant: {id?: string | undefined, seal_id?: string | undefined, order?: number | undefined}}}; export type PatchApiTemplatesIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templatePatchResponse = z.object({message: z.string(), updated_fields: z.array(z.string()), user: z.object({}).optional()}); export const sealParticipantApi = z.object({id: z.string(), order: z.number().int().gte(1), seal: z.object({id: z.string(), lineage_id: z.string(), name: z.string(), version: z.number().int(), display_name: z.string()}), status: z.enum(["pending", "paused", "applied"]), applied_at: z.string().nullable(), paused_at: z.string().nullable(), pause_reason: z.enum(["seal_revoked_stop_pending", "seal_integrity_mismatch", "null"]).nullable(), swapped_from_seal_id: z.string().nullable(), triggered_by_recipient_id: z.string().nullable(), sender_kind: z.enum(["dashboard", "api_key", "embedded", "null"]).nullable(), seal_out_of_scope: z.boolean()}); export const patchApiTemplatesIdResponse = z.union([templatePatchResponse, z.object({seal_participant: sealParticipantApi})]); 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 }; /** * Partially Update Template * * PATCH /templates/{id} */ export const patchApiTemplatesId = async (args: PatchApiTemplatesIdArgs, options?: PatchApiTemplatesIdOptions) => { const {baseUrl = "https://api.firma.dev/functions/v1/signing-request-api", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/templates/${encodeURIComponent(String(args.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(patchApiTemplatesIdResponse, await res.json()) };