@trainheroic-unofficial/cli 3.1.0 → 3.1.1

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
@@ -305,6 +305,18 @@ const programWorkoutResponseSchema = z.looseObject({
305
305
  });
306
306
  /** The calendar edit-view response we read sessions back from. */
307
307
  const programsEditResponseSchema = z.looseObject({ programWorkouts: z.array(programWorkoutResponseSchema).optional() });
308
+ /** A standalone program create response (container id + usable calendar/program id). */
309
+ const programCreateResponseSchema = z.looseObject({
310
+ id: intLike,
311
+ title: z.string(),
312
+ programId: intLike,
313
+ group_program: intLike,
314
+ type: intLike,
315
+ program: z.looseObject({
316
+ id: intLike,
317
+ program_type: intLike
318
+ }).optional()
319
+ });
308
320
  //#endregion
309
321
  //#region ../dto/src/workout.ts
310
322
  /** A single exercise prescription inside a block. */
@@ -521,7 +533,7 @@ var TrainHeroicClient = class {
521
533
  session = await this.#ensureSession();
522
534
  res = await this.#send(method, url, session, options.body);
523
535
  }
524
- if (!res.ok) notifyHttpError(this.#onHttpError, method, url, res.status);
536
+ if (!res.ok && !options.expectedStatuses?.includes(res.status)) notifyHttpError(this.#onHttpError, method, url, res.status);
525
537
  const text = await res.text();
526
538
  let data = text;
527
539
  if (text.length > 0) try {
@@ -2572,6 +2584,45 @@ function readExercise(ex) {
2572
2584
  instruction: str(ex.instruction)
2573
2585
  };
2574
2586
  }
2587
+ const PROGRAM_KINDS = ["calendar", "fixed"];
2588
+ /**
2589
+ * Create a standalone ongoing calendar or fixed-length program.
2590
+ *
2591
+ * TrainHeroic returns both a container id and the underlying program id. They are deliberately
2592
+ * named separately here because `/3.0/coach/program/{containerId}` returns 401, while the
2593
+ * `programId` works. This write is not idempotent: callers must not blindly retry an uncertain
2594
+ * result. The live API currently accepts `name` but may generate its own title; `nameApplied`
2595
+ * reports what happened.
2596
+ */
2597
+ async function createProgram(client, args) {
2598
+ if (!PROGRAM_KINDS.includes(args.kind)) throw new Error(`Unknown program kind: ${String(args.kind)}.`);
2599
+ if (typeof args.name !== "string" || args.name.trim() === "") throw new Error("Program name must not be blank.");
2600
+ const requestedName = args.name.trim();
2601
+ const res = await client.request("POST", "/1.0/coach/programs/create", { body: {
2602
+ finite: args.kind === "fixed",
2603
+ name: requestedName
2604
+ } });
2605
+ if (!res.ok) {
2606
+ const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
2607
+ throw new Error(`Program create failed (HTTP ${res.status}): ${detail}`);
2608
+ }
2609
+ checkResponse(programCreateResponseSchema, res.data, "program create");
2610
+ const rec = isRecord(res.data) ? res.data : {};
2611
+ const nested = isRecord(rec.program) ? rec.program : {};
2612
+ const containerId = coerceInt(rec.id);
2613
+ const programId = coerceInt(rec.programId) ?? coerceInt(rec.group_program) ?? coerceInt(nested.id);
2614
+ if (containerId === null || programId === null) throw new Error("Program create response is missing the container id or program id.");
2615
+ if (typeof rec.title !== "string" || rec.title.trim() === "") throw new Error("Program create response is missing the assigned title.");
2616
+ const title = rec.title;
2617
+ return {
2618
+ containerId,
2619
+ programId,
2620
+ title,
2621
+ kind: args.kind,
2622
+ requestedName,
2623
+ nameApplied: title === requestedName
2624
+ };
2625
+ }
2575
2626
  /** Normalize one-or-many emails into a deduped, trimmed list. */
2576
2627
  function emailList(emails) {
2577
2628
  return [...new Set(emails.map((e) => e.trim()).filter((e) => e.length > 0))];
@@ -3486,6 +3537,7 @@ Coach — manage a roster (needs a coach account):
3486
3537
  coach athlete-restore --athletes <id,id,...>
3487
3538
 
3488
3539
  teams & join codes:
3540
+ coach program-create --kind calendar|fixed --name "..."
3489
3541
  coach team-create --title "..."
3490
3542
  coach team-update --team <id> [--title "..."] [--group-program <id>]
3491
3543
  coach team-delete --team <id> --yes
@@ -4139,7 +4191,7 @@ async function cmdInstallSkill() {
4139
4191
  }
4140
4192
  out({ installed });
4141
4193
  }
4142
- const COACH_USAGE = "usage: trainheroic coach <head-coach|athletes|programs|teams|notifications|analytics|program <id>|team <id>|team-codes <id>|roster-activity|team-volume|athlete-training|athlete-lift-history|main-lift-prs|athlete-workouts|log-set|log-session|prescribe-set|swap-exercise|athlete-invite|athlete-archive|athlete-restore|team-create|team-update|team-delete|team-code-create|team-code-delete|session-copy|session-unpublish|session-save-template|analytics-query|exercise|workout|message>";
4194
+ const COACH_USAGE = "usage: trainheroic coach <head-coach|athletes|programs|teams|notifications|analytics|program <id>|program-create|team <id>|team-codes <id>|roster-activity|team-volume|athlete-training|athlete-lift-history|main-lift-prs|athlete-workouts|log-set|log-session|prescribe-set|swap-exercise|athlete-invite|athlete-archive|athlete-restore|team-create|team-update|team-delete|team-code-create|team-code-delete|session-copy|session-unpublish|session-save-template|analytics-query|exercise|workout|message>";
4143
4195
  const COACH_LOG_SET_USAGE = "coach log-set --athlete <id> --date Y-M-D --set <savedWorkoutSetId> <resultsJson> --yes";
4144
4196
  async function cmdCoachLogSet(client, a) {
4145
4197
  const { values, positionals } = parse(a, {
@@ -4327,6 +4379,20 @@ async function cmdCoachTeamCreate(client, a) {
4327
4379
  const { values } = parse(a, { title: { type: "string" } });
4328
4380
  return out(await mutate(client, "POST", "/1.0/coach/team/createWithTitleAndCode", { title: need(values.title, "coach team-create --title \"...\"") }));
4329
4381
  }
4382
+ async function cmdCoachProgramCreate(client, a) {
4383
+ const usage = "coach program-create --kind calendar|fixed --name \"...\"";
4384
+ const { values } = parse(a, {
4385
+ kind: { type: "string" },
4386
+ name: { type: "string" }
4387
+ });
4388
+ const kind = need(values.kind, usage);
4389
+ const name = need(values.name, usage);
4390
+ if (!PROGRAM_KINDS.includes(kind)) fail(usage);
4391
+ return out(await createProgram(client, {
4392
+ kind,
4393
+ name
4394
+ }));
4395
+ }
4330
4396
  async function cmdCoachTeamUpdate(client, a) {
4331
4397
  const usage = "coach team-update --team <id> [--title \"...\"] [--group-program <id>] (at least one of --title / --group-program)";
4332
4398
  const { values } = parse(a, {
@@ -4513,6 +4579,7 @@ async function cmdCoach(client, rest) {
4513
4579
  case "athlete-archive": return cmdCoachAthleteArchive(client, a);
4514
4580
  case "athlete-restore": return cmdCoachAthleteRestore(client, a);
4515
4581
  case "team-create": return cmdCoachTeamCreate(client, a);
4582
+ case "program-create": return cmdCoachProgramCreate(client, a);
4516
4583
  case "team-update": return cmdCoachTeamUpdate(client, a);
4517
4584
  case "team-delete": return cmdCoachTeamDelete(client, a);
4518
4585
  case "team-code-create": return cmdCoachTeamCodeCreate(client, a);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,8 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "3.1.0",
25
- "@trainheroic-unofficial/js": "3.1.0"
24
+ "@trainheroic-unofficial/dto": "3.1.1",
25
+ "@trainheroic-unofficial/js": "3.1.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^26.2.0",
@@ -90,7 +90,7 @@ response shape, or an area not covered here.
90
90
  | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
91
91
  | Athletes | `$TH coach athletes`; `coach athlete-invite\|athlete-archive\|athlete-restore` |
92
92
  | Teams / join codes | `$TH coach teams\|team <id>\|team-codes <id>`; `coach team-create\|team-update\|team-delete\|team-code-create\|team-code-delete` |
93
- | Programs | `$TH coach programs`, `$TH coach program <id>` |
93
+ | Programs | `$TH coach programs`, `$TH coach program <id>`, `$TH coach program-create --kind calendar\|fixed --name "..."` |
94
94
  | Exercises | `$TH coach exercise resolve\|search\|get\|sync\|create\|forget\|stats` (below) |
95
95
  | Sessions / workouts | `$TH coach workout build\|read\|publish\|remove`; `coach session-copy\|session-unpublish\|session-save-template` |
96
96
  | Roster training | `$TH coach roster-activity\|athlete-training\|athlete-lift-history\|athlete-workouts` (read another athlete's training) |
@@ -286,16 +286,17 @@ Deletes an access code. No request body needed.
286
286
 
287
287
  ### Programs
288
288
 
289
- | Method | Endpoint | Description |
290
- | ------ | ------------------------------- | ------------------------------- |
291
- | GET | `/v5/programs` | List programs |
292
- | GET | `/v5/programs/new` | New programs |
293
- | GET | `/v5/programs/free` | Free programs |
294
- | GET | `/v5/programs/fixed` | Fixed programs |
295
- | GET | `/1.0/coach/programs` | Coach programs list |
296
- | GET | `/1.0/coach/programs/edit/{id}` | Program edit data |
297
- | GET | `/3.0/coach/program/{id}` | Program detail (full structure) |
298
- | GET | `/1.0/coach/subscriptions` | Program subscriptions |
289
+ | Method | Endpoint | Description |
290
+ | ------ | ------------------------------- | --------------------------- |
291
+ | GET | `/v5/programs` | List programs |
292
+ | GET | `/v5/programs/new` | New programs |
293
+ | GET | `/v5/programs/free` | Free programs |
294
+ | GET | `/v5/programs/fixed` | Fixed programs |
295
+ | GET | `/1.0/coach/programs` | Coach programs list |
296
+ | POST | `/1.0/coach/programs/create` | Create a standalone program |
297
+ | GET | `/1.0/coach/programs/edit/{id}` | Program edit data |
298
+ | GET | `/3.0/coach/program/{id}` | Program metadata/detail |
299
+ | GET | `/1.0/coach/subscriptions` | Program subscriptions |
299
300
 
300
301
  #### `GET /3.0/coach/program/{id}` Response
301
302
 
@@ -336,6 +337,14 @@ Deletes an access code. No request body needed.
336
337
  ]
337
338
  ```
338
339
 
340
+ #### `POST /1.0/coach/programs/create`
341
+
342
+ Send `{ "finite": false, "name": "..." }` for an ongoing calendar or `finite: true` for a
343
+ fixed-length program. The endpoint is not idempotent. Its response contains two different ids:
344
+ `id` is the container returned by `GET /1.0/coach/programs`, while `programId` (also
345
+ `group_program`) is the actual program id used by `/3.0/coach/program/{id}` and workout writes.
346
+ The live API may ignore `name` and assign a generated title; use the response's `title` as truth.
347
+
339
348
  ---
340
349
 
341
350
  ### Exercises