@trainheroic-unofficial/js 1.7.4 → 2.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 +36 -15
- package/dist/index.mjs +15 -5
- package/dist/node.d.mts +0 -1
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,6 @@ import { n as LibrarySnapshot, r as MemoryLibraryCache, t as LibraryCache } from
|
|
|
2
2
|
import { Advisory, AthletePrefs, AthleteProfileSummary, AthleteUser, AthleteWorkingMax, AthleteWorkoutSummary, AthleteWorkoutView, BlockSpec, CoachAthleteTraining, ExerciseHistoryDetail, ExerciseHistoryListItem, ExerciseRow, ExerciseSpec, ExerciseStats, ExerciseView, PersonalRecord, PresentedExerciseHistory, ProgramWorkout, ReadResult, ResolveResult, RosterActivityRow, TeamVolumeReport, WorkoutDate, WorkoutHistoryExport } from "@trainheroic-unofficial/dto";
|
|
3
3
|
import { ZodType } from "zod";
|
|
4
4
|
export * from "@trainheroic-unofficial/dto";
|
|
5
|
-
|
|
6
5
|
//#region src/auth.d.ts
|
|
7
6
|
type TrainHeroicSession = {
|
|
8
7
|
thUserId: number;
|
|
@@ -32,15 +31,29 @@ type ClientResult<T = unknown> = {
|
|
|
32
31
|
ok: boolean;
|
|
33
32
|
data: T;
|
|
34
33
|
};
|
|
34
|
+
type ClientOptions = {
|
|
35
|
+
/**
|
|
36
|
+
* Called with each newly acquired session token — the cold login and the post-401 re-login
|
|
37
|
+
* both fire it. Callers that outlive the client (the hosted Worker's per-request factory, the
|
|
38
|
+
* CLI's on-disk cache) use this to hold the token, so the next client can be seeded with it
|
|
39
|
+
* instead of logging in again.
|
|
40
|
+
*/
|
|
41
|
+
onSession?: (sessionId: string) => void;
|
|
42
|
+
};
|
|
35
43
|
/**
|
|
36
|
-
* Authenticated TrainHeroic API client. Holds the
|
|
37
|
-
* encrypted props) and a lazily-acquired session token cached in memory for the life
|
|
38
|
-
*
|
|
44
|
+
* Authenticated TrainHeroic API client. Holds the credentials (on the hosted Worker, from the
|
|
45
|
+
* grant's encrypted props) and a lazily-acquired session token cached in memory for the life of
|
|
46
|
+
* *this client instance* — nothing longer. On a 401/403 it re-logs in once and retries, since
|
|
39
47
|
* TrainHeroic has no refresh token and sessions expire after ~1-2h.
|
|
48
|
+
*
|
|
49
|
+
* Anything that wants a session to outlive one client owns that itself: pass a previously
|
|
50
|
+
* acquired token as `sessionId` and keep the new one via `options.onSession`. That matters
|
|
51
|
+
* wherever clients are short-lived — the hosted Worker builds one per HTTP request, and each CLI
|
|
52
|
+
* invocation is a fresh process — because otherwise every operation replays the password.
|
|
40
53
|
*/
|
|
41
54
|
declare class TrainHeroicClient {
|
|
42
55
|
#private;
|
|
43
|
-
constructor(email: string, password: string, sessionId?: string | null);
|
|
56
|
+
constructor(email: string, password: string, sessionId?: string | null, options?: ClientOptions);
|
|
44
57
|
get sessionId(): string | null;
|
|
45
58
|
request<T = unknown>(method: string, path: string, options?: RequestOptions): Promise<ClientResult<T>>;
|
|
46
59
|
}
|
|
@@ -231,15 +244,18 @@ declare function presentAthleteWorkoutsExport(list: readonly ProgramWorkout[]):
|
|
|
231
244
|
/** The minimal id scaffold `log-set` / `logForAthlete` needs for one saved workout set. */
|
|
232
245
|
type LogSetTarget = {
|
|
233
246
|
date: string;
|
|
234
|
-
workoutTitle: string;
|
|
247
|
+
workoutTitle: string;
|
|
248
|
+
/** The program this saved workout belongs to — lets a caller pick the right one of several. */
|
|
235
249
|
program: string | null;
|
|
236
250
|
programId: number | null;
|
|
237
251
|
team: string | null;
|
|
238
|
-
teamId: number | null;
|
|
252
|
+
teamId: number | null;
|
|
253
|
+
/** The id for `--set` (logAthleteSet/logForAthlete `savedWorkoutSetId`). */
|
|
239
254
|
savedWorkoutSetId: number;
|
|
240
255
|
setTitle: string | null;
|
|
241
256
|
exercises: Array<{
|
|
242
|
-
/** The id for each result's `savedWorkoutSetExerciseId`. */
|
|
257
|
+
/** The id for each result's `savedWorkoutSetExerciseId`. */
|
|
258
|
+
savedWorkoutSetExerciseId: number;
|
|
243
259
|
title: string;
|
|
244
260
|
units: ReturnType<typeof exerciseUnits>;
|
|
245
261
|
prescribed: string[];
|
|
@@ -455,10 +471,13 @@ declare function prescribeForAthlete(client: TrainHeroicClient, args: {
|
|
|
455
471
|
}>;
|
|
456
472
|
/** Outcome of a per-athlete exercise swap: the slot that changed and what it changed to/from. */
|
|
457
473
|
type SwapExerciseResult = {
|
|
458
|
-
savedWorkoutSetExerciseId: number;
|
|
459
|
-
|
|
474
|
+
savedWorkoutSetExerciseId: number;
|
|
475
|
+
/** The athlete who owns the slot (`user_id` off the updated row), when the API returns it. */
|
|
476
|
+
athleteId: number | null;
|
|
477
|
+
/** The exercise now scheduled in the slot. */
|
|
460
478
|
newExerciseId: number;
|
|
461
|
-
newExerciseTitle: string | null;
|
|
479
|
+
newExerciseTitle: string | null;
|
|
480
|
+
/** The team/program's original prescription, left untouched (`workout_set_exercise.exercise_id`). */
|
|
462
481
|
originalTeamExerciseId: number | null;
|
|
463
482
|
};
|
|
464
483
|
/**
|
|
@@ -608,7 +627,8 @@ declare function serializeWorkoutHistory(workouts: readonly WorkoutHistoryExport
|
|
|
608
627
|
type MainLiftFamilyKey = "cleanjerk" | "snatch" | "deadlift" | "squat" | "bench" | "overhead";
|
|
609
628
|
type MainLiftFamily = {
|
|
610
629
|
key: MainLiftFamilyKey;
|
|
611
|
-
label: string;
|
|
630
|
+
label: string;
|
|
631
|
+
/** True when a lowercased exercise title belongs to this family. */
|
|
612
632
|
matches: (titleLower: string) => boolean;
|
|
613
633
|
};
|
|
614
634
|
/**
|
|
@@ -787,7 +807,7 @@ declare function teamVolume(client: TrainHeroicClient, args: {
|
|
|
787
807
|
* Example:
|
|
788
808
|
* definedProps({ metric, teamId: maybeId, date }) // omits teamId/date if they're undefined
|
|
789
809
|
*/
|
|
790
|
-
declare function definedProps<T extends object>(obj: T): { [K in keyof T]: Exclude<T[K], undefined
|
|
810
|
+
declare function definedProps<T extends object>(obj: T): { [K in keyof T]: Exclude<T[K], undefined>; };
|
|
791
811
|
//#endregion
|
|
792
812
|
//#region src/workout-encode.d.ts
|
|
793
813
|
declare const LEADERBOARD_TYPE: Readonly<Record<string, number>>;
|
|
@@ -822,7 +842,8 @@ type BuildOptions = {
|
|
|
822
842
|
blocks: BlockSpec[];
|
|
823
843
|
date?: WorkoutDate;
|
|
824
844
|
timelineDay?: number;
|
|
825
|
-
publish?: boolean;
|
|
845
|
+
publish?: boolean;
|
|
846
|
+
/** Optional session-level note ("Coach Instructions"), set after the blocks save. */
|
|
826
847
|
instruction?: string;
|
|
827
848
|
};
|
|
828
849
|
declare function buildSession(client: TrainHeroicClient, opts: BuildOptions): Promise<{
|
|
@@ -878,4 +899,4 @@ declare class ExerciseLibrary implements ExerciseIndex {
|
|
|
878
899
|
stats(): Promise<Record<string, unknown>>;
|
|
879
900
|
}
|
|
880
901
|
//#endregion
|
|
881
|
-
export { ANALYTICS_METRIC_KEYS, AddedExercise, AnalyticsMetric, AnalyticsQueryArgs, ApiBase, AthleteMainLiftPRs, BuildOptions, ClientResult, CoachRosterAthlete, DEFAULT_INVITE_MESSAGE, ExerciseIndex, ExerciseLibrary, type ExerciseSetInput, type ExerciseSetPayload, LEADERBOARD_LABEL, LEADERBOARD_TYPE, Leaderboard, LibraryCache, LibrarySnapshot, LogSessionResult, LogSetTarget, MAIN_LIFT_FAMILIES, MAX_PARAM_SLOTS, MainLiftFamily, MainLiftFamilyKey, MainLiftPR, MainLiftResolution, MemoryLibraryCache, PARAM_NONE, PARAM_PCT_MAX, PARAM_REPS, PARAM_RPE, PARAM_UNIT, PARAM_WEIGHT, PersonalWorkoutCreated, RequestOptions, ResolvedLift, ScheduledAlternative, SerializedExport, SessionExercise, SetResult, type SetWriteMode, SwapExerciseResult, TrainHeroicAuthError, TrainHeroicClient, TrainHeroicSession, WorkoutExportFormat, addExercisesToWorkout, analyticsMetricCatalog, asExerciseList, buildBlockPayload, buildCommentPayload, buildExerciseSetPayload, buildSearchText, buildSession, buildSetCompletePayload, checkResponse, chunk, classifyMainLift, coerceInt, coerceNum, collectAdvisories, copySession, createPersonalWorkout, dateWindows, definedProps, deleteComment, exerciseTitle, exerciseUnits, fetchAthleteMainLiftPRs, fetchAthletePrefs, fetchAthleteProfileSummary, fetchAthleteUser, fetchAthleteWorkouts, fetchAthleteWorkoutsChunked, fetchCoachAthleteCalendarSummary, fetchCoachAthleteWorkouts, fetchCoachRoster, fetchExerciseHistoryDetail, fetchExerciseHistoryList, fetchExerciseStats, fetchLeaderboard, fetchPersonalRecords, fetchRosterActivity, fetchRosterMainLiftPRs, fetchStreams, fetchTeamAthleteIds, fetchWorkingMaxes, findSavedWorkoutSet, inviteAthletes, isPersonalSession, isRecord, logAdHocSession, logAthleteSet, logForAthlete, logSessionForAthlete, loginTrainHeroic, makeExercise, mapPool, mergeWorkoutsById, prescribeForAthlete, presentAthleteWorkout, presentAthleteWorkoutExport, presentAthleteWorkouts, presentAthleteWorkoutsExport, presentCoachAthleteTraining, presentExercise, presentExerciseHistory, presentLogTargets, publishSession, queryAnalytics, rankSearch, readLive, readSession, removePersonalWorkout, removeSession, repsList, resolveAthleteMainLifts, resolveAthleteUserId, resolveLeaderboard, searchExerciseHistory, selectWorkouts, selectWorkoutsByProgram, sendComment, serializeWorkoutHistory, setSessionInstruction, sortRosterByRecency, str, summarizeAthleteWorkouts, swapAthleteExercise, teamVolume, toSetResults, unitAdvisory, unitLabel, unwrapEnvelope, withUnits, workoutsToCsv, workoutsToJson, workoutsToText };
|
|
902
|
+
export { ANALYTICS_METRIC_KEYS, AddedExercise, AnalyticsMetric, AnalyticsQueryArgs, ApiBase, AthleteMainLiftPRs, BuildOptions, ClientOptions, ClientResult, CoachRosterAthlete, DEFAULT_INVITE_MESSAGE, ExerciseIndex, ExerciseLibrary, type ExerciseSetInput, type ExerciseSetPayload, LEADERBOARD_LABEL, LEADERBOARD_TYPE, Leaderboard, LibraryCache, LibrarySnapshot, LogSessionResult, LogSetTarget, MAIN_LIFT_FAMILIES, MAX_PARAM_SLOTS, MainLiftFamily, MainLiftFamilyKey, MainLiftPR, MainLiftResolution, MemoryLibraryCache, PARAM_NONE, PARAM_PCT_MAX, PARAM_REPS, PARAM_RPE, PARAM_UNIT, PARAM_WEIGHT, PersonalWorkoutCreated, RequestOptions, ResolvedLift, ScheduledAlternative, SerializedExport, SessionExercise, SetResult, type SetWriteMode, SwapExerciseResult, TrainHeroicAuthError, TrainHeroicClient, TrainHeroicSession, WorkoutExportFormat, addExercisesToWorkout, analyticsMetricCatalog, asExerciseList, buildBlockPayload, buildCommentPayload, buildExerciseSetPayload, buildSearchText, buildSession, buildSetCompletePayload, checkResponse, chunk, classifyMainLift, coerceInt, coerceNum, collectAdvisories, copySession, createPersonalWorkout, dateWindows, definedProps, deleteComment, exerciseTitle, exerciseUnits, fetchAthleteMainLiftPRs, fetchAthletePrefs, fetchAthleteProfileSummary, fetchAthleteUser, fetchAthleteWorkouts, fetchAthleteWorkoutsChunked, fetchCoachAthleteCalendarSummary, fetchCoachAthleteWorkouts, fetchCoachRoster, fetchExerciseHistoryDetail, fetchExerciseHistoryList, fetchExerciseStats, fetchLeaderboard, fetchPersonalRecords, fetchRosterActivity, fetchRosterMainLiftPRs, fetchStreams, fetchTeamAthleteIds, fetchWorkingMaxes, findSavedWorkoutSet, inviteAthletes, isPersonalSession, isRecord, logAdHocSession, logAthleteSet, logForAthlete, logSessionForAthlete, loginTrainHeroic, makeExercise, mapPool, mergeWorkoutsById, prescribeForAthlete, presentAthleteWorkout, presentAthleteWorkoutExport, presentAthleteWorkouts, presentAthleteWorkoutsExport, presentCoachAthleteTraining, presentExercise, presentExerciseHistory, presentLogTargets, publishSession, queryAnalytics, rankSearch, readLive, readSession, removePersonalWorkout, removeSession, repsList, resolveAthleteMainLifts, resolveAthleteUserId, resolveLeaderboard, searchExerciseHistory, selectWorkouts, selectWorkoutsByProgram, sendComment, serializeWorkoutHistory, setSessionInstruction, sortRosterByRecency, str, summarizeAthleteWorkouts, swapAthleteExercise, teamVolume, toSetResults, unitAdvisory, unitLabel, unwrapEnvelope, withUnits, workoutsToCsv, workoutsToJson, workoutsToText };
|
package/dist/index.mjs
CHANGED
|
@@ -62,20 +62,27 @@ var TrainHeroicAuthError = class extends Error {
|
|
|
62
62
|
name = "TrainHeroicAuthError";
|
|
63
63
|
};
|
|
64
64
|
/**
|
|
65
|
-
* Authenticated TrainHeroic API client. Holds the
|
|
66
|
-
* encrypted props) and a lazily-acquired session token cached in memory for the life
|
|
67
|
-
*
|
|
65
|
+
* Authenticated TrainHeroic API client. Holds the credentials (on the hosted Worker, from the
|
|
66
|
+
* grant's encrypted props) and a lazily-acquired session token cached in memory for the life of
|
|
67
|
+
* *this client instance* — nothing longer. On a 401/403 it re-logs in once and retries, since
|
|
68
68
|
* TrainHeroic has no refresh token and sessions expire after ~1-2h.
|
|
69
|
+
*
|
|
70
|
+
* Anything that wants a session to outlive one client owns that itself: pass a previously
|
|
71
|
+
* acquired token as `sessionId` and keep the new one via `options.onSession`. That matters
|
|
72
|
+
* wherever clients are short-lived — the hosted Worker builds one per HTTP request, and each CLI
|
|
73
|
+
* invocation is a fresh process — because otherwise every operation replays the password.
|
|
69
74
|
*/
|
|
70
75
|
var TrainHeroicClient = class {
|
|
71
76
|
#email;
|
|
72
77
|
#password;
|
|
78
|
+
#onSession;
|
|
73
79
|
#sessionId;
|
|
74
80
|
#loginInFlight = null;
|
|
75
|
-
constructor(email, password, sessionId = null) {
|
|
81
|
+
constructor(email, password, sessionId = null, options = {}) {
|
|
76
82
|
this.#email = email;
|
|
77
83
|
this.#password = password;
|
|
78
84
|
this.#sessionId = sessionId;
|
|
85
|
+
this.#onSession = options.onSession;
|
|
79
86
|
}
|
|
80
87
|
get sessionId() {
|
|
81
88
|
return this.#sessionId;
|
|
@@ -93,6 +100,9 @@ var TrainHeroicClient = class {
|
|
|
93
100
|
const session = await loginTrainHeroic(this.#email, this.#password);
|
|
94
101
|
if (!session) throw new TrainHeroicAuthError("TrainHeroic login failed");
|
|
95
102
|
this.#sessionId = session.sessionId;
|
|
103
|
+
try {
|
|
104
|
+
this.#onSession?.(this.#sessionId);
|
|
105
|
+
} catch {}
|
|
96
106
|
return this.#sessionId;
|
|
97
107
|
}
|
|
98
108
|
async request(method, path, options = {}) {
|
|
@@ -2702,7 +2712,7 @@ var MemoryLibraryCache = class {
|
|
|
2702
2712
|
//#region src/exercise-index.ts
|
|
2703
2713
|
const LIBRARY_PATH = "/v5/exerciseLibrary/all";
|
|
2704
2714
|
const CREATE_PATH = "/2.0/coach/exercise/create";
|
|
2705
|
-
const TTL_MS =
|
|
2715
|
+
const TTL_MS = 6048e5;
|
|
2706
2716
|
function toStored(ex) {
|
|
2707
2717
|
const id = coerceInt(ex.id);
|
|
2708
2718
|
if (id === null) return null;
|
package/dist/node.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trainheroic-unofficial/js",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"zod": "^4.4.3",
|
|
30
|
-
"@trainheroic-unofficial/dto": "
|
|
30
|
+
"@trainheroic-unofficial/dto": "2.0.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/node": "^26.1.
|
|
34
|
-
"tsdown": "^0.22.
|
|
33
|
+
"@types/node": "^26.1.2",
|
|
34
|
+
"tsdown": "^0.22.14",
|
|
35
35
|
"vitest": "^4.1.10"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|