@trainheroic-unofficial/cli 1.7.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/README.md CHANGED
@@ -78,18 +78,6 @@ trainheroic coach workout build --program 988 --date 2026-6-22 --file day.json
78
78
  trainheroic coach workout build --program 988 --date 2026-6-22 --file day.json --publish --yes
79
79
  ```
80
80
 
81
- ### Raw requests
82
-
83
- For an endpoint with no dedicated command, call it directly:
84
-
85
- ```bash
86
- trainheroic request GET /user/simple
87
- trainheroic request POST /some/path '{"key":"value"}' --base apis
88
- ```
89
-
90
- `--base` selects the host: `coach` (the default, `api.trainheroic.com`) or `apis`
91
- (`apis.trainheroic.com`). The body can be inline JSON (as above) or `--file <path>`.
92
-
93
81
  ## Conventions
94
82
 
95
83
  - Output is JSON on stdout; errors go to stderr with a non-zero exit code and a readable
package/dist/cli.mjs CHANGED
@@ -426,20 +426,27 @@ var TrainHeroicAuthError = class extends Error {
426
426
  name = "TrainHeroicAuthError";
427
427
  };
428
428
  /**
429
- * Authenticated TrainHeroic API client. Holds the coach credentials (from the grant's
430
- * encrypted props) and a lazily-acquired session token cached in memory for the life
431
- * of the Durable Object instance. On a 401/403 it re-logs in once and retries, since
429
+ * Authenticated TrainHeroic API client. Holds the credentials (on the hosted Worker, from the
430
+ * grant's encrypted props) and a lazily-acquired session token cached in memory for the life of
431
+ * *this client instance* — nothing longer. On a 401/403 it re-logs in once and retries, since
432
432
  * TrainHeroic has no refresh token and sessions expire after ~1-2h.
433
+ *
434
+ * Anything that wants a session to outlive one client owns that itself: pass a previously
435
+ * acquired token as `sessionId` and keep the new one via `options.onSession`. That matters
436
+ * wherever clients are short-lived — the hosted Worker builds one per HTTP request, and each CLI
437
+ * invocation is a fresh process — because otherwise every operation replays the password.
433
438
  */
434
439
  var TrainHeroicClient = class {
435
440
  #email;
436
441
  #password;
442
+ #onSession;
437
443
  #sessionId;
438
444
  #loginInFlight = null;
439
- constructor(email, password, sessionId = null) {
445
+ constructor(email, password, sessionId = null, options = {}) {
440
446
  this.#email = email;
441
447
  this.#password = password;
442
448
  this.#sessionId = sessionId;
449
+ this.#onSession = options.onSession;
443
450
  }
444
451
  get sessionId() {
445
452
  return this.#sessionId;
@@ -457,6 +464,9 @@ var TrainHeroicClient = class {
457
464
  const session = await loginTrainHeroic(this.#email, this.#password);
458
465
  if (!session) throw new TrainHeroicAuthError("TrainHeroic login failed");
459
466
  this.#sessionId = session.sessionId;
467
+ try {
468
+ this.#onSession?.(this.#sessionId);
469
+ } catch {}
460
470
  return this.#sessionId;
461
471
  }
462
472
  async request(method, path, options = {}) {
@@ -3050,7 +3060,7 @@ var MemoryLibraryCache = class {
3050
3060
  //#region ../js/src/exercise-index.ts
3051
3061
  const LIBRARY_PATH = "/v5/exerciseLibrary/all";
3052
3062
  const CREATE_PATH = "/2.0/coach/exercise/create";
3053
- const TTL_MS$1 = 168 * 3600 * 1e3;
3063
+ const TTL_MS$1 = 6048e5;
3054
3064
  function toStored(ex) {
3055
3065
  const id = coerceInt(ex.id);
3056
3066
  if (id === null) return null;
@@ -3244,7 +3254,7 @@ function looksLikeJson(s) {
3244
3254
  }
3245
3255
  //#endregion
3246
3256
  //#region src/session-cache.ts
3247
- const TTL_MS = 6 * 3600 * 1e3;
3257
+ const TTL_MS = 216e5;
3248
3258
  function sessionPath() {
3249
3259
  return process.env.TRAINHEROIC_SESSION_FILE ?? join(homedir(), ".trainheroic", "session.json");
3250
3260
  }
@@ -3280,8 +3290,7 @@ Setup:
3280
3290
  install-skill copy the Claude Code skills to ~/.claude/skills/
3281
3291
 
3282
3292
  Shared:
3283
- whoami the logged-in account
3284
- request <METHOD> <path> [json] [--base coach|apis] [--file f] raw API call (--base defaults to coach)
3293
+ whoami the logged-in account
3285
3294
 
3286
3295
  Coach — manage a roster (needs a coach account):
3287
3296
  coach head-coach | athletes | programs | teams | notifications | analytics
@@ -3466,27 +3475,6 @@ function idList(value, label) {
3466
3475
  function library(client) {
3467
3476
  return new ExerciseLibrary(client, new JsonFileLibraryCache());
3468
3477
  }
3469
- async function cmdRequest(client, rest) {
3470
- const { values, positionals } = parse(rest, {
3471
- base: { type: "string" },
3472
- file: { type: "string" }
3473
- });
3474
- const method = need(positionals[0], "request <METHOD> <path> [json]").toUpperCase();
3475
- const path = need(positionals[1], "request <METHOD> <path> [json]");
3476
- const base = values.base;
3477
- if (base !== void 0 && base !== "coach" && base !== "apis") fail("--base must be coach or apis.");
3478
- const opts = {};
3479
- if (base !== void 0) opts.base = base;
3480
- if (method !== "GET" && method !== "DELETE") {
3481
- if (positionals[2] !== void 0 || values.file !== void 0 || !process.stdin.isTTY) opts.body = await jsonInput(positionals[2], values.file);
3482
- }
3483
- const res = await client.request(method, path, opts);
3484
- out({
3485
- status: res.status,
3486
- ok: res.ok,
3487
- data: res.data
3488
- });
3489
- }
3490
3478
  async function cmdExercise(client, rest) {
3491
3479
  const [sub, ...a] = rest;
3492
3480
  const lib = library(client);
@@ -3507,7 +3495,8 @@ async function cmdExercise(client, rest) {
3507
3495
  }
3508
3496
  case "create": {
3509
3497
  const { values, positionals } = parse(a, { file: { type: "string" } });
3510
- const exercise = validate(exerciseCreateSchema, await jsonInput(positionals[0], values.file), "exercise");
3498
+ const body = await jsonInput(positionals[0], values.file);
3499
+ const exercise = validate(exerciseCreateSchema, body, "exercise");
3511
3500
  return out(await lib.create(exercise));
3512
3501
  }
3513
3502
  case "forget": {
@@ -3713,10 +3702,11 @@ async function cmdAthleteLogSet(client, a) {
3713
3702
  });
3714
3703
  const date = isoDate(need(values.date, LOG_SET_USAGE), "--date");
3715
3704
  const savedWorkoutSetId = toInt(need(values.set, LOG_SET_USAGE), "--set");
3705
+ const results = await jsonInput(positionals[0], values.file);
3716
3706
  const args = validate(logSetArgsSchema, {
3717
3707
  date,
3718
3708
  savedWorkoutSetId,
3719
- results: await jsonInput(positionals[0], values.file)
3709
+ results
3720
3710
  }, "log-set args");
3721
3711
  if (values.yes !== true) fail(`logging to set ${savedWorkoutSetId} writes to your coach-visible log; add --yes.`);
3722
3712
  return out(await logAthleteSet(client, {
@@ -3750,9 +3740,10 @@ async function cmdAthleteLogSession(client, a) {
3750
3740
  yes: { type: "boolean" }
3751
3741
  });
3752
3742
  const date = isoDate(need(values.date, LOG_SESSION_USAGE), "--date");
3743
+ const exercises = await jsonInput(positionals[0], values.file);
3753
3744
  const args = validate(logSessionArgsSchema, {
3754
3745
  date,
3755
- exercises: await jsonInput(positionals[0], values.file)
3746
+ exercises
3756
3747
  }, "log-session args");
3757
3748
  if (values.yes !== true) fail(`logging a session on ${date} writes to your coach-visible log; add --yes.`);
3758
3749
  const result = await logAdHocSession(client, {
@@ -3968,11 +3959,12 @@ async function cmdCoachLogSet(client, a) {
3968
3959
  const athleteId = toInt(need(values.athlete, COACH_LOG_SET_USAGE), "--athlete");
3969
3960
  const date = isoDate(need(values.date, COACH_LOG_SET_USAGE), "--date");
3970
3961
  const savedWorkoutSetId = toInt(need(values.set, COACH_LOG_SET_USAGE), "--set");
3962
+ const results = await jsonInput(positionals[0], values.file);
3971
3963
  const args = validate(coachLogSetArgsSchema, {
3972
3964
  athleteId,
3973
3965
  date,
3974
3966
  savedWorkoutSetId,
3975
- results: await jsonInput(positionals[0], values.file)
3967
+ results
3976
3968
  }, "coach log-set args");
3977
3969
  if (values.yes !== true) fail(`logging to athlete ${athleteId}'s set ${savedWorkoutSetId} writes to their training log; add --yes.`);
