@nativesquare/soma 0.12.0 → 0.13.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.
Files changed (60) hide show
  1. package/dist/client/garmin.d.ts +5 -1
  2. package/dist/client/garmin.d.ts.map +1 -1
  3. package/dist/client/garmin.js +148 -0
  4. package/dist/client/garmin.js.map +1 -1
  5. package/dist/client/index.d.ts +5 -6
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +5 -211
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/client/strava.d.ts +11 -6
  10. package/dist/client/strava.d.ts.map +1 -1
  11. package/dist/client/strava.js +64 -0
  12. package/dist/client/strava.js.map +1 -1
  13. package/dist/client/types.d.ts +93 -20
  14. package/dist/client/types.d.ts.map +1 -1
  15. package/dist/component/_generated/component.d.ts +24 -5
  16. package/dist/component/_generated/component.d.ts.map +1 -1
  17. package/dist/component/garmin/private.d.ts +53 -68
  18. package/dist/component/garmin/private.d.ts.map +1 -1
  19. package/dist/component/garmin/private.js +87 -85
  20. package/dist/component/garmin/private.js.map +1 -1
  21. package/dist/component/garmin/public.d.ts +97 -43
  22. package/dist/component/garmin/public.d.ts.map +1 -1
  23. package/dist/component/garmin/public.js +75 -51
  24. package/dist/component/garmin/public.js.map +1 -1
  25. package/dist/component/garmin/webhooks.d.ts +22 -20
  26. package/dist/component/garmin/webhooks.d.ts.map +1 -1
  27. package/dist/component/garmin/webhooks.js +115 -76
  28. package/dist/component/garmin/webhooks.js.map +1 -1
  29. package/dist/component/public.d.ts +15 -15
  30. package/dist/component/schema.d.ts +25 -25
  31. package/dist/component/strava/public.d.ts +12 -8
  32. package/dist/component/strava/public.d.ts.map +1 -1
  33. package/dist/component/strava/public.js +7 -7
  34. package/dist/component/strava/public.js.map +1 -1
  35. package/dist/component/validators/activity.d.ts +4 -4
  36. package/dist/component/validators/body.d.ts +4 -4
  37. package/dist/component/validators/daily.d.ts +4 -4
  38. package/dist/component/validators/nutrition.d.ts +3 -3
  39. package/dist/component/validators/samples.d.ts +4 -4
  40. package/dist/component/validators/shared.d.ts +13 -4
  41. package/dist/component/validators/shared.d.ts.map +1 -1
  42. package/dist/component/validators/shared.js +7 -0
  43. package/dist/component/validators/shared.js.map +1 -1
  44. package/dist/component/validators/sleep.d.ts +5 -5
  45. package/dist/validators.d.ts +41 -40
  46. package/dist/validators.d.ts.map +1 -1
  47. package/dist/validators.js +1 -0
  48. package/dist/validators.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/client/garmin.ts +692 -487
  51. package/src/client/index.ts +10 -279
  52. package/src/client/strava.ts +199 -108
  53. package/src/client/types.ts +303 -215
  54. package/src/component/_generated/component.ts +19 -19
  55. package/src/component/garmin/private.ts +1872 -1870
  56. package/src/component/garmin/public.ts +104 -80
  57. package/src/component/garmin/webhooks.ts +122 -81
  58. package/src/component/strava/public.ts +393 -393
  59. package/src/component/validators/shared.ts +9 -0
  60. package/src/validators.ts +1 -0
