import {z} from 'zod' export type GetApiV3UsersBiosensingSpo2Args = {from?: string | undefined, to?: string | undefined}; export type GetApiV3UsersBiosensingSpo2Options = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const spo2TestResult = z.object({source_device_id: z.string().optional(), test_time: z.number().int().optional(), time_zone_offset: z.number().int().optional(), test_status: z.enum(["SPO2_TEST_PASSED", "SPO2_TEST_INCONCLUSIVE_TOO_LOW_QUALITY_IN_SAMPLES", "SPO2_TEST_INCONCLUSIVE_TOO_LOW_OVERALL_QUALITY", "SPO2_TEST_INCONCLUSIVE_TOO_MANY_MISSING_SAMPLES"]).optional(), blood_oxygen_percent: z.number().int().optional(), spo2_class: z.enum(["SPO2_CLASS_UNKNOWN", "SPO2_CLASS_VERY_LOW", "SPO2_CLASS_LOW", "SPO2_CLASS_NORMAL"]).optional(), spo2_value_deviation_from_baseline: z.enum(["DEVIATION_NO_BASELINE", "DEVIATION_BELOW_USUAL", "DEVIATION_USUAL", "DEVIATION_ABOVE_USUAL"]).optional(), spo2_quality_average_percent: z.number().optional(), average_heart_rate_bpm: z.number().int().optional(), heart_rate_variability_ms: z.number().optional(), spo2_hrv_deviation_from_baseline: z.enum(["DEVIATION_NO_BASELINE", "DEVIATION_BELOW_USUAL", "DEVIATION_USUAL", "DEVIATION_ABOVE_USUAL"]).optional(), altitude_meters: z.number().optional()}); export const getApiV3UsersBiosensingSpo2Response = z.array(spo2TestResult); 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 }; /** * SpO2 test result data * * GET /v3/users/biosensing/spo2 */ export const getApiV3UsersBiosensingSpo2 = async (args: GetApiV3UsersBiosensingSpo2Args, options?: GetApiV3UsersBiosensingSpo2Options) => { 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/spo2${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(getApiV3UsersBiosensingSpo2Response, await res.json()) };