3978
3970
  return out(await logForAthlete(client, {
@@ -3994,11 +3986,12 @@ async function cmdCoachPrescribeSet(client, a) {
3994
3986
  const athleteId = toInt(need(values.athlete, COACH_PRESCRIBE_SET_USAGE), "--athlete");
3995
3987
  const date = isoDate(need(values.date, COACH_PRESCRIBE_SET_USAGE), "--date");
3996
3988
  const savedWorkoutSetId = toInt(need(values.set, COACH_PRESCRIBE_SET_USAGE), "--set");
3989
+ const results = await jsonInput(positionals[0], values.file);
3997
3990
  const args = validate(coachPrescribeSetArgsSchema, {
3998
3991
  athleteId,
3999
3992
  date,
4000
3993
  savedWorkoutSetId,
4001
- results: await jsonInput(positionals[0], values.file)
3994
+ results
4002
3995
  }, "coach prescribe-set args");
4003
3996
  if (values.yes !== true) fail(`prescribing to athlete ${athleteId}'s set ${savedWorkoutSetId} changes their plan; add --yes.`);
4004
3997
  return out(await prescribeForAthlete(client, {
@@ -4017,10 +4010,12 @@ async function cmdCoachLogSession(client, a) {
4017
4010
  yes: { type: "boolean" }
4018
4011
  });
4019
4012
  const athleteId = toInt(need(values.athlete, COACH_LOG_SESSION_USAGE), "--athlete");
4013
+ const date = isoDate(need(values.date, COACH_LOG_SESSION_USAGE), "--date");
4014
+ const exercises = await jsonInput(positionals[0], values.file);
4020
4015
  const args = validate(coachLogSessionArgsSchema, {
4021
4016
  athleteId,
4022
- date: isoDate(need(values.date, COACH_LOG_SESSION_USAGE), "--date"),
4023
- exercises: await jsonInput(positionals[0], values.file)
4017
+ date,
4018
+ exercises
4024
4019
  }, "coach log-session args");
4025
4020
  if (values.yes !== true) fail(`logging a session for athlete ${athleteId} writes to their training log; add --yes.`);
4026
4021
  return out(await logSessionForAthlete(client, {
@@ -4336,7 +4331,6 @@ async function cmdCoach(client, rest) {
4336
4331
  async function dispatch(client, group, rest) {
4337
4332
  switch (group) {
4338
4333
  case "whoami": return out(await get(client, "/user/simple"));
4339
- case "request": return cmdRequest(client, rest);
4340
4334
  case "coach": return cmdCoach(client, rest);
4341
4335
  case "athlete": return cmdAthlete(client, rest);
4342
4336
  default: return fail(`unknown command "${group}". Run 'trainheroic help'.`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "1.7.4",
3
+ "version": "2.0.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,14 +21,14 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "1.7.4",
25
- "@trainheroic-unofficial/js": "1.7.4"
24
+ "@trainheroic-unofficial/dto": "2.0.0",
25
+ "@trainheroic-unofficial/js": "2.0.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^26.1.0",
29
- "tsdown": "^0.22.3",
30
- "tsx": "^4.23.0",
31
- "typescript": "^6.0.3",
28
+ "@types/node": "^26.1.2",
29
+ "tsdown": "^0.22.14",
30
+ "tsx": "^4.23.4",
31
+ "typescript": "^7.0.2",
32
32
  "vitest": "^4.1.10"
33
33
  },
34
34
  "scripts": {
@@ -528,22 +528,23 @@ This creates A1: Back Squat, A2: Front Squat displayed as a superset.
528
528
  ```
529
529
 
530
530
  **Parameter types (param_1_type / param_2_type):**
531
- | Value | Type | Display |
532
- |-------|------|---------|
533
- | 0 | None | (no parameter) |
534
- | 1 | Weight | `225 lb` |
535
- | 2 | Weight (% of max) | `75%` |
536
- | 3 | Reps | `5` |
537
- | 4 | Time (seconds) | `1:00` |
538
- | 5 | Distance (yards) | `50yd` |
539
- | 6 | Distance (meters) | `50m` |
540
- | 7 | Height | `inches` |
541
- | 10 | Distance (miles) | miles |
542
- | 11 | Distance (feet) | feet |
543
- | 12 | Height (inches) | inches |
544
- | 13 | Heart Rate | bpm |
545
- | 14 | RPE | rating |
546
- | 18 | Time (seconds, alt) | `0:30` |
531
+
532
+ | Value | Type | Display |
533
+ | ----- | ------------------- | -------------- |
534
+ | 0 | None | (no parameter) |
535
+ | 1 | Weight | `225 lb` |
536
+ | 2 | Weight (% of max) | `75%` |
537
+ | 3 | Reps | `5` |
538
+ | 4 | Time (seconds) | `1:00` |
539
+ | 5 | Distance (yards) | `50yd` |
540
+ | 6 | Distance (meters) | `50m` |
541
+ | 7 | Height | `inches` |
542
+ | 10 | Distance (miles) | miles |
543
+ | 11 | Distance (feet) | feet |
544
+ | 12 | Height (inches) | inches |
545
+ | 13 | Heart Rate | bpm |
546
+ | 14 | RPE | rating |
547
+ | 18 | Time (seconds, alt) | `0:30` |
547
548
 
548
549
  > **The unit is fixed per exercise — you cannot set it at prescribe time (verified).**
549
550
  > On save the API discards the `param_1_type`/`param_2_type` you send and restores
@@ -1189,12 +1190,13 @@ With weight (`param_2_type: 1`):
1189
1190
  Displays as: `2 x 40yd @ 50lb`
1190
1191
 
1191
1192
  **Distance type values:**
1192
- | param type | Unit |
1193
- |-----------|------|
1194
- | 5 | yards |
1195
- | 6 | meters |
1196
- | 10 | miles |
1197
- | 11 | feet |
1193
+
1194
+ | param type | Unit |
1195
+ | ---------- | ------ |
1196
+ | 5 | yards |
1197
+ | 6 | meters |
1198
+ | 10 | miles |
1199
+ | 11 | feet |
1198
1200
 
1199
1201
  Existing distance exercises: 193 in library (e.g. Sled Push, Bear Crawl, Sprint, Carioca, Run)
1200
1202