import {z} from 'zod' export type GetApiFormsFormPathArgs = {TENANT: string, 'API-KEY': string, form_path: string, patient_id: string}; export type GetApiFormsFormPathOptions = {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 questionData = z.object({name: z.string(), description: z.string().nullable().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 questionDetailOutput = 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: questionData, conditions: z.array(questionConditionDetail).optional()}); export const answerDetail = z.object({question: questionDetailOutput.nullable().optional(), data: z.union([z.string(), z.boolean(), z.array(z.string()), z.record(z.string(), z.string().nullable()), z.number()]).nullable().optional()}); export const answeredBlockDetail = z.object({blockType: blockType, name: z.string(), position: z.number().int(), description: z.string().nullable().optional(), id: z.string(), answers: z.array(answerDetail)}); export const answeredFormDetail = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), patientId: z.string().nullable(), blocks: z.array(answeredBlockDetail), completed: z.boolean().nullable().optional(), isCompleted: z.boolean().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 }; /** * Get Form * * GET /forms/{form_path} */ export const getApiFormsFormPath = async (args: GetApiFormsFormPathArgs, options: GetApiFormsFormPathOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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'])) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) const url = `${baseUrl.replace(/\/+$/, '')}/forms/${encodeURIComponent(String(args.form_path))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(answeredFormDetail, await res.json()) };