@trainheroic-unofficial/cli 2.1.0 → 3.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/dist/cli.mjs CHANGED
@@ -375,6 +375,34 @@ function parseWorkoutDate(s) {
375
375
  ];
376
376
  }
377
377
  //#endregion
378
+ //#region ../js/src/http-error.ts
379
+ /**
380
+ * A final non-2xx response from TrainHeroic. The error deliberately carries only request
381
+ * metadata that is safe to send to telemetry: never the path, query string, request body,
382
+ * response body, credentials, or session token.
383
+ */
384
+ var TrainHeroicHttpError = class extends Error {
385
+ name = "TrainHeroicHttpError";
386
+ method;
387
+ status;
388
+ host;
389
+ constructor(method, url, status) {
390
+ const parsed = new URL(url);
391
+ const normalizedMethod = method.toUpperCase();
392
+ super(`TrainHeroic ${normalizedMethod} request failed with HTTP ${status}`);
393
+ this.method = normalizedMethod;
394
+ this.status = status;
395
+ this.host = parsed.host;
396
+ }
397
+ };
398
+ /** Call observability hooks without allowing them to change SDK behavior. */
399
+ function notifyHttpError(handler, method, url, status) {
400
+ if (!handler) return;
401
+ try {
402
+ Promise.resolve(handler(new TrainHeroicHttpError(method, url, status))).catch(() => {});
403
+ } catch {}
404
+ }
405
+ //#endregion
378
406
  //#region ../js/src/auth.ts
379
407
  const DEFAULT_AUTH_URL = "https://apis.trainheroic.com/auth";
380
408
  /**
@@ -396,8 +424,9 @@ function authUrl() {
396
424
  * the Phase 0 spike: no refresh_token, no api_token, no TTL). The 48-char session_id
397
425
  * is sent as the `session-token` header and works against both API hosts.
398
426
  */
