@trainheroic-unofficial/js 3.0.0 → 3.0.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/index.d.mts CHANGED
@@ -958,7 +958,7 @@ declare function fetchStreams(client: TrainHeroicClient): Promise<Array<{
958
958
  declare function buildCommentPayload(streamId: number, text: string, replyTo?: number | null): Record<string, unknown>;
959
959
  declare function sendComment(client: TrainHeroicClient, streamId: number, text: string, replyTo?: number | null): Promise<Record<string, unknown>>;
960
960
  declare function deleteComment(client: TrainHeroicClient, streamId: number, commentId: number): Promise<unknown>;
961
- declare function readLive(client: TrainHeroicClient, streamId: number, limit?: number): Promise<unknown[]>;
961
+ declare function readLive(client: TrainHeroicClient, streamId: number, limit?: number, afterCommentId?: number): Promise<unknown[]>;
962
962
  //#endregion
963
963
  //#region src/exercise-index.d.ts
964
964
  /**
package/dist/index.mjs CHANGED
@@ -439,8 +439,9 @@ function sortRosterByRecency(rows) {
439
439
  async function fetchRosterActivity(client, athleteIds, useMetric = false) {
440
440
  const rows = [];
441
441
  const concurrency = 6;
442
- for (let i = 0; i < athleteIds.length; i += concurrency) {
443
- const chunk = athleteIds.slice(i, i + concurrency);
442
+ const uniqueAthleteIds = [...new Set(athleteIds)];
443
+ for (let i = 0; i < uniqueAthleteIds.length; i += concurrency) {
444
+ const chunk = uniqueAthleteIds.slice(i, i + concurrency);
444
445
  const settled = await Promise.all(chunk.map(async (id) => {
445
446
  try {
446
447
  return {
@@ -1618,7 +1619,7 @@ function prescribedExerciseLabels(workouts) {
1618
1619
  return labels;
1619
1620
  }
1620
1621
  /** Group resolved exercises by saved set and write each set via the given log target. */
1621
- async function logResolvedExercises(client, target, date, resolved) {
1622
+ async function logResolvedExercises(client, target, workouts, resolved) {
1622
1623
  const bySet = /* @__PURE__ */ new Map();
1623
1624
  for (const r of resolved) {
1624
1625
  const list = bySet.get(r.savedWorkoutSetId) ?? [];
@@ -1630,19 +1631,10 @@ async function logResolvedExercises(client, target, date, resolved) {
1630
1631
  }
1631
1632
  const out = [];
1632
1633
  for (const [savedWorkoutSetId, results] of bySet) {
1633
- const written = target.role === "coach" ? await logForAthlete(client, {
1634
- athleteId: target.athleteId,
1635
- date,
1636
- savedWorkoutSetId,
1637
- results
1638
- }) : await logAthleteSet(client, {
1639
- date,
1640
- savedWorkoutSetId,
1641
- results
1642
- });
1634
+ const written = await writeSetResults(client, target, workouts, savedWorkoutSetId, results, "log");
1643
1635
  out.push({
1644
1636
  savedWorkoutSetId: written.savedWorkoutSetId,
1645
- exercisesLogged: written.exercisesLogged
1637
+ exercisesLogged: written.exercisesWritten
1646
1638
  });
1647
1639
  }
1648
1640
  return out;
@@ -1680,7 +1672,7 @@ async function logAdHocSession(client, args) {
1680
1672
  sets: e.sets
1681
1673
  };
1682
1674
  });
1683
- const sets = await logResolvedExercises(client, { role: "athlete" }, args.date, resolved);
1675
+ const sets = await logResolvedExercises(client, { role: "athlete" }, await fetchAthleteWorkouts(client, args.date, args.date), resolved);
1684
1676
  const result = {
1685
1677
  date: args.date,
1686
1678
  created,
@@ -1718,7 +1710,7 @@ async function logSessionForAthlete(client, args) {
1718
1710
  const sets = await logResolvedExercises(client, {
1719
1711
  role: "coach",
1720
1712
  athleteId: args.athleteId
1721
- }, args.date, resolved);
1713
+ }, day, resolved);
1722
1714
  return {
1723
1715
  date: args.date,
1724
1716
  created: false,
@@ -2081,6 +2073,7 @@ async function collectAdvisories(blocks, index) {
2081
2073
  }
2082
2074
  //#endregion
2083
2075
  //#region src/workout-session.ts
2076
+ const EXERCISE_SAVE_CONCURRENCY = 4;
2084
2077
  /**
2085
2078
  * Coach calendar writes (`createWorkoutForDay`, timeline create, `copyProgramWorkout`) 500/401
2086
2079
  * on personal calendars (Coach Plan / `personal_cal`). Shared by `buildSession` and
@@ -2114,15 +2107,14 @@ async function buildSession(client, opts) {
2114
2107
  const created = await req(client, "POST", "/2.0/coach/calendar/saveProgramWorkoutSets", buildBlockPayload(blocks, workoutId));
2115
2108
  const byOrder = new Map(created.map((b) => [b.order, b.id]));
2116
2109
  let counter = 0;
2117
- const payloads = blocks.map((block, i) => {
2110
+ await mapPool(blocks.map((block, i) => {
2118
2111
  const wsid = byOrder.get(i + 1);
2119
2112
  if (wsid === void 0) throw new Error(`No saved block for order ${i + 1}.`);
2120
2113
  return block.exercises.map((ex, j) => {
2121
2114
  counter += 1;
2122
2115
  return makeExercise(ex, wsid, j + 1, `k::${workoutId}${String(counter).padStart(3, "0")}`);
2123
2116
  });
2124
- });
2125
- await Promise.all(payloads.filter((p) => p.length > 0).map((p) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", p)));
2117
+ }).filter((p) => p.length > 0), EXERCISE_SAVE_CONCURRENCY, (payload) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", payload));
2126
2118
  if (opts.instruction !== void 0 && opts.instruction !== "") {
2127
2119
  const blockIds = [...byOrder.entries()].sort((a, b) => a[0] - b[0]).map(([, id]) => id);
2128
2120
  await setSessionInstruction(client, workoutId, sess, opts.instruction, blockIds);
@@ -2831,9 +2823,11 @@ async function deleteComment(client, streamId, commentId) {
2831
2823
  if (!res.ok) throw new Error(`Message delete failed (HTTP ${res.status}).`);
2832
2824
  return res.data;
2833
2825
  }
2834
- async function readLive(client, streamId, limit) {
2835
- const res = await client.request("GET", `/v5/messaging/streams/${streamId}/comments?lastCommentId=`);
2826
+ async function readLive(client, streamId, limit, afterCommentId) {
2827
+ const cursor = afterCommentId === void 0 ? "" : String(afterCommentId);
2828
+ const res = await client.request("GET", `/v5/messaging/streams/${streamId}/comments?lastCommentId=${cursor}`);
2836
2829
  if (!res.ok || !Array.isArray(res.data)) throw new Error(`Message read failed (HTTP ${res.status}).`);
2830
+ if (afterCommentId !== void 0) return limit !== void 0 && limit > 0 ? res.data.slice(0, limit) : res.data;
2837
2831
  return limit !== void 0 && limit > 0 ? res.data.slice(-limit) : res.data;
2838
2832
  }
2839
2833
  //#endregion
@@ -2964,21 +2958,12 @@ var ExerciseLibrary = class {
2964
2958
  }
2965
2959
  async resolve(name) {
2966
2960
  await this.ensureFresh();
2967
- let hit = this.#exact(name);
2961
+ const hit = this.#exact(name);
2968
2962
  if (hit) return {
2969
2963
  match: hit,
2970
2964
  candidates: [hit]
2971
2965
  };
2972
- let candidates = this.#searchOnly(name, 20);
2973
- if (candidates.length === 0) {
2974
- await this.refresh();
2975
- hit = this.#exact(name);
2976
- if (hit) return {
2977
- match: hit,
2978
- candidates: [hit]
2979
- };
2980
- candidates = this.#searchOnly(name, 20);
2981
- }
2966
+ const candidates = this.#searchOnly(name, 20);
2982
2967
  if (candidates.length === 1) return {
2983
2968
  match: candidates[0] ?? null,
2984
2969
  candidates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/js",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "zod": "^4.4.3",
30
- "@trainheroic-unofficial/dto": "3.0.0"
30
+ "@trainheroic-unofficial/dto": "3.0.1"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "^26.2.0",