import {z} from 'zod' export type BlockType = 'DYNAMIC' | 'DEMOGRAPHICS' | 'INSURANCE' | 'PAYMENT_METHODS' | 'MEDICAL_HISTORY'; export type QuestionType = 'ADDRESS' | 'DATE' | 'TEXT' | 'NUMBER' | 'TEXT_AREA' | 'SINGLE_SELECT' | 'RADIAL_SINGLE_SELECT' | 'MULTI_SELECT' | 'RADIAL_MULTI_SELECT' | 'TOGGLE' | 'SIGNATURE' | 'FILE_UPLOAD' | 'PHONE_NUMBER' | 'EMAIL' | 'PAYMENT_METHOD' | 'TAG' | 'DISCLAIMER' | 'STATEMENT' | 'DOCUMENT' | 'FEET_INCHES_HEIGHT' | 'POUNDS_WEIGHT' | 'CALCULATED_SCORE'; export type Address = {name: string, description?: (string | null) | undefined}; export type RelativeDate = {years?: (number | null) | undefined, months?: (number | null) | undefined, days?: (number | null) | undefined}; export type DateInput = {name: string, description?: (string | null) | undefined, minDate?: (string | null) | undefined, maxDate?: (string | null) | undefined, minTimeAgo?: (RelativeDate | null) | undefined}; export type Text = {name: string, description?: (string | null) | undefined, placeholder: string}; export type SingleSelect = {name: string, description?: (string | null) | undefined, options: Array}; export type MultiSelect = {name: string, description?: (string | null) | undefined, options: Array, maxSelections?: (number | null) | undefined, minSelections?: (number | null) | undefined}; export type Toggle = {name: string, description?: (string | null) | undefined}; export type Signature = {name: string, description?: (string | null) | undefined, text?: (string | null) | undefined}; export type FileUpload = {name: string, description?: (string | null) | undefined, allowedFileTypes: string, maxFileSize: number, maxFileCount: number}; export type PaymentMethod = {name: string, description?: (string | null) | undefined}; export type Tag = {name: string, description?: (string | null) | undefined, placeholder: string, allowedTags?: (Array | null) | undefined}; export type Disclaimer = {name: string, description?: (string | null) | undefined, content: string}; export type Statement = {name: string, description?: (string | null) | undefined, points: Array}; export type DocumentDetails = {name: string, path: string}; export type Document = {name: string, description?: (string | null) | undefined, docs: Array}; export type Number = {name: string, description?: (string | null) | undefined, placeholder: string}; export type CalculatedScore = {name: string, description?: (string | null) | undefined, displayFormat?: string | undefined, calculationType?: string | undefined}; export type ConditionOperator = 'EQUALS' | 'NOT_EQUALS' | 'CONTAINS' | 'YEARS_AGO_MINIMUM' | 'YEARS_AGO_MAXIMUM' | 'SUM'; export type QuestionConditionDetail = {id: string, questionId: string, dependentQuestionId: string, operator: ConditionOperator, value: string, groupId?: (string | null) | undefined}; export type QuestionDetailUnionAddressDateTextSingleSelectMultiSelectToggleSignatureFileUploadPaymentMethodTagDisclaimerStatementDocumentNumberCalculatedScoreInput = {id: string, blockId: string, formId: string, position: number, questionType: QuestionType, required: boolean, isArchived?: boolean | undefined, mappingKey?: (string | null) | undefined, data: Address | DateInput | Text | SingleSelect | MultiSelect | Toggle | Signature | FileUpload | PaymentMethod | Tag | Disclaimer | Statement | Document | Number | CalculatedScore, conditions?: Array | undefined}; export type BlockDetailInput = {id: string, formId: string, position: number, description?: (string | null) | undefined, blockType: BlockType, name: string, questions?: Array | undefined, isArchived?: boolean | undefined}; export type UpdateApiFormsFormFormIdInsertBlockArgs = {TENANT: string, 'API-KEY': string, form_id: string, body: BlockDetailInput}; export type UpdateApiFormsFormFormIdInsertBlockOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const blockType = z.enum(["DYNAMIC", "DEMOGRAPHICS", "INSURANCE", "PAYMENT_METHODS", "MEDICAL_HISTORY"]); export const questionType = z.enum(["ADDRESS", "DATE", "TEXT", "NUMBER", "TEXT_AREA", "SINGLE_SELECT", "RADIAL_SINGLE_SELECT", "MULTI_SELECT", "RADIAL_MULTI_SELECT", "TOGGLE", "SIGNATURE", "FILE_UPLOAD", "PHONE_NUMBER", "EMAIL", "PAYMENT_METHOD", "TAG", "DISCLAIMER", "STATEMENT", "DOCUMENT", "FEET_INCHES_HEIGHT", "POUNDS_WEIGHT", "CALCULATED_SCORE"]); export const address = z.object({name: z.string(), description: z.string().nullable().optional()}); export const relativeDate = z.object({years: z.number().int().nullable().optional(), months: z.number().int().nullable().optional(), days: z.number().int().nullable().optional()}); export const dateOutput = z.object({name: z.string(), description: z.string().nullable().optional(), minDate: z.string().nullable().optional(), maxDate: z.string().nullable().optional(), minTimeAgo: relativeDate.nullable().optional()}); export const text = z.object({name: z.string(), description: z.string().nullable().optional(), placeholder: z.string()}); export const singleSelect = z.object({name: z.string(), description: z.string().nullable().optional(), options: z.array(z.string())}); export const multiSelect = z.object({name: z.string(), description: z.string().nullable().optional(), options: z.array(z.string()), maxSelections: z.number().int().nullable().optional(), minSelections: z.number().int().nullable().optional()}); export const toggle = z.object({name: z.string(), description: z.string().nullable().optional()}); export const signature = z.object({name: z.string(), description: z.string().nullable().optional(), text: z.string().nullable().optional()}); export const fileUpload = z.object({name: z.string(), description: z.string().nullable().optional(), allowedFileTypes: z.string(), maxFileSize: z.number().int(), maxFileCount: z.number().int()}); export const paymentMethod = z.object({name: z.string(), description: z.string().nullable().optional()}); export const tag = z.object({name: z.string(), description: z.string().nullable().optional(), placeholder: z.string(), allowedTags: z.array(z.string()).nullable().optional()}); export const disclaimer = z.object({name: z.string(), description: z.string().nullable().optional(), content: z.string()}); export const statement = z.object({name: z.string(), description: z.string().nullable().optional(), points: z.array(z.string())}); export const documentDetails = z.object({name: z.string(), path: z.string()}); export const document = z.object({name: z.string(), description: z.string().nullable().optional(), docs: z.array(documentDetails)}); export const number = z.object({name: z.string(), description: z.string().nullable().optional(), placeholder: z.string()}); export const calculatedScore = z.object({name: z.string(), description: z.string().nullable().optional(), displayFormat: z.string().optional(), calculationType: z.string().optional()}); export const conditionOperator = z.enum(["EQUALS", "NOT_EQUALS", "CONTAINS", "YEARS_AGO_MINIMUM", "YEARS_AGO_MAXIMUM", "SUM"]); export const questionConditionDetail = z.object({id: z.string(), questionId: z.string(), dependentQuestionId: z.string(), operator: conditionOperator, value: z.string(), groupId: z.string().nullable().optional()}); export const questionDetailUnionAddressDateTextSingleSelectMultiSelectToggleSignatureFileUploadPaymentMethodTagDisclaimerStatementDocumentNumberCalculatedScoreOutput = z.object({id: z.string(), blockId: z.string(), formId: z.string(), position: z.number().int(), questionType: questionType, required: z.boolean(), isArchived: z.boolean().optional(), mappingKey: z.string().nullable().optional(), data: z.union([address, dateOutput, text, singleSelect, multiSelect, toggle, signature, fileUpload, paymentMethod, tag, disclaimer, statement, document, number, calculatedScore]), conditions: z.array(questionConditionDetail).optional()}); export const blockDetailOutput = z.object({id: z.string(), formId: z.string(), position: z.number().int(), description: z.string().nullable().optional(), blockType: blockType, name: z.string(), questions: z.array(questionDetailUnionAddressDateTextSingleSelectMultiSelectToggleSignatureFileUploadPaymentMethodTagDisclaimerStatementDocumentNumberCalculatedScoreOutput).optional(), isArchived: z.boolean().optional()}); export const formDetailOutput = z.object({id: z.string(), name: z.string(), pathName: z.string(), description: z.string().nullable().optional(), blocks: z.array(blockDetailOutput).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 }; /** * Insert Block * * PUT /forms/form/{form_id}/insert-block */ export const updateApiFormsFormFormIdInsertBlock = async (args: UpdateApiFormsFormFormIdInsertBlockArgs, options: UpdateApiFormsFormFormIdInsertBlockOptions) => { 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") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) const url = `${baseUrl.replace(/\/+$/, '')}/forms/form/${encodeURIComponent(String(args.form_id))}/insert-block${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(formDetailOutput, await res.json()) };