import {z} from 'zod' export type GetApiPatientsPatientIdStatsArgs = {patient_id: number, today_iso?: string | undefined, limit?: number | undefined}; export type GetApiPatientsPatientIdStatsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const careTeamExperienceMigrationStats = z.object({blueprint_reveal_eligible: z.boolean(), enabled_at: z.string(), enabled_local_date: z.string(), has_qualifying_meal_today: z.boolean(), patient_local_today: z.string(), qualifying_days_logged: z.number().int().gte(0).lte(3), third_qualifying_date: z.string().nullable()}); export const patientStatsResponse = z.object({care_team_experience_migration: careTeamExperienceMigrationStats.nullable().optional(), logged_meal_dates_in_window: z.array(z.string()).optional(), meal_count_in_window: z.number().int().optional(), meal_count_today_in_window: z.number().int().optional(), patient_id: z.number().int(), total_days_logged: z.number().int(), total_meals_logged: z.number().int()}); 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 all-time patient meal stats with optional bounded meal-window facts. * * GET /patients/{patient_id}/stats */ export const getApiPatientsPatientIdStats = async (args: GetApiPatientsPatientIdStatsArgs, options: GetApiPatientsPatientIdStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.today_iso != null) query.append("today_iso", String(args.today_iso)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/patients/${encodeURIComponent(String(args.patient_id))}/stats${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(patientStatsResponse, await res.json()) };