import {z} from 'zod' export type GetApiApi20MedicationHistoryDownloadsIdArgs = {id: number}; export type GetApiApi20MedicationHistoryDownloadsIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const errorCodeEnum = z.enum(["Communication problem - try again later", "Receiver System Error", "Receiver unable to process", "Transaction rejected"]); export const nullEnum = z.unknown(); export const errorDescriptionCodeEnum = z.enum(["Internal processing error has occurred.", "Invalid password for receiver", "Invalid password for sender.", "No password on file for receiver.", "No password on file for sender.", "Receiver ID not on file.", "Request timed out before response could be received.", "Required segment UIB is missing.", "Required segment UIH is missing.", "Required segment UIT is missing.", "Required segment UIZ is missing.", "Sender ID not on file."]); export const rxHistoryDownload = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), eligibility: z.number().int().nullable().optional(), error_code: z.union([errorCodeEnum, nullEnum]).optional(), error_description: z.string().nullable().optional(), error_description_code: z.union([errorDescriptionCodeEnum, nullEnum]).optional(), from_date: z.string().nullable().optional(), id: z.number().int().optional(), is_active: z.boolean().optional(), num_items: z.string().optional(), patient: z.number().int(), pbm_member_id: z.string().max(100).nullable().optional(), pbm_name: z.string().optional(), pbm_surescripts_participant_id: z.string().optional(), practice: z.number().int(), signed_by: z.number().int().nullable().optional(), signed_date: z.string().nullable().optional(), success: z.boolean().optional(), until_date: z.string().nullable().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 Medication History Download * * GET /api/2.0/medication_history_downloads/{id}/ */ export const getApiApi20MedicationHistoryDownloadsId = async (args: GetApiApi20MedicationHistoryDownloadsIdArgs, options?: GetApiApi20MedicationHistoryDownloadsIdOptions) => { 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/medication_history_downloads/${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(rxHistoryDownload, await res.json()) };