import {z} from 'zod' export type DocumentSendRequest = {message?: string | undefined, subject?: string | undefined, silent?: boolean | undefined, sender?: ({membership_id?: string | undefined, email?: string | undefined}) | undefined, forwarding_settings?: ({forwarding_allowed?: boolean | undefined, forwarding_with_reassigning_allowed?: boolean | undefined}) | undefined, reply_to?: string | undefined, selected_approvers?: ({steps?: (Array<{id: string, group: {id: string, type: string, assignees: Array<{user?: string | undefined, is_selected?: boolean | undefined}>}}>) | undefined}) | undefined}; export type CreateApiPublicV1DocumentsIdSendArgs = {id: string, body: DocumentSendRequest}; export type CreateApiPublicV1DocumentsIdSendOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const recipientTypeEnum = z.enum(["signer", "CC", "approver"]); export const recipientDeliveryMethods = z.object({email: z.boolean().optional(), sms: z.boolean().optional()}).nullable(); export const documentSendResponse = z.object({id: z.string().optional(), name: z.string().optional(), status: z.string().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), date_completed: z.string().nullable().optional(), expiration_date: z.string().optional(), version: z.string().optional(), uuid: z.string().optional(), recipients: z.array(z.object({id: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), recipient_type: recipientTypeEnum.optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), delivery_methods: recipientDeliveryMethods.optional(), signing_order: z.number().int().nullable().optional(), shared_link: z.string().optional(), type: z.string().nullable().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 }; /** * Send Document * * POST /public/v1/documents/{id}/send * * Also accepts auth: oauth2 */ export const createApiPublicV1DocumentsIdSend = async (args: CreateApiPublicV1DocumentsIdSendArgs, options?: CreateApiPublicV1DocumentsIdSendOptions) => { const {baseUrl = "https://api.pandadoc.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/documents/${encodeURIComponent(String(args.id))}/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(documentSendResponse, await res.json()) };