import {z} from 'zod' export type GetApiV2CycleCycleIdRecoveryArgs = {cycleId: number}; export type GetApiV2CycleCycleIdRecoveryOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const recoveryScore = z.object({user_calibrating: z.boolean(), recovery_score: z.number(), resting_heart_rate: z.number(), hrv_rmssd_milli: z.number(), spo2_percentage: z.number().optional(), skin_temp_celsius: z.number().optional()}); export const recovery = z.object({cycle_id: z.number().int(), sleep_id: z.string(), user_id: z.number().int(), created_at: z.string(), updated_at: z.string(), score_state: z.enum(["SCORED", "PENDING_SCORE", "UNSCORABLE"]), score: recoveryScore.optional()}); 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 recovery for a cycle * * GET /v2/cycle/{cycleId}/recovery */ export const getApiV2CycleCycleIdRecovery = async (args: GetApiV2CycleCycleIdRecoveryArgs, options?: GetApiV2CycleCycleIdRecoveryOptions) => { const {baseUrl = "https://api.prod.whoop.com/developer", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cycle/${encodeURIComponent(String(args.cycleId))}/recovery${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(recovery, await res.json()) };