import {z} from 'zod' export type DataFormatEnum = 'ccda' | 'ccr' | 'qrda_1'; export type XmlFile = {base64_content?: string | undefined, content_type?: string | undefined, document?: string | undefined, external_content: unknown, id?: number | undefined, original_filename?: string | undefined}; export type XmlPatientSummary = {allergies_imported?: boolean | undefined, author_name?: (string | null) | undefined, authoring_practice: number, created_date?: (string | null) | undefined, data_format?: DataFormatEnum | undefined, deleted_date?: (string | null) | undefined, demographics_imported?: boolean | undefined, encounters_imported?: boolean | undefined, id?: number | undefined, immunizations_imported?: boolean | undefined, labs_imported?: boolean | undefined, medications_imported?: boolean | undefined, patient: number, problems_imported?: boolean | undefined, procedures_imported?: boolean | undefined, vitals_imported?: boolean | undefined, xml_file: XmlFile}; export type CreateApiApi20ClinicalDocumentsArgs = {body: XmlPatientSummary}; export type CreateApiApi20ClinicalDocumentsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const dataFormatEnum = z.enum(["ccda", "ccr", "qrda_1"]); export const xmlFile = z.object({base64_content: z.string().optional(), content_type: z.string().optional(), document: z.string().optional(), external_content: z.unknown(), id: z.number().int().optional(), original_filename: z.string().optional()}); export const xmlPatientSummary = z.object({allergies_imported: z.boolean().optional(), author_name: z.string().max(255).nullable().optional(), authoring_practice: z.number().int(), created_date: z.string().nullable().optional(), data_format: dataFormatEnum.optional(), deleted_date: z.string().nullable().optional(), demographics_imported: z.boolean().optional(), encounters_imported: z.boolean().optional(), id: z.number().int().optional(), immunizations_imported: z.boolean().optional(), labs_imported: z.boolean().optional(), medications_imported: z.boolean().optional(), patient: z.number().int(), problems_imported: z.boolean().optional(), procedures_imported: z.boolean().optional(), vitals_imported: z.boolean().optional(), xml_file: xmlFile}); 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 Clinical Document * * POST /api/2.0/clinical_documents/ */ export const createApiApi20ClinicalDocuments = async (args: CreateApiApi20ClinicalDocumentsArgs, options?: CreateApiApi20ClinicalDocumentsOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/clinical_documents/${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(xmlPatientSummary, await res.json()) };