import {z} from 'zod' export type MedicationCode = {coding?: (Array<{display: string, code: string, system: string}> | null) | undefined, text: string}; export type Ingredient = {name?: (string | null) | undefined, strength?: (string | number | null) | undefined, strength_unit?: (string | null) | undefined}; export type MedicationEditableProperties = {encounter_id?: (string | null) | undefined, medication_code?: MedicationCode | undefined, ingredients?: (Array | null) | undefined, dose_form?: (string | null) | undefined, route?: (string | null) | undefined, frequency?: (string | null) | undefined, quantity?: (number | null) | undefined, period_start?: (string | null) | undefined, period_end?: (string | null) | undefined, instructions?: (string | null) | undefined, status?: ('active' | 'inactive') | undefined, source?: ('patient_reported' | 'external_provider' | 'clinic_practitioner') | undefined, external_provider_type?: ('family_doctor_gp' | 'specialist' | 'pharmacist' | 'nd' | 'other' | 'null' | null) | undefined, change_reason?: (string | null) | undefined, prescription_valid_until?: (string | null) | undefined, prescription_refills?: (number | null) | undefined, prescription_days_supply?: (number | null) | undefined, prescription_reason?: (string | null) | undefined, document_ids?: Array | undefined}; export type CreateMedicationRequest = {data: MedicationEditableProperties} | {[key: string]: unknown}; export type CreateApiApi20260101MedicalRecordMedicationsArgs = {body: CreateMedicationRequest}; export type CreateApiApi20260101MedicalRecordMedicationsOptions = {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 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 }; /** * Create a medication * * POST /api/2026-01-01/medical-record/medications */ export const createApiApi20260101MedicalRecordMedications = async (args: CreateApiApi20260101MedicalRecordMedicationsArgs, options?: CreateApiApi20260101MedicalRecordMedicationsOptions) => { const {baseUrl = "https://jdpdocsdemo.jane.qa", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(medication, await res.json()) };