@thebriangao/totem 1.4.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dev.vars.example +5 -0
- package/.dockerignore +5 -0
- package/.env.example +29 -20
- package/CHANGELOG.md +99 -1
- package/CONTRIBUTING.md +48 -116
- package/Dockerfile +46 -12
- package/README.md +172 -498
- package/SECURITY.md +36 -59
- package/TOOLS.md +448 -311
- package/WHOOP.md +497 -2659
- package/assets/brand/SOURCES.md +38 -0
- package/assets/brand/totem.png +0 -0
- package/assets/brand/totem.svg +13 -0
- package/assets/fonts/Manrope-OFL.txt +93 -0
- package/assets/fonts/manrope-latin.woff2 +0 -0
- package/dist/build_info.js +5 -0
- package/dist/build_info.js.map +1 -0
- package/dist/cli/app.js +675 -0
- package/dist/cli/app.js.map +1 -0
- package/dist/cli/auth.js +297 -0
- package/dist/cli/auth.js.map +1 -0
- package/dist/cli/auth_flow.js +268 -0
- package/dist/cli/auth_flow.js.map +1 -0
- package/dist/cli/browser_login.js +368 -0
- package/dist/cli/browser_login.js.map +1 -0
- package/dist/cli/clients.js +403 -0
- package/dist/cli/clients.js.map +1 -0
- package/dist/cli/deploy_record.js +96 -0
- package/dist/cli/deploy_record.js.map +1 -0
- package/dist/cli/index.js +39 -575
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/links.js +28 -0
- package/dist/cli/links.js.map +1 -0
- package/dist/cli/logo.js +63 -0
- package/dist/cli/logo.js.map +1 -0
- package/dist/cli/setup.js +1157 -1017
- package/dist/cli/setup.js.map +1 -1
- package/dist/cli/spawn.js +80 -0
- package/dist/cli/spawn.js.map +1 -0
- package/dist/cli/sys.js +219 -0
- package/dist/cli/sys.js.map +1 -0
- package/dist/cli/ui.js +712 -463
- package/dist/cli/ui.js.map +1 -1
- package/dist/cli/update.js +425 -282
- package/dist/cli/update.js.map +1 -1
- package/dist/cloudflare/token_store.js +48 -0
- package/dist/cloudflare/token_store.js.map +1 -0
- package/dist/cloudflare/worker.js +155 -0
- package/dist/cloudflare/worker.js.map +1 -0
- package/dist/data/behaviors.js +2733 -617
- package/dist/data/behaviors.js.map +1 -1
- package/dist/data/endpoint_catalog.js +2681 -0
- package/dist/data/endpoint_catalog.js.map +1 -0
- package/dist/data/exercises.js +373 -4859
- package/dist/data/exercises.js.map +1 -1
- package/dist/data/garments.js +19 -0
- package/dist/data/garments.js.map +1 -0
- package/dist/data/sports.js +219 -208
- package/dist/data/sports.js.map +1 -1
- package/dist/lib/activity_list.js +123 -0
- package/dist/lib/activity_list.js.map +1 -0
- package/dist/lib/activity_records.js +195 -0
- package/dist/lib/activity_records.js.map +1 -0
- package/dist/lib/activity_results.js +44 -0
- package/dist/lib/activity_results.js.map +1 -0
- package/dist/lib/activity_types.js +164 -0
- package/dist/lib/activity_types.js.map +1 -0
- package/dist/lib/brand.js +63 -0
- package/dist/lib/brand.js.map +1 -0
- package/dist/lib/clock.js +24 -0
- package/dist/lib/clock.js.map +1 -0
- package/dist/lib/coach_session.js +345 -0
- package/dist/lib/coach_session.js.map +1 -0
- package/dist/lib/concurrency.js.map +1 -1
- package/dist/lib/dates.js +19 -0
- package/dist/lib/dates.js.map +1 -1
- package/dist/lib/env_file.js +195 -0
- package/dist/lib/env_file.js.map +1 -0
- package/dist/lib/health_plans_write.js +44 -0
- package/dist/lib/health_plans_write.js.map +1 -0
- package/dist/lib/heart_rate.js +34 -0
- package/dist/lib/heart_rate.js.map +1 -0
- package/dist/lib/highlight.js +81 -0
- package/dist/lib/highlight.js.map +1 -0
- package/dist/lib/install_links.js +47 -0
- package/dist/lib/install_links.js.map +1 -0
- package/dist/lib/instant.js +50 -0
- package/dist/lib/instant.js.map +1 -0
- package/dist/lib/journal_behaviors.js +382 -0
- package/dist/lib/journal_behaviors.js.map +1 -0
- package/dist/lib/lift_catalog.js +218 -0
- package/dist/lib/lift_catalog.js.map +1 -0
- package/dist/lib/lift_history.js +66 -0
- package/dist/lib/lift_history.js.map +1 -0
- package/dist/lib/lift_sets.js +146 -0
- package/dist/lib/lift_sets.js.map +1 -0
- package/dist/lib/lift_tool.js +20 -0
- package/dist/lib/lift_tool.js.map +1 -0
- package/dist/lib/profile_settings.js +101 -0
- package/dist/lib/profile_settings.js.map +1 -0
- package/dist/lib/screen_content.js +44 -0
- package/dist/lib/screen_content.js.map +1 -0
- package/dist/lib/timezone.js +48 -6
- package/dist/lib/timezone.js.map +1 -1
- package/dist/lib/walk.js +6 -1
- package/dist/lib/walk.js.map +1 -1
- package/dist/lib/weight_readings.js +61 -0
- package/dist/lib/weight_readings.js.map +1 -0
- package/dist/projections/activities.js +193 -0
- package/dist/projections/activities.js.map +1 -0
- package/dist/projections/alarm_schedule.js +102 -0
- package/dist/projections/alarm_schedule.js.map +1 -0
- package/dist/projections/coach.js +203 -0
- package/dist/projections/coach.js.map +1 -0
- package/dist/projections/community.js +165 -0
- package/dist/projections/community.js.map +1 -0
- package/dist/projections/health_features.js +167 -0
- package/dist/projections/health_features.js.map +1 -0
- package/dist/projections/hormonal_insights.js +239 -0
- package/dist/projections/hormonal_insights.js.map +1 -0
- package/dist/projections/journal_v2.js +157 -0
- package/dist/projections/journal_v2.js.map +1 -0
- package/dist/projections/lift_cards.js +115 -0
- package/dist/projections/lift_cards.js.map +1 -0
- package/dist/projections/lift_exercise_v2.js +131 -0
- package/dist/projections/lift_exercise_v2.js.map +1 -0
- package/dist/projections/lift_library_v2.js +67 -0
- package/dist/projections/lift_library_v2.js.map +1 -0
- package/dist/projections/lift_sessions.js +211 -0
- package/dist/projections/lift_sessions.js.map +1 -0
- package/dist/projections/memory.js +95 -0
- package/dist/projections/memory.js.map +1 -0
- package/dist/projections/milestones.js +119 -0
- package/dist/projections/milestones.js.map +1 -0
- package/dist/projections/profile_v2.js +150 -0
- package/dist/projections/profile_v2.js.map +1 -0
- package/dist/projections/settings_v2.js +153 -0
- package/dist/projections/settings_v2.js.map +1 -0
- package/dist/projections/sleep.js +4 -21
- package/dist/projections/sleep.js.map +1 -1
- package/dist/projections/v2/calendar.js +43 -0
- package/dist/projections/v2/calendar.js.map +1 -0
- package/dist/projections/v2/core_common.js +86 -0
- package/dist/projections/v2/core_common.js.map +1 -0
- package/dist/projections/v2/core_home.js +136 -0
- package/dist/projections/v2/core_home.js.map +1 -0
- package/dist/projections/v2/core_sleep_need.js +23 -0
- package/dist/projections/v2/core_sleep_need.js.map +1 -0
- package/dist/projections/v2/day.js +92 -0
- package/dist/projections/v2/day.js.map +1 -0
- package/dist/projections/v2/heart_rate.js +54 -0
- package/dist/projections/v2/heart_rate.js.map +1 -0
- package/dist/projections/v2/history.js +140 -0
- package/dist/projections/v2/history.js.map +1 -0
- package/dist/projections/v2/now.js +39 -0
- package/dist/projections/v2/now.js.map +1 -0
- package/dist/projections/v2/recovery.js +77 -0
- package/dist/projections/v2/recovery.js.map +1 -0
- package/dist/projections/v2/sleep.js +218 -0
- package/dist/projections/v2/sleep.js.map +1 -0
- package/dist/projections/v2/strain.js +39 -0
- package/dist/projections/v2/strain.js.map +1 -0
- package/dist/projections/v2/stress.js +92 -0
- package/dist/projections/v2/stress.js.map +1 -0
- package/dist/projections/v2/trend.js +205 -0
- package/dist/projections/v2/trend.js.map +1 -0
- package/dist/projections/weekly_plan.js +66 -0
- package/dist/projections/weekly_plan.js.map +1 -0
- package/dist/projections/weight_readings.js +57 -0
- package/dist/projections/weight_readings.js.map +1 -0
- package/dist/projections/workout.js +37 -15
- package/dist/projections/workout.js.map +1 -1
- package/dist/schemas/activities.js +123 -0
- package/dist/schemas/activities.js.map +1 -0
- package/dist/schemas/alarm_schedule.js +62 -0
- package/dist/schemas/alarm_schedule.js.map +1 -0
- package/dist/schemas/coach.js +76 -8
- package/dist/schemas/coach.js.map +1 -1
- package/dist/schemas/community.js +70 -0
- package/dist/schemas/community.js.map +1 -0
- package/dist/schemas/health.js +36 -0
- package/dist/schemas/health.js.map +1 -0
- package/dist/schemas/hormonal_insights.js +90 -0
- package/dist/schemas/hormonal_insights.js.map +1 -0
- package/dist/schemas/journal.js +4 -0
- package/dist/schemas/journal.js.map +1 -1
- package/dist/schemas/journal_v2.js +106 -0
- package/dist/schemas/journal_v2.js.map +1 -0
- package/dist/schemas/memory.js +42 -0
- package/dist/schemas/memory.js.map +1 -0
- package/dist/schemas/milestones.js +39 -0
- package/dist/schemas/milestones.js.map +1 -0
- package/dist/schemas/profile_v2.js +60 -0
- package/dist/schemas/profile_v2.js.map +1 -0
- package/dist/schemas/settings_v2.js +56 -0
- package/dist/schemas/settings_v2.js.map +1 -0
- package/dist/schemas/sleep.js +8 -1
- package/dist/schemas/sleep.js.map +1 -1
- package/dist/schemas/strength.js +10 -1
- package/dist/schemas/strength.js.map +1 -1
- package/dist/schemas/strength_v2.js +190 -0
- package/dist/schemas/strength_v2.js.map +1 -0
- package/dist/schemas/v2/calendar.js +8 -0
- package/dist/schemas/v2/calendar.js.map +1 -0
- package/dist/schemas/v2/core_common.js +42 -0
- package/dist/schemas/v2/core_common.js.map +1 -0
- package/dist/schemas/v2/day.js +73 -0
- package/dist/schemas/v2/day.js.map +1 -0
- package/dist/schemas/v2/now.js +25 -0
- package/dist/schemas/v2/now.js.map +1 -0
- package/dist/schemas/v2/recovery.js +35 -0
- package/dist/schemas/v2/recovery.js.map +1 -0
- package/dist/schemas/v2/sleep.js +44 -0
- package/dist/schemas/v2/sleep.js.map +1 -0
- package/dist/schemas/v2/strain.js +16 -0
- package/dist/schemas/v2/strain.js.map +1 -0
- package/dist/schemas/v2/stress.js +20 -0
- package/dist/schemas/v2/stress.js.map +1 -0
- package/dist/schemas/v2/trend.js +42 -0
- package/dist/schemas/v2/trend.js.map +1 -0
- package/dist/schemas/weekly_plan.js +31 -0
- package/dist/schemas/weekly_plan.js.map +1 -0
- package/dist/schemas/weight.js +28 -0
- package/dist/schemas/weight.js.map +1 -0
- package/dist/scripts/build_behaviors.js +114 -0
- package/dist/scripts/build_behaviors.js.map +1 -0
- package/dist/scripts/cognito_bootstrap.js +9 -277
- package/dist/scripts/cognito_bootstrap.js.map +1 -1
- package/dist/scripts/gen_activity_catalogs.js +104 -0
- package/dist/scripts/gen_activity_catalogs.js.map +1 -0
- package/dist/server-admin.js +156 -0
- package/dist/server-admin.js.map +1 -0
- package/dist/server-http.js +270 -48
- package/dist/server-http.js.map +1 -1
- package/dist/server.js +136 -23
- package/dist/server.js.map +1 -1
- package/dist/tools/annotations.js +15 -0
- package/dist/tools/annotations.js.map +1 -0
- package/dist/tools/define.js +31 -0
- package/dist/tools/define.js.map +1 -0
- package/dist/tools/register.js +59 -94
- package/dist/tools/register.js.map +1 -1
- package/dist/tools/resources.js +70 -0
- package/dist/tools/resources.js.map +1 -0
- package/dist/tools/slim_schema.js +90 -0
- package/dist/tools/slim_schema.js.map +1 -0
- package/dist/tools/v2/activity_delete.js +63 -17
- package/dist/tools/v2/activity_delete.js.map +1 -1
- package/dist/tools/v2/activity_edit.js +396 -0
- package/dist/tools/v2/activity_edit.js.map +1 -0
- package/dist/tools/v2/activity_log.js +152 -0
- package/dist/tools/v2/activity_log.js.map +1 -0
- package/dist/tools/v2/calendar.js +19 -21
- package/dist/tools/v2/calendar.js.map +1 -1
- package/dist/tools/v2/coach_ask.js +74 -110
- package/dist/tools/v2/coach_ask.js.map +1 -1
- package/dist/tools/v2/coach_history.js +120 -0
- package/dist/tools/v2/coach_history.js.map +1 -0
- package/dist/tools/v2/community.js +124 -0
- package/dist/tools/v2/community.js.map +1 -0
- package/dist/tools/v2/cycle.js +63 -15
- package/dist/tools/v2/cycle.js.map +1 -1
- package/dist/tools/v2/cycle_log.js +289 -32
- package/dist/tools/v2/cycle_log.js.map +1 -1
- package/dist/tools/v2/day.js +126 -24
- package/dist/tools/v2/day.js.map +1 -1
- package/dist/tools/v2/health.js +116 -0
- package/dist/tools/v2/health.js.map +1 -0
- package/dist/tools/v2/hr_zones_set.js +134 -34
- package/dist/tools/v2/hr_zones_set.js.map +1 -1
- package/dist/tools/v2/journal.js +75 -17
- package/dist/tools/v2/journal.js.map +1 -1
- package/dist/tools/v2/journal_log.js +306 -65
- package/dist/tools/v2/journal_log.js.map +1 -1
- package/dist/tools/v2/lift_exercise.js +125 -22
- package/dist/tools/v2/lift_exercise.js.map +1 -1
- package/dist/tools/v2/lift_exercise_set.js +291 -0
- package/dist/tools/v2/lift_exercise_set.js.map +1 -0
- package/dist/tools/v2/lift_library.js +51 -12
- package/dist/tools/v2/lift_library.js.map +1 -1
- package/dist/tools/v2/lift_log.js +309 -72
- package/dist/tools/v2/lift_log.js.map +1 -1
- package/dist/tools/v2/lift_sessions.js +248 -0
- package/dist/tools/v2/lift_sessions.js.map +1 -0
- package/dist/tools/v2/lift_template.js +232 -0
- package/dist/tools/v2/lift_template.js.map +1 -0
- package/dist/tools/v2/memory.js +45 -0
- package/dist/tools/v2/memory.js.map +1 -0
- package/dist/tools/v2/memory_set.js +107 -0
- package/dist/tools/v2/memory_set.js.map +1 -0
- package/dist/tools/v2/milestones.js +50 -0
- package/dist/tools/v2/milestones.js.map +1 -0
- package/dist/tools/v2/now.js +49 -0
- package/dist/tools/v2/now.js.map +1 -0
- package/dist/tools/v2/preferences.js +51 -0
- package/dist/tools/v2/preferences.js.map +1 -0
- package/dist/tools/v2/preferences_set.js +388 -0
- package/dist/tools/v2/preferences_set.js.map +1 -0
- package/dist/tools/v2/profile.js +54 -22
- package/dist/tools/v2/profile.js.map +1 -1
- package/dist/tools/v2/profile_update.js +167 -55
- package/dist/tools/v2/profile_update.js.map +1 -1
- package/dist/tools/v2/raw.js +91 -34
- package/dist/tools/v2/raw.js.map +1 -1
- package/dist/tools/v2/recovery.js +37 -23
- package/dist/tools/v2/recovery.js.map +1 -1
- package/dist/tools/v2/sleep.js +52 -16
- package/dist/tools/v2/sleep.js.map +1 -1
- package/dist/tools/v2/smart_alarm.js +54 -20
- package/dist/tools/v2/smart_alarm.js.map +1 -1
- package/dist/tools/v2/smart_alarm_set.js +264 -86
- package/dist/tools/v2/smart_alarm_set.js.map +1 -1
- package/dist/tools/v2/strain.js +15 -15
- package/dist/tools/v2/strain.js.map +1 -1
- package/dist/tools/v2/stress.js +17 -15
- package/dist/tools/v2/stress.js.map +1 -1
- package/dist/tools/v2/trend.js +56 -20
- package/dist/tools/v2/trend.js.map +1 -1
- package/dist/tools/v2/weekly_plan.js +30 -0
- package/dist/tools/v2/weekly_plan.js.map +1 -0
- package/dist/tools/v2/weekly_plan_set.js +134 -0
- package/dist/tools/v2/weekly_plan_set.js.map +1 -0
- package/dist/tools/v2/weight.js +56 -0
- package/dist/tools/v2/weight.js.map +1 -0
- package/dist/tools/v2/weight_log.js +188 -0
- package/dist/tools/v2/weight_log.js.map +1 -0
- package/dist/tools/v2/workouts.js +159 -25
- package/dist/tools/v2/workouts.js.map +1 -1
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/dist/whoop/bff.js +94 -0
- package/dist/whoop/bff.js.map +1 -0
- package/dist/whoop/client.js +392 -18
- package/dist/whoop/client.js.map +1 -1
- package/dist/whoop/cognito.js +106 -79
- package/dist/whoop/cognito.js.map +1 -1
- package/dist/whoop/connect_page.js +165 -0
- package/dist/whoop/connect_page.js.map +1 -0
- package/dist/whoop/consent_font.js +287 -0
- package/dist/whoop/consent_font.js.map +1 -0
- package/dist/whoop/consent_logos.js +22 -0
- package/dist/whoop/consent_logos.js.map +1 -0
- package/dist/whoop/consent_page.js +291 -0
- package/dist/whoop/consent_page.js.map +1 -0
- package/dist/whoop/constants.js +5 -1
- package/dist/whoop/constants.js.map +1 -1
- package/dist/whoop/device.js +4 -4
- package/dist/whoop/device.js.map +1 -1
- package/dist/whoop/errors.js +85 -4
- package/dist/whoop/errors.js.map +1 -1
- package/dist/whoop/init_timezone.js +57 -17
- package/dist/whoop/init_timezone.js.map +1 -1
- package/dist/whoop/installation.js +15 -10
- package/dist/whoop/installation.js.map +1 -1
- package/dist/whoop/json_out.js +52 -6
- package/dist/whoop/json_out.js.map +1 -1
- package/dist/whoop/oauth_provider.js +81 -78
- package/dist/whoop/oauth_provider.js.map +1 -1
- package/dist/whoop/token_lock.js +331 -0
- package/dist/whoop/token_lock.js.map +1 -0
- package/dist/whoop/token_manager.js +536 -42
- package/dist/whoop/token_manager.js.map +1 -1
- package/dist/whoop/token_store.js +303 -36
- package/dist/whoop/token_store.js.map +1 -1
- package/dist/whoop/units.js +28 -0
- package/dist/whoop/units.js.map +1 -0
- package/package.json +29 -13
- package/scripts/build-mcpb.mjs +72 -0
- package/scripts/embed-consent-font.mjs +34 -0
- package/scripts/readme-media.ts +195 -0
- package/scripts/tools-md.ts +89 -0
- package/scripts/whoop-md.ts +119 -0
- package/src/build_info.ts +4 -0
- package/src/cli/app.ts +696 -0
- package/src/cli/auth.ts +350 -0
- package/src/cli/auth_flow.ts +356 -0
- package/src/cli/browser_login.ts +402 -0
- package/src/cli/clients.ts +467 -0
- package/src/cli/deploy_record.ts +100 -0
- package/src/cli/index.ts +41 -583
- package/src/cli/links.ts +43 -0
- package/src/cli/logo.ts +64 -0
- package/src/cli/setup.ts +1147 -979
- package/src/cli/spawn.ts +88 -0
- package/src/cli/sys.ts +267 -0
- package/src/cli/ui.ts +778 -436
- package/src/cli/update.ts +436 -274
- package/src/cloudflare/runtime.d.ts +27 -0
- package/src/cloudflare/token_store.ts +57 -0
- package/src/cloudflare/worker.ts +174 -0
- package/src/data/behaviors.ts +2760 -622
- package/src/data/endpoint_catalog.ts +2695 -0
- package/src/data/exercises.ts +379 -4861
- package/src/data/garments.ts +28 -0
- package/src/data/sports.ts +239 -211
- package/src/lib/activity_list.ts +150 -0
- package/src/lib/activity_records.ts +213 -0
- package/src/lib/activity_results.ts +51 -0
- package/src/lib/activity_types.ts +176 -0
- package/src/lib/brand.ts +71 -0
- package/src/lib/clock.ts +16 -0
- package/src/lib/coach_session.ts +434 -0
- package/src/lib/dates.ts +18 -0
- package/src/lib/env_file.ts +191 -0
- package/src/lib/health_plans_write.ts +53 -0
- package/src/lib/heart_rate.ts +42 -0
- package/src/lib/highlight.ts +71 -0
- package/src/lib/install_links.ts +61 -0
- package/src/lib/instant.ts +54 -0
- package/src/lib/journal_behaviors.ts +453 -0
- package/src/lib/lift_catalog.ts +258 -0
- package/src/lib/lift_history.ts +75 -0
- package/src/lib/lift_sets.ts +164 -0
- package/src/lib/lift_tool.ts +25 -0
- package/src/lib/profile_settings.ts +102 -0
- package/src/lib/screen_content.ts +40 -0
- package/src/lib/timezone.ts +44 -8
- package/src/lib/walk.ts +4 -1
- package/src/lib/weight_readings.ts +82 -0
- package/src/projections/activities.ts +199 -0
- package/src/projections/alarm_schedule.ts +98 -0
- package/src/projections/coach.ts +218 -0
- package/src/projections/community.ts +187 -0
- package/src/projections/health_features.ts +191 -0
- package/src/projections/hormonal_insights.ts +277 -0
- package/src/projections/journal_v2.ts +173 -0
- package/src/projections/lift_cards.ts +131 -0
- package/src/projections/lift_exercise_v2.ts +147 -0
- package/src/projections/lift_library_v2.ts +77 -0
- package/src/projections/lift_sessions.ts +250 -0
- package/src/projections/memory.ts +100 -0
- package/src/projections/milestones.ts +133 -0
- package/src/projections/profile_v2.ts +166 -0
- package/src/projections/settings_v2.ts +179 -0
- package/src/projections/sleep.ts +7 -21
- package/src/projections/v2/calendar.ts +48 -0
- package/src/projections/v2/core_common.ts +95 -0
- package/src/projections/v2/core_home.ts +177 -0
- package/src/projections/v2/core_sleep_need.ts +37 -0
- package/src/projections/v2/day.ts +162 -0
- package/src/projections/v2/heart_rate.ts +73 -0
- package/src/projections/v2/history.ts +153 -0
- package/src/projections/v2/now.ts +59 -0
- package/src/projections/v2/recovery.ts +120 -0
- package/src/projections/v2/sleep.ts +281 -0
- package/src/projections/v2/strain.ts +63 -0
- package/src/projections/v2/stress.ts +111 -0
- package/src/projections/v2/trend.ts +242 -0
- package/src/projections/weekly_plan.ts +79 -0
- package/src/projections/weight_readings.ts +57 -0
- package/src/projections/workout.ts +36 -17
- package/src/schemas/activities.ts +136 -0
- package/src/schemas/alarm_schedule.ts +70 -0
- package/src/schemas/coach.ts +98 -8
- package/src/schemas/community.ts +77 -0
- package/src/schemas/health.ts +36 -0
- package/src/schemas/hormonal_insights.ts +108 -0
- package/src/schemas/journal.ts +4 -0
- package/src/schemas/journal_v2.ts +112 -0
- package/src/schemas/memory.ts +48 -0
- package/src/schemas/milestones.ts +48 -0
- package/src/schemas/profile_v2.ts +63 -0
- package/src/schemas/settings_v2.ts +63 -0
- package/src/schemas/sleep.ts +9 -1
- package/src/schemas/strength.ts +10 -1
- package/src/schemas/strength_v2.ts +210 -0
- package/src/schemas/v2/calendar.ts +8 -0
- package/src/schemas/v2/core_common.ts +50 -0
- package/src/schemas/v2/day.ts +79 -0
- package/src/schemas/v2/now.ts +25 -0
- package/src/schemas/v2/recovery.ts +38 -0
- package/src/schemas/v2/sleep.ts +46 -0
- package/src/schemas/v2/strain.ts +16 -0
- package/src/schemas/v2/stress.ts +20 -0
- package/src/schemas/v2/trend.ts +45 -0
- package/src/schemas/weekly_plan.ts +36 -0
- package/src/schemas/weight.ts +53 -0
- package/src/scripts/build_behaviors.ts +116 -0
- package/src/scripts/cognito_bootstrap.ts +10 -249
- package/src/scripts/gen_activity_catalogs.ts +121 -0
- package/src/server-admin.ts +171 -0
- package/src/server-http.ts +290 -53
- package/src/server.ts +130 -26
- package/src/tools/annotations.ts +25 -0
- package/src/tools/define.ts +53 -0
- package/src/tools/register.ts +64 -94
- package/src/tools/resources.ts +92 -0
- package/src/tools/slim_schema.ts +102 -0
- package/src/tools/v2/activity_delete.ts +61 -23
- package/src/tools/v2/activity_edit.ts +378 -0
- package/src/tools/v2/activity_log.ts +154 -0
- package/src/tools/v2/calendar.ts +22 -21
- package/src/tools/v2/coach_ask.ts +83 -119
- package/src/tools/v2/coach_history.ts +140 -0
- package/src/tools/v2/community.ts +137 -0
- package/src/tools/v2/cycle.ts +78 -20
- package/src/tools/v2/cycle_log.ts +291 -40
- package/src/tools/v2/day.ts +126 -25
- package/src/tools/v2/health.ts +114 -0
- package/src/tools/v2/hr_zones_set.ts +132 -45
- package/src/tools/v2/journal.ts +78 -23
- package/src/tools/v2/journal_log.ts +321 -69
- package/src/tools/v2/lift_exercise.ts +135 -27
- package/src/tools/v2/lift_exercise_set.ts +272 -0
- package/src/tools/v2/lift_library.ts +57 -21
- package/src/tools/v2/lift_log.ts +325 -83
- package/src/tools/v2/lift_sessions.ts +255 -0
- package/src/tools/v2/lift_template.ts +229 -0
- package/src/tools/v2/memory.ts +54 -0
- package/src/tools/v2/memory_set.ts +110 -0
- package/src/tools/v2/milestones.ts +59 -0
- package/src/tools/v2/now.ts +61 -0
- package/src/tools/v2/preferences.ts +61 -0
- package/src/tools/v2/preferences_set.ts +403 -0
- package/src/tools/v2/profile.ts +56 -26
- package/src/tools/v2/profile_update.ts +151 -60
- package/src/tools/v2/raw.ts +89 -36
- package/src/tools/v2/recovery.ts +44 -25
- package/src/tools/v2/sleep.ts +57 -18
- package/src/tools/v2/smart_alarm.ts +62 -24
- package/src/tools/v2/smart_alarm_set.ts +240 -90
- package/src/tools/v2/strain.ts +19 -16
- package/src/tools/v2/stress.ts +22 -17
- package/src/tools/v2/trend.ts +58 -20
- package/src/tools/v2/weekly_plan.ts +38 -0
- package/src/tools/v2/weekly_plan_set.ts +132 -0
- package/src/tools/v2/weight.ts +61 -0
- package/src/tools/v2/weight_log.ts +193 -0
- package/src/tools/v2/workouts.ts +156 -26
- package/src/version.ts +5 -0
- package/src/whoop/bff.ts +84 -0
- package/src/whoop/client.ts +448 -18
- package/src/whoop/cognito.ts +110 -99
- package/src/whoop/connect_page.ts +183 -0
- package/src/whoop/consent_font.ts +289 -0
- package/src/whoop/consent_logos.ts +24 -0
- package/src/whoop/consent_page.ts +326 -0
- package/src/whoop/constants.ts +5 -1
- package/src/whoop/device.ts +4 -4
- package/src/whoop/errors.ts +89 -4
- package/src/whoop/init_timezone.ts +57 -16
- package/src/whoop/installation.ts +16 -7
- package/src/whoop/json_out.ts +51 -6
- package/src/whoop/oauth_provider.ts +84 -91
- package/src/whoop/token_lock.ts +345 -0
- package/src/whoop/token_manager.ts +580 -53
- package/src/whoop/token_store.ts +341 -32
- package/src/whoop/units.ts +35 -0
- package/wrangler.jsonc +17 -0
- package/assets/banner.svg +0 -38
- package/dist/data/endpoints.js +0 -320
- package/dist/data/endpoints.js.map +0 -1
- package/dist/lib/format.js +0 -22
- package/dist/lib/format.js.map +0 -1
- package/dist/lib/stats.js +0 -32
- package/dist/lib/stats.js.map +0 -1
- package/dist/projections/calendar.js +0 -33
- package/dist/projections/calendar.js.map +0 -1
- package/dist/projections/communities.js +0 -105
- package/dist/projections/communities.js.map +0 -1
- package/dist/projections/cycle.js +0 -81
- package/dist/projections/cycle.js.map +0 -1
- package/dist/projections/hr_zones.js +0 -35
- package/dist/projections/hr_zones.js.map +0 -1
- package/dist/projections/journal.js +0 -56
- package/dist/projections/journal.js.map +0 -1
- package/dist/projections/leaderboard.js +0 -37
- package/dist/projections/leaderboard.js.map +0 -1
- package/dist/projections/lift_exercise.js +0 -22
- package/dist/projections/lift_exercise.js.map +0 -1
- package/dist/projections/lift_history.js +0 -133
- package/dist/projections/lift_history.js.map +0 -1
- package/dist/projections/lift_library.js +0 -71
- package/dist/projections/lift_library.js.map +0 -1
- package/dist/projections/lift_progression.js +0 -138
- package/dist/projections/lift_progression.js.map +0 -1
- package/dist/projections/lift_prs.js +0 -20
- package/dist/projections/lift_prs.js.map +0 -1
- package/dist/projections/live_hr.js +0 -19
- package/dist/projections/live_hr.js.map +0 -1
- package/dist/projections/live_state.js +0 -23
- package/dist/projections/live_state.js.map +0 -1
- package/dist/projections/live_stress.js +0 -11
- package/dist/projections/live_stress.js.map +0 -1
- package/dist/projections/performance_assessment.js +0 -15
- package/dist/projections/performance_assessment.js.map +0 -1
- package/dist/projections/profile.js +0 -59
- package/dist/projections/profile.js.map +0 -1
- package/dist/projections/recovery.js +0 -131
- package/dist/projections/recovery.js.map +0 -1
- package/dist/projections/sleep_need.js +0 -32
- package/dist/projections/sleep_need.js.map +0 -1
- package/dist/projections/smart_alarm.js +0 -45
- package/dist/projections/smart_alarm.js.map +0 -1
- package/dist/projections/strain.js +0 -128
- package/dist/projections/strain.js.map +0 -1
- package/dist/projections/stress.js +0 -86
- package/dist/projections/stress.js.map +0 -1
- package/dist/projections/today.js +0 -129
- package/dist/projections/today.js.map +0 -1
- package/dist/projections/trend.js +0 -149
- package/dist/projections/trend.js.map +0 -1
- package/dist/projections/workouts.js +0 -37
- package/dist/projections/workouts.js.map +0 -1
- package/dist/schemas/calendar.js +0 -12
- package/dist/schemas/calendar.js.map +0 -1
- package/dist/schemas/communities.js +0 -51
- package/dist/schemas/communities.js.map +0 -1
- package/dist/schemas/compare.js +0 -15
- package/dist/schemas/compare.js.map +0 -1
- package/dist/schemas/leaderboard.js +0 -25
- package/dist/schemas/leaderboard.js.map +0 -1
- package/dist/schemas/live.js +0 -26
- package/dist/schemas/live.js.map +0 -1
- package/dist/schemas/performance.js +0 -12
- package/dist/schemas/performance.js.map +0 -1
- package/dist/schemas/profile.js +0 -39
- package/dist/schemas/profile.js.map +0 -1
- package/dist/schemas/recovery.js +0 -27
- package/dist/schemas/recovery.js.map +0 -1
- package/dist/schemas/settings.js +0 -26
- package/dist/schemas/settings.js.map +0 -1
- package/dist/schemas/sleep_need.js +0 -15
- package/dist/schemas/sleep_need.js.map +0 -1
- package/dist/schemas/smart_alarm.js +0 -36
- package/dist/schemas/smart_alarm.js.map +0 -1
- package/dist/schemas/strain.js +0 -24
- package/dist/schemas/strain.js.map +0 -1
- package/dist/schemas/stress.js +0 -16
- package/dist/schemas/stress.js.map +0 -1
- package/dist/schemas/today.js +0 -38
- package/dist/schemas/today.js.map +0 -1
- package/dist/schemas/trend.js +0 -50
- package/dist/schemas/trend.js.map +0 -1
- package/dist/schemas/womens_health.js +0 -23
- package/dist/schemas/womens_health.js.map +0 -1
- package/dist/tools/v2/activity_create.js +0 -64
- package/dist/tools/v2/activity_create.js.map +0 -1
- package/dist/tools/v2/behavior_impact.js +0 -36
- package/dist/tools/v2/behavior_impact.js.map +0 -1
- package/dist/tools/v2/communities.js +0 -60
- package/dist/tools/v2/communities.js.map +0 -1
- package/dist/tools/v2/compare.js +0 -67
- package/dist/tools/v2/compare.js.map +0 -1
- package/dist/tools/v2/endpoints.js +0 -28
- package/dist/tools/v2/endpoints.js.map +0 -1
- package/dist/tools/v2/hidden_metric.js +0 -28
- package/dist/tools/v2/hidden_metric.js.map +0 -1
- package/dist/tools/v2/hr_zones.js +0 -24
- package/dist/tools/v2/hr_zones.js.map +0 -1
- package/dist/tools/v2/journal_autopop.js +0 -19
- package/dist/tools/v2/journal_autopop.js.map +0 -1
- package/dist/tools/v2/journal_catalog.js +0 -44
- package/dist/tools/v2/journal_catalog.js.map +0 -1
- package/dist/tools/v2/leaderboard.js +0 -79
- package/dist/tools/v2/leaderboard.js.map +0 -1
- package/dist/tools/v2/lift_catalog.js +0 -47
- package/dist/tools/v2/lift_catalog.js.map +0 -1
- package/dist/tools/v2/lift_custom_exercise.js +0 -108
- package/dist/tools/v2/lift_custom_exercise.js.map +0 -1
- package/dist/tools/v2/lift_history.js +0 -39
- package/dist/tools/v2/lift_history.js.map +0 -1
- package/dist/tools/v2/lift_progression.js +0 -30
- package/dist/tools/v2/lift_progression.js.map +0 -1
- package/dist/tools/v2/lift_prs.js +0 -21
- package/dist/tools/v2/lift_prs.js.map +0 -1
- package/dist/tools/v2/lift_template_save.js +0 -83
- package/dist/tools/v2/lift_template_save.js.map +0 -1
- package/dist/tools/v2/live_hr.js +0 -21
- package/dist/tools/v2/live_hr.js.map +0 -1
- package/dist/tools/v2/live_state.js +0 -21
- package/dist/tools/v2/live_state.js.map +0 -1
- package/dist/tools/v2/live_stress.js +0 -22
- package/dist/tools/v2/live_stress.js.map +0 -1
- package/dist/tools/v2/performance_assessment.js +0 -38
- package/dist/tools/v2/performance_assessment.js.map +0 -1
- package/dist/tools/v2/sleep_need.js +0 -21
- package/dist/tools/v2/sleep_need.js.map +0 -1
- package/dist/tools/v2/sports_catalog.js +0 -26
- package/dist/tools/v2/sports_catalog.js.map +0 -1
- package/dist/tools/v2/symptom_log.js +0 -72
- package/dist/tools/v2/symptom_log.js.map +0 -1
- package/dist/tools/v2/today.js +0 -30
- package/dist/tools/v2/today.js.map +0 -1
- package/dist/tools/v2/workout.js +0 -23
- package/dist/tools/v2/workout.js.map +0 -1
- package/dist/whoop/build_lift_body.js +0 -67
- package/dist/whoop/build_lift_body.js.map +0 -1
- package/dist/whoop/session_state.js +0 -34
- package/dist/whoop/session_state.js.map +0 -1
- package/dist/whoop/types.js +0 -82
- package/dist/whoop/types.js.map +0 -1
- package/src/data/endpoints.ts +0 -319
- package/src/lib/format.ts +0 -27
- package/src/lib/stats.ts +0 -35
- package/src/projections/calendar.ts +0 -46
- package/src/projections/communities.ts +0 -135
- package/src/projections/cycle.ts +0 -85
- package/src/projections/hr_zones.ts +0 -43
- package/src/projections/journal.ts +0 -55
- package/src/projections/leaderboard.ts +0 -49
- package/src/projections/lift_exercise.ts +0 -29
- package/src/projections/lift_history.ts +0 -140
- package/src/projections/lift_library.ts +0 -70
- package/src/projections/lift_progression.ts +0 -138
- package/src/projections/lift_prs.ts +0 -23
- package/src/projections/live_hr.ts +0 -21
- package/src/projections/live_state.ts +0 -28
- package/src/projections/live_stress.ts +0 -13
- package/src/projections/performance_assessment.ts +0 -19
- package/src/projections/profile.ts +0 -71
- package/src/projections/recovery.ts +0 -133
- package/src/projections/sleep_need.ts +0 -36
- package/src/projections/smart_alarm.ts +0 -70
- package/src/projections/strain.ts +0 -131
- package/src/projections/stress.ts +0 -95
- package/src/projections/today.ts +0 -166
- package/src/projections/trend.ts +0 -149
- package/src/projections/workouts.ts +0 -36
- package/src/schemas/calendar.ts +0 -13
- package/src/schemas/communities.ts +0 -56
- package/src/schemas/compare.ts +0 -17
- package/src/schemas/leaderboard.ts +0 -26
- package/src/schemas/live.ts +0 -31
- package/src/schemas/performance.ts +0 -13
- package/src/schemas/profile.ts +0 -40
- package/src/schemas/recovery.ts +0 -28
- package/src/schemas/settings.ts +0 -30
- package/src/schemas/sleep_need.ts +0 -16
- package/src/schemas/smart_alarm.ts +0 -40
- package/src/schemas/strain.ts +0 -25
- package/src/schemas/stress.ts +0 -17
- package/src/schemas/today.ts +0 -39
- package/src/schemas/trend.ts +0 -53
- package/src/schemas/womens_health.ts +0 -26
- package/src/tools/v2/activity_create.ts +0 -74
- package/src/tools/v2/behavior_impact.ts +0 -39
- package/src/tools/v2/communities.ts +0 -65
- package/src/tools/v2/compare.ts +0 -83
- package/src/tools/v2/endpoints.ts +0 -32
- package/src/tools/v2/hidden_metric.ts +0 -33
- package/src/tools/v2/hr_zones.ts +0 -29
- package/src/tools/v2/journal_autopop.ts +0 -26
- package/src/tools/v2/journal_catalog.ts +0 -48
- package/src/tools/v2/leaderboard.ts +0 -92
- package/src/tools/v2/lift_catalog.ts +0 -48
- package/src/tools/v2/lift_custom_exercise.ts +0 -117
- package/src/tools/v2/lift_history.ts +0 -52
- package/src/tools/v2/lift_progression.ts +0 -34
- package/src/tools/v2/lift_prs.ts +0 -26
- package/src/tools/v2/lift_template_save.ts +0 -89
- package/src/tools/v2/live_hr.ts +0 -26
- package/src/tools/v2/live_state.ts +0 -26
- package/src/tools/v2/live_stress.ts +0 -27
- package/src/tools/v2/performance_assessment.ts +0 -44
- package/src/tools/v2/sleep_need.ts +0 -26
- package/src/tools/v2/sports_catalog.ts +0 -32
- package/src/tools/v2/symptom_log.ts +0 -78
- package/src/tools/v2/today.ts +0 -35
- package/src/tools/v2/workout.ts +0 -28
- package/src/whoop/build_lift_body.ts +0 -84
- package/src/whoop/session_state.ts +0 -41
- package/src/whoop/types.ts +0 -89
package/src/whoop/bff.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Helpers for WHOOP's server-driven screens ("BFF" responses): deeply nested
|
|
2
|
+
// `sections[] / items[] / content` trees whose values are display strings.
|
|
3
|
+
// Key off stable ids and types, never array positions.
|
|
4
|
+
|
|
5
|
+
export type Node = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
const isObj = (v: unknown): v is Node => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
8
|
+
|
|
9
|
+
/** Depth-first walk over every object node in a JSON tree. */
|
|
10
|
+
export function* walk(root: unknown): Generator<Node> {
|
|
11
|
+
const stack: unknown[] = [root];
|
|
12
|
+
while (stack.length) {
|
|
13
|
+
const cur = stack.pop();
|
|
14
|
+
if (Array.isArray(cur)) {
|
|
15
|
+
for (let i = cur.length - 1; i >= 0; i--) stack.push(cur[i]);
|
|
16
|
+
} else if (isObj(cur)) {
|
|
17
|
+
yield cur;
|
|
18
|
+
const vals = Object.values(cur);
|
|
19
|
+
for (let i = vals.length - 1; i >= 0; i--) stack.push(vals[i]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function findAll(root: unknown, pred: (n: Node) => boolean): Node[] {
|
|
25
|
+
const out: Node[] = [];
|
|
26
|
+
for (const n of walk(root)) if (pred(n)) out.push(n);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
export function findFirst(root: unknown, pred: (n: Node) => boolean): Node | undefined {
|
|
30
|
+
for (const n of walk(root)) if (pred(n)) return n;
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
/** Nodes whose `type` equals one of the given values. */
|
|
34
|
+
export function byType(root: unknown, ...types: string[]): Node[] {
|
|
35
|
+
return findAll(root, (n) => typeof n.type === "string" && types.includes(n.type));
|
|
36
|
+
}
|
|
37
|
+
/** First node whose `id` equals the given value. */
|
|
38
|
+
export function byId(root: unknown, id: string): Node | undefined {
|
|
39
|
+
return findFirst(root, (n) => n.id === id);
|
|
40
|
+
}
|
|
41
|
+
/** Read a nested property path safely: get(n, "content", "title"). */
|
|
42
|
+
export function get(node: unknown, ...path: Array<string | number>): unknown {
|
|
43
|
+
let cur: unknown = node;
|
|
44
|
+
for (const key of path) {
|
|
45
|
+
if (cur === null || cur === undefined) return undefined;
|
|
46
|
+
cur = (cur as Record<string | number, unknown>)[key];
|
|
47
|
+
}
|
|
48
|
+
return cur;
|
|
49
|
+
}
|
|
50
|
+
export function str(v: unknown): string | null {
|
|
51
|
+
return typeof v === "string" && v.trim() !== "" ? v : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** "1,234" / "12.5" / "-3" / "+4%" → number; "--", "---", "" → null. */
|
|
55
|
+
export function parseNumber(display: unknown): number | null {
|
|
56
|
+
if (typeof display === "number") return Number.isFinite(display) ? display : null;
|
|
57
|
+
if (typeof display !== "string") return null;
|
|
58
|
+
const m = display.replace(/,/g, "").match(/[-+]?\d*\.?\d+/);
|
|
59
|
+
return m ? Number(m[0]) : null;
|
|
60
|
+
}
|
|
61
|
+
/** "32%" → 32. */
|
|
62
|
+
export function parsePercent(display: unknown): number | null {
|
|
63
|
+
return typeof display === "string" && display.includes("%") ? parseNumber(display) : parseNumber(display);
|
|
64
|
+
}
|
|
65
|
+
/** "0:38" (h:mm) or "1:02:03" (h:mm:ss) → milliseconds. "45m", "2h 5m" also accepted. */
|
|
66
|
+
export function parseDurationMs(display: unknown): number | null {
|
|
67
|
+
if (typeof display !== "string") return null;
|
|
68
|
+
const s = display.trim();
|
|
69
|
+
const clock = s.match(/^(\d+):(\d{2})(?::(\d{2}))?$/);
|
|
70
|
+
if (clock) return ((Number(clock[1]) * 60 + Number(clock[2])) * 60 + Number(clock[3] ?? 0)) * 1000;
|
|
71
|
+
const h = s.match(/(\d+)\s*h/)?.[1];
|
|
72
|
+
const m = s.match(/(\d+)\s*m(?!s)/)?.[1];
|
|
73
|
+
const sec = s.match(/(\d+)\s*s/)?.[1];
|
|
74
|
+
if (h === undefined && m === undefined && sec === undefined) return null;
|
|
75
|
+
return ((Number(h ?? 0) * 60 + Number(m ?? 0)) * 60 + Number(sec ?? 0)) * 1000;
|
|
76
|
+
}
|
|
77
|
+
/** "174.2 lb" / "79 kg" / "126 lbs" → {value, unit}. */
|
|
78
|
+
export function parseWeight(display: unknown): { value: number; unit: "kg" | "lb" } | null {
|
|
79
|
+
if (typeof display !== "string") return null;
|
|
80
|
+
const value = parseNumber(display);
|
|
81
|
+
if (value === null) return null;
|
|
82
|
+
const unit = /kg/i.test(display) ? "kg" : /lb/i.test(display) ? "lb" : null;
|
|
83
|
+
return unit ? { value, unit } : null;
|
|
84
|
+
}
|
package/src/whoop/client.ts
CHANGED
|
@@ -1,26 +1,368 @@
|
|
|
1
|
-
import { BASE_URL,
|
|
1
|
+
import { BASE_URL, REQUEST_TIMEOUT_MS } from "./constants.js";
|
|
2
2
|
import { deviceHeaders } from "./device.js";
|
|
3
|
+
import { isoDay } from "../lib/dates.js";
|
|
3
4
|
import {
|
|
4
5
|
WhoopApiError,
|
|
5
|
-
|
|
6
|
+
WhoopConflictError,
|
|
7
|
+
WhoopEndpointUnauthorizedError,
|
|
6
8
|
WhoopServerError,
|
|
9
|
+
apiErrorDetail,
|
|
10
|
+
htmlTitle,
|
|
11
|
+
parseOverlaps,
|
|
7
12
|
} from "./errors.js";
|
|
8
13
|
|
|
9
14
|
export interface WhoopClientConfig {
|
|
10
15
|
/** Async function returning a fresh bearer token. Called before each request. */
|
|
11
16
|
getToken: () => Promise<string>;
|
|
17
|
+
/** Injectable clock for deterministic cache tests. */
|
|
18
|
+
now?: () => number;
|
|
19
|
+
/**
|
|
20
|
+
* Called with the rejected token when Whoop answers 401, before the single
|
|
21
|
+
* retry, so the token source can force a refresh.
|
|
22
|
+
*/
|
|
23
|
+
onUnauthorized?: (rejectedToken: string) => void;
|
|
24
|
+
/** Injectable sleep for the 412 retry backoff (tests pass a no-op). */
|
|
25
|
+
sleep?: (ms: number) => Promise<void>;
|
|
12
26
|
}
|
|
13
27
|
|
|
28
|
+
/** Backoff for WHOOP's 412 "User's cycle is concurrently being updated" on writes. */
|
|
29
|
+
const RETRY_412_DELAYS_MS = [2_000, 10_000] as const;
|
|
30
|
+
/** How long lift reads stay uncached after a write that changes them. */
|
|
31
|
+
const LIFT_SETTLE_MS = 2 * 60_000;
|
|
32
|
+
|
|
14
33
|
type QueryValue = string | number | boolean | undefined | null;
|
|
15
34
|
|
|
35
|
+
type CacheEntry = {
|
|
36
|
+
value: unknown;
|
|
37
|
+
expiresAt: number;
|
|
38
|
+
bytes: number;
|
|
39
|
+
tags: readonly string[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const MAX_CACHE_ENTRIES = 100;
|
|
43
|
+
const MAX_CACHE_BYTES = 10 * 1024 * 1024;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Process-local LRU for safe GET results. It deliberately never persists data,
|
|
47
|
+
* and only keeps completed values for endpoints with an explicit TTL policy.
|
|
48
|
+
*/
|
|
49
|
+
class RequestCache {
|
|
50
|
+
private readonly values = new Map<string, CacheEntry>();
|
|
51
|
+
private readonly inFlight = new Map<string, Promise<unknown>>();
|
|
52
|
+
private bytes = 0;
|
|
53
|
+
// Bumped on every invalidation so a read that started before a write can't
|
|
54
|
+
// store (or be shared as) pre-write data once it lands.
|
|
55
|
+
private generation = 0;
|
|
56
|
+
private loadSeq = 0;
|
|
57
|
+
private readonly latestLoad = new Map<string, number>();
|
|
58
|
+
private readonly unsettledUntil = new Map<string, number>();
|
|
59
|
+
|
|
60
|
+
constructor(private readonly now: () => number) {}
|
|
61
|
+
|
|
62
|
+
async getOrLoad<T>(
|
|
63
|
+
key: string,
|
|
64
|
+
policy: { ttlMs: number; tags: readonly string[] } | null,
|
|
65
|
+
load: () => Promise<T>,
|
|
66
|
+
fresh = false,
|
|
67
|
+
): Promise<T> {
|
|
68
|
+
const cached = fresh ? undefined : this.values.get(key);
|
|
69
|
+
if (cached) {
|
|
70
|
+
if (cached.expiresAt > this.now()) {
|
|
71
|
+
// Map insertion order is the LRU order.
|
|
72
|
+
this.values.delete(key);
|
|
73
|
+
this.values.set(key, cached);
|
|
74
|
+
return cached.value as T;
|
|
75
|
+
}
|
|
76
|
+
this.delete(key, cached);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Even live endpoints share an already-running identical request, but their
|
|
80
|
+
// completed values are never retained.
|
|
81
|
+
const existing = fresh ? undefined : this.inFlight.get(key);
|
|
82
|
+
if (existing) return existing as Promise<T>;
|
|
83
|
+
|
|
84
|
+
const generation = this.generation;
|
|
85
|
+
const seq = ++this.loadSeq;
|
|
86
|
+
this.latestLoad.set(key, seq);
|
|
87
|
+
const pending: Promise<T> = load()
|
|
88
|
+
.then((value) => {
|
|
89
|
+
// Pending activities are mutable server-side objects; serving one after
|
|
90
|
+
// Whoop finishes scoring it would be misleading. A load superseded by a
|
|
91
|
+
// newer one for the same key (a fresh read) must not overwrite it.
|
|
92
|
+
const latest = this.latestLoad.get(key) === seq;
|
|
93
|
+
if (latest && generation === this.generation) {
|
|
94
|
+
// The newest read replaces what was cached even when it can't be kept
|
|
95
|
+
// itself (a pending record, an oversized body, or a tag WHOOP is still
|
|
96
|
+
// catching up on after a write). Otherwise a fresh read that came back
|
|
97
|
+
// pending left the older entry in place, and the next ordinary read
|
|
98
|
+
// served data from before the one just returned.
|
|
99
|
+
const kept = policy !== null && !containsPendingState(value) && !this.unsettled(policy.tags) &&
|
|
100
|
+
this.store(key, value, policy);
|
|
101
|
+
if (!kept) this.evict(key);
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
104
|
+
})
|
|
105
|
+
.finally(() => {
|
|
106
|
+
if (this.inFlight.get(key) === pending) this.inFlight.delete(key);
|
|
107
|
+
if (this.latestLoad.get(key) === seq) this.latestLoad.delete(key);
|
|
108
|
+
});
|
|
109
|
+
this.inFlight.set(key, pending);
|
|
110
|
+
return pending;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
invalidate(tags: readonly string[]): void {
|
|
114
|
+
this.generation++;
|
|
115
|
+
this.inFlight.clear();
|
|
116
|
+
if (tags.includes("all")) {
|
|
117
|
+
this.values.clear();
|
|
118
|
+
this.bytes = 0;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
for (const [key, entry] of this.values) {
|
|
122
|
+
if (entry.tags.some((tag) => tags.includes(tag))) this.delete(key, entry);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private evict(key: string): void {
|
|
127
|
+
const entry = this.values.get(key);
|
|
128
|
+
if (entry) this.delete(key, entry);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Serve reads tagged `tag` live, without retaining them, for the next `ms`: WHOOP is still catching up with a write. */
|
|
132
|
+
holdOff(tag: string, ms: number): void {
|
|
133
|
+
this.unsettledUntil.set(tag, this.now() + ms);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private unsettled(tags: readonly string[]): boolean {
|
|
137
|
+
return tags.some((tag) => (this.unsettledUntil.get(tag) ?? 0) > this.now());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Returns whether the value was kept. */
|
|
141
|
+
private store(key: string, value: unknown, policy: { ttlMs: number; tags: readonly string[] }): boolean {
|
|
142
|
+
let bytes = 0;
|
|
143
|
+
try {
|
|
144
|
+
bytes = Buffer.byteLength(JSON.stringify(value));
|
|
145
|
+
} catch {
|
|
146
|
+
return false; // Never let cache accounting affect a successful API response.
|
|
147
|
+
}
|
|
148
|
+
if (bytes > MAX_CACHE_BYTES) return false;
|
|
149
|
+
const existing = this.values.get(key);
|
|
150
|
+
if (existing) this.delete(key, existing);
|
|
151
|
+
const entry: CacheEntry = {
|
|
152
|
+
value,
|
|
153
|
+
expiresAt: this.now() + policy.ttlMs,
|
|
154
|
+
bytes,
|
|
155
|
+
tags: policy.tags,
|
|
156
|
+
};
|
|
157
|
+
this.values.set(key, entry);
|
|
158
|
+
this.bytes += bytes;
|
|
159
|
+
while (this.values.size > MAX_CACHE_ENTRIES || this.bytes > MAX_CACHE_BYTES) {
|
|
160
|
+
const oldest = this.values.entries().next().value as [string, CacheEntry] | undefined;
|
|
161
|
+
if (!oldest) break;
|
|
162
|
+
this.delete(oldest[0], oldest[1]);
|
|
163
|
+
}
|
|
164
|
+
return this.values.has(key);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private delete(key: string, entry: CacheEntry): void {
|
|
168
|
+
this.values.delete(key);
|
|
169
|
+
this.bytes -= entry.bytes;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function containsPendingState(value: unknown): boolean {
|
|
174
|
+
try {
|
|
175
|
+
// Matches both the iOS "PENDING" state and the developer API's "PENDING_SCORE".
|
|
176
|
+
return /"pending(_score)?"/.test(JSON.stringify(value).toLowerCase());
|
|
177
|
+
} catch {
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Refuse a path the URL parser would not send as written. `new URL()` resolves
|
|
184
|
+
* "." and ".." segments (also spelled "%2e"), turns "\" into "/", drops tabs
|
|
185
|
+
* and newlines, cuts at "#", and without a leading "/" reads the text as part
|
|
186
|
+
* of the host. A path built from a caller-supplied id could otherwise reach a
|
|
187
|
+
* different endpoint (or host, with the bearer token) than the one the calling
|
|
188
|
+
* tool, and whoop_raw's write denylist, checked.
|
|
189
|
+
*/
|
|
190
|
+
export function assertVerbatimPath(path: string): void {
|
|
191
|
+
const beforeQuery = path.split("?")[0]!;
|
|
192
|
+
const dotSegment = beforeQuery.split("/").some((segment) => /^(?:\.|%2e){1,2}$/i.test(segment));
|
|
193
|
+
if (!path.startsWith("/") || dotSegment || /[\\#\u0000-\u001f\u007f]/.test(path)) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
`Refusing to call WHOOP path ${JSON.stringify(path)}: it must start with "/" and contain no "." or ".." segments, backslashes, "#" or control characters.`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function cacheKey(path: string, query: Record<string, QueryValue>): string {
|
|
201
|
+
const params = Object.entries(query)
|
|
202
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
203
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
204
|
+
// Encoded, so {a: "1&b=2"} and {a: "1", b: "2"} (different requests) don't share a key.
|
|
205
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
206
|
+
.join("&");
|
|
207
|
+
return `${path}?${params}`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function cachePolicy(
|
|
211
|
+
path: string,
|
|
212
|
+
query: Record<string, QueryValue>,
|
|
213
|
+
now: () => number,
|
|
214
|
+
): { ttlMs: number; tags: readonly string[] } | null {
|
|
215
|
+
const suppliedDate = [query.date, query.endDate, query.end]
|
|
216
|
+
.find((value): value is string | number | boolean => value !== undefined && value !== null);
|
|
217
|
+
// Some APIs put their date in the path (for example weekly-plan), rather
|
|
218
|
+
// than in a query parameter. Treat them consistently with query-based reads.
|
|
219
|
+
const pathDate = path.match(/\/(\d{4}-\d{2}-\d{2})(?:\/|$)/)?.[1];
|
|
220
|
+
const dateHint = typeof suppliedDate === "string" ? suppliedDate.slice(0, 10) : pathDate;
|
|
221
|
+
// Dates represent the member's day, not UTC server time. Current-day reads
|
|
222
|
+
// use the following bounded TTLs: home 30s; activity 60s; recovery, sleep,
|
|
223
|
+
// trends, lifts, communities and journal 5m; calendar 15m; settings 10m.
|
|
224
|
+
// Historical reads may use their longer endpoint-specific TTLs below.
|
|
225
|
+
const today = isoDay(new Date(now()));
|
|
226
|
+
// Calendar reads cover the whole month containing the date, and weekly-plan
|
|
227
|
+
// reads the week containing it, so they're only historical once that whole
|
|
228
|
+
// period is over.
|
|
229
|
+
const periodEnd = !dateHint
|
|
230
|
+
? undefined
|
|
231
|
+
: path.includes("calendar/")
|
|
232
|
+
? `${dateHint.slice(0, 7)}-31`
|
|
233
|
+
: path.includes("weekly-plan")
|
|
234
|
+
? new Date(Date.parse(`${dateHint}T00:00:00Z`) + 6 * 86_400_000).toISOString().slice(0, 10)
|
|
235
|
+
: dateHint;
|
|
236
|
+
const historical = Boolean(periodEnd && periodEnd < today);
|
|
237
|
+
// Freshness-sensitive endpoints must never return a completed cached value.
|
|
238
|
+
if (
|
|
239
|
+
path === "/health-tab-bff/v1/health-tab" ||
|
|
240
|
+
path === "/activities-service/v1/user-state" ||
|
|
241
|
+
path.startsWith("/health-service/v2/stress-bff/")
|
|
242
|
+
) return null;
|
|
243
|
+
// Never cache: the coach and memory (every read can change state), other
|
|
244
|
+
// people's data (D9: community + Humans), and today's health monitor.
|
|
245
|
+
if (
|
|
246
|
+
path.startsWith("/ai-conversation-") ||
|
|
247
|
+
path.startsWith("/context-hub-bff/") ||
|
|
248
|
+
path.startsWith("/community-service/") ||
|
|
249
|
+
path.startsWith("/followers-service/") ||
|
|
250
|
+
path.startsWith("/coaching-service/v1/health/bff/monitor")
|
|
251
|
+
) return null;
|
|
252
|
+
// Heart rate: a window that ended over a day ago is settled; anything newer can
|
|
253
|
+
// still gain readings when the strap syncs, so it is always read fresh.
|
|
254
|
+
if (path.startsWith("/metrics-service/")) {
|
|
255
|
+
const end = typeof query.end === "string" ? Date.parse(query.end) : NaN;
|
|
256
|
+
return Number.isFinite(end) && end < now() - 24 * 60 * 60_000 ? { ttlMs: 15 * 60_000, tags: ["daily"] } : null;
|
|
257
|
+
}
|
|
258
|
+
// Bulk cycle history spans many days, so it is short-lived and dropped by any activity, sleep or journal write.
|
|
259
|
+
if (path === "/core-details-bff/v0/cycles/details") return { ttlMs: 5 * 60_000, tags: ["daily", "activity", "sleep", "recovery"] };
|
|
260
|
+
// Static catalogs change with app releases, not with user actions.
|
|
261
|
+
if (
|
|
262
|
+
path === "/activities-service/v1/sports" ||
|
|
263
|
+
path === "/activities-service/v2/activity-types" ||
|
|
264
|
+
path === "/weightlifting-service/v2/exercise" ||
|
|
265
|
+
path === "/strap-location-service/v1/garment"
|
|
266
|
+
) return { ttlMs: 24 * 60 * 60_000, tags: ["catalog"] };
|
|
267
|
+
// Per-activity detail screens carry no date hint; their content changes on edits.
|
|
268
|
+
if (path.startsWith("/core-details-bff/v2/sleep-details/") || path.includes("recovery-details")) {
|
|
269
|
+
return { ttlMs: 5 * 60_000, tags: ["activity", "sleep", "daily"] };
|
|
270
|
+
}
|
|
271
|
+
if (path.startsWith("/progression-service/v3/trends/WEIGHT/readings")) {
|
|
272
|
+
return { ttlMs: 5 * 60_000, tags: ["trend", "weight", "profile"] };
|
|
273
|
+
}
|
|
274
|
+
if (path.includes("/weekly-plan")) return { ttlMs: historical ? 60 * 60_000 : 5 * 60_000, tags: ["trend", "weekly_plan", "daily"] };
|
|
275
|
+
if (path.startsWith("/milestones-service/") || path.startsWith("/streaks-service/")) {
|
|
276
|
+
return { ttlMs: 10 * 60_000, tags: ["milestones"] };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (path === "/home-service/v1/home") return { ttlMs: historical ? 60 * 60_000 : 30_000, tags: ["daily"] };
|
|
280
|
+
if (path.includes("calendar/")) return { ttlMs: historical ? 12 * 60 * 60_000 : 15 * 60_000, tags: ["calendar", "daily"] };
|
|
281
|
+
if (path.includes("deep-dive/recovery") || path === "/developer/v2/recovery") {
|
|
282
|
+
return { ttlMs: historical ? 60 * 60_000 : 5 * 60_000, tags: ["recovery", "daily"] };
|
|
283
|
+
}
|
|
284
|
+
if (path.includes("deep-dive/sleep") || path === "/developer/v2/activity/sleep") {
|
|
285
|
+
return { ttlMs: historical ? 60 * 60_000 : 5 * 60_000, tags: ["sleep", "daily"] };
|
|
286
|
+
}
|
|
287
|
+
if (path.includes("sleepneed")) return { ttlMs: 60_000, tags: ["sleep_plan", "daily"] };
|
|
288
|
+
if (path.includes("progression-service")) return { ttlMs: historical ? 60 * 60_000 : 5 * 60_000, tags: ["trend"] };
|
|
289
|
+
if (path.includes("activity/workout") || path.includes("cardio-details")) {
|
|
290
|
+
return { ttlMs: historical ? 15 * 60_000 : 60_000, tags: ["activity", "daily"] };
|
|
291
|
+
}
|
|
292
|
+
if (path.includes("weightlifting-service")) return { ttlMs: 5 * 60_000, tags: ["lift"] };
|
|
293
|
+
if (
|
|
294
|
+
path.includes("bootstrap") ||
|
|
295
|
+
path.includes("hidden-metrics") ||
|
|
296
|
+
path.includes("stealth-mode") ||
|
|
297
|
+
path.includes("hr-zones") ||
|
|
298
|
+
path.includes("smart-alarm")
|
|
299
|
+
) return { ttlMs: 10 * 60_000, tags: ["settings", "profile"] };
|
|
300
|
+
if (path.includes("journal-service")) return { ttlMs: 5 * 60_000, tags: ["journal", "daily"] };
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function writeInvalidationTags(path: string): readonly string[] {
|
|
305
|
+
const ACTIVITY = ["activity", "daily", "sleep", "recovery", "sleep_plan", "calendar", "trend", "lift", "milestones"] as const;
|
|
306
|
+
if (
|
|
307
|
+
path.includes("create-activity") || path.includes("cardio-details") || path.includes("sleep-details") ||
|
|
308
|
+
path.includes("recovery-details") || path.startsWith("/activities-service/v2/activities") ||
|
|
309
|
+
path.startsWith("/core-details-bff/v1/activities/merge")
|
|
310
|
+
) return ACTIVITY;
|
|
311
|
+
// The coach can create, edit and delete activities, journal answers and
|
|
312
|
+
// templates server-side, but only in a chat turn. Opening a conversation
|
|
313
|
+
// (whoop_coach_history does it just to read), image files and AI settings
|
|
314
|
+
// change nothing that is cached.
|
|
315
|
+
if (path.startsWith("/ai-conversation-")) {
|
|
316
|
+
return /\/conversation\/[^/]+\/turn$/.test(path) ? [...ACTIVITY, "journal", "settings"] : [];
|
|
317
|
+
}
|
|
318
|
+
if (path.startsWith("/progression-service/v3/trends/WEIGHT/readings")) {
|
|
319
|
+
// The latest reading also rewrites the profile weight (bootstrap).
|
|
320
|
+
return ["trend", "weight", "profile", "settings", "daily"];
|
|
321
|
+
}
|
|
322
|
+
if (path.includes("/weekly-plan")) return ["trend", "weekly_plan", "daily"];
|
|
323
|
+
if (
|
|
324
|
+
path.includes("journal-service") || path.includes("menstrual") || path.includes("symptom") ||
|
|
325
|
+
path.includes("hormonal") || path.includes("pregnancy") || path.startsWith("/womens-health-service/") ||
|
|
326
|
+
path.startsWith("/activities-service/v1/journals/")
|
|
327
|
+
) {
|
|
328
|
+
return ["journal", "daily", "recovery", "sleep", "trend", "settings"];
|
|
329
|
+
}
|
|
330
|
+
if (path.includes("weightlifting-service")) {
|
|
331
|
+
// A custom exercise is a row of the 24 h exercise catalog, which lifts send verbatim.
|
|
332
|
+
return path.includes("/custom-exercise") ? ["lift", "activity", "daily", "trend", "milestones", "catalog"] : ["lift", "activity", "daily", "trend", "milestones"];
|
|
333
|
+
}
|
|
334
|
+
if (
|
|
335
|
+
path.includes("profile-service") || path.includes("hr-zones") || path.includes("smart-alarm") ||
|
|
336
|
+
path.includes("hidden-metrics") || path.includes("stealth-mode") || path.startsWith("/users-service/") ||
|
|
337
|
+
path.startsWith("/user-preferences-service/") || path.startsWith("/notification-service/") ||
|
|
338
|
+
path.startsWith("/privacy-service")
|
|
339
|
+
) {
|
|
340
|
+
return ["settings", "profile", "daily", "sleep_plan"];
|
|
341
|
+
}
|
|
342
|
+
// Memory writes touch nothing cached (the context hub is never cached).
|
|
343
|
+
if (path.startsWith("/context-hub-bff/")) return [];
|
|
344
|
+
// whoop_raw can mutate any undocumented endpoint.
|
|
345
|
+
return ["all"];
|
|
346
|
+
}
|
|
347
|
+
|
|
16
348
|
export class WhoopClient {
|
|
17
|
-
|
|
349
|
+
private readonly cache: RequestCache;
|
|
350
|
+
|
|
351
|
+
constructor(private readonly config: WhoopClientConfig) {
|
|
352
|
+
this.cache = new RequestCache(config.now ?? Date.now);
|
|
353
|
+
}
|
|
18
354
|
|
|
19
355
|
async get<T = unknown>(
|
|
20
356
|
path: string,
|
|
21
357
|
query: Record<string, QueryValue> = {},
|
|
358
|
+
options: { fresh?: boolean } = {},
|
|
22
359
|
): Promise<T> {
|
|
23
|
-
|
|
360
|
+
// `fresh` skips cached and in-flight values. Use it for reads that a write
|
|
361
|
+
// merges into or compares against, since the app may have changed them.
|
|
362
|
+
return this.cache.getOrLoad(cacheKey(path, query), cachePolicy(path, query, this.config.now ?? Date.now), () =>
|
|
363
|
+
this.request<T>("GET", path, query, undefined),
|
|
364
|
+
options.fresh,
|
|
365
|
+
);
|
|
24
366
|
}
|
|
25
367
|
|
|
26
368
|
async post<T = unknown>(
|
|
@@ -39,6 +381,14 @@ export class WhoopClient {
|
|
|
39
381
|
return this.request<T>("PUT", path, query, body);
|
|
40
382
|
}
|
|
41
383
|
|
|
384
|
+
async patch<T = unknown>(
|
|
385
|
+
path: string,
|
|
386
|
+
body: unknown,
|
|
387
|
+
query: Record<string, QueryValue> = {},
|
|
388
|
+
): Promise<T> {
|
|
389
|
+
return this.request<T>("PATCH", path, query, body);
|
|
390
|
+
}
|
|
391
|
+
|
|
42
392
|
async delete<T = unknown>(
|
|
43
393
|
path: string,
|
|
44
394
|
query: Record<string, QueryValue> = {},
|
|
@@ -46,14 +396,60 @@ export class WhoopClient {
|
|
|
46
396
|
return this.request<T>("DELETE", path, query, undefined);
|
|
47
397
|
}
|
|
48
398
|
|
|
399
|
+
/**
|
|
400
|
+
* A write that also says which status WHOOP answered with (200, 201, 204…).
|
|
401
|
+
* For whoop_raw, which shows a response as it was instead of assuming 200.
|
|
402
|
+
*/
|
|
403
|
+
async write<T = unknown>(
|
|
404
|
+
method: "POST" | "PUT" | "PATCH" | "DELETE",
|
|
405
|
+
path: string,
|
|
406
|
+
body: unknown,
|
|
407
|
+
query: Record<string, QueryValue> = {},
|
|
408
|
+
): Promise<{ status: number; body: T }> {
|
|
409
|
+
const seen: { status?: number } = {};
|
|
410
|
+
const result = await this.request<T>(method, path, query, method === "DELETE" ? undefined : body, seen);
|
|
411
|
+
return { status: seen.status ?? 200, body: result };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Raw writes can target unknown resources, so callers may flush all state. */
|
|
415
|
+
invalidateAll(): void {
|
|
416
|
+
this.cache.invalidate(["all"]);
|
|
417
|
+
}
|
|
418
|
+
|
|
49
419
|
private async request<T>(
|
|
50
420
|
method: string,
|
|
51
421
|
path: string,
|
|
52
422
|
query: Record<string, QueryValue>,
|
|
53
423
|
body: unknown,
|
|
424
|
+
seen?: { status?: number },
|
|
54
425
|
): Promise<T> {
|
|
426
|
+
if (method === "GET") return this.send<T>(method, path, query, body);
|
|
427
|
+
// A failed or timed-out write may still have been applied server-side, so
|
|
428
|
+
// drop dependent reads whatever the outcome.
|
|
429
|
+
try {
|
|
430
|
+
return await this.send<T>(method, path, query, body, false, 0, seen);
|
|
431
|
+
} finally {
|
|
432
|
+
const tags = writeInvalidationTags(path);
|
|
433
|
+
this.cache.invalidate(tags);
|
|
434
|
+
// Exercise history, PRs and e1RM lag a lift or activity write (live: seconds,
|
|
435
|
+
// up to ~30 s). A page read in that gap must not be replayed for its 5-min TTL.
|
|
436
|
+
if (tags.includes("lift")) this.cache.holdOff("lift", LIFT_SETTLE_MS);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
private async send<T>(
|
|
441
|
+
method: string,
|
|
442
|
+
path: string,
|
|
443
|
+
query: Record<string, QueryValue>,
|
|
444
|
+
body: unknown,
|
|
445
|
+
retried = false,
|
|
446
|
+
attempt412 = 0,
|
|
447
|
+
seen?: { status?: number },
|
|
448
|
+
): Promise<T> {
|
|
449
|
+
assertVerbatimPath(path);
|
|
450
|
+
// No `apiVersion` param: the app sent it on 0 of 5,813 requests (2026-11
|
|
451
|
+
// capture), so it was totem's clearest single tell.
|
|
55
452
|
const url = new URL(BASE_URL + path);
|
|
56
|
-
url.searchParams.set("apiVersion", API_VERSION);
|
|
57
453
|
for (const [k, v] of Object.entries(query)) {
|
|
58
454
|
if (v === undefined || v === null) continue;
|
|
59
455
|
url.searchParams.set(k, String(v));
|
|
@@ -64,7 +460,7 @@ export class WhoopClient {
|
|
|
64
460
|
// accept, priority — see device.ts), then layer auth and, for writes, the
|
|
65
461
|
// body content-type on top so they always win. `accept-encoding` is left
|
|
66
462
|
// unset on purpose: setting it manually disables undici's automatic response
|
|
67
|
-
// decompression, which would break response.
|
|
463
|
+
// decompression, which would break response parsing.
|
|
68
464
|
const headers: Record<string, string> = {
|
|
69
465
|
...deviceHeaders(),
|
|
70
466
|
authorization: `Bearer ${token}`,
|
|
@@ -86,33 +482,67 @@ export class WhoopClient {
|
|
|
86
482
|
if (bodyString !== undefined) init.body = bodyString;
|
|
87
483
|
|
|
88
484
|
let response: Response;
|
|
485
|
+
let bodyText: string;
|
|
89
486
|
try {
|
|
90
487
|
response = await fetch(url.toString(), init);
|
|
488
|
+
// fetch() resolves on the headers, so the body is read under the same
|
|
489
|
+
// timer: a response that stalled mid-body used to hang forever, and every
|
|
490
|
+
// identical GET joined the hung in-flight load. An error status is still
|
|
491
|
+
// reported when its body can't be read.
|
|
492
|
+
const { ok } = response;
|
|
493
|
+
bodyText = await response.text().catch((error: unknown) => {
|
|
494
|
+
if (ok) throw error;
|
|
495
|
+
return "";
|
|
496
|
+
});
|
|
91
497
|
} finally {
|
|
92
498
|
clearTimeout(timeout);
|
|
93
499
|
}
|
|
94
500
|
|
|
95
501
|
if (response.status === 401) {
|
|
96
|
-
|
|
502
|
+
// Whoop can reject a token before its local expiry (revocation, clock
|
|
503
|
+
// skew, a birthday/country profile edit). Force one refresh and retry.
|
|
504
|
+
if (!retried && this.config.onUnauthorized) {
|
|
505
|
+
this.config.onUnauthorized(token);
|
|
506
|
+
return this.send<T>(method, path, query, body, true, attempt412, seen);
|
|
507
|
+
}
|
|
508
|
+
// A 401 that survives a fresh token is path-specific (WHOOP refuses some
|
|
509
|
+
// endpoints for user tokens), not an expired login. Login expiry surfaces
|
|
510
|
+
// from the refresh itself (WhoopAuthExpiredError in the token manager).
|
|
511
|
+
throw new WhoopEndpointUnauthorizedError(path);
|
|
512
|
+
}
|
|
513
|
+
if (response.status === 412 && method !== "GET" && attempt412 < RETRY_412_DELAYS_MS.length) {
|
|
514
|
+
// "User's cycle is concurrently being updated": WHOOP is recomputing the
|
|
515
|
+
// day (often right after another write). It succeeds on retry.
|
|
516
|
+
await (this.config.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))))(RETRY_412_DELAYS_MS[attempt412]!);
|
|
517
|
+
return this.send<T>(method, path, query, body, retried, attempt412 + 1, seen);
|
|
97
518
|
}
|
|
98
519
|
if (response.status >= 500) {
|
|
99
|
-
|
|
520
|
+
const text = bodyText;
|
|
521
|
+
throw new WhoopServerError(response.status, path, htmlTitle(text) ?? apiErrorDetail(text));
|
|
100
522
|
}
|
|
101
523
|
if (!response.ok) {
|
|
102
|
-
const text =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const parsed = JSON.parse(text) as { error_description?: string; error?: string };
|
|
106
|
-
description = parsed.error_description ?? parsed.error;
|
|
107
|
-
} catch {
|
|
108
|
-
// body not JSON
|
|
524
|
+
const text = bodyText;
|
|
525
|
+
if (response.status === 412) {
|
|
526
|
+
throw new WhoopApiError(412, path, text, "WHOOP is updating this day; try again in a minute");
|
|
109
527
|
}
|
|
528
|
+
if (response.status === 409) {
|
|
529
|
+
throw new WhoopConflictError(path, text, parseOverlaps(text), apiErrorDetail(text));
|
|
530
|
+
}
|
|
531
|
+
const description = apiErrorDetail(text) ??
|
|
532
|
+
(response.status === 422 && !text.trim() ? "WHOOP rejected the request shape (422, no details)" : undefined);
|
|
110
533
|
throw new WhoopApiError(response.status, path, text, description);
|
|
111
534
|
}
|
|
112
535
|
|
|
113
|
-
if (
|
|
114
|
-
|
|
536
|
+
if (seen) seen.status = response.status;
|
|
537
|
+
if (response.status === 204) return undefined as T;
|
|
538
|
+
// Some endpoints answer 200 with an empty body, or a bare JSON literal
|
|
539
|
+
// (profile PUT returns `false` when it refused the values).
|
|
540
|
+
const text = bodyText;
|
|
541
|
+
if (!text.trim()) return undefined as T;
|
|
542
|
+
try {
|
|
543
|
+
return JSON.parse(text) as T;
|
|
544
|
+
} catch {
|
|
545
|
+
return text as T;
|
|
115
546
|
}
|
|
116
|
-
return (await response.json()) as T;
|
|
117
547
|
}
|
|
118
548
|
}
|