@nativesquare/soma 0.11.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 +291 -0
  2. package/dist/client/garmin.d.ts.map +1 -0
  3. package/dist/client/garmin.js +493 -0
  4. package/dist/client/garmin.js.map +1 -0
  5. package/dist/client/index.d.ts +29 -394
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +30 -520
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/client/strava.d.ts +97 -0
  10. package/dist/client/strava.d.ts.map +1 -0
  11. package/dist/client/strava.js +160 -0
  12. package/dist/client/strava.js.map +1 -0
  13. package/dist/client/types.d.ts +238 -0
  14. package/dist/client/types.d.ts.map +1 -1
  15. package/dist/component/_generated/component.d.ts +24 -12
  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 -53
  22. package/dist/component/garmin/public.d.ts.map +1 -1
  23. package/dist/component/garmin/public.js +75 -148
  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 -0
  51. package/src/client/index.ts +68 -933
  52. package/src/client/strava.ts +199 -0
  53. package/src/client/types.ts +285 -0
  54. package/src/component/_generated/component.ts +19 -32
  55. package/src/component/garmin/private.ts +1872 -1870
  56. package/src/component/garmin/public.ts +1073 -1184
  57. package/src/component/garmin/webhooks.ts +898 -857
  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
@@ -0,0 +1,692 @@
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
+ }