import {z} from 'zod' export type GetApiV3UsersSleepwiseAlertnessOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const alertnessHourlyData = z.object({validity: z.enum(["VALIDITY_UNKNOWN", "VALIDITY_RESET", "VALIDITY_NOT_VALID", "VALIDITY_ESTIMATE", "VALIDITY_VALID"]).optional(), alertness_level: z.enum(["ALERTNESS_LEVEL_UNKNOWN", "ALERTNESS_LEVEL_MINIMAL", "ALERTNESS_LEVEL_VERY_LOW", "ALERTNESS_LEVEL_LOW", "ALERTNESS_LEVEL_HIGH", "ALERTNESS_LEVEL_VERY_HIGH"]).optional(), start_time: z.string().optional(), end_time: z.string().optional()}); export const alertness = z.object({grade: z.number().optional(), grade_validity_seconds: z.number().int().optional(), grade_type: z.enum(["GRADE_TYPE_UNKNOWN", "GRADE_TYPE_PRIMARY", "GRADE_TYPE_ADDITIONAL"]).optional(), grade_classification: z.enum(["GRADE_CLASSIFICATION_UNKNOWN", "GRADE_CLASSIFICATION_WEAK", "GRADE_CLASSIFICATION_FAIR", "GRADE_CLASSIFICATION_STRONG", "GRADE_CLASSIFICATION_EXCELLENT"]).optional(), validity: z.enum(["VALIDITY_UNKNOWN", "VALIDITY_RESET", "VALIDITY_NOT_VALID", "VALIDITY_ESTIMATE", "VALIDITY_VALID"]).optional(), sleep_inertia: z.enum(["SLEEP_INERTIA_UNKNOWN", "SLEEP_INERTIA_NO_INERTIA", "SLEEP_INERTIA_MILD", "SLEEP_INERTIA_MODERATE", "SLEEP_INERTIA_HEAVY"]).optional(), sleep_type: z.enum(["SLEEP_TYPE_UNKNOWN", "SLEEP_TYPE_PRIMARY", "SLEEP_TYPE_SECONDARY", "SLEEP_TYPE_ARTIFICIAL"]).optional(), result_type: z.enum(["ALERTNESS_TYPE_UNKNOWN", "ALERTNESS_TYPE_PREDICTION", "ALERTNESS_TYPE_HISTORY"]).optional(), period_start_time: z.string().optional(), period_end_time: z.string().optional(), sleep_period_start_time: z.string().optional(), sleep_period_end_time: z.string().optional(), sleep_timezone_offset_minutes: z.number().int().optional(), hourly_data: z.array(alertnessHourlyData).optional()}); export const getApiV3UsersSleepwiseAlertnessResponse = z.array(alertness); 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 }; /** * Alertness period data (last 28 days) * * GET /v3/users/sleepwise/alertness */ export const getApiV3UsersSleepwiseAlertness = async (options?: GetApiV3UsersSleepwiseAlertnessOptions) => { 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/users/sleepwise/alertness${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(getApiV3UsersSleepwiseAlertnessResponse, await res.json()) };