@nativesquare/soma 0.16.1 → 0.16.3

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 (37) hide show
  1. package/dist/client/garmin.d.ts.map +1 -1
  2. package/dist/client/garmin.js +8 -0
  3. package/dist/client/garmin.js.map +1 -1
  4. package/dist/client/healthkit.d.ts +63 -97
  5. package/dist/client/healthkit.d.ts.map +1 -1
  6. package/dist/client/healthkit.js +62 -492
  7. package/dist/client/healthkit.js.map +1 -1
  8. package/dist/component/_generated/api.d.ts +4 -0
  9. package/dist/component/_generated/api.d.ts.map +1 -1
  10. package/dist/component/_generated/api.js.map +1 -1
  11. package/dist/component/_generated/component.d.ts +472 -0
  12. package/dist/component/_generated/component.d.ts.map +1 -1
  13. package/dist/component/healthkit/index.d.ts +1 -0
  14. package/dist/component/healthkit/index.d.ts.map +1 -1
  15. package/dist/component/healthkit/index.js +5 -0
  16. package/dist/component/healthkit/index.js.map +1 -1
  17. package/dist/component/healthkit/public.d.ts +451 -0
  18. package/dist/component/healthkit/public.d.ts.map +1 -0
  19. package/dist/component/healthkit/public.js +386 -0
  20. package/dist/component/healthkit/public.js.map +1 -0
  21. package/dist/component/healthkit/types.d.ts +13 -76
  22. package/dist/component/healthkit/types.d.ts.map +1 -1
  23. package/dist/component/healthkit/types.js +9 -6
  24. package/dist/component/healthkit/types.js.map +1 -1
  25. package/dist/component/healthkit/validators.d.ts +1933 -0
  26. package/dist/component/healthkit/validators.d.ts.map +1 -0
  27. package/dist/component/healthkit/validators.js +199 -0
  28. package/dist/component/healthkit/validators.js.map +1 -0
  29. package/dist/component/strava/types/stravaApi/zod.gen.d.ts +3 -3
  30. package/package.json +1 -1
  31. package/src/client/healthkit.ts +283 -791
  32. package/src/component/_generated/api.ts +4 -0
  33. package/src/component/_generated/component.ts +412 -0
  34. package/src/component/healthkit/index.ts +74 -46
  35. package/src/component/healthkit/public.ts +597 -0
  36. package/src/component/healthkit/types.ts +45 -114
  37. package/src/component/healthkit/validators.ts +253 -0
