import {z} from 'zod' export type PatientFamilyHistoryRelationshipEnum = 'Aunt' | 'Brother' | 'Daughter' | 'Father' | 'Grandfather' | 'Grandmother' | 'Mother' | 'Other' | 'Sister' | 'Son' | 'Uncle'; export type PatientFamilyHistory = {created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, icd9_code?: (string | null) | undefined, id?: number | undefined, patient: number, rank?: number | undefined, relationship: PatientFamilyHistoryRelationshipEnum, snomed_code?: (string | null) | undefined, text?: string | undefined}; export type CreateApiApi20FamilyHistoriesArgs = {body: PatientFamilyHistory}; export type CreateApiApi20FamilyHistoriesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const patientFamilyHistoryRelationshipEnum = z.enum(["Aunt", "Brother", "Daughter", "Father", "Grandfather", "Grandmother", "Mother", "Other", "Sister", "Son", "Uncle"]); export const patientFamilyHistory = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), icd9_code: z.string().nullable().optional(), id: z.number().int().optional(), patient: z.number().int(), rank: z.number().int().optional(), relationship: patientFamilyHistoryRelationshipEnum, snomed_code: z.string().nullable().optional(), text: z.string().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 }; /** * Create Family History * * POST /api/2.0/family_histories/ */ export const createApiApi20FamilyHistories = async (args: CreateApiApi20FamilyHistoriesArgs, options?: CreateApiApi20FamilyHistoriesOptions) => { 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/family_histories/${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(patientFamilyHistory, await res.json()) };