import {z} from 'zod' export type GetApiPatientsPatientIdPatientPotassiumReportArgs = {patient_id: number, date_since: string, date_until: string, disaggregated: boolean}; export type GetApiPatientsPatientIdPatientPotassiumReportOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patientNutrientDataRowResponse = z.object({avg: z.number().nullable(), count: z.number().nullable(), pct_of_overall: z.number().nullable(), stddev: z.number().nullable(), sum: z.number().nullable()}); export const patientNutrientDataResponse = z.object({data: patientNutrientDataRowResponse, name: z.enum(["overall", "breakfast", "lunch", "dinner", "snack", "morning", "lunchtime", "afternoon", "dinnertime", "evening"])}); export const patientNutrientResponse = z.object({date_since: z.string(), date_until: z.string(), frequency_high: z.array(z.string()).nullable(), frequency_low: z.array(z.string()).nullable(), meal_data: z.array(patientNutrientDataResponse), n_days_with_data: z.number(), nutrient: z.string(), overall_data: z.array(patientNutrientDataResponse), period_data: z.array(patientNutrientDataResponse), potassium_frequency_low_w_threshold: z.array(z.string()).nullable().optional(), statement: z.string(), unit: 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 }; /** * Get the patient's potassium details * * GET /patients/{patient_id}/patient_potassium_report */ export const getApiPatientsPatientIdPatientPotassiumReport = async (args: GetApiPatientsPatientIdPatientPotassiumReportArgs, options: GetApiPatientsPatientIdPatientPotassiumReportOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_since != null) query.append("date_since", String(args.date_since)) if (args.date_until != null) query.append("date_until", String(args.date_until)) if (args.disaggregated != null) query.append("disaggregated", String(args.disaggregated)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/patients/${encodeURIComponent(String(args.patient_id))}/patient_potassium_report${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(patientNutrientResponse, await res.json()) };