@@ -1,791 +1,283 @@
1
- import type { SomaComponent } from "./index.js";
2
- import type { MutationCtx, SomaError } from "./types.js";
3
- import type {
4
- HKWorkout,
5
- HKCategorySample,
6
- HKQuantitySample,
7
- HKActivitySummary,
8
- HKCharacteristics,
9
- } from "../component/healthkit/types.js";
10
- import { transformWorkout } from "../component/healthkit/transform/activity.js";
11
- import { transformSleep } from "../component/healthkit/transform/sleep.js";
12
- import { transformBody } from "../component/healthkit/transform/body.js";
13
- import {
14
- transformDaily,
15
- transformDailyFromSummary,
16
- } from "../component/healthkit/transform/daily.js";
17
- import { transformNutrition } from "../component/healthkit/transform/nutrition.js";
18
- import { transformMenstruation } from "../component/healthkit/transform/menstruation.js";
19
- import { transformAthlete } from "../component/healthkit/transform/athlete.js";
20
-
21
- const PROVIDER = "APPLE";
22
-
23
- type SyncResult<T extends Record<string, number>> = {
24
- data: { synced: T };
25
- errors: SomaError[];
26
- };
27
-
28
- /**
29
- * Client class for Apple HealthKit integration with Soma.
30
- *
31
- * Unlike {@link import("./strava.js").SomaStrava | SomaStrava} and
32
- * {@link import("./garmin.js").SomaGarmin | SomaGarmin}, HealthKit is an
33
- * on-device provider — data is queried locally on iOS, not fetched from a
34
- * cloud API. This class wraps the transform + ingest pipeline so the host
35
- * app gets the same ergonomic interface as cloud providers.
36
- *
37
- * No configuration is required — HealthKit has no OAuth credentials.
38
- *
39
- * @example
40
- * ```ts
41
- * // Sync workouts received from the React Native client:
42
- * const result = await soma.healthkit.syncActivities(ctx, {
43
- * userId: "user_123",
44
- * workouts: hkWorkouts,
45
- * });
46
- * // result: { data: { synced: { activities: 5 } }, errors: [] }
47
- *
48
- * // Or sync everything at once:
49
- * await soma.healthkit.syncAll(ctx, {
50
- * userId: "user_123",
51
- * workouts: hkWorkouts,
52
- * sleepSessions: [nightSamples],
53
- * bodySamples: hkBodySamples,
54
- * });
55
- * ```
56
- */
57
- export class SomaHealthKit {
58
- constructor(private component: SomaComponent) { }
59
-
60
- // ─── Per-Type Sync Methods ─────────────────────────────────────────────
61
-
62
- /**
63
- * Sync workout activities from HealthKit.
64
- *
65
- * Transforms each `HKWorkout` into the Soma Activity schema and ingests it
66
- * with automatic deduplication by `workout.uuid`.
67
- *
68
- * @param ctx - Mutation (or action) context from the host app
69
- * @param args.userId - The host app's user identifier
70
- * @param args.workouts - Array of HKWorkout objects from HealthKit
71
- */
72
- async syncActivities(
73
- ctx: MutationCtx,
74
- args: { userId: string; workouts: HKWorkout[] },
75
- ): Promise<SyncResult<{ activities: number }>> {
76
- const connectionId = await this.resolveConnection(ctx, args.userId);
77
- const errors: SomaError[] = [];
78
- let count = 0;
79
-
80
- for (const workout of args.workouts) {
81
- try {
82
- const data = transformWorkout(workout);
83
- await ctx.runMutation(this.component.public.ingestActivity, {
84
- connectionId,
85
- userId: args.userId,
86
- ...data,
87
- });
88
- count++;
89
- } catch (err) {
90
- errors.push({
91
- type: "activity",
92
- id: workout.uuid,
93
- message: err instanceof Error ? err.message : String(err),
94
- });
95
- }
96
- }
97
-
98
- await this.updateLastDataUpdate(ctx, connectionId);
99
- return { data: { synced: { activities: count } }, errors };
100
- }
101
-
102
- /**
103
- * Sync sleep sessions from HealthKit.
104
- *
105
- * Each inner array represents one sleep session (all `HKCategorySample`
106
- * stage records for a single night). Each session is aggregated into a
107
- * single Soma Sleep document.
108
- *
109
- * @param ctx - Mutation (or action) context from the host app
110
- * @param args.userId - The host app's user identifier
111
- * @param args.sessions - Array of sessions, each an array of sleep-stage samples
112
- */
113
- async syncSleep(
114
- ctx: MutationCtx,
115
- args: { userId: string; sessions: HKCategorySample[][] },
116
- ): Promise<SyncResult<{ sleep: number }>> {
117
- const connectionId = await this.resolveConnection(ctx, args.userId);
118
- const errors: SomaError[] = [];
119
- let count = 0;
120
-
121
- for (const session of args.sessions) {
122
- const sessionId = session[0]?.uuid ?? "unknown";
123
- try {
124
- const data = transformSleep(session);
125
- await ctx.runMutation(this.component.public.ingestSleep, {
126
- connectionId,
127
- userId: args.userId,
128
- ...data,
129
- });
130
- count++;
131
- } catch (err) {
132
- errors.push({
133
- type: "sleep",
134
- id: sessionId,
135
- message: err instanceof Error ? err.message : String(err),
136
- });
137
- }
138
- }
139
-
140
- await this.updateLastDataUpdate(ctx, connectionId);
141
- return { data: { synced: { sleep: count } }, errors };
142
- }
143
-
144
- /**
145
- * Sync body metrics from HealthKit.
146
- *
147
- * Accepts a mixed array of body-related quantity samples (heart rate, HRV,
148
- * blood pressure, SpO2, weight, etc.) for a single time window and produces
149
- * one Soma Body document.
150
- *
151
- * @param ctx - Mutation (or action) context from the host app
152
- * @param args.userId - The host app's user identifier
153
- * @param args.samples - Array of HKQuantitySample for the desired time range
154
- * @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
155
- */
156
- async syncBody(
157
- ctx: MutationCtx,
158
- args: {
159
- userId: string;
160
- samples: HKQuantitySample[];
161
- timeRange?: { start_time: string; end_time: string };
162
- },
163
- ): Promise<SyncResult<{ body: number }>> {
164
- const connectionId = await this.resolveConnection(ctx, args.userId);
165
- const errors: SomaError[] = [];
166
- let count = 0;
167
-
168
- try {
169
- const data = transformBody(args.samples, args.timeRange);
170
- await ctx.runMutation(this.component.public.ingestBody, {
171
- connectionId,
172
- userId: args.userId,
173
- ...data,
174
- });
175
- count++;
176
- } catch (err) {
177
- errors.push({
178
- type: "body",
179
- id: "transform",
180
- message: err instanceof Error ? err.message : String(err),
181
- });
182
- }
183
-
184
- await this.updateLastDataUpdate(ctx, connectionId);
185
- return { data: { synced: { body: count } }, errors };
186
- }
187
-
188
- /**
189
- * Sync daily activity data from HealthKit quantity samples.
190
- *
191
- * Accepts samples for a single day (steps, distance, energy, exercise time,
192
- * heart rate, etc.) and produces one Soma Daily document.
193
- *
194
- * @param ctx - Mutation (or action) context from the host app
195
- * @param args.userId - The host app's user identifier
196
- * @param args.samples - Array of HKQuantitySample for the desired day
197
- * @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
198
- */
199
- async syncDaily(
200
- ctx: MutationCtx,
201
- args: {
202
- userId: string;
203
- samples: HKQuantitySample[];
204
- timeRange?: { start_time: string; end_time: string };
205
- },
206
- ): Promise<SyncResult<{ daily: number }>> {
207
- const connectionId = await this.resolveConnection(ctx, args.userId);
208
- const errors: SomaError[] = [];
209
- let count = 0;
210
-
211
- try {
212
- const data = transformDaily(args.samples, args.timeRange);
213
- await ctx.runMutation(this.component.public.ingestDaily, {
214
- connectionId,
215
- userId: args.userId,
216
- ...data,
217
- });
218
- count++;
219
- } catch (err) {
220
- errors.push({
221
- type: "daily",
222
- id: "transform",
223
- message: err instanceof Error ? err.message : String(err),
224
- });
225
- }
226
-
227
- await this.updateLastDataUpdate(ctx, connectionId);
228
- return { data: { synced: { daily: count } }, errors };
229
- }
230
-
231
- /**
232
- * Sync daily activity data from HealthKit activity ring summaries.
233
- *
234
- * Each `HKActivitySummary` represents one day's activity rings (Move,
235
- * Exercise, Stand). Each summary produces one Soma Daily document.
236
- *
237
- * @param ctx - Mutation (or action) context from the host app
238
- * @param args.userId - The host app's user identifier
239
- * @param args.summaries - Array of HKActivitySummary from HealthKit
240
- */
241
- async syncDailyFromSummary(
242
- ctx: MutationCtx,
243
- args: { userId: string; summaries: HKActivitySummary[] },
244
- ): Promise<SyncResult<{ daily: number }>> {
245
- const connectionId = await this.resolveConnection(ctx, args.userId);
246
- const errors: SomaError[] = [];
247
- let count = 0;
248
-
249
- for (const summary of args.summaries) {
250
- const { year, month, day } = summary.dateComponents;
251
- const summaryId = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
252
- try {
253
- const data = transformDailyFromSummary(summary);
254
- await ctx.runMutation(this.component.public.ingestDaily, {
255
- connectionId,
256
- userId: args.userId,
257
- ...data,
258
- });
259
- count++;
260
- } catch (err) {
261
- errors.push({
262
- type: "daily",
263
- id: summaryId,
264
- message: err instanceof Error ? err.message : String(err),
265
- });
266
- }
267
- }
268
-
269
- await this.updateLastDataUpdate(ctx, connectionId);
270
- return { data: { synced: { daily: count } }, errors };
271
- }
272
-
273
- /**
274
- * Sync nutrition data from HealthKit.
275
- *
276
- * Accepts dietary quantity samples for a single time window and produces
277
- * one Soma Nutrition document with macros and micros.
278
- *
279
- * @param ctx - Mutation (or action) context from the host app
280
- * @param args.userId - The host app's user identifier
281
- * @param args.samples - Array of HKQuantitySample with dietary type identifiers
282
- * @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
283
- */
284
- async syncNutrition(
285
- ctx: MutationCtx,
286
- args: {
287
- userId: string;
288
- samples: HKQuantitySample[];
289
- timeRange?: { start_time: string; end_time: string };
290
- },
291
- ): Promise<SyncResult<{ nutrition: number }>> {
292
- const connectionId = await this.resolveConnection(ctx, args.userId);
293
- const errors: SomaError[] = [];
294
- let count = 0;
295
-
296
- try {
297
- const data = transformNutrition(args.samples, args.timeRange);
298
- await ctx.runMutation(this.component.public.ingestNutrition, {
299
- connectionId,
300
- userId: args.userId,
301
- ...data,
302
- });
303
- count++;
304
- } catch (err) {
305
- errors.push({
306
- type: "nutrition" as SomaError["type"],
307
- id: "transform",
308
- message: err instanceof Error ? err.message : String(err),
309
- });
310
- }
311
-
312
- await this.updateLastDataUpdate(ctx, connectionId);
313
- return { data: { synced: { nutrition: count } }, errors };
314
- }
315
-
316
- /**
317
- * Sync menstruation data from HealthKit.
318
- *
319
- * Accepts menstrual flow category samples for a single time window and
320
- * produces one Soma Menstruation document.
321
- *
322
- * @param ctx - Mutation (or action) context from the host app
323
- * @param args.userId - The host app's user identifier
324
- * @param args.samples - Array of HKCategorySample with menstrual flow values
325
- * @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
326
- */
327
- async syncMenstruation(
328
- ctx: MutationCtx,
329
- args: {
330
- userId: string;
331
- samples: HKCategorySample[];
332
- timeRange?: { start_time: string; end_time: string };
333
- },
334
- ): Promise<SyncResult<{ menstruation: number }>> {
335
- const connectionId = await this.resolveConnection(ctx, args.userId);
336
- const errors: SomaError[] = [];
337
- let count = 0;
338
-
339
- try {
340
- const data = transformMenstruation(args.samples, args.timeRange);
341
- await ctx.runMutation(this.component.public.ingestMenstruation, {
342
- connectionId,
343
- userId: args.userId,
344
- ...data,
345
- });
346
- count++;
347
- } catch (err) {
348
- errors.push({
349
- type: "menstruation",
350
- id: "transform",
351
- message: err instanceof Error ? err.message : String(err),
352
- });
353
- }
354
-
355
- await this.updateLastDataUpdate(ctx, connectionId);
356
- return { data: { synced: { menstruation: count } }, errors };
357
- }
358
-
359
- /**
360
- * Sync athlete profile from HealthKit.
361
- *
362
- * HealthKit exposes limited profile data (biological sex, date of birth).
363
- * Produces one Soma Athlete document per connection.
364
- *
365
- * @param ctx - Mutation (or action) context from the host app
366
- * @param args.userId - The host app's user identifier
367
- * @param args.characteristics - The HKCharacteristics from HealthKit
368
- */
369
- async syncAthlete(
370
- ctx: MutationCtx,
371
- args: { userId: string; characteristics: HKCharacteristics },
372
- ): Promise<SyncResult<{ athletes: number }>> {
373
- const connectionId = await this.resolveConnection(ctx, args.userId);
374
- const errors: SomaError[] = [];
375
- let count = 0;
376
-
377
- try {
378
- const data = transformAthlete(args.characteristics);
379
- await ctx.runMutation(this.component.public.ingestAthlete, {
380
- connectionId,
381
- userId: args.userId,
382
- ...data,
383
- });
384
- count++;
385
- } catch (err) {
386
- errors.push({
387
- type: "athlete",
388
- id: "transform",
389
- message: err instanceof Error ? err.message : String(err),
390
- });
391
- }
392
-
393
- await this.updateLastDataUpdate(ctx, connectionId);
394
- return { data: { synced: { athletes: count } }, errors };
395
- }
396
-
397
- // ─── Orchestrator ────────────────────────────────────────────────────────
398
-
399
- /**
400
- * Sync all provided HealthKit data types in a single call.
401
- *
402
- * Only data types with values provided are synced. Errors from individual
403
- * data types are collected — one failing type does not block others.
404
- *
405
- * @param ctx - Mutation (or action) context from the host app
406
- * @param args.userId - The host app's user identifier
407
- * @param args.workouts - Optional array of HKWorkout objects
408
- * @param args.sleepSessions - Optional array of sleep sessions (each an array of stage samples)
409
- * @param args.bodySamples - Optional body-related quantity samples
410
- * @param args.dailySamples - Optional daily activity quantity samples
411
- * @param args.dailySummaries - Optional daily activity ring summaries
412
- * @param args.nutritionSamples - Optional dietary quantity samples
413
- * @param args.menstruationSamples - Optional menstrual flow category samples
414
- * @param args.characteristics - Optional user characteristics
415
- *
416
- * @example
417
- * ```ts
418
- * const result = await soma.healthkit.syncAll(ctx, {
419
- * userId: "user_123",
420
- * workouts: hkWorkouts,
421
- * sleepSessions: [nightSamples],
422
- * bodySamples: hkBodySamples,
423
- * dailySummaries: hkSummaries,
424
- * characteristics: hkCharacteristics,
425
- * });
426
- * ```
427
- */
428
- async syncAll(
429
- ctx: MutationCtx,
430
- args: {
431
- userId: string;
432
- workouts?: HKWorkout[];
433
- sleepSessions?: HKCategorySample[][];
434
- bodySamples?: HKQuantitySample[];
435
- bodyTimeRange?: { start_time: string; end_time: string };
436
- dailySamples?: HKQuantitySample[];
437
- dailyTimeRange?: { start_time: string; end_time: string };
438
- dailySummaries?: HKActivitySummary[];
439
- nutritionSamples?: HKQuantitySample[];
440
- nutritionTimeRange?: { start_time: string; end_time: string };
441
- menstruationSamples?: HKCategorySample[];
442
- menstruationTimeRange?: { start_time: string; end_time: string };
443
- characteristics?: HKCharacteristics;
444
- },
445
- ): Promise<SyncResult<Record<string, number>>> {
446
- // Resolve connection once, up front
447
- const connectionId = await this.resolveConnection(ctx, args.userId);
448
- const allErrors: SomaError[] = [];
449
- const synced: Record<string, number> = {};
450
-
451
- const run = async <T extends Record<string, number>>(
452
- fn: () => Promise<SyncResult<T>>,
453
- fallbackType: SomaError["type"],
454
- ) => {
455
- try {
456
- const result = await fn();
457
- Object.assign(synced, result.data.synced);
458
- allErrors.push(...result.errors);
459
- } catch (err) {
460
- allErrors.push({
461
- type: fallbackType,
462
- id: "sync",
463
- message: err instanceof Error ? err.message : String(err),
464
- });
465
- }
466
- };
467
-
468
- if (args.workouts) {
469
- await run(
470
- () => this.syncActivitiesInternal(ctx, connectionId, args.userId, args.workouts!),
471
- "activity",
472
- );
473
- }
474
- if (args.sleepSessions) {
475
- await run(
476
- () => this.syncSleepInternal(ctx, connectionId, args.userId, args.sleepSessions!),
477
- "sleep",
478
- );
479
- }
480
- if (args.bodySamples) {
481
- await run(
482
- () => this.syncBodyInternal(ctx, connectionId, args.userId, args.bodySamples!, args.bodyTimeRange),
483
- "body",
484
- );
485
- }
486
- if (args.dailySamples) {
487
- await run(
488
- () => this.syncDailyInternal(ctx, connectionId, args.userId, args.dailySamples!, args.dailyTimeRange),
489
- "daily",
490
- );
491
- }
492
- if (args.dailySummaries) {
493
- await run(
494
- () => this.syncDailyFromSummaryInternal(ctx, connectionId, args.userId, args.dailySummaries!),
495
- "daily",
496
- );
497
- }
498
- if (args.nutritionSamples) {
499
- await run(
500
- () => this.syncNutritionInternal(ctx, connectionId, args.userId, args.nutritionSamples!, args.nutritionTimeRange),
501
- "nutrition" as SomaError["type"],
502
- );
503
- }
504
- if (args.menstruationSamples) {
505
- await run(
506
- () => this.syncMenstruationInternal(ctx, connectionId, args.userId, args.menstruationSamples!, args.menstruationTimeRange),
507
- "menstruation",
508
- );
509
- }
510
- if (args.characteristics) {
511
- await run(
512
- () => this.syncAthleteInternal(ctx, connectionId, args.userId, args.characteristics!),
513
- "athlete",
514
- );
515
- }
516
-
517
- // Update lastDataUpdate once at the end
518
- await this.updateLastDataUpdate(ctx, connectionId);
519
-
520
- return { data: { synced }, errors: allErrors };
521
- }
522
-
523
- // ─── Private Helpers ───────────��─────────────────────────────────────────
524
-
525
- /**
526
- * Resolve or create the APPLE connection for a user.
527
- *
528
- * HealthKit permissions are managed on-device by the React Native library,
529
- * so Soma auto-ensures the connection record exists. If the connection was
530
- * previously disconnected it is re-activated automatically.
531
- */
532
- private async resolveConnection(
533
- ctx: MutationCtx,
534
- userId: string,
535
- ): Promise<string> {
536
- // `connect` is idempotent: creates if missing, re-activates if inactive.
537
- return (await ctx.runMutation(this.component.public.connect, {
538
- userId,
539
- provider: PROVIDER,
540
- })) as string;
541
- }
542
-
543
- private async updateLastDataUpdate(
544
- ctx: MutationCtx,
545
- connectionId: string,
546
- ): Promise<void> {
547
- await ctx.runMutation(this.component.public.updateConnection, {
548
- connectionId,
549
- lastDataUpdate: new Date().toISOString(),
550
- });
551
- }
552
-
553
- // ─── Internal sync methods (skip connection resolution + lastDataUpdate) ─
554
-
555
- private async syncActivitiesInternal(
556
- ctx: MutationCtx,
557
- connectionId: string,
558
- userId: string,
559
- workouts: HKWorkout[],
560
- ): Promise<SyncResult<{ activities: number }>> {
561
- const errors: SomaError[] = [];
562
- let count = 0;
563
-
564
- for (const workout of workouts) {
565
- try {
566
- const data = transformWorkout(workout);
567
- await ctx.runMutation(this.component.public.ingestActivity, {
568
- connectionId,
569
- userId,
570
- ...data,
571
- });
572
- count++;
573
- } catch (err) {
574
- errors.push({
575
- type: "activity",
576
- id: workout.uuid,
577
- message: err instanceof Error ? err.message : String(err),
578
- });
579
- }
580
- }
581
-
582
- return { data: { synced: { activities: count } }, errors };
583
- }
584
-
585
- private async syncSleepInternal(
586
- ctx: MutationCtx,
587
- connectionId: string,
588
- userId: string,
589
- sessions: HKCategorySample[][],
590
- ): Promise<SyncResult<{ sleep: number }>> {
591
- const errors: SomaError[] = [];
592
- let count = 0;
593
-
594
- for (const session of sessions) {
595
- const sessionId = session[0]?.uuid ?? "unknown";
596
- try {
597
- const data = transformSleep(session);
598
- await ctx.runMutation(this.component.public.ingestSleep, {
599
- connectionId,
600
- userId,
601
- ...data,
602
- });
603
- count++;
604
- } catch (err) {
605
- errors.push({
606
- type: "sleep",
607
- id: sessionId,
608
- message: err instanceof Error ? err.message : String(err),
609
- });
610
- }
611
- }
612
-
613
- return { data: { synced: { sleep: count } }, errors };
614
- }
615
-
616
- private async syncBodyInternal(
617
- ctx: MutationCtx,
618
- connectionId: string,
619
- userId: string,
620
- samples: HKQuantitySample[],
621
- timeRange?: { start_time: string; end_time: string },
622
- ): Promise<SyncResult<{ body: number }>> {
623
- const errors: SomaError[] = [];
624
- let count = 0;
625
-
626
- try {
627
- const data = transformBody(samples, timeRange);
628
- await ctx.runMutation(this.component.public.ingestBody, {
629
- connectionId,
630
- userId,
631
- ...data,
632
- });
633
- count++;
634
- } catch (err) {
635
- errors.push({
636
- type: "body",
637
- id: "transform",
638
- message: err instanceof Error ? err.message : String(err),
639
- });
640
- }
641
-
642
- return { data: { synced: { body: count } }, errors };
643
- }
644
-
645
- private async syncDailyInternal(
646
- ctx: MutationCtx,
647
- connectionId: string,
648
- userId: string,
649
- samples: HKQuantitySample[],
650
- timeRange?: { start_time: string; end_time: string },
651
- ): Promise<SyncResult<{ daily: number }>> {
652
- const errors: SomaError[] = [];
653
- let count = 0;
654
-
655
- try {
656
- const data = transformDaily(samples, timeRange);
657
- await ctx.runMutation(this.component.public.ingestDaily, {
658
- connectionId,
659
- userId,
660
- ...data,
661
- });
662
- count++;
663
- } catch (err) {
664
- errors.push({
665
- type: "daily",
666
- id: "transform",
667
- message: err instanceof Error ? err.message : String(err),
668
- });
669
- }
670
-
671
- return { data: { synced: { daily: count } }, errors };
672
- }
673
-
674
- private async syncDailyFromSummaryInternal(
675
- ctx: MutationCtx,
676
- connectionId: string,
677
- userId: string,
678
- summaries: HKActivitySummary[],
679
- ): Promise<SyncResult<{ daily: number }>> {
680
- const errors: SomaError[] = [];
681
- let count = 0;
682
-
683
- for (const summary of summaries) {
684
- const { year, month, day } = summary.dateComponents;
685
- const summaryId = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
686
- try {
687
- const data = transformDailyFromSummary(summary);
688
- await ctx.runMutation(this.component.public.ingestDaily, {
689
- connectionId,
690
- userId,
691
- ...data,
692
- });
693
- count++;
694
- } catch (err) {
695
- errors.push({
696
- type: "daily",
697
- id: summaryId,
698
- message: err instanceof Error ? err.message : String(err),
699
- });
700
- }
701
- }
702
-
703
- return { data: { synced: { daily: count } }, errors };
704
- }
705
-
706
- private async syncNutritionInternal(
707
- ctx: MutationCtx,
708
- connectionId: string,
709
- userId: string,
710
- samples: HKQuantitySample[],
711
- timeRange?: { start_time: string; end_time: string },
712
- ): Promise<SyncResult<{ nutrition: number }>> {
713
- const errors: SomaError[] = [];
714
- let count = 0;
715
-
716
- try {
717
- const data = transformNutrition(samples, timeRange);
718
- await ctx.runMutation(this.component.public.ingestNutrition, {
719
- connectionId,
720
- userId,
721
- ...data,
722
- });
723
- count++;
724
- } catch (err) {
725
- errors.push({
726
- type: "nutrition" as SomaError["type"],
727
- id: "transform",
728
- message: err instanceof Error ? err.message : String(err),
729
- });
730
- }
731
-
732
- return { data: { synced: { nutrition: count } }, errors };
733
- }
734
-
735
- private async syncMenstruationInternal(
736
- ctx: MutationCtx,
737
- connectionId: string,
738
- userId: string,
739
- samples: HKCategorySample[],
740
- timeRange?: { start_time: string; end_time: string },
741
- ): Promise<SyncResult<{ menstruation: number }>> {
742
- const errors: SomaError[] = [];
743
- let count = 0;
744
-
745
- try {
746
- const data = transformMenstruation(samples, timeRange);
747
- await ctx.runMutation(this.component.public.ingestMenstruation, {
748
- connectionId,
749
- userId,
750
- ...data,
751
- });
752
- count++;
753
- } catch (err) {
754
- errors.push({
755
- type: "menstruation",
756
- id: "transform",
757
- message: err instanceof Error ? err.message : String(err),
758
- });
759
- }
760
-
761
- return { data: { synced: { menstruation: count } }, errors };
762
- }
763
-
764
- private async syncAthleteInternal(
765
- ctx: MutationCtx,
766
- connectionId: string,
767
- userId: string,
768
- characteristics: HKCharacteristics,
769
- ): Promise<SyncResult<{ athletes: number }>> {
770
- const errors: SomaError[] = [];
771
- let count = 0;
772
-
773
- try {
774
- const data = transformAthlete(characteristics);
775
- await ctx.runMutation(this.component.public.ingestAthlete, {
776
- connectionId,
777
- userId,
778
- ...data,
779
- });
780
- count++;
781
- } catch (err) {
782
- errors.push({
783
- type: "athlete",
784
- id: "transform",
785
- message: err instanceof Error ? err.message : String(err),
786
- });
787
- }
788
-
789
- return { data: { synced: { athletes: count } }, errors };
790
- }
791
- }
1
+ import type { SomaComponent } from "./index.js";
2
+ import type { MutationCtx, SomaError, SomaResult } from "./types.js";
3
+ import type {
4
+ HKWorkout,
5
+ HKCategorySample,
6
+ HKQuantitySample,
7
+ HKActivitySummary,
8
+ HKCharacteristics,
9
+ } from "../component/healthkit/types.js";
10
+
11
+ /**
12
+ * Client class for Apple HealthKit integration with Soma.
13
+ *
14
+ * Unlike {@link import("./strava.js").SomaStrava | SomaStrava} and
15
+ * {@link import("./garmin.js").SomaGarmin | SomaGarmin}, HealthKit is an
16
+ * on-device provider — data is queried locally on iOS, not fetched from a
17
+ * cloud API. This class is a thin wrapper over the component's public
18
+ * mutations, which own the transform + ingest pipeline.
19
+ *
20
+ * Because HealthKit permissions are managed in iOS Settings, the server
21
+ * cannot independently verify whether the user has granted or revoked
22
+ * access. The `connect()` / `disconnect()` methods therefore represent
23
+ * assertions by the host app about the state it has observed on-device:
24
+ *
25
+ * - Call {@link SomaHealthKit.connect | connect} once the React Native
26
+ * HealthKit library confirms permissions were granted.
27
+ * - Call {@link SomaHealthKit.disconnect | disconnect} when the user
28
+ * turns HealthKit off in your app's settings, or when you otherwise
29
+ * detect that syncs are no longer returning data.
30
+ *
31
+ * Sync methods refuse to run unless an active connection exists.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // Once, after the RN library confirms permissions:
36
+ * await soma.healthkit.connect(ctx, { userId: "user_123" });
37
+ *
38
+ * // Sync workouts received from the React Native client:
39
+ * const result = await soma.healthkit.syncActivities(ctx, {
40
+ * userId: "user_123",
41
+ * workouts: hkWorkouts,
42
+ * });
43
+ * // result: { data: { activities: 5 }, errors: [] }
44
+ *
45
+ * // Or sync everything at once:
46
+ * await soma.healthkit.syncAll(ctx, {
47
+ * userId: "user_123",
48
+ * workouts: hkWorkouts,
49
+ * sleepSessions: [nightSamples],
50
+ * bodySamples: hkBodySamples,
51
+ * });
52
+ *
53
+ * // When the user disables HealthKit in your app:
54
+ * await soma.healthkit.disconnect(ctx, { userId: "user_123" });
55
+ * ```
56
+ */
57
+ export class SomaHealthKit {
58
+ constructor(private component: SomaComponent) { }
59
+
60
+ // ─── Connect / Disconnect ──────────────────────────────────────────────
61
+
62
+ /**
63
+ * Assert that HealthKit is connected for this user.
64
+ *
65
+ * Call this once after the React Native HealthKit library confirms
66
+ * permissions were granted. Creates the APPLE connection if missing,
67
+ * or re-activates it if previously disconnected. Idempotent.
68
+ *
69
+ * @param ctx - Mutation context from the host app
70
+ * @param args.userId - The host app's user identifier
71
+ * @returns The connection document ID
72
+ */
73
+ async connect(
74
+ ctx: MutationCtx,
75
+ args: { userId: string },
76
+ ): Promise<string> {
77
+ return await ctx.runMutation(this.component.healthkit.public.connect, args);
78
+ }
79
+
80
+ /**
81
+ * Mark the HealthKit connection inactive for this user.
82
+ *
83
+ * Call this when the user disables HealthKit in your app's settings, or
84
+ * when you detect that sync calls are no longer returning data (suggesting
85
+ * the user revoked permissions in iOS Settings). Subsequent sync methods
86
+ * will throw until {@link SomaHealthKit.connect | connect} is called again.
87
+ *
88
+ * Does not delete the connection row or any previously synced data —
89
+ * re-connecting later preserves history.
90
+ *
91
+ * @param ctx - Mutation context from the host app
92
+ * @param args.userId - The host app's user identifier
93
+ */
94
+ async disconnect(
95
+ ctx: MutationCtx,
96
+ args: { userId: string },
97
+ ): Promise<null> {
98
+ return await ctx.runMutation(this.component.healthkit.public.disconnect, args);
99
+ }
100
+
101
+ // ─── Per-Type Sync Methods ─────────────────────────────────────────────
102
+
103
+ /**
104
+ * Sync workout activities from HealthKit.
105
+ *
106
+ * Transforms each `HKWorkout` into the Soma Activity schema and ingests it
107
+ * with automatic deduplication by `workout.uuid`.
108
+ */
109
+ async syncActivities(
110
+ ctx: MutationCtx,
111
+ args: { userId: string; workouts: HKWorkout[] },
112
+ ): Promise<SomaResult<{ activities: number }>> {
113
+ return (await ctx.runMutation(
114
+ this.component.healthkit.public.syncActivities,
115
+ args,
116
+ )) as SomaResult<{ activities: number }>;
117
+ }
118
+
119
+ /**
120
+ * Sync sleep sessions from HealthKit.
121
+ *
122
+ * Each inner array represents one sleep session (all `HKCategorySample`
123
+ * stage records for a single night). Each session is aggregated into a
124
+ * single Soma Sleep document.
125
+ */
126
+ async syncSleep(
127
+ ctx: MutationCtx,
128
+ args: { userId: string; sessions: HKCategorySample[][] },
129
+ ): Promise<SomaResult<{ sleep: number }>> {
130
+ return (await ctx.runMutation(
131
+ this.component.healthkit.public.syncSleep,
132
+ args,
133
+ )) as SomaResult<{ sleep: number }>;
134
+ }
135
+
136
+ /**
137
+ * Sync body metrics from HealthKit.
138
+ *
139
+ * Accepts a mixed array of body-related quantity samples (heart rate, HRV,
140
+ * blood pressure, SpO2, weight, etc.) for a single time window and produces
141
+ * one Soma Body document.
142
+ */
143
+ async syncBody(
144
+ ctx: MutationCtx,
145
+ args: {
146
+ userId: string;
147
+ samples: HKQuantitySample[];
148
+ timeRange?: { start_time: string; end_time: string };
149
+ },
150
+ ): Promise<SomaResult<{ body: number }>> {
151
+ return (await ctx.runMutation(
152
+ this.component.healthkit.public.syncBody,
153
+ args,
154
+ )) as SomaResult<{ body: number }>;
155
+ }
156
+
157
+ /**
158
+ * Sync daily activity data from HealthKit quantity samples.
159
+ */
160
+ async syncDaily(
161
+ ctx: MutationCtx,
162
+ args: {
163
+ userId: string;
164
+ samples: HKQuantitySample[];
165
+ timeRange?: { start_time: string; end_time: string };
166
+ },
167
+ ): Promise<SomaResult<{ daily: number }>> {
168
+ return (await ctx.runMutation(
169
+ this.component.healthkit.public.syncDaily,
170
+ args,
171
+ )) as SomaResult<{ daily: number }>;
172
+ }
173
+
174
+ /**
175
+ * Sync daily activity data from HealthKit activity ring summaries.
176
+ */
177
+ async syncDailyFromSummary(
178
+ ctx: MutationCtx,
179
+ args: { userId: string; summaries: HKActivitySummary[] },
180
+ ): Promise<SomaResult<{ daily: number }>> {
181
+ return (await ctx.runMutation(
182
+ this.component.healthkit.public.syncDailyFromSummary,
183
+ args,
184
+ )) as SomaResult<{ daily: number }>;
185
+ }
186
+
187
+ /**
188
+ * Sync nutrition data from HealthKit.
189
+ */
190
+ async syncNutrition(
191
+ ctx: MutationCtx,
192
+ args: {
193
+ userId: string;
194
+ samples: HKQuantitySample[];
195
+ timeRange?: { start_time: string; end_time: string };
196
+ },
197
+ ): Promise<SomaResult<{ nutrition: number }>> {
198
+ return (await ctx.runMutation(
199
+ this.component.healthkit.public.syncNutrition,
200
+ args,
201
+ )) as SomaResult<{ nutrition: number }>;
202
+ }
203
+
204
+ /**
205
+ * Sync menstruation data from HealthKit.
206
+ */
207
+ async syncMenstruation(
208
+ ctx: MutationCtx,
209
+ args: {
210
+ userId: string;
211
+ samples: HKCategorySample[];
212
+ timeRange?: { start_time: string; end_time: string };
213
+ },
214
+ ): Promise<SomaResult<{ menstruation: number }>> {
215
+ return (await ctx.runMutation(
216
+ this.component.healthkit.public.syncMenstruation,
217
+ args,
218
+ )) as SomaResult<{ menstruation: number }>;
219
+ }
220
+
221
+ /**
222
+ * Sync athlete profile from HealthKit.
223
+ *
224
+ * HealthKit exposes limited profile data (biological sex, date of birth).
225
+ * Produces one Soma Athlete document per connection.
226
+ */
227
+ async syncAthlete(
228
+ ctx: MutationCtx,
229
+ args: { userId: string; characteristics: HKCharacteristics },
230
+ ): Promise<SomaResult<{ athletes: number }>> {
231
+ return (await ctx.runMutation(
232
+ this.component.healthkit.public.syncAthlete,
233
+ args,
234
+ )) as SomaResult<{ athletes: number }>;
235
+ }
236
+
237
+ // ─── Orchestrator ────────────────────────────────────────────────────────
238
+
239
+ /**
240
+ * Sync all provided HealthKit data types in a single call.
241
+ *
242
+ * Only data types with values provided are synced. Errors from individual
243
+ * data types are collected one failing type does not block others.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const result = await soma.healthkit.syncAll(ctx, {
248
+ * userId: "user_123",
249
+ * workouts: hkWorkouts,
250
+ * sleepSessions: [nightSamples],
251
+ * bodySamples: hkBodySamples,
252
+ * dailySummaries: hkSummaries,
253
+ * characteristics: hkCharacteristics,
254
+ * });
255
+ * ```
256
+ */
257
+ async syncAll(
258
+ ctx: MutationCtx,
259
+ args: {
260
+ userId: string;
261
+ workouts?: HKWorkout[];
262
+ sleepSessions?: HKCategorySample[][];
263
+ bodySamples?: HKQuantitySample[];
264
+ bodyTimeRange?: { start_time: string; end_time: string };
265
+ dailySamples?: HKQuantitySample[];
266
+ dailyTimeRange?: { start_time: string; end_time: string };
267
+ dailySummaries?: HKActivitySummary[];
268
+ nutritionSamples?: HKQuantitySample[];
269
+ nutritionTimeRange?: { start_time: string; end_time: string };
270
+ menstruationSamples?: HKCategorySample[];
271
+ menstruationTimeRange?: { start_time: string; end_time: string };
272
+ characteristics?: HKCharacteristics;
273
+ },
274
+ ): Promise<SomaResult<Record<string, number>>> {
275
+ return (await ctx.runMutation(
276
+ this.component.healthkit.public.syncAll,
277
+ args,
278
+ )) as SomaResult<Record<string, number>>;
279
+ }
280
+ }
281
+
282
+ // Re-export for JSDoc link resolution above.
283
+ export type { SomaError };