import {z} from 'zod' export type GetApiApi20MedicationOrderTemplatesArgs = {deleted_date__gte?: string | undefined, deleted_date__isnull?: boolean | undefined, deleted_date__lte?: string | undefined, limit?: number | undefined, offset?: number | undefined, order_by?: string | undefined, practice?: Array | undefined}; export type GetApiApi20MedicationOrderTemplatesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const medicationDrugType = z.enum(["user", "medispan", "ss", "disp"]); export const medicationType = z.enum(["otc", "prescription", "discontinued"]); export const medication = z.object({active_ndcs: z.array(z.string()).optional(), brand_name: z.string().optional(), created_date: z.string().nullable().optional(), creation_type: medicationDrugType.optional(), form: z.string().optional(), generic_name: z.string().optional(), id: z.number().int().optional(), is_controlled: z.boolean().optional(), market_end_date: z.string().nullable().optional(), name: z.string().optional(), ndcs: z.array(z.string()).optional(), obsolete_date: z.string().nullable().optional(), practice: z.number().int().optional(), route: z.string().optional(), rxnorm_cuis: z.array(z.string()).optional(), strength: z.string().max(255).nullable().optional(), type: medicationType.optional()}); export const qtyUnitsEnum = z.enum(["BAG", "BOTTLE", "BOX", "CAPSULE", "CARTRIDGE", "CONTAINER", "DROP", "GRAM", "INHALER", "INTERNATIONAL UNITE", "KIT", "LITER", "LOZENGE", "MILLIGRAM", "MILLILITER", "MILLION UNITS", "MUTUALLY DEFINED", "FLUID OUNCE", "NOT SPECIFIED", "PACK", "PACKET", "PINT", "SUPPOSITORY", "SYRINGE", "TABLESPOON", "TABLET", "TEASPOON", "TRANSDERMAL PATCH", "TUBE", "UNIT", "VIAL", "EACH", "GUM", "AMPULE", "APPLICATOR", "APPLICATORFUL", "BAR", "BEAD", "BLISTER", "BLOCK", "BOLUS", "CAN", "CANISTER", "CAPLET", "CARTON", "CASE", "CASSETTE", "CYLINDER", "DISK", "DOSE PACK", "DUAL PACK", "FILM", "GALLON", "IMPLANT", "INHALATION", "INHALER REFILL", "INSERT", "INTRAVENOUS BAG", "KILOGRAM", "METRIC DROP", "MILLIMETER", "NEBULE", "NEEDLE FREE INJECTION", "OCULAR SYSTEM", "OUNCE", "PACKAGE", "PAD", "PAPER", "PATCH", "PEN NEEDLE", "POUCH", "POUND", "PRE-FILLED PEN SYRINGE", "PUFF", "QUART", "RING", "SACHET", "SCOOPFUL", "SPONGE", "SPRAY", "STICK", "STRIP", "SWAB", "TABMINDER", "TAMPON", "TRAY", "TROCHE", "WAFER"]); export const blankEnum = z.literal(""); export const nullEnum = z.unknown(); export const medOrderTemplate = z.object({auth_refills: z.number().int().gte(0).lte(99).nullable().optional(), deleted_date: z.string().nullable().optional(), directions: z.string().max(300).nullable().optional(), id: z.number().int().optional(), medication: medication, practice: z.number().int(), qty: z.string().nullable().optional(), qty_units: z.union([qtyUnitsEnum, blankEnum, nullEnum]).optional(), rank: z.number().int().optional(), specific_ndc: z.string().nullable().optional()}); export const paginatedMedOrderTemplateList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(medOrderTemplate)}); 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 }; /** * List Medication Order Templates * * GET /api/2.0/medication_order_templates/ */ export const getApiApi20MedicationOrderTemplates = async (args: GetApiApi20MedicationOrderTemplatesArgs, options?: GetApiApi20MedicationOrderTemplatesOptions) => { 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 (args.deleted_date__gte != null) query.append("deleted_date__gte", String(args.deleted_date__gte)) if (args.deleted_date__isnull != null) query.append("deleted_date__isnull", String(args.deleted_date__isnull)) if (args.deleted_date__lte != null) query.append("deleted_date__lte", String(args.deleted_date__lte)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.practice != null) query.append("practice", args.practice.join(',')) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/medication_order_templates/${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(paginatedMedOrderTemplateList, await res.json()) };