import {z} from 'zod' export type CreateApiFinalizeV2AgreementsArgs = {body: {data: {name: string, kind: 'draft' | 'intake' | 'request', template_id: string, agreement_file?: ({name?: string | undefined}) | undefined, tokens?: (Array<{id: string, value: string | number} | {id: string, selected: Array<{id: string}>}>) | undefined, answers?: (Array<{id: string, value: string | number} | {id: string, selected: Array<{id: string, custom_description?: string | undefined}>} | {id: string, files: Array<{name: string}>}>) | undefined, tables_values?: (Array<{id?: string | undefined, start_token: string, end_token: string, total_tokens: Record, row_tokens: Array>}>) | undefined, source?: ({source_name: string, metadata: {source_record_id?: string | undefined, source_record_name?: string | undefined, source_record_url?: string | undefined, color?: string | undefined}}) | undefined}}}; export type CreateApiFinalizeV2AgreementsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiFinalizeV2AgreementsResponse = z.object({data: z.object({id: z.string(), name: z.string(), created_at: z.string(), status_updated_at: z.string().nullable(), kind: z.enum(["draft", "intake", "request", "sign", "webform"]), analyze_agreement_id: z.string().nullable(), template_id: z.string().nullable(), author_id: z.string(), owner_id: z.string().nullable(), collaborator_ids: z.array(z.string()), status_id: z.string().nullable(), question_ids: z.array(z.string()), token_value_ids: z.array(z.string()), pen: z.object({assigned_to_counterparty: z.boolean().optional(), holder_ids: z.array(z.string()).optional()}).optional()}), _links: z.object({agreement_file_upload: z.object({href: z.string(), operation: z.string(), file: z.string()}), answer_file_uploads: z.array(z.object({href: z.string(), operation: z.string(), file: z.string()})).optional(), agreement_uploads_complete: z.object({href: z.string(), operation: z.string(), body: z.object({data: z.object({upload_complete: z.boolean()})})})})}); 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 Agreement * * POST /finalize/v2/agreements */ export const createApiFinalizeV2Agreements = async (args: CreateApiFinalizeV2AgreementsArgs, options?: CreateApiFinalizeV2AgreementsOptions) => { const {baseUrl = "https://api.linksquares.com/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("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/finalize/v2/agreements${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(createApiFinalizeV2AgreementsResponse, await res.json()) };