import {z} from 'zod' export type GetApiApi20PackagedMedicationsArgs = {is_active?: boolean | undefined, is_deleted?: boolean | undefined, limit?: number | undefined, name?: Array | undefined, ndc?: (Array) | undefined, offset?: number | undefined, order_by?: string | undefined, reference_medication_id?: Array | undefined}; export type GetApiApi20PackagedMedicationsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const packagedMedication = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), din: z.string().nullable().optional(), gpi: z.string().nullable().optional(), has_outer_packaged_drug: z.boolean().nullable().optional(), hri: z.string().nullable().optional(), id: z.number().int().optional(), is_active: z.boolean().optional(), is_clinic_pack: z.boolean().nullable().optional(), is_repackaged: z.boolean().nullable().optional(), labeler_id: z.number().int().optional(), market_end_date: z.string().nullable().optional(), medispanppid: z.string().nullable().optional(), multi_source: z.string().nullable().optional(), name: z.string().optional(), ndc: z.string().nullable().optional(), obsolete_date: z.string().nullable().optional(), packaging_name: z.string().nullable().optional(), reference_medication_id: z.number().int().optional(), size_description: z.string().nullable().optional(), upc: z.string().nullable().optional()}); export const paginatedPackagedMedicationList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(packagedMedication)}); 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 Packaged Medications * * GET /api/2.0/packaged_medications/ */ export const getApiApi20PackagedMedications = async (args: GetApiApi20PackagedMedicationsArgs, options?: GetApiApi20PackagedMedicationsOptions) => { 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.is_active != null) query.append("is_active", String(args.is_active)) if (args.is_deleted != null) query.append("is_deleted", String(args.is_deleted)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", args.name.join(',')) if (args.ndc != null) query.append("ndc", args.ndc.join(',')) 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.reference_medication_id != null) query.append("reference_medication_id", args.reference_medication_id.join(',')) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/packaged_medications/${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(paginatedPackagedMedicationList, await res.json()) };