import {z} from 'zod' export type GetApiApi20260101MedicalRecordMedicationsArgs = {'page[limit]'?: number | undefined, 'page[cursor]'?: string | undefined, sort?: string | undefined, 'patient_id[eq]'?: string | undefined, 'status[eq]'?: ('active' | 'inactive' | 'pending') | undefined}; export type GetApiApi20260101MedicalRecordMedicationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const medicationCode = z.object({coding: z.array(z.object({display: z.string(), code: z.string(), system: z.string()})).nullable().optional(), text: z.string()}); export const ingredient = z.object({name: z.string().nullable().optional(), strength: z.union([z.string(), z.number()]).nullable().optional(), strength_unit: z.string().nullable().optional()}); export const medication = z.object({id: z.string(), patient_id: z.string(), practitioner_id: z.string().nullable().optional(), practitioner_name: z.string().nullable().optional(), encounter_id: z.string().nullable().optional(), medication_code: medicationCode, ingredients: z.array(ingredient).nullable().optional(), dose_form: z.string().nullable().optional(), route: z.string().nullable().optional(), frequency: z.string().nullable().optional(), quantity: z.number().int().gte(1).nullable().optional(), period_start: z.string().nullable().optional(), period_end: z.string().nullable().optional(), instructions: z.string().nullable().optional(), status: z.enum(["active", "inactive", "pending"]), source: z.enum(["prescribed", "patient_reported", "external_provider", "clinic_practitioner"]), external_provider_type: z.enum(["family_doctor_gp", "specialist", "pharmacist", "nd", "other", "null"]).nullable().optional(), change_reason: z.string().nullable().optional(), prescription_valid_until: z.string().nullable().optional(), prescription_refills: z.number().int().gte(0).nullable().optional(), prescription_days_supply: z.number().int().gte(1).nullable().optional(), prescription_reason: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), documents: z.array(z.object({id: z.string()}))}); export const medicationsResponse = z.object({items: z.array(medication), limit: z.number().int(), prescriptions_last_synced: z.string().nullable().optional(), dosespot_onboarded: z.boolean().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 }; /** * List medications * * GET /api/2026-01-01/medical-record/medications */ export const getApiApi20260101MedicalRecordMedications = async (args: GetApiApi20260101MedicalRecordMedicationsArgs, options?: GetApiApi20260101MedicalRecordMedicationsOptions) => { const {baseUrl = "https://jdpdocsdemo.jane.qa", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[limit]'] != null) query.append("page[limit]", String(args['page[limit]'])) if (args['page[cursor]'] != null) query.append("page[cursor]", String(args['page[cursor]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args['patient_id[eq]'] != null) query.append("patient_id[eq]", String(args['patient_id[eq]'])) if (args['status[eq]'] != null) query.append("status[eq]", String(args['status[eq]'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2026-01-01/medical-record/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(medicationsResponse, await res.json()) };