@nativesquare/soma 0.10.2 → 0.12.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.
@@ -0,0 +1,487 @@
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
+ }