import {z} from 'zod' export type GetApiApi20ProblemsIdArgs = {id: number}; export type GetApiApi20ProblemsIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const imoCodeRelation = z.object({icd10: z.array(z.string()).optional(), icd9: z.array(z.string()).optional(), snomed: z.string().optional()}); export const patientProblemStatusEnum = z.enum(["Active", "Controlled", "Resolved"]); export const verificationStatusEnum = z.enum(["confirmed", "differential", "entered_in_error", "provisional", "refuted", "unconfirmed"]); export const blankEnum = z.literal(""); export const nullEnum = z.unknown(); export const patientProblem = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), description: z.string().max(200), dx: z.array(imoCodeRelation), id: z.number().int().optional(), patient: z.number().int(), practice: z.number().int().optional(), rank: z.number().int().optional(), resolved_date: z.string().nullable().optional(), start_date: z.string(), status: patientProblemStatusEnum, synopsis: z.string().optional(), verification_status: z.union([verificationStatusEnum, blankEnum, nullEnum]).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 }; /** * Retrieve Problem * * GET /api/2.0/problems/{id}/ */ export const getApiApi20ProblemsId = async (args: GetApiApi20ProblemsIdArgs, options?: GetApiApi20ProblemsIdOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/problems/${encodeURIComponent(String(args.id))}/${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(patientProblem, await res.json()) };