import {z} from 'zod' export type Recipient = {id?: string | undefined, _temp_id?: string | undefined, template_user_id?: string | undefined, first_name: string, last_name?: string | undefined, email: string, designation: 'Signer' | 'Approver' | 'CC', order?: number | undefined, phone_number?: (string | null) | undefined, street_address?: (string | null) | undefined, city?: (string | null) | undefined, state_province?: (string | null) | undefined, postal_code?: (string | null) | undefined, country?: (string | null) | undefined, title?: (string | null) | undefined, company?: (string | null) | undefined, custom_fields?: {[key: string]: unknown} | undefined}; export type AnchorTag = {anchor_string: string, type: 'signature' | 'initial' | 'initials' | 'text' | 'date' | 'checkbox' | 'radio_buttons' | 'radio' | 'dropdown' | 'textarea' | 'text_area' | 'url' | 'approval_signature' | 'approval_checkmark' | 'approval_date', recipient_id: number | string, x_offset?: number | undefined, y_offset?: number | undefined, offset_units?: ('percent' | 'pixels') | undefined, width?: number | undefined, height?: number | undefined, case_sensitive?: boolean | undefined, match_whole_word?: boolean | undefined, ignore_if_not_present?: boolean | undefined, occurrence?: number | undefined, remove_anchor_text?: boolean | undefined, add_white_background?: boolean | undefined, required?: boolean | undefined, read_only?: boolean | undefined, read_only_value?: (string | null) | undefined, variable_name?: (string | null) | undefined, variable_defined_name?: (string | null) | undefined, background_color?: (string | null) | undefined, font_size?: number | undefined, dropdown_options?: (Array | Record) | undefined, date_default?: (string | null) | undefined, date_signing_default?: boolean | undefined, multi_group_id?: (string | null) | undefined, document_index?: number | undefined}; export type SealParticipantInput = {seal_id?: string | undefined, order: number, temp_id: string}; export type CreateApiSigningRequestsCreateAndSendArgs = {body: {name: string, description?: string | undefined, document: string, template_id?: string | undefined, expiration_hours?: number | undefined, recipients?: Array | undefined, fields?: (Array<{recipient_id?: string | undefined, type: 'signature' | 'initial' | 'text' | 'date' | 'checkbox' | 'dropdown' | 'radio_buttons' | 'text_area' | 'url' | 'file' | 'stamp' | 'approval_signature' | 'approval_checkmark' | 'approval_date', page: number, x: number, y: number, width?: number | undefined, height?: number | undefined, variable_name?: ('first_name' | 'last_name' | 'full_name' | 'email' | 'phone_number' | 'street_address' | 'city' | 'state_province' | 'postal_code' | 'country' | 'title' | 'company') | undefined, variable_defined_name?: (string | null) | undefined, required?: boolean | undefined, read_only?: boolean | undefined, final_value?: string | undefined, background_color?: (string | null) | undefined, dropdown_options?: (Array | Record) | undefined, seal_participant_temp_id?: string | undefined, seal_attribute?: ('display_name' | 'signatory_title' | 'null' | null) | undefined}>) | undefined, anchor_tags?: Array | undefined, reminders?: Array<{hours_before_expiration: number}> | undefined, settings?: ({use_signing_order?: boolean | undefined, allow_download?: boolean | undefined, attach_pdf_on_finish?: boolean | undefined, send_signing_email?: boolean | undefined, send_finish_email?: boolean | undefined, send_expiration_email?: boolean | undefined, send_cancellation_email?: boolean | undefined, hand_drawn_only?: boolean | undefined, identity_editable_fields?: (Array<'name' | 'company' | 'title' | 'phone' | 'address'> | null) | undefined, notify_identity_change_webhook?: boolean | undefined, notify_identity_change_email?: boolean | undefined, allow_decline?: (boolean | null) | undefined, show_save_for_later?: (boolean | null) | undefined}) | undefined, document_id?: string | undefined, language?: ('en' | 'es' | 'it' | 'pt' | 'fr' | 'de' | 'el' | 'ru' | 'pl' | 'cs' | 'sv' | 'nl' | 'ro' | 'nb' | 'ca' | null) | undefined, completion_title?: (string | null) | undefined, completion_message?: (string | null) | undefined, completion_redirect_url?: (string | null) | undefined, completion_redirect_delay?: (number | null) | undefined, seal_participants?: (Array | null) | undefined, document_ids?: Array | undefined} | {name: string, description?: string | undefined, document?: string | undefined, template_id: string, expiration_hours?: number | undefined, recipients?: Array | undefined, fields?: (Array<{recipient_id?: string | undefined, type: 'signature' | 'initial' | 'text' | 'date' | 'checkbox' | 'dropdown' | 'radio_buttons' | 'text_area' | 'url' | 'file' | 'stamp' | 'approval_signature' | 'approval_checkmark' | 'approval_date', page: number, x: number, y: number, width?: number | undefined, height?: number | undefined, variable_name?: ('first_name' | 'last_name' | 'full_name' | 'email' | 'phone_number' | 'street_address' | 'city' | 'state_province' | 'postal_code' | 'country' | 'title' | 'company') | undefined, variable_defined_name?: (string | null) | undefined, required?: boolean | undefined, read_only?: boolean | undefined, final_value?: string | undefined, background_color?: (string | null) | undefined, dropdown_options?: (Array | Record) | undefined, seal_participant_temp_id?: string | undefined, seal_attribute?: ('display_name' | 'signatory_title' | 'null' | null) | undefined}>) | undefined, anchor_tags?: Array | undefined, reminders?: Array<{hours_before_expiration: number}> | undefined, settings?: ({use_signing_order?: boolean | undefined, allow_download?: boolean | undefined, attach_pdf_on_finish?: boolean | undefined, send_signing_email?: boolean | undefined, send_finish_email?: boolean | undefined, send_expiration_email?: boolean | undefined, send_cancellation_email?: boolean | undefined, hand_drawn_only?: boolean | undefined, identity_editable_fields?: (Array<'name' | 'company' | 'title' | 'phone' | 'address'> | null) | undefined, notify_identity_change_webhook?: boolean | undefined, notify_identity_change_email?: boolean | undefined, allow_decline?: (boolean | null) | undefined, show_save_for_later?: (boolean | null) | undefined}) | undefined, document_id?: string | undefined, language?: ('en' | 'es' | 'it' | 'pt' | 'fr' | 'de' | 'el' | 'ru' | 'pl' | 'cs' | 'sv' | 'nl' | 'ro' | 'nb' | 'ca' | null) | undefined, completion_title?: (string | null) | undefined, completion_message?: (string | null) | undefined, completion_redirect_url?: (string | null) | undefined, completion_redirect_delay?: (number | null) | undefined, seal_participants?: (Array | null) | undefined, document_ids?: Array | undefined} | {name: string, description?: string | undefined, document?: string | undefined, template_id?: string | undefined, expiration_hours?: number | undefined, recipients?: Array | undefined, fields?: (Array<{recipient_id?: string | undefined, type: 'signature' | 'initial' | 'text' | 'date' | 'checkbox' | 'dropdown' | 'radio_buttons' | 'text_area' | 'url' | 'file' | 'stamp' | 'approval_signature' | 'approval_checkmark' | 'approval_date', page: number, x: number, y: number, width?: number | undefined, height?: number | undefined, variable_name?: ('first_name' | 'last_name' | 'full_name' | 'email' | 'phone_number' | 'street_address' | 'city' | 'state_province' | 'postal_code' | 'country' | 'title' | 'company') | undefined, variable_defined_name?: (string | null) | undefined, required?: boolean | undefined, read_only?: boolean | undefined, final_value?: string | undefined, background_color?: (string | null) | undefined, dropdown_options?: (Array | Record) | undefined, seal_participant_temp_id?: string | undefined, seal_attribute?: ('display_name' | 'signatory_title' | 'null' | null) | undefined}>) | undefined, anchor_tags?: Array | undefined, reminders?: Array<{hours_before_expiration: number}> | undefined, settings?: ({use_signing_order?: boolean | undefined, allow_download?: boolean | undefined, attach_pdf_on_finish?: boolean | undefined, send_signing_email?: boolean | undefined, send_finish_email?: boolean | undefined, send_expiration_email?: boolean | undefined, send_cancellation_email?: boolean | undefined, hand_drawn_only?: boolean | undefined, identity_editable_fields?: (Array<'name' | 'company' | 'title' | 'phone' | 'address'> | null) | undefined, notify_identity_change_webhook?: boolean | undefined, notify_identity_change_email?: boolean | undefined, allow_decline?: (boolean | null) | undefined, show_save_for_later?: (boolean | null) | undefined}) | undefined, document_id?: string | undefined, language?: ('en' | 'es' | 'it' | 'pt' | 'fr' | 'de' | 'el' | 'ru' | 'pl' | 'cs' | 'sv' | 'nl' | 'ro' | 'nb' | 'ca' | null) | undefined, completion_title?: (string | null) | undefined, completion_message?: (string | null) | undefined, completion_redirect_url?: (string | null) | undefined, completion_redirect_delay?: (number | null) | undefined, seal_participants?: (Array | null) | undefined, document_ids: Array}}; export type CreateApiSigningRequestsCreateAndSendOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const signingRequestSettings = z.object({allow_download: z.boolean().optional(), attach_pdf_on_finish: z.boolean().optional(), allow_editing_before_sending: z.boolean().optional(), use_signing_order: z.boolean().optional(), hand_drawn_only: z.boolean().optional(), send_signing_email: z.boolean().optional(), send_finish_email: z.boolean().optional(), send_expiration_email: z.boolean().optional(), send_cancellation_email: z.boolean().optional(), require_otp_verification: z.boolean().nullable().optional(), disable_guided_navigation: z.boolean().nullable().optional(), allow_presigning_download: z.boolean().nullable().optional(), show_qr_code: z.boolean().nullable().optional(), allow_decline: z.boolean().nullable().optional(), show_save_for_later: z.boolean().nullable().optional(), identity_editable_fields: z.array(z.string()).nullable().optional(), notify_identity_change_email: z.boolean().optional()}); export const sealParticipantCreated = 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(), dropped: z.literal(false)}); export const sealParticipantDropped = z.object({id: z.literal("null").nullable(), order: z.literal(null).nullable(), seal: z.object({id: z.string(), lineage_id: z.string(), name: z.string(), version: z.number().int(), display_name: z.string()}).nullable(), seal_out_of_scope: z.literal(true), dropped: z.literal(true)}); export const createAndSendResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), status: z.literal("sent"), document_url: z.string().optional(), page_count: z.number().int().optional(), expiration_hours: z.number().int().optional(), settings: signingRequestSettings.optional(), created_date: z.string().optional(), sent_date: z.string().optional(), template_id: z.string().nullable().optional(), first_signer: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), signing_link: z.string().optional()}).optional(), recipients: z.array(z.object({id: z.string().optional(), first_name: z.string().optional(), last_name: z.string().nullable().optional(), name: z.string().optional(), email: z.string().optional(), designation: z.string().optional(), order: z.number().int().optional()})).optional(), fields: z.array(z.record(z.string(), z.unknown())).optional(), seal_participants: z.array(z.union([sealParticipantCreated, sealParticipantDropped])).optional(), credits_remaining: z.number().int().optional(), warnings: z.array(z.string()).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 and Send Signing Request (Atomic) * * POST /signing-requests/create-and-send */ export const createApiSigningRequestsCreateAndSend = async (args: CreateApiSigningRequestsCreateAndSendArgs, options?: CreateApiSigningRequestsCreateAndSendOptions) => { 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(/\/+$/, '')}/signing-requests/create-and-send${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(createAndSendResponse, await res.json()) };