import {z} from 'zod' export type GetApiV3UsersBiosensingEcgArgs = {from?: string | undefined, to?: string | undefined}; export type GetApiV3UsersBiosensingEcgOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const ecgSample = z.object({recording_time_delta_ms: z.number().int().optional(), amplitude_mv: z.number().optional()}); export const qualityMeasurement = z.object({recording_time_delta_ms: z.number().int().optional(), quality_level: z.enum(["ECG_QUALITY_UNKNOWN", "ECG_QUALITY_NO_CONTACT", "ECG_QUALITY_LOW", "ECG_QUALITY_HIGH"]).optional()}); export const ecgTestResult = z.object({source_device_id: z.string().optional(), test_time: z.number().int().optional(), time_zone_offset: z.number().int().optional(), average_heart_rate_bpm: z.number().int().optional(), heart_rate_variability_ms: z.number().optional(), heart_rate_variability_level: z.enum(["ECG_HRV_LEVEL_NO_BASELINE", "ECG_HRV_LEVEL_BELOW_USUAL", "ECG_HRV_LEVEL_USUAL", "ECG_HRV_LEVEL_ABOVE_USUAL"]).optional(), rri_ms: z.number().optional(), pulse_transit_time_systolic_ms: z.number().optional(), pulse_transit_time_diastolic_ms: z.number().optional(), pulse_transit_time_quality_index: z.number().optional(), samples: z.array(ecgSample).optional(), quality_measurements: z.array(qualityMeasurement).optional()}); export const getApiV3UsersBiosensingEcgResponse = z.array(ecgTestResult); 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 }; /** * Wrist ECG test result data * * GET /v3/users/biosensing/ecg */ export const getApiV3UsersBiosensingEcg = async (args: GetApiV3UsersBiosensingEcgArgs, options?: GetApiV3UsersBiosensingEcgOptions) => { const {baseUrl = "https://www.polaraccesslink.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/users/biosensing/ecg${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(getApiV3UsersBiosensingEcgResponse, await res.json()) };