399
- async function loginTrainHeroic(email, password) {
400
- const res = await fetch(authUrl(), {
427
+ async function loginTrainHeroic(email, password, options = {}) {
428
+ const url = authUrl();
429
+ const res = await fetch(url, {
401
430
  method: "POST",
402
431
  headers: {
403
432
  "content-type": "application/x-www-form-urlencoded",
@@ -408,7 +437,10 @@ async function loginTrainHeroic(email, password) {
408
437
  password
409
438
  }).toString()
410
439
  });
411
- if (!res.ok) return null;
440
+ if (!res.ok) {
441
+ notifyHttpError(options.onHttpError, "POST", url, res.status);
442
+ return null;
443
+ }
412
444
  const data = await res.json().catch(() => null);
413
445
  if (!data || typeof data.id !== "number" || !data.session_id) return null;
414
446
  return {
@@ -451,6 +483,7 @@ var TrainHeroicClient = class {
451
483
  #email;
452
484
  #password;
453
485
  #onSession;
486
+ #onHttpError;
454
487
  #sessionId;
455
488
  #loginInFlight = null;
456
489
  constructor(email, password, sessionId = null, options = {}) {
@@ -458,6 +491,7 @@ var TrainHeroicClient = class {
458
491
  this.#password = password;
459
492
  this.#sessionId = sessionId;
460
493
  this.#onSession = options.onSession;
494
+ this.#onHttpError = options.onHttpError;
461
495
  }
462
496
  get sessionId() {
463
497
  return this.#sessionId;
@@ -472,7 +506,7 @@ var TrainHeroicClient = class {
472
506
  }
473
507
  }
474
508
  async #login() {
475
- const session = await loginTrainHeroic(this.#email, this.#password);
509
+ const session = await loginTrainHeroic(this.#email, this.#password, this.#onHttpError ? { onHttpError: this.#onHttpError } : {});
476
510
  if (!session) throw new TrainHeroicAuthError("TrainHeroic login failed");
477
511
  this.#sessionId = session.sessionId;
478
512
  try {
@@ -489,6 +523,7 @@ var TrainHeroicClient = class {
489
523
  session = await this.#ensureSession();
490
524
  res = await this.#send(method, url, session, options.body);
491
525
  }
526
+ if (!res.ok) notifyHttpError(this.#onHttpError, method, url, res.status);
492
527
  const text = await res.text();
493
528
  let data = text;
494
529
  if (text.length > 0) try {
@@ -2364,8 +2399,10 @@ function unitAdvisory(blockTitle, ex, defaults) {
2364
2399
  if (ex.weight !== void 0 && ex.weight !== null) {
2365
2400
  const sentP2 = Math.trunc(Number(ex.param_2_type ?? 1));
2366
2401
  const effP2 = defaults.param2 === 0 || defaults.param2 === null ? 1 : defaults.param2;
2367
- if (sentP2 !== effP2) if (sentP2 === 2 || sentP2 === 14) warnings.push(`${label}: ${u(sentP2)} does not stick on this exercise — it renders as ${u(effP2)}. Put it in the exercise 'instr' text and leave load blank.`);
2368
- else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
2402
+ if (sentP2 !== effP2) {
2403
+ if (sentP2 === 2 || sentP2 === 14) warnings.push(`${label}: ${u(sentP2)} does not stick on this exercise — it renders as ${u(effP2)}. Put it in the exercise 'instr' text and leave load blank.`);
2404
+ else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
2405
+ }
2369
2406
  }
2370
2407
  return {
2371
2408
  notes,
@@ -2375,7 +2412,7 @@ function unitAdvisory(blockTitle, ex, defaults) {
2375
2412
  /**
2376
2413
  * Run unit advisories across a whole block list against an exercise index. Shared by the
2377
2414
  * MCP workout_build tool and the CLI so both surface the same notes/warnings. Ensures the
2378
- * index is loaded first, otherwise `defaults` returns null on a cold index and every
2415
+ * index is loaded first, otherwise the defaults map is empty on a cold index and every
2379
2416
  * advisory is silently dropped.
2380
2417
  */
2381
2418
  async function collectAdvisories(blocks, index) {
@@ -2384,14 +2421,12 @@ async function collectAdvisories(blocks, index) {
2384
2421
  block: b,
2385
2422
  ex
2386
2423
  })));
2387
- const defaults = await Promise.all(pairs.map((p) => {
2388
- const id = Number(p.ex.id);
2389
- return Number.isFinite(id) ? index.defaults(id) : Promise.resolve(null);
2390
- }));
2424
+ const ids = [...new Set(pairs.map((p) => Number(p.ex.id)).filter((id) => Number.isFinite(id)))];
2425
+ const defaultsById = await index.defaultsMany(ids);
2391
2426
  const notes = [];
2392
2427
  const warnings = [];
2393
- pairs.forEach((p, i) => {
2394
- const def = defaults[i];
2428
+ pairs.forEach((p) => {
2429
+ const def = defaultsById.get(Number(p.ex.id));
2395
2430
  if (!def) return;
2396
2431
  const advisory = unitAdvisory(p.block.title, p.ex, def);
2397
2432
  notes.push(...advisory.notes);
@@ -2648,7 +2683,8 @@ async function copySession(client, args) {
2648
2683
  year,
2649
2684
  dayOfWeek,
2650
2685
  isToday: false
2651
- }
2686
+ },
2687
+ toTimelineDate: null
2652
2688
  } });
2653
2689
  if (!res.ok) {
2654
2690
  const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
@@ -3256,12 +3292,16 @@ var ExerciseLibrary = class {
3256
3292
  if (!s) return null;
3257
3293
  return presentExercise(s.raw);
3258
3294
  }
3259
- async defaults(id) {
3260
- const s = this.#byId.get(id);
3261
- return s ? {
3262
- param1: s.param_1_type,
3263
- param2: s.param_2_type
3264
- } : null;
3295
+ async defaultsMany(ids) {
3296
+ const result = /* @__PURE__ */ new Map();
3297
+ for (const id of ids) {
3298
+ const s = this.#byId.get(id);
3299
+ if (s) result.set(id, {
3300
+ param1: s.param_1_type,
3301
+ param2: s.param_2_type
3302
+ });
3303
+ }
3304
+ return result;
3265
3305
  }
3266
3306
  async search(query, limit = 20) {
3267
3307
  await this.ensureFresh();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,13 +21,13 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "2.1.0",
25
- "@trainheroic-unofficial/js": "2.1.0"
24
+ "@trainheroic-unofficial/dto": "3.0.0",
25
+ "@trainheroic-unofficial/js": "3.0.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^26.1.2",
28
+ "@types/node": "^26.2.0",
29
29
  "tsdown": "^0.22.14",
30
- "tsx": "^4.23.4",
30
+ "tsx": "^4.23.11",
31
31
  "typescript": "^7.0.2",
32
32
  "vitest": "^4.1.10"
33
33
  },
@@ -1301,10 +1301,14 @@ Copies or repeats a session to a target date. Used for both "Copy" and "Repeat"
1301
1301
  "year": 2026,
1302
1302
  "dayOfWeek": 0,
1303
1303
  "isToday": false
1304
- }
1304
+ },
1305
+ "toTimelineDate": null
1305
1306
  }
1306
1307
  ```
1307
1308
 
1309
+ `toTimelineDate` must be present as an explicit `null` for a calendar-date copy. Omitting it
1310
+ causes this legacy endpoint to return HTTP 500.
1311
+
1308
1312
  #### `POST /2.0/coach/calendar/programWorkout/saveWorkoutAsTemplate/{workoutId}`
1309
1313
 
1310
1314
  Saves an existing session as a reusable template in the session library. No request body needed — uses the workout ID in the URL path.