hevy-shared 1.0.960 → 1.0.962

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.
Files changed (88) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +21 -0
  3. package/.github/workflows/ci.yml +15 -0
  4. package/.github/workflows/npm-publish.yml +59 -0
  5. package/.github/workflows/pr-auto-assign.yml +15 -0
  6. package/.prettierrc.js +5 -0
  7. package/README.md +2 -17
  8. package/built/chat.d.ts +23 -25
  9. package/built/coachPlans.d.ts +1 -2
  10. package/built/coachPlans.js +2 -2
  11. package/built/filterExercises.d.ts +3 -19
  12. package/built/filterExercises.js +60 -72
  13. package/built/index.d.ts +304 -1140
  14. package/built/index.js +75 -269
  15. package/built/setIndicatorUtils.d.ts +3 -4
  16. package/built/setIndicatorUtils.js +1 -15
  17. package/built/tests/utils.test.js +0 -748
  18. package/built/tests/workoutVolume.test.js +49 -165
  19. package/built/units.d.ts +7 -14
  20. package/built/units.js +14 -24
  21. package/built/utils.d.ts +5 -192
  22. package/built/utils.js +85 -598
  23. package/built/websocket.d.ts +2 -14
  24. package/built/workoutVolume.d.ts +5 -24
  25. package/built/workoutVolume.js +34 -25
  26. package/jest.config.js +4 -0
  27. package/package.json +10 -32
  28. package/src/chat.ts +130 -0
  29. package/src/coachPlans.ts +57 -0
  30. package/src/constants.ts +14 -0
  31. package/src/filterExercises.ts +222 -0
  32. package/src/index.ts +1576 -0
  33. package/src/setIndicatorUtils.ts +137 -0
  34. package/src/tests/utils.test.ts +156 -0
  35. package/src/tests/workoutVolume.test.ts +93 -0
  36. package/src/units.ts +41 -0
  37. package/src/utils.ts +516 -0
  38. package/src/websocket.ts +36 -0
  39. package/src/workoutVolume.ts +175 -0
  40. package/tsconfig.json +70 -0
  41. package/built/API/APIClient.d.ts +0 -157
  42. package/built/API/APIClient.js +0 -381
  43. package/built/API/index.d.ts +0 -2
  44. package/built/API/index.js +0 -18
  45. package/built/API/types.d.ts +0 -38
  46. package/built/API/types.js +0 -18
  47. package/built/adjustEventTokens.d.ts +0 -16
  48. package/built/adjustEventTokens.js +0 -18
  49. package/built/adminPermissions.d.ts +0 -4
  50. package/built/adminPermissions.js +0 -22
  51. package/built/async.d.ts +0 -50
  52. package/built/async.js +0 -170
  53. package/built/cue.d.ts +0 -12
  54. package/built/cue.js +0 -22
  55. package/built/exerciseLocaleUtils.d.ts +0 -17
  56. package/built/exerciseLocaleUtils.js +0 -62
  57. package/built/hevyTrainer.d.ts +0 -250
  58. package/built/hevyTrainer.js +0 -676
  59. package/built/muscleHeatmaps.d.ts +0 -31
  60. package/built/muscleHeatmaps.js +0 -68
  61. package/built/muscleSplits.d.ts +0 -36
  62. package/built/muscleSplits.js +0 -100
  63. package/built/normalizedWorkoutUtils.d.ts +0 -88
  64. package/built/normalizedWorkoutUtils.js +0 -112
  65. package/built/notifications.d.ts +0 -215
  66. package/built/notifications.js +0 -9
  67. package/built/routineUtils.d.ts +0 -14
  68. package/built/routineUtils.js +0 -186
  69. package/built/schemas.d.ts +0 -6
  70. package/built/schemas.js +0 -12
  71. package/built/tests/async.test.d.ts +0 -1
  72. package/built/tests/async.test.js +0 -49
  73. package/built/tests/hevyTrainer.test.d.ts +0 -1
  74. package/built/tests/hevyTrainer.test.js +0 -1199
  75. package/built/tests/muscleSplit.test.d.ts +0 -1
  76. package/built/tests/muscleSplit.test.js +0 -153
  77. package/built/tests/routineUtils.test.d.ts +0 -1
  78. package/built/tests/routineUtils.test.js +0 -745
  79. package/built/tests/testUtils.d.ts +0 -85
  80. package/built/tests/testUtils.js +0 -319
  81. package/built/translations/index.d.ts +0 -2
  82. package/built/translations/index.js +0 -18
  83. package/built/translations/translationUtils.d.ts +0 -2
  84. package/built/translations/translationUtils.js +0 -61
  85. package/built/translations/types.d.ts +0 -8
  86. package/built/translations/types.js +0 -20
  87. package/built/typeUtils.d.ts +0 -70
  88. package/built/typeUtils.js +0 -55