@@ -1,487 +1,692 @@
1
- import type { SomaComponent } from "./index.js";
2
- import type { ActionCtx, SomaGarminConfig } from "./types.js";
3
-
4
- export class SomaGarmin {
5
- constructor(
6
- private component: SomaComponent,
7
- private requireConfig: () => SomaGarminConfig,
8
- ) {}
9
-
10
- /**
11
- * Generate a Garmin OAuth 2.0 authorization URL with PKCE.
12
- *
13
- * The state and code verifier are stored inside the component automatically,
14
- * and the callback handler registered by `registerRoutes` completes
15
- * the flow without further host-app intervention.
16
- *
17
- * @param ctx - Action context from the host app
18
- * @param opts.userId - The host app's user identifier
19
- * @param opts.redirectUri - Optional override for the OAuth callback URL
20
- * @returns `{ authUrl, state }`
21
- *
22
- * @example
23
- * ```ts
24
- * const { authUrl } = await soma.garmin.getAuthUrl(ctx, {
25
- * userId: "user_123",
26
- * redirectUri: "https://your-app.convex.site/api/garmin/callback",
27
- * });
28
- * // Redirect user to authUrl — the callback is handled automatically
29
- * ```
30
- */
31
- async getAuthUrl(
32
- ctx: ActionCtx,
33
- opts: { userId: string; redirectUri?: string },
34
- ) {
35
- const config = this.requireConfig();
36
- return await ctx.runAction(this.component.garmin.public.getGarminAuthUrl, {
37
- clientId: config.clientId,
38
- redirectUri: opts.redirectUri,
39
- userId: opts.userId,
40
- });
41
- }
42
-
43
- /**
44
- * Disconnect a user from Garmin.
45
- *
46
- * Deregisters the user at Garmin (best-effort), deletes stored tokens,
47
- * and sets the connection to inactive.
48
- *
49
- * @param ctx - Action context from the host app
50
- * @param args.userId - The host app's user identifier
51
- *
52
- * @example
53
- * ```ts
54
- * await soma.garmin.disconnect(ctx, { userId: "user_123" });
55
- * ```
56
- */
57
- async disconnect(
58
- ctx: ActionCtx,
59
- args: { userId: string },
60
- ) {
61
- return await ctx.runAction(this.component.garmin.public.disconnectGarmin, args);
62
- }
63
-
64
- /**
65
- * Pull activity summaries from Garmin.
66
- *
67
- * Fetches activities, transforms them into the normalized Soma schema,
68
- * and ingests them with automatic deduplication.
69
- *
70
- * @param ctx - Action context from the host app
71
- * @param args.userId - The host app's user identifier
72
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
73
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
74
- */
75
- async pullActivities(
76
- ctx: ActionCtx,
77
- args: {
78
- userId: string;
79
- startTimeInSeconds?: number;
80
- endTimeInSeconds?: number;
81
- },
82
- ) {
83
- const config = this.requireConfig();
84
- return await ctx.runAction(this.component.garmin.public.pullActivities, {
85
- ...args,
86
- clientId: config.clientId,
87
- clientSecret: config.clientSecret,
88
- });
89
- }
90
-
91
- /**
92
- * Pull daily wellness summaries from Garmin.
93
- *
94
- * @param ctx - Action context from the host app
95
- * @param args.userId - The host app's user identifier
96
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
97
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
98
- */
99
- async pullDailies(
100
- ctx: ActionCtx,
101
- args: {
102
- userId: string;
103
- startTimeInSeconds?: number;
104
- endTimeInSeconds?: number;
105
- },
106
- ) {
107
- const config = this.requireConfig();
108
- return await ctx.runAction(this.component.garmin.public.pullDailies, {
109
- ...args,
110
- clientId: config.clientId,
111
- clientSecret: config.clientSecret,
112
- });
113
- }
114
-
115
- /**
116
- * Pull sleep summaries from Garmin.
117
- *
118
- * @param ctx - Action context from the host app
119
- * @param args.userId - The host app's user identifier
120
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
121
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
122
- */
123
- async pullSleep(
124
- ctx: ActionCtx,
125
- args: {
126
- userId: string;
127
- startTimeInSeconds?: number;
128
- endTimeInSeconds?: number;
129
- },
130
- ) {
131
- const config = this.requireConfig();
132
- return await ctx.runAction(this.component.garmin.public.pullSleep, {
133
- ...args,
134
- clientId: config.clientId,
135
- clientSecret: config.clientSecret,
136
- });
137
- }
138
-
139
- /**
140
- * Pull body composition data from Garmin.
141
- *
142
- * @param ctx - Action context from the host app
143
- * @param args.userId - The host app's user identifier
144
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
145
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
146
- */
147
- async pullBody(
148
- ctx: ActionCtx,
149
- args: {
150
- userId: string;
151
- startTimeInSeconds?: number;
152
- endTimeInSeconds?: number;
153
- },
154
- ) {
155
- const config = this.requireConfig();
156
- return await ctx.runAction(this.component.garmin.public.pullBody, {
157
- ...args,
158
- clientId: config.clientId,
159
- clientSecret: config.clientSecret,
160
- });
161
- }
162
-
163
- /**
164
- * Pull menstrual cycle tracking data from Garmin.
165
- *
166
- * @param ctx - Action context from the host app
167
- * @param args.userId - The host app's user identifier
168
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
169
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
170
- */
171
- async pullMenstruation(
172
- ctx: ActionCtx,
173
- args: {
174
- userId: string;
175
- startTimeInSeconds?: number;
176
- endTimeInSeconds?: number;
177
- },
178
- ) {
179
- const config = this.requireConfig();
180
- return await ctx.runAction(this.component.garmin.public.pullMenstruation, {
181
- ...args,
182
- clientId: config.clientId,
183
- clientSecret: config.clientSecret,
184
- });
185
- }
186
-
187
- /**
188
- * Pull blood pressure readings from Garmin.
189
- *
190
- * @param ctx - Action context from the host app
191
- * @param args.userId - The host app's user identifier
192
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
193
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
194
- */
195
- async pullBloodPressures(
196
- ctx: ActionCtx,
197
- args: {
198
- userId: string;
199
- startTimeInSeconds?: number;
200
- endTimeInSeconds?: number;
201
- },
202
- ) {
203
- const config = this.requireConfig();
204
- return await ctx.runAction(this.component.garmin.public.pullBloodPressures, {
205
- ...args,
206
- clientId: config.clientId,
207
- clientSecret: config.clientSecret,
208
- });
209
- }
210
-
211
- /**
212
- * Pull skin temperature data from Garmin.
213
- *
214
- * @param ctx - Action context from the host app
215
- * @param args.userId - The host app's user identifier
216
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
217
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
218
- */
219
- async pullSkinTemperature(
220
- ctx: ActionCtx,
221
- args: {
222
- userId: string;
223
- startTimeInSeconds?: number;
224
- endTimeInSeconds?: number;
225
- },
226
- ) {
227
- const config = this.requireConfig();
228
- return await ctx.runAction(this.component.garmin.public.pullSkinTemperature, {
229
- ...args,
230
- clientId: config.clientId,
231
- clientSecret: config.clientSecret,
232
- });
233
- }
234
-
235
- /**
236
- * Pull user metrics (VO2 max, fitness age, etc.) from Garmin.
237
- *
238
- * @param ctx - Action context from the host app
239
- * @param args.userId - The host app's user identifier
240
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
241
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
242
- */
243
- async pullUserMetrics(
244
- ctx: ActionCtx,
245
- args: {
246
- userId: string;
247
- startTimeInSeconds?: number;
248
- endTimeInSeconds?: number;
249
- },
250
- ) {
251
- const config = this.requireConfig();
252
- return await ctx.runAction(this.component.garmin.public.pullUserMetrics, {
253
- ...args,
254
- clientId: config.clientId,
255
- clientSecret: config.clientSecret,
256
- });
257
- }
258
-
259
- /**
260
- * Pull heart rate variability (HRV) summaries from Garmin.
261
- *
262
- * @param ctx - Action context from the host app
263
- * @param args.userId - The host app's user identifier
264
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
265
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
266
- */
267
- async pullHRV(
268
- ctx: ActionCtx,
269
- args: {
270
- userId: string;
271
- startTimeInSeconds?: number;
272
- endTimeInSeconds?: number;
273
- },
274
- ) {
275
- const config = this.requireConfig();
276
- return await ctx.runAction(this.component.garmin.public.pullHRV, {
277
- ...args,
278
- clientId: config.clientId,
279
- clientSecret: config.clientSecret,
280
- });
281
- }
282
-
283
- /**
284
- * Pull stress detail data from Garmin.
285
- *
286
- * @param ctx - Action context from the host app
287
- * @param args.userId - The host app's user identifier
288
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
289
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
290
- */
291
- async pullStressDetails(
292
- ctx: ActionCtx,
293
- args: {
294
- userId: string;
295
- startTimeInSeconds?: number;
296
- endTimeInSeconds?: number;
297
- },
298
- ) {
299
- const config = this.requireConfig();
300
- return await ctx.runAction(this.component.garmin.public.pullStressDetails, {
301
- ...args,
302
- clientId: config.clientId,
303
- clientSecret: config.clientSecret,
304
- });
305
- }
306
-
307
- /**
308
- * Pull pulse oximetry (SpO2) data from Garmin.
309
- *
310
- * @param ctx - Action context from the host app
311
- * @param args.userId - The host app's user identifier
312
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
313
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
314
- */
315
- async pullPulseOx(
316
- ctx: ActionCtx,
317
- args: {
318
- userId: string;
319
- startTimeInSeconds?: number;
320
- endTimeInSeconds?: number;
321
- },
322
- ) {
323
- const config = this.requireConfig();
324
- return await ctx.runAction(this.component.garmin.public.pullPulseOx, {
325
- ...args,
326
- clientId: config.clientId,
327
- clientSecret: config.clientSecret,
328
- });
329
- }
330
-
331
- /**
332
- * Pull respiration (breathing rate) data from Garmin.
333
- *
334
- * @param ctx - Action context from the host app
335
- * @param args.userId - The host app's user identifier
336
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
337
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
338
- */
339
- async pullRespiration(
340
- ctx: ActionCtx,
341
- args: {
342
- userId: string;
343
- startTimeInSeconds?: number;
344
- endTimeInSeconds?: number;
345
- },
346
- ) {
347
- const config = this.requireConfig();
348
- return await ctx.runAction(this.component.garmin.public.pullRespiration, {
349
- ...args,
350
- clientId: config.clientId,
351
- clientSecret: config.clientSecret,
352
- });
353
- }
354
-
355
- /**
356
- * Pull all supported data types from Garmin in a single call.
357
- *
358
- * Equivalent to calling every individual `pull*` method. Automatically
359
- * refreshes the access token if expired.
360
- *
361
- * @param ctx - Action context from the host app
362
- * @param args.userId - The host app's user identifier
363
- * @param args.startTimeInSeconds - Optional Unix epoch lower bound
364
- * @param args.endTimeInSeconds - Optional Unix epoch upper bound
365
- *
366
- * @example
367
- * ```ts
368
- * await soma.garmin.pullAll(ctx, { userId: "user_123" });
369
- * ```
370
- */
371
- async pullAll(
372
- ctx: ActionCtx,
373
- args: {
374
- userId: string;
375
- startTimeInSeconds?: number;
376
- endTimeInSeconds?: number;
377
- },
378
- ) {
379
- const config = this.requireConfig();
380
- return await ctx.runAction(this.component.garmin.public.pullAll, {
381
- ...args,
382
- clientId: config.clientId,
383
- clientSecret: config.clientSecret,
384
- });
385
- }
386
-
387
- /**
388
- * Push a planned workout to Garmin Connect.
389
- *
390
- * Creates the workout on the user's Garmin account from a planned
391
- * workout stored in Soma.
392
- *
393
- * @param ctx - Action context from the host app
394
- * @param args.userId - The host app's user identifier
395
- * @param args.plannedWorkoutId - The Soma planned workout document ID
396
- * @param args.workoutProvider - Optional provider identifier for the workout source
397
- */
398
- async pushWorkout(
399
- ctx: ActionCtx,
400
- args: {
401
- userId: string;
402
- plannedWorkoutId: string;
403
- workoutProvider?: string;
404
- },
405
- ) {
406
- const config = this.requireConfig();
407
- return await ctx.runAction(this.component.garmin.public.pushWorkout, {
408
- ...args,
409
- clientId: config.clientId,
410
- clientSecret: config.clientSecret,
411
- });
412
- }
413
-
414
- /**
415
- * Schedule a planned workout on the user's Garmin calendar.
416
- *
417
- * The workout must already exist on Garmin (via `pushWorkout`).
418
- *
419
- * @param ctx - Action context from the host app
420
- * @param args.userId - The host app's user identifier
421
- * @param args.plannedWorkoutId - The Soma planned workout document ID
422
- * @param args.date - Optional target date (YYYY-MM-DD); defaults to the workout's planned_date
423
- */
424
- async pushSchedule(
425
- ctx: ActionCtx,
426
- args: {
427
- userId: string;
428
- plannedWorkoutId: string;
429
- date?: string;
430
- },
431
- ) {
432
- const config = this.requireConfig();
433
- return await ctx.runAction(this.component.garmin.public.pushSchedule, {
434
- ...args,
435
- clientId: config.clientId,
436
- clientSecret: config.clientSecret,
437
- });
438
- }
439
-
440
- /**
441
- * Delete a planned workout from Garmin Connect.
442
- *
443
- * Removes the workout from the user's Garmin account.
444
- *
445
- * @param ctx - Action context from the host app
446
- * @param args.userId - The host app's user identifier
447
- * @param args.plannedWorkoutId - The Soma planned workout document ID
448
- */
449
- async deleteWorkout(
450
- ctx: ActionCtx,
451
- args: {
452
- userId: string;
453
- plannedWorkoutId: string;
454
- },
455
- ) {
456
- const config = this.requireConfig();
457
- return await ctx.runAction(this.component.garmin.public.deleteWorkout, {
458
- ...args,
459
- clientId: config.clientId,
460
- clientSecret: config.clientSecret,
461
- });
462
- }
463
-
464
- /**
465
- * Remove a scheduled workout from the user's Garmin calendar.
466
- *
467
- * Unschedules the workout without deleting the workout itself.
468
- *
469
- * @param ctx - Action context from the host app
470
- * @param args.userId - The host app's user identifier
471
- * @param args.plannedWorkoutId - The Soma planned workout document ID
472
- */
473
- async deleteSchedule(
474
- ctx: ActionCtx,
475
- args: {
476
- userId: string;
477
- plannedWorkoutId: string;
478
- },
479
- ) {
480
- const config = this.requireConfig();
481
- return await ctx.runAction(this.component.garmin.public.deleteSchedule, {
482
- ...args,
483
- clientId: config.clientId,
484
- clientSecret: config.clientSecret,
485
- });
486
- }
487
- }
1
+ import type { SomaComponent } from "./index.js";
2
+ import type {
3
+ ActionCtx,
4
+ SomaGarminConfig,
5
+ RegisterRoutesOptions,
6
+ GarminWebhookActionArgs,
7
+ GarminWebhookActionResult,
8
+ GarminWebhookEventName,
9
+ GarminWebhookEvent,
10
+ } from "./types.js";
11
+ import {
12
+ httpActionGeneric,
13
+ type FunctionReference,
14
+ type HttpRouter,
15
+ } from "convex/server";
16
+
17
+ export const GARMIN_OAUTH_CALLBACK_PATH = "/api/garmin/callback";
18
+ export const GARMIN_WEBHOOK_BASE_PATH = "/api/garmin/webhook";
19
+
20
+ export class SomaGarmin {
21
+ constructor(
22
+ private component: SomaComponent,
23
+ private requireConfig: () => SomaGarminConfig,
24
+ ) { }
25
+
26
+ /**
27
+ * Generate a Garmin OAuth 2.0 authorization URL with PKCE.
28
+ *
29
+ * The state and code verifier are stored inside the component automatically,
30
+ * and the callback handler registered by `registerRoutes` completes
31
+ * the flow without further host-app intervention.
32
+ *
33
+ * @param ctx - Action context from the host app
34
+ * @param opts.userId - The host app's user identifier
35
+ * @param opts.redirectUri - Optional override for the OAuth callback URL
36
+ * @returns `{ authUrl, state }`
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const { authUrl } = await soma.garmin.getAuthUrl(ctx, {
41
+ * userId: "user_123",
42
+ * redirectUri: "https://your-app.convex.site/api/garmin/callback",
43
+ * });
44
+ * // Redirect user to authUrl — the callback is handled automatically
45
+ * ```
46
+ */
47
+ async getAuthUrl(
48
+ ctx: ActionCtx,
49
+ opts: { userId: string; redirectUri?: string },
50
+ ) {
51
+ const config = this.requireConfig();
52
+ return await ctx.runAction(this.component.garmin.public.getGarminAuthUrl, {
53
+ clientId: config.clientId,
54
+ redirectUri: opts.redirectUri,
55
+ userId: opts.userId,
56
+ });
57
+ }
58
+
59
+ /**
60
+ * Disconnect a user from Garmin.
61
+ *
62
+ * Deregisters the user at Garmin (best-effort), deletes stored tokens,
63
+ * and sets the connection to inactive.
64
+ *
65
+ * @param ctx - Action context from the host app
66
+ * @param args.userId - The host app's user identifier
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * await soma.garmin.disconnect(ctx, { userId: "user_123" });
71
+ * ```
72
+ */
73
+ async disconnect(
74
+ ctx: ActionCtx,
75
+ args: { userId: string },
76
+ ) {
77
+ return await ctx.runAction(this.component.garmin.public.disconnectGarmin, args);
78
+ }
79
+
80
+ /**
81
+ * Pull activity summaries from Garmin.
82
+ *
83
+ * Fetches activities, transforms them into the normalized Soma schema,
84
+ * and ingests them with automatic deduplication.
85
+ *
86
+ * @param ctx - Action context from the host app
87
+ * @param args.userId - The host app's user identifier
88
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
89
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
90
+ */
91
+ async pullActivities(
92
+ ctx: ActionCtx,
93
+ args: {
94
+ userId: string;
95
+ startTimeInSeconds?: number;
96
+ endTimeInSeconds?: number;
97
+ },
98
+ ) {
99
+ const config = this.requireConfig();
100
+ return await ctx.runAction(this.component.garmin.public.pullActivities, {
101
+ ...args,
102
+ clientId: config.clientId,
103
+ clientSecret: config.clientSecret,
104
+ });
105
+ }
106
+
107
+ /**
108
+ * Pull daily wellness summaries from Garmin.
109
+ *
110
+ * @param ctx - Action context from the host app
111
+ * @param args.userId - The host app's user identifier
112
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
113
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
114
+ */
115
+ async pullDailies(
116
+ ctx: ActionCtx,
117
+ args: {
118
+ userId: string;
119
+ startTimeInSeconds?: number;
120
+ endTimeInSeconds?: number;
121
+ },
122
+ ) {
123
+ const config = this.requireConfig();
124
+ return await ctx.runAction(this.component.garmin.public.pullDailies, {
125
+ ...args,
126
+ clientId: config.clientId,
127
+ clientSecret: config.clientSecret,
128
+ });
129
+ }
130
+
131
+ /**
132
+ * Pull sleep summaries from Garmin.
133
+ *
134
+ * @param ctx - Action context from the host app
135
+ * @param args.userId - The host app's user identifier
136
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
137
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
138
+ */
139
+ async pullSleep(
140
+ ctx: ActionCtx,
141
+ args: {
142
+ userId: string;
143
+ startTimeInSeconds?: number;
144
+ endTimeInSeconds?: number;
145
+ },
146
+ ) {
147
+ const config = this.requireConfig();
148
+ return await ctx.runAction(this.component.garmin.public.pullSleep, {
149
+ ...args,
150
+ clientId: config.clientId,
151
+ clientSecret: config.clientSecret,
152
+ });
153
+ }
154
+
155
+ /**
156
+ * Pull body composition data from Garmin.
157
+ *
158
+ * @param ctx - Action context from the host app
159
+ * @param args.userId - The host app's user identifier
160
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
161
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
162
+ */
163
+ async pullBody(
164
+ ctx: ActionCtx,
165
+ args: {
166
+ userId: string;
167
+ startTimeInSeconds?: number;
168
+ endTimeInSeconds?: number;
169
+ },
170
+ ) {
171
+ const config = this.requireConfig();
172
+ return await ctx.runAction(this.component.garmin.public.pullBody, {
173
+ ...args,
174
+ clientId: config.clientId,
175
+ clientSecret: config.clientSecret,
176
+ });
177
+ }
178
+
179
+ /**
180
+ * Pull menstrual cycle tracking data from Garmin.
181
+ *
182
+ * @param ctx - Action context from the host app
183
+ * @param args.userId - The host app's user identifier
184
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
185
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
186
+ */
187
+ async pullMenstruation(
188
+ ctx: ActionCtx,
189
+ args: {
190
+ userId: string;
191
+ startTimeInSeconds?: number;
192
+ endTimeInSeconds?: number;
193
+ },
194
+ ) {
195
+ const config = this.requireConfig();
196
+ return await ctx.runAction(this.component.garmin.public.pullMenstruation, {
197
+ ...args,
198
+ clientId: config.clientId,
199
+ clientSecret: config.clientSecret,
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Pull blood pressure readings from Garmin.
205
+ *
206
+ * @param ctx - Action context from the host app
207
+ * @param args.userId - The host app's user identifier
208
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
209
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
210
+ */
211
+ async pullBloodPressures(
212
+ ctx: ActionCtx,
213
+ args: {
214
+ userId: string;
215
+ startTimeInSeconds?: number;
216
+ endTimeInSeconds?: number;
217
+ },
218
+ ) {
219
+ const config = this.requireConfig();
220
+ return await ctx.runAction(this.component.garmin.public.pullBloodPressures, {
221
+ ...args,
222
+ clientId: config.clientId,
223
+ clientSecret: config.clientSecret,
224
+ });
225
+ }
226
+
227
+ /**
228
+ * Pull skin temperature data from Garmin.
229
+ *
230
+ * @param ctx - Action context from the host app
231
+ * @param args.userId - The host app's user identifier
232
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
233
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
234
+ */
235
+ async pullSkinTemperature(
236
+ ctx: ActionCtx,
237
+ args: {
238
+ userId: string;
239
+ startTimeInSeconds?: number;
240
+ endTimeInSeconds?: number;
241
+ },
242
+ ) {
243
+ const config = this.requireConfig();
244
+ return await ctx.runAction(this.component.garmin.public.pullSkinTemperature, {
245
+ ...args,
246
+ clientId: config.clientId,
247
+ clientSecret: config.clientSecret,
248
+ });
249
+ }
250
+
251
+ /**
252
+ * Pull user metrics (VO2 max, fitness age, etc.) from Garmin.
253
+ *
254
+ * @param ctx - Action context from the host app
255
+ * @param args.userId - The host app's user identifier
256
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
257
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
258
+ */
259
+ async pullUserMetrics(
260
+ ctx: ActionCtx,
261
+ args: {
262
+ userId: string;
263
+ startTimeInSeconds?: number;
264
+ endTimeInSeconds?: number;
265
+ },
266
+ ) {
267
+ const config = this.requireConfig();
268
+ return await ctx.runAction(this.component.garmin.public.pullUserMetrics, {
269
+ ...args,
270
+ clientId: config.clientId,
271
+ clientSecret: config.clientSecret,
272
+ });
273
+ }
274
+
275
+ /**
276
+ * Pull heart rate variability (HRV) summaries from Garmin.
277
+ *
278
+ * @param ctx - Action context from the host app
279
+ * @param args.userId - The host app's user identifier
280
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
281
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
282
+ */
283
+ async pullHRV(
284
+ ctx: ActionCtx,
285
+ args: {
286
+ userId: string;
287
+ startTimeInSeconds?: number;
288
+ endTimeInSeconds?: number;
289
+ },
290
+ ) {
291
+ const config = this.requireConfig();
292
+ return await ctx.runAction(this.component.garmin.public.pullHRV, {
293
+ ...args,
294
+ clientId: config.clientId,
295
+ clientSecret: config.clientSecret,
296
+ });
297
+ }
298
+
299
+ /**
300
+ * Pull stress detail data from Garmin.
301
+ *
302
+ * @param ctx - Action context from the host app
303
+ * @param args.userId - The host app's user identifier
304
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
305
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
306
+ */
307
+ async pullStressDetails(
308
+ ctx: ActionCtx,
309
+ args: {
310
+ userId: string;
311
+ startTimeInSeconds?: number;
312
+ endTimeInSeconds?: number;
313
+ },
314
+ ) {
315
+ const config = this.requireConfig();
316
+ return await ctx.runAction(this.component.garmin.public.pullStressDetails, {
317
+ ...args,
318
+ clientId: config.clientId,
319
+ clientSecret: config.clientSecret,
320
+ });
321
+ }
322
+
323
+ /**
324
+ * Pull pulse oximetry (SpO2) data from Garmin.
325
+ *
326
+ * @param ctx - Action context from the host app
327
+ * @param args.userId - The host app's user identifier
328
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
329
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
330
+ */
331
+ async pullPulseOx(
332
+ ctx: ActionCtx,
333
+ args: {
334
+ userId: string;
335
+ startTimeInSeconds?: number;
336
+ endTimeInSeconds?: number;
337
+ },
338
+ ) {
339
+ const config = this.requireConfig();
340
+ return await ctx.runAction(this.component.garmin.public.pullPulseOx, {
341
+ ...args,
342
+ clientId: config.clientId,
343
+ clientSecret: config.clientSecret,
344
+ });
345
+ }
346
+
347
+ /**
348
+ * Pull respiration (breathing rate) data from Garmin.
349
+ *
350
+ * @param ctx - Action context from the host app
351
+ * @param args.userId - The host app's user identifier
352
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
353
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
354
+ */
355
+ async pullRespiration(
356
+ ctx: ActionCtx,
357
+ args: {
358
+ userId: string;
359
+ startTimeInSeconds?: number;
360
+ endTimeInSeconds?: number;
361
+ },
362
+ ) {
363
+ const config = this.requireConfig();
364
+ return await ctx.runAction(this.component.garmin.public.pullRespiration, {
365
+ ...args,
366
+ clientId: config.clientId,
367
+ clientSecret: config.clientSecret,
368
+ });
369
+ }
370
+
371
+ /**
372
+ * Pull all supported data types from Garmin in a single call.
373
+ *
374
+ * Equivalent to calling every individual `pull*` method. Automatically
375
+ * refreshes the access token if expired.
376
+ *
377
+ * @param ctx - Action context from the host app
378
+ * @param args.userId - The host app's user identifier
379
+ * @param args.startTimeInSeconds - Optional Unix epoch lower bound
380
+ * @param args.endTimeInSeconds - Optional Unix epoch upper bound
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * await soma.garmin.pullAll(ctx, { userId: "user_123" });
385
+ * ```
386
+ */
387
+ async pullAll(
388
+ ctx: ActionCtx,
389
+ args: {
390
+ userId: string;
391
+ startTimeInSeconds?: number;
392
+ endTimeInSeconds?: number;
393
+ },
394
+ ) {
395
+ const config = this.requireConfig();
396
+ return await ctx.runAction(this.component.garmin.public.pullAll, {
397
+ ...args,
398
+ clientId: config.clientId,
399
+ clientSecret: config.clientSecret,
400
+ });
401
+ }
402
+
403
+ /**
404
+ * Push a planned workout to Garmin Connect.
405
+ *
406
+ * Creates the workout on the user's Garmin account from a planned
407
+ * workout stored in Soma.
408
+ *
409
+ * @param ctx - Action context from the host app
410
+ * @param args.userId - The host app's user identifier
411
+ * @param args.plannedWorkoutId - The Soma planned workout document ID
412
+ * @param args.workoutProvider - Optional provider identifier for the workout source
413
+ */
414
+ async pushWorkout(
415
+ ctx: ActionCtx,
416
+ args: {
417
+ userId: string;
418
+ plannedWorkoutId: string;
419
+ workoutProvider?: string;
420
+ },
421
+ ) {
422
+ const config = this.requireConfig();
423
+ return await ctx.runAction(this.component.garmin.public.pushWorkout, {
424
+ ...args,
425
+ clientId: config.clientId,
426
+ clientSecret: config.clientSecret,
427
+ });
428
+ }
429
+
430
+ /**
431
+ * Schedule a planned workout on the user's Garmin calendar.
432
+ *
433
+ * The workout must already exist on Garmin (via `pushWorkout`).
434
+ *
435
+ * @param ctx - Action context from the host app
436
+ * @param args.userId - The host app's user identifier
437
+ * @param args.plannedWorkoutId - The Soma planned workout document ID
438
+ * @param args.date - Optional target date (YYYY-MM-DD); defaults to the workout's planned_date
439
+ */
440
+ async pushSchedule(
441
+ ctx: ActionCtx,
442
+ args: {
443
+ userId: string;
444
+ plannedWorkoutId: string;
445
+ date?: string;
446
+ },
447
+ ) {
448
+ const config = this.requireConfig();
449
+ return await ctx.runAction(this.component.garmin.public.pushSchedule, {
450
+ ...args,
451
+ clientId: config.clientId,
452
+ clientSecret: config.clientSecret,
453
+ });
454
+ }
455
+
456
+ /**
457
+ * Delete a planned workout from Garmin Connect.
458
+ *
459
+ * Removes the workout from the user's Garmin account.
460
+ *
461
+ * @param ctx - Action context from the host app
462
+ * @param args.userId - The host app's user identifier
463
+ * @param args.plannedWorkoutId - The Soma planned workout document ID
464
+ */
465
+ async deleteWorkout(
466
+ ctx: ActionCtx,
467
+ args: {
468
+ userId: string;
469
+ plannedWorkoutId: string;
470
+ },
471
+ ) {
472
+ const config = this.requireConfig();
473
+ return await ctx.runAction(this.component.garmin.public.deleteWorkout, {
474
+ ...args,
475
+ clientId: config.clientId,
476
+ clientSecret: config.clientSecret,
477
+ });
478
+ }
479
+
480
+ /**
481
+ * Remove a scheduled workout from the user's Garmin calendar.
482
+ *
483
+ * Unschedules the workout without deleting the workout itself.
484
+ *
485
+ * @param ctx - Action context from the host app
486
+ * @param args.userId - The host app's user identifier
487
+ * @param args.plannedWorkoutId - The Soma planned workout document ID
488
+ */
489
+ async deleteSchedule(
490
+ ctx: ActionCtx,
491
+ args: {
492
+ userId: string;
493
+ plannedWorkoutId: string;
494
+ },
495
+ ) {
496
+ const config = this.requireConfig();
497
+ return await ctx.runAction(this.component.garmin.public.deleteSchedule, {
498
+ ...args,
499
+ clientId: config.clientId,
500
+ clientSecret: config.clientSecret,
501
+ });
502
+ }
503
+
504
+ static registerRoutes(
505
+ http: HttpRouter,
506
+ component: SomaComponent,
507
+ opts?: RegisterRoutesOptions["garmin"],
508
+ ) {
509
+ const oauth = opts?.oauth ?? {};
510
+ const path = oauth.path ?? GARMIN_OAUTH_CALLBACK_PATH;
511
+
512
+ http.route({
513
+ path,
514
+ method: "GET",
515
+ handler: httpActionGeneric(async (ctx, request) => {
516
+ const url = new URL(request.url);
517
+ const code = url.searchParams.get("code");
518
+ const state = url.searchParams.get("state");
519
+
520
+ if (!code) {
521
+ return new Response("Missing authorization code", {
522
+ status: 400,
523
+ });
524
+ }
525
+ if (!state) {
526
+ return new Response(
527
+ "Missing state parameter. Ensure the state was included " +
528
+ "when building the Garmin auth URL.",
529
+ { status: 400 },
530
+ );
531
+ }
532
+
533
+ const clientId =
534
+ opts?.clientId ?? process.env.GARMIN_CLIENT_ID;
535
+ const clientSecret =
536
+ opts?.clientSecret ?? process.env.GARMIN_CLIENT_SECRET;
537
+
538
+ if (!clientId || !clientSecret) {
539
+ return new Response(
540
+ "Garmin credentials not configured. Set GARMIN_CLIENT_ID and " +
541
+ "GARMIN_CLIENT_SECRET environment variables, or pass them to registerRoutes.",
542
+ { status: 500 },
543
+ );
544
+ }
545
+
546
+ let result: {
547
+ connectionId: string;
548
+ userId: string;
549
+ };
550
+ try {
551
+ result = await ctx.runAction(component.garmin.public.completeGarminOAuth, {
552
+ code,
553
+ state,
554
+ clientId,
555
+ clientSecret,
556
+ });
557
+ } catch (error) {
558
+ const message =
559
+ error instanceof Error ? error.message : "Unknown error";
560
+ return new Response(`Garmin OAuth callback failed: ${message}`, {
561
+ status: 500,
562
+ });
563
+ }
564
+
565
+ if (oauth.onComplete) {
566
+ try {
567
+ await oauth.onComplete(ctx, {
568
+ provider: "GARMIN",
569
+ userId: result.userId,
570
+ connectionId: result.connectionId,
571
+ });
572
+ } catch (callbackError) {
573
+ console.error(
574
+ "[soma] garmin oauth.onComplete callback error:",
575
+ callbackError instanceof Error ? callbackError.message : callbackError,
576
+ );
577
+ }
578
+ }
579
+
580
+ if (oauth.redirectTo) {
581
+ return new Response(null, {
582
+ status: 302,
583
+ headers: { Location: oauth.redirectTo },
584
+ });
585
+ }
586
+
587
+ return new Response("Successfully connected to Garmin!", {
588
+ status: 200,
589
+ });
590
+ }),
591
+ });
592
+
593
+ // ── Garmin Webhook Routes ──────────────────────────────────
594
+ const webhookCfg = typeof opts?.webhook === "object" ? opts.webhook : undefined;
595
+ if (webhookCfg?.events) {
596
+ const webhookBase = webhookCfg.basePath ?? GARMIN_WEBHOOK_BASE_PATH;
597
+
598
+ const autoIngest = webhookCfg.autoIngest ?? true;
599
+
600
+ const webhookRoutes: Array<{
601
+ dataType: GarminWebhookEventName;
602
+ action: FunctionReference<"action", "internal", GarminWebhookActionArgs, GarminWebhookActionResult>;
603
+ }> = [
604
+ // ACTIVITY category
605
+ { dataType: "activities", action: component.garmin.webhooks.handleGarminWebhookActivities },
606
+ { dataType: "activity-details", action: component.garmin.webhooks.handleGarminWebhookActivityDetails },
607
+ { dataType: "manually-updated-activities", action: component.garmin.webhooks.handleGarminWebhookManuallyUpdatedActivities },
608
+ { dataType: "move-iq", action: component.garmin.webhooks.handleGarminWebhookMoveIQ },
609
+ // HEALTH category
610
+ { dataType: "blood-pressures", action: component.garmin.webhooks.handleGarminWebhookBloodPressures },
611
+ { dataType: "body-compositions", action: component.garmin.webhooks.handleGarminWebhookBodyCompositions },
612
+ { dataType: "dailies", action: component.garmin.webhooks.handleGarminWebhookDailies },
613
+ { dataType: "epochs", action: component.garmin.webhooks.handleGarminWebhookEpochs },
614
+ { dataType: "health-snapshot", action: component.garmin.webhooks.handleGarminWebhookHealthSnapshot },
615
+ { dataType: "sleeps", action: component.garmin.webhooks.handleGarminWebhookSleeps },
616
+ { dataType: "hrv", action: component.garmin.webhooks.handleGarminWebhookHRVSummary },
617
+ { dataType: "stress", action: component.garmin.webhooks.handleGarminWebhookStress },
618
+ { dataType: "pulse-ox", action: component.garmin.webhooks.handleGarminWebhookPulseOx },
619
+ { dataType: "respiration", action: component.garmin.webhooks.handleGarminWebhookRespiration },
620
+ { dataType: "skin-temp", action: component.garmin.webhooks.handleGarminWebhookSkinTemp },
621
+ { dataType: "user-metrics", action: component.garmin.webhooks.handleGarminWebhookUserMetrics },
622
+ // WOMEN_HEALTH category
623
+ { dataType: "menstrual-cycle-tracking", action: component.garmin.webhooks.handleGarminWebhookMenstrualCycleTracking },
624
+ ];
625
+
626
+ for (const route of webhookRoutes) {
627
+ const { dataType } = route;
628
+
629
+ // Only register routes the host app explicitly opted into
630
+ if (!webhookCfg.events?.[dataType]) continue;
631
+
632
+ http.route({
633
+ path: `${webhookBase}/${dataType}`,
634
+ method: "POST",
635
+ handler: httpActionGeneric(async (ctx, request) => {
636
+ let payload: unknown;
637
+ try {
638
+ payload = await request.json();
639
+ } catch {
640
+ return new Response("Invalid JSON body", { status: 400 });
641
+ }
642
+
643
+ let result: GarminWebhookActionResult | undefined;
644
+ try {
645
+ result = await ctx.runAction(route.action, { payload, autoIngest });
646
+ } catch (error) {
647
+ // Log but return 200 to prevent Garmin from retrying
648
+ console.error(
649
+ `Garmin webhook error (${dataType}):`,
650
+ error instanceof Error ? error.message : error,
651
+ );
652
+ }
653
+
654
+ if (result) {
655
+ const event: GarminWebhookEvent = {
656
+ dataType,
657
+ errors: result.errors,
658
+ rawPayload: payload,
659
+ items: result.items,
660
+ };
661
+
662
+ const specificHandler = webhookCfg.events?.[dataType];
663
+ if (typeof specificHandler === "function") {
664
+ try {
665
+ await specificHandler(ctx, event);
666
+ } catch (callbackError) {
667
+ console.error(
668
+ `[soma] garmin webhook events["${dataType}"] callback error:`,
669
+ callbackError instanceof Error ? callbackError.message : callbackError,
670
+ );
671
+ }
672
+ }
673
+
674
+ if (webhookCfg.onEvent) {
675
+ try {
676
+ await webhookCfg.onEvent(ctx, event);
677
+ } catch (callbackError) {
678
+ console.error(
679
+ `[soma] garmin webhook onEvent callback error:`,
680
+ callbackError instanceof Error ? callbackError.message : callbackError,
681
+ );
682
+ }
683
+ }
684
+ }
685
+
686
+ return new Response("OK", { status: 200 });
687
+ }),
688
+ });
689
+ }
690
+ }
691
+ }
692
+ }