@@ -0,0 +1,175 @@
1
+ import { flatten } from 'lodash';
2
+ import {
3
+ BodyMeasurement,
4
+ ExerciseType,
5
+ getClosestDataPointBeforeTargetDate,
6
+ Workout,
7
+ } from '.';
8
+
9
+ export const is100PercentBodyWeightExercise = (
10
+ exerciseTemplateId: string,
11
+ ): boolean => {
12
+ const exerciseTemplateIds = new Set([
13
+ '1B2B1E7C', // Pull Up
14
+ '729237D1', // Pull Up (Weighted)
15
+ '2C37EC5E', // Pull Up (Assisted)
16
+ '56808FD2', // Pull Up (Band)
17
+ 'A91838C0', // Kipping Pull Up
18
+ 'EE2938D1', // Sternum Pull up (Gironda)
19
+ '7C50F118', // Wide Pull Up
20
+ 'C7AE420A', // Scapular Pull Ups
21
+ '29083183', // Chin Up
22
+ '023943F1', // Chin Up (Weighted)
23
+ 'D23C609B', // Chin Up (Assisted)
24
+ '28BB4A95', // Triceps Dip
25
+ '10347BAC', // Triceps Dip (Weighted)
26
+ '4B4BF8C2', // Triceps Dip (Assisted)
27
+ '6FCD7755', // Chest Dip
28
+ '29472BE1', // Chest Dip (Weighted)
29
+ 'E9E4089F', // Chest Dip (Assisted)
30
+ '51A0EDAA', // Ring Dips
31
+ '9F9C164B', // Muscle Up
32
+ '30F03BF0', // Front Lever Raise
33
+ '90B04F96', // Handstand Push Up
34
+ ]);
35
+
36
+ return exerciseTemplateIds.has(exerciseTemplateId);
37
+ };
38
+
39
+ interface WorkoutVolumeKgProps {
40
+ workout: Workout;
41
+ measurements: BodyMeasurement[];
42
+ }
43
+
44
+ export type BodyWeightMeasurement = BodyMeasurement &
45
+ Required<Pick<BodyMeasurement, 'weight_kg'>>;
46
+
47
+ export const workoutVolumeKg = ({
48
+ workout,
49
+ measurements,
50
+ }: WorkoutVolumeKgProps) => {
51
+ const workoutDate = new Date(workout.start_time * 1000);
52
+ /** We consider only measurements that include a body weight */
53
+ const bodyWeightMeasurements = measurements.filter(
54
+ (m): m is BodyWeightMeasurement => !!m.weight_kg,
55
+ );
56
+
57
+ /** latest logged bodyweight that is older than the workout itself */
58
+ const mostRecentMeasurementBeforeWorkout =
59
+ getClosestRelevantBodyWeightKgForDate(bodyWeightMeasurements, workoutDate);
60
+
61
+ /**
62
+ * there are no bodyweights logged *before* the workout, so take the oldest
63
+ * logged bodyweight, even though it's newer than the workout, as that's the
64
+ * closest approximation that we've got. if there is nothing logged at all,
65
+ * then we have to take 0
66
+ */
67
+ const oldestLoggedMeasurement = bodyWeightMeasurements[0]?.weight_kg ?? 0;
68
+
69
+ const bodyweightKg =
70
+ mostRecentMeasurementBeforeWorkout ?? oldestLoggedMeasurement;
71
+
72
+ return workoutVolumeSetsVolumeKg({
73
+ bodyweightKg,
74
+ sets: workoutToWorkoutVolumeSets(workout),
75
+ });
76
+ };
77
+
78
+ /**
79
+ * Given a date, this will return the nearest bodyweight value
80
+ * that was logged before it.
81
+ */
82
+ export const getClosestRelevantBodyWeightKgForDate = (
83
+ bodyWeightMeasurements: BodyWeightMeasurement[],
84
+ date: Date,
85
+ ): number | undefined => {
86
+ return getClosestDataPointBeforeTargetDate(
87
+ bodyWeightMeasurements,
88
+ (m: BodyWeightMeasurement) => new Date(m.date),
89
+ date,
90
+ )?.weight_kg;
91
+ };
92
+
93
+ export interface WorkoutVolumeSetsVolumeKgProps {
94
+ bodyweightKg: number; // Pass 0 if it's not known
95
+ sets: WorkoutVolumeSet[];
96
+ }
97
+
98
+ export const workoutVolumeSetsVolumeKg = ({
99
+ bodyweightKg,
100
+ sets,
101
+ }: WorkoutVolumeSetsVolumeKgProps): number => {
102
+ return sets.reduce((accu, s) => {
103
+ return accu + setVolumeKg(s, bodyweightKg);
104
+ }, 0);
105
+ };
106
+
107
+ export interface WorkoutVolumeSet {
108
+ type: ExerciseType;
109
+ exerciseTemplateId: string;
110
+ weightKg: number;
111
+ reps: number;
112
+ distanceMeters: number;
113
+ }
114
+
115
+ export const setVolumeKg = (
116
+ set: WorkoutVolumeSet,
117
+ bodyweightKg: number,
118
+ ): number => {
119
+ const { type, exerciseTemplateId, weightKg, reps, distanceMeters } = set;
120
+
121
+ switch (type) {
122
+ case 'weight_reps':
123
+ // eg. Bench Press (Barbell)
124
+ return weightKg * reps;
125
+ case 'reps_only':
126
+ if (is100PercentBodyWeightExercise(exerciseTemplateId)) {
127
+ // eg. Pull Up
128
+ return bodyweightKg * reps;
129
+ } else {
130
+ // eg. Push Up
131
+ return 0;
132
+ }
133
+ case 'bodyweight_reps':
134
+ if (is100PercentBodyWeightExercise(exerciseTemplateId)) {
135
+ // eg. Pull Up (Weighted)
136
+ return (weightKg + bodyweightKg) * reps;
137
+ } else {
138
+ // eg. Push Up (Weighted)
139
+ return weightKg * reps;
140
+ }
141
+ case 'bodyweight_assisted_reps':
142
+ if (is100PercentBodyWeightExercise(exerciseTemplateId)) {
143
+ // eg. Pull Up (Assisted)
144
+ const bodyweightAssistedRepsVolume = (bodyweightKg - weightKg) * reps;
145
+ return Math.max(bodyweightAssistedRepsVolume, 0);
146
+ } else {
147
+ // e.g. a custom `bodyweight_assisted_reps` exercise
148
+ return 0;
149
+ }
150
+ case 'short_distance_weight':
151
+ // eg. Farmers Walk
152
+ return distanceMeters * weightKg;
153
+ default:
154
+ // `duration` and `distance_duration` exercises
155
+ return 0;
156
+ }
157
+ };
158
+
159
+ const workoutToWorkoutVolumeSets = (workout: Workout) => {
160
+ const unflatSets = workout.exercises.map((e) => {
161
+ return e.sets.map((s) => {
162
+ return {
163
+ type: e.exercise_type || 'weight_reps',
164
+ exerciseTemplateId: e.exercise_template_id,
165
+ weightKg: s.weight_kg ?? 0,
166
+ reps: s.reps ?? 0,
167
+ distanceMeters: s.distance_meters ?? 0,
168
+ } as WorkoutVolumeSet;
169
+ });
170
+ });
171
+
172
+ const sets: WorkoutVolumeSet[] = flatten(unflatSets);
173
+
174
+ return sets;
175
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Basic Options */
4
+ // "incremental": true, /* Enable incremental compilation */
5
+ "target": "ES2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6
+ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
7
+ // "lib": [], /* Specify library files to be included in the compilation. */
8
+ // "allowJs": true, /* Allow javascript files to be compiled. */
9
+ // "checkJs": true, /* Report errors in .js files. */
10
+ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11
+ "declaration": true, /* Generates corresponding '.d.ts' file. */
12
+ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13
+ // "sourceMap": true, /* Generates corresponding '.map' file. */
14
+ // "outFile": "./", /* Concatenate and emit output to single file. */
15
+ "outDir": "./built", /* Redirect output structure to the directory. */
16
+ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17
+ // "composite": true, /* Enable project compilation */
18
+ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19
+ // "removeComments": true, /* Do not emit comments to output. */
20
+ // "noEmit": true, /* Do not emit outputs. */
21
+ // "importHelpers": true, /* Import emit helpers from 'tslib'. */
22
+ // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23
+ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24
+
25
+ /* Strict Type-Checking Options */
26
+ "strict": true, /* Enable all strict type-checking options. */
27
+ // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28
+ // "strictNullChecks": true, /* Enable strict null checks. */
29
+ // "strictFunctionTypes": true, /* Enable strict checking of function types. */
30
+ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31
+ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32
+ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33
+ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34
+
35
+ /* Additional Checks */
36
+ // "noUnusedLocals": true, /* Report errors on unused locals. */
37
+ // "noUnusedParameters": true, /* Report errors on unused parameters. */
38
+ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39
+ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40
+
41
+ /* Module Resolution Options */
42
+ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43
+ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44
+ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45
+ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46
+ // "typeRoots": [], /* List of folders to include type definitions from. */
47
+ // "types": [], /* Type declaration files to be included in compilation. */
48
+ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49
+ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50
+ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52
+
53
+ /* Source Map Options */
54
+ // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56
+ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57
+ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58
+
59
+ /* Experimental Options */
60
+ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61
+ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62
+
63
+ /* Advanced Options */
64
+ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
65
+ },
66
+ "exclude": [
67
+ "node_modules",
68
+ "built"
69
+ ]
70
+ }
@@ -1,157 +0,0 @@
1
- import { ClientAuthToken, DeepReadonly } from '..';
2
- import { RequestConfig, HTTPResponse, HTTPClient, HTTPRequestCompletionCallback, HTTPErrorHandler } from './types';
3
- interface HevyAPIClientConfig<UserContext> {
4
- /**
5
- * How long before predicted token expiry to request a new token. We request
6
- * new tokens a little bit before they expire, so that we don't have to wait
7
- * for the backend to send us a 401 first. We do it pre-emptively so that we
8
- * wouldn't need to wait for requests to fail first, and then retry them.
9
- *
10
- * Optional.
11
- *
12
- * @default {1 minute}
13
- */
14
- readonly tokenExpirySafetyThresholdMs?: number;
15
- /**
16
- * How long we expect the access token to be valid for. Used to override the
17
- * expiry time sent by the backend in case the client clock is in the future
18
- * and the client thinks that the token will expire sooner than it actually
19
- * will. Even if this assumption ever stops being correct, the client will be
20
- * able to self-correct by receiving an `AccessTokenExpired` 401 response.
21
- *
22
- * Optional.
23
- *
24
- * @default {15 minutes}
25
- */
26
- readonly accessTokenMinimumValidAgeMs?: number;
27
- /**
28
- * Minimum time between successive calls to the `auth/refresh_token` endpoint
29
- * in case it doesn't return an error. Also used to deal with potential clock
30
- * issues on the client side, or some unforeseen issue on the backend, as the
31
- * last line of defence. No matter what may happen, we absolutely never ever
32
- * want to end up accidentally spamming this endpoint because it could have a
33
- * cascading clusterfuck effect on all the other requests.
34
- *
35
- * Optional.
36
- *
37
- * @default {20 seconds}
38
- */
39
- readonly tokenRefreshThrottleMs?: number;
40
- /**
41
- * The API endpoint used to refresh the auth tokens using a refresh token.
42
- * The URL is relative to the backend base URL.
43
- *
44
- * Optional.
45
- *
46
- * @default {POST /auth/refresh_token}
47
- */
48
- readonly refreshAuthTokenApiEndpoint?: {
49
- readonly method: 'post';
50
- readonly url: string;
51
- };
52
- /**
53
- * Callback to use when receiving a new auth token. Used to send it to the
54
- * consumer of the class to save the new token to its local storage.
55
- *
56
- * Required.
57
- */
58
- onNewAuthToken(newAuthToken: ClientAuthToken, userContext: UserContext): void;
59
- /**
60
- * Defines an arbitrary object to be passed back to the `onNewAuthToken`. The
61
- * value is computed at the time when the request is made. Useful for passing
62
- * back state that may have gotten changed during the time while the request
63
- * was being processed, such as holding onto a userId across a logout action.
64
- *
65
- * Optional.
66
- */
67
- getUserContext?(): UserContext;
68
- /**
69
- * Callback to use when receiving an HTTP error response from the backend, to
70
- * determine whether it is a response indicating that the token has expired.
71
- *
72
- * Optional.
73
- *
74
- * @default ...
75
- */
76
- isAccessTokenExpiredResponse?(response: HTTPResponse<unknown>): boolean;
77
- /**
78
- * Callback to use when receiving an HTTP error response from the backend, to
79
- * determine whether it is a response indicating that the token is invalid.
80
- *
81
- * Optional.
82
- *
83
- * @default ...
84
- */
85
- isAccessTokenInvalidResponse?(response: HTTPResponse<unknown>): boolean;
86
- }
87
- export declare class HevyAPIClient<UserContext = never> {
88
- private static readonly DEFAULT_TOKEN_EXPIRY_SAFETY_THRESHOLD_MS;
89
- private static readonly DEFAULT_ACCESS_TOKEN_MINIMUM_VALID_AGE_MS;
90
- private static readonly DEFAULT_TOKEN_REFRESH_THROTTLE_MS;
91
- private static readonly DEFAULT_REFRESH_AUTH_TOKEN_API_ENDPOINT;
92
- private readonly _config;
93
- private _requestCompletionCallbacks;
94
- private _errorHandlers;
95
- private _authTokenFactory;
96
- private _legacyAuthToken;
97
- private _httpClient;
98
- private _lastTokenRefresh;
99
- private _lastSessionDelete;
100
- constructor(httpClient: HTTPClient, config: HevyAPIClientConfig<UserContext>);
101
- private get _authToken();
102
- private get _authHeaders();
103
- private _addHeaders;
104
- private refreshExpiredAuthToken;
105
- private forceRefreshAuthToken;
106
- private waitForTokenRefresh;
107
- private get _isTokenRecentlyRefreshed();
108
- private _refreshAuthToken;
109
- private _handleResponse;
110
- isAccessTokenExpiredResponse(response: HTTPResponse<unknown>): boolean;
111
- isAccessTokenInvalidResponse(response: HTTPResponse<unknown>): boolean;
112
- get refreshAuthTokenApiEndpoint(): {
113
- readonly method: "post";
114
- readonly url: string;
115
- };
116
- setAuthToken(newAuthToken: {
117
- authToken: DeepReadonly<ClientAuthToken> | null;
118
- legacyAuthToken: string | null;
119
- }): Promise<void>;
120
- setAuthToken(newAuthToken: {
121
- getAuthToken: () => ClientAuthToken | null;
122
- legacyAuthToken: string | null;
123
- }): Promise<void>;
124
- clearAuthToken(): Promise<void>;
125
- get isAuthenticated(): boolean;
126
- markSessionDeleted(): void;
127
- /**
128
- * Adds a callback to be executed whenever a request has finished processing.
129
- * This means either that the request has received a response, or that there
130
- * was an error. In the case of an error, it may be either an HTTP error from
131
- * the server, or some other type of error, such as a network error.
132
- *
133
- * This is a lower level API than {@link attachErrorHandler} - prefer using
134
- * that one instead of this one if it's enough to suit your needs.
135
- */
136
- attachRequestCompletionCallback(onResult: HTTPRequestCompletionCallback): void;
137
- removeRequestCompletionCallbacks(): void;
138
- /**
139
- * Adds a callback to be executed on receiving an HTTP error from the server.
140
- * This callback will not be executed for any other type of error, such as a
141
- * network error. For that and more, use {@link attachRequestCompletionCallback}.
142
- */
143
- attachErrorHandler(onError: HTTPErrorHandler<{
144
- willRetry: boolean;
145
- isTokenRefreshedAfterRequest: boolean;
146
- isPreviousSession: boolean;
147
- }>): void;
148
- removeErrorHandlers(): void;
149
- get<T = never>(url: string, config?: RequestConfig): Promise<HTTPResponse<T>>;
150
- delete<T = never>(url: string, config?: RequestConfig): Promise<HTTPResponse<T>>;
151
- head<T = never>(url: string, config?: RequestConfig): Promise<HTTPResponse<T>>;
152
- options<T = never>(url: string, config?: RequestConfig): Promise<HTTPResponse<T>>;
153
- post<T = never, R = unknown>(url: string, data?: R, config?: RequestConfig): Promise<HTTPResponse<T>>;
154
- put<T = never, R = unknown>(url: string, data?: R, config?: RequestConfig): Promise<HTTPResponse<T>>;
155
- patch<T = never, R = unknown>(url: string, data?: R, config?: RequestConfig): Promise<HTTPResponse<T>>;
156
- }
157
- export {};