@nativesquare/soma 0.17.1 → 0.18.1

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.
@@ -8,6 +8,7 @@ import type {
8
8
  GarminWebhookEventName,
9
9
  GarminWebhookEvent,
10
10
  } from "./types.js";
11
+ import { resolveEventConfig } from "./types.js";
11
12
  import {
12
13
  httpActionGeneric,
13
14
  type FunctionReference,
@@ -705,8 +706,6 @@ export class SomaGarmin {
705
706
  if (webhookCfg?.events) {
706
707
  const webhookBase = webhookCfg.basePath ?? GARMIN_WEBHOOK_BASE_PATH;
707
708
 
708
- const autoIngest = webhookCfg.autoIngest ?? true;
709
-
710
709
  const webhookRoutes: Array<{
711
710
  dataType: GarminWebhookEventName;
712
711
  action: FunctionReference<"action", "internal", GarminWebhookActionArgs, GarminWebhookActionResult>;
@@ -739,7 +738,19 @@ export class SomaGarmin {
739
738
  const { dataType } = route;
740
739
 
741
740
  // Only register routes the host app explicitly opted into
742
- if (!webhookCfg.events?.[dataType]) continue;
741
+ const entry = webhookCfg.events?.[dataType];
742
+ if (!entry) continue;
743
+
744
+ const { handler: specificHandler, autoIngest, rawPassthrough } =
745
+ resolveEventConfig(entry);
746
+
747
+ if (autoIngest && rawPassthrough) {
748
+ throw new Error(
749
+ `[soma:garmin:webhook] events["${dataType}"] cannot enable both ` +
750
+ `autoIngest and rawPassthrough — raw Garmin items can't be ` +
751
+ `auto-ingested into Soma's normalized schema. Set autoIngest: false.`,
752
+ );
753
+ }
743
754
 
744
755
  http.route({
745
756
  path: `${webhookBase}/${dataType}`,
@@ -764,7 +775,11 @@ export class SomaGarmin {
764
775
 
765
776
  let result: GarminWebhookActionResult | undefined;
766
777
  try {
767
- result = await ctx.runAction(route.action, { payload, autoIngest });
778
+ result = await ctx.runAction(route.action, {
779
+ payload,
780
+ autoIngest,
781
+ rawPassthrough,
782
+ });
768
783
  } catch (error) {
769
784
  // Log but return 200 to prevent Garmin from retrying
770
785
  console.error(
@@ -784,8 +799,7 @@ export class SomaGarmin {
784
799
  items: result.items,
785
800
  };
786
801
 
787
- const specificHandler = webhookCfg.events?.[dataType];
788
- if (typeof specificHandler === "function") {
802
+ if (specificHandler) {
789
803
  try {
790
804
  await specificHandler(ctx, event);
791
805
  } catch (callbackError) {
@@ -6,7 +6,9 @@ import type {
6
6
  StravaWebhookActionResult,
7
7
  StravaWebhookEvent,
8
8
  StravaWebhookEventName,
9
+ StravaWebhookHandler,
9
10
  } from "./types.js";
11
+ import { resolveEventConfig } from "./types.js";
10
12
  import { httpActionGeneric, type HttpRouter } from "convex/server";
11
13
 
12
14
  export const STRAVA_OAUTH_CALLBACK_PATH = "/api/strava/callback";
@@ -324,7 +326,19 @@ export class SomaStrava {
324
326
  if (webhookCfg?.events) {
325
327
  const webhookPath = webhookCfg.path ?? STRAVA_WEBHOOK_BASE_PATH;
326
328
  const verifyToken = webhookCfg.verifyToken ?? process.env.STRAVA_WEBHOOK_VERIFY_TOKEN;
327
- const autoIngest = webhookCfg.autoIngest ?? true;
329
+
330
+ // Resolve each registered event into its handler + autoIngest pair once,
331
+ // up-front. The dispatcher action receives `autoIngestByEvent` and the
332
+ // POST handler reuses `handlersByEvent` for per-event callback dispatch.
333
+ const autoIngestByEvent: Partial<Record<StravaWebhookEventName, boolean>> = {};
334
+ const handlersByEvent: Partial<Record<StravaWebhookEventName, StravaWebhookHandler>> = {};
335
+ for (const [eventName, entry] of Object.entries(webhookCfg.events) as Array<
336
+ [StravaWebhookEventName, typeof webhookCfg.events[StravaWebhookEventName]]
337
+ >) {
338
+ const { handler, autoIngest } = resolveEventConfig(entry);
339
+ autoIngestByEvent[eventName] = autoIngest;
340
+ if (handler) handlersByEvent[eventName] = handler;
341
+ }
328
342
 
329
343
  // GET: Strava subscription verification challenge
330
344
  http.route({
@@ -403,7 +417,12 @@ export class SomaStrava {
403
417
  try {
404
418
  result = await ctx.runAction(
405
419
  component.strava.webhooks.handleStravaWebhook,
406
- { payload, clientId: webhookClientId, clientSecret: webhookClientSecret, autoIngest },
420
+ {
421
+ payload,
422
+ clientId: webhookClientId,
423
+ clientSecret: webhookClientSecret,
424
+ autoIngestByEvent,
425
+ },
407
426
  );
408
427
  } catch (error) {
409
428
  console.error(
@@ -424,8 +443,8 @@ export class SomaStrava {
424
443
  };
425
444
 
426
445
  // Fire per-event handler
427
- const specificHandler = webhookCfg.events?.[eventName];
428
- if (typeof specificHandler === "function") {
446
+ const specificHandler = handlersByEvent[eventName];
447
+ if (specificHandler) {
429
448
  try {
430
449
  await specificHandler(ctx, event);
431
450
  } catch (callbackError) {
@@ -153,6 +153,7 @@ export type GarminWebhookActionArgs = {
153
153
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
154
  payload: any;
155
155
  autoIngest?: boolean;
156
+ rawPassthrough?: boolean;
156
157
  };
157
158
 
158
159
  /** Result returned by all Garmin webhook handler actions inside the component. */
@@ -167,7 +168,16 @@ export interface GarminWebhookItem {
167
168
  connectionId: string;
168
169
  /** The host app's user ID. */
169
170
  userId: string;
170
- /** The transformed data in Soma's normalized format, or null for terminal events like deregistration. */
171
+ /**
172
+ * The data for this item.
173
+ *
174
+ * - In the default mode, this is the data in Soma's normalized format
175
+ * (the result of `transformX(rawItem)`).
176
+ * - When the event was registered with `rawPassthrough: true`, this is the
177
+ * raw Garmin item exactly as received (post zod validation), so the host
178
+ * app can act on the provider's native shape.
179
+ * - `null` for terminal events like deregistration.
180
+ */
171
181
  data: Record<string, unknown> | null;
172
182
  }
173
183
 
@@ -234,7 +244,12 @@ export type StravaWebhookActionArgs = {
234
244
  };
235
245
  clientId: string;
236
246
  clientSecret: string;
237
- autoIngest?: boolean;
247
+ /**
248
+ * Per-event override of the auto-ingest flag. The dispatcher resolves the
249
+ * event name from the payload and looks up the flag here. Defaults to `true`
250
+ * for any event missing from the map.
251
+ */
252
+ autoIngestByEvent?: Partial<Record<StravaWebhookEventName, boolean>>;
238
253
  };
239
254
 
240
255
  /** Result returned by the Strava webhook handler action inside the component. */
@@ -313,21 +328,48 @@ export interface GarminOAuthOptions {
313
328
  ) => Promise<void>;
314
329
  }
315
330
 
331
+ /**
332
+ * Configuration for a single Garmin webhook data type.
333
+ *
334
+ * Pass `true` as a shortcut for "register the route with default processing
335
+ * and `autoIngest: true`". Pass an object to customize per-type behavior.
336
+ */
337
+ export type GarminWebhookEventConfig =
338
+ | true
339
+ | {
340
+ /** Custom logic to run after the payload is processed. */
341
+ handler?: GarminWebhookHandler;
342
+ /**
343
+ * Whether the component should auto-write the transformed data to the
344
+ * Soma database for this data type. When `false`, the payload is still
345
+ * validated and transformed (and surfaced to `handler` / `onEvent`), but
346
+ * the DB write is skipped so the host app can ingest it itself.
347
+ *
348
+ * Cannot be `true` simultaneously with `rawPassthrough: true` — raw
349
+ * provider items can't be auto-ingested into Soma's normalized schema.
350
+ *
351
+ * @default true
352
+ */
353
+ autoIngest?: boolean;
354
+ /**
355
+ * When `true`, skip Soma's `transformX(rawItem)` step and surface the
356
+ * raw Garmin item directly in `items[].data`. Soma still parses the
357
+ * payload with zod and resolves Garmin `userId` → Soma
358
+ * `connectionId`/`userId`, so the host app gets raw Garmin data already
359
+ * keyed to its own users.
360
+ *
361
+ * Implies `autoIngest: false` — raw items cannot be auto-ingested into
362
+ * Soma's normalized schema. Setting `autoIngest: true` alongside
363
+ * `rawPassthrough: true` throws at route registration time.
364
+ *
365
+ * @default false
366
+ */
367
+ rawPassthrough?: boolean;
368
+ };
369
+
316
370
  export interface GarminWebhookOptions {
317
371
  /** Base path prefix for all webhook routes. @default "/api/garmin/webhook" */
318
372
  basePath?: string;
319
- /**
320
- * Whether to automatically ingest (upsert) transformed data into the Soma
321
- * database when a webhook payload is received.
322
- *
323
- * When `true` (default), incoming data is validated, transformed, and written
324
- * to the database automatically. When `false`, the webhook still receives and
325
- * validates the payload, but skips the database write — useful when you want
326
- * to handle ingestion yourself via the `onEvent` / per-type callbacks.
327
- *
328
- * @default true
329
- */
330
- autoIngest?: boolean;
331
373
  /** Called after every webhook payload is processed, regardless of data type. */
332
374
  onEvent?: GarminWebhookHandler;
333
375
  /**
@@ -336,21 +378,44 @@ export interface GarminWebhookOptions {
336
378
  * **Only data types listed here get an HTTP route registered.**
337
379
  * Unlisted types are ignored — Garmin receives a 404 if it POSTs to them.
338
380
  *
339
- * Pass a handler function to run custom logic after ingestion,
340
- * or `true` to register the route with default processing only.
381
+ * Pass `true` as a shortcut for default processing with auto-ingest, or
382
+ * an object to attach a handler and/or override `autoIngest`.
341
383
  *
342
384
  * @example
343
385
  * ```ts
344
386
  * events: {
345
- * "activities": async (ctx, event) => { // custom side-effect },
346
- * "sleeps": true, // register route, default processing only
347
- * "dailies": true,
387
+ * activities: { handler: async (ctx, event) => { /* side-effect *\/ } },
388
+ * sleeps: true, // default processing
389
+ * dailies: { autoIngest: false }, // skip DB write
390
+ * "body-compositions": { autoIngest: false, handler: customHandler },
348
391
  * }
349
392
  * ```
350
393
  */
351
- events?: Partial<Record<GarminWebhookEventName, GarminWebhookHandler | true>>;
394
+ events?: Partial<Record<GarminWebhookEventName, GarminWebhookEventConfig>>;
352
395
  }
353
396
 
397
+ /**
398
+ * Configuration for a single Strava webhook event.
399
+ *
400
+ * Pass `true` as a shortcut for "process this event with default behavior and
401
+ * `autoIngest: true`". Pass an object to customize per-event behavior.
402
+ */
403
+ export type StravaWebhookEventConfig =
404
+ | true
405
+ | {
406
+ /** Custom logic to run after the event is processed. */
407
+ handler?: StravaWebhookHandler;
408
+ /**
409
+ * Whether the component should auto-write the transformed data to the
410
+ * Soma database for this event. When `false`, the payload is still
411
+ * fetched and transformed (and surfaced to `handler` / `onEvent`), but
412
+ * the DB write is skipped so the host app can ingest it itself.
413
+ *
414
+ * @default true
415
+ */
416
+ autoIngest?: boolean;
417
+ };
418
+
354
419
  export interface StravaWebhookOptions {
355
420
  /** HTTP path for the webhook endpoint. @default "/api/strava/webhook" */
356
421
  path?: string;
@@ -360,17 +425,6 @@ export interface StravaWebhookOptions {
360
425
  * Falls back to `STRAVA_WEBHOOK_VERIFY_TOKEN` env var.
361
426
  */
362
427
  verifyToken?: string;
363
- /**
364
- * Whether to automatically ingest transformed data into the Soma database.
365
- *
366
- * When `true` (default), fetched data is transformed and written to the
367
- * database automatically. When `false`, the webhook still fetches and
368
- * transforms the data, but skips the database write — useful when you want
369
- * to handle ingestion yourself via the `onEvent` / per-event callbacks.
370
- *
371
- * @default true
372
- */
373
- autoIngest?: boolean;
374
428
  /** Called after every webhook event is processed, regardless of event type. */
375
429
  onEvent?: StravaWebhookHandler;
376
430
  /**
@@ -380,20 +434,42 @@ export interface StravaWebhookOptions {
380
434
  * events to a single endpoint. **Only event types listed here are processed.**
381
435
  * Unlisted event types are silently ignored (200 returned, no processing).
382
436
  *
383
- * Pass a handler function for custom logic after processing,
384
- * or `true` to enable default processing for that event type.
437
+ * Pass `true` as a shortcut for default processing with auto-ingest, or an
438
+ * object to attach a handler and/or override `autoIngest`.
385
439
  *
386
440
  * @example
387
441
  * ```ts
388
442
  * events: {
389
- * "activity-create": async (ctx, event) => { // custom side-effect },
390
- * "activity-update": true, // default processing only
391
- * "athlete-update": true,
443
+ * "activity-create": { handler: async (ctx, event) => { /* side-effect *\/ } },
444
+ * "activity-update": true, // default processing
445
+ * "athlete-update": { autoIngest: false }, // skip DB write
392
446
  * "athlete-deauthorize": true,
393
447
  * }
394
448
  * ```
395
449
  */
396
- events?: Partial<Record<StravaWebhookEventName, StravaWebhookHandler | true>>;
450
+ events?: Partial<Record<StravaWebhookEventName, StravaWebhookEventConfig>>;
451
+ }
452
+
453
+ /**
454
+ * Narrow a `GarminWebhookEventConfig` / `StravaWebhookEventConfig` value into
455
+ * its resolved `{ handler, autoIngest, rawPassthrough }` triple. `true`
456
+ * becomes a no-handler config with auto-ingest enabled and no passthrough;
457
+ * the object form fills in defaults.
458
+ *
459
+ * `rawPassthrough` is a Garmin-only concept today. Strava call-sites supply
460
+ * entries without the field, which resolves to `false` — a safe default.
461
+ */
462
+ export function resolveEventConfig<H>(
463
+ entry: true | { handler?: H; autoIngest?: boolean; rawPassthrough?: boolean } | undefined,
464
+ ): { handler: H | undefined; autoIngest: boolean; rawPassthrough: boolean } {
465
+ if (entry === undefined || entry === true) {
466
+ return { handler: undefined, autoIngest: true, rawPassthrough: false };
467
+ }
468
+ return {
469
+ handler: entry.handler,
470
+ autoIngest: entry.autoIngest ?? true,
471
+ rawPassthrough: entry.rawPassthrough ?? false,
472
+ };
397
473
  }
398
474
 
399
475
  export interface RegisterRoutesOptions {
@@ -296,126 +296,126 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
296
296
  handleGarminWebhookActivities: FunctionReference<
297
297
  "action",
298
298
  "internal",
299
- { autoIngest?: boolean; payload: any },
299
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
300
300
  any,
301
301
  Name
302
302
  >;
303
303
  handleGarminWebhookActivityDetails: FunctionReference<
304
304
  "action",
305
305
  "internal",
306
- { autoIngest?: boolean; payload: any },
306
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
307
307
  any,
308
308
  Name
309
309
  >;
310
310
  handleGarminWebhookBloodPressures: FunctionReference<
311
311
  "action",
312
312
  "internal",
313
- { autoIngest?: boolean; payload: any },
313
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
314
314
  any,
315
315
  Name
316
316
  >;
317
317
  handleGarminWebhookBodyCompositions: FunctionReference<
318
318
  "action",
319
319
  "internal",
320
- { autoIngest?: boolean; payload: any },
320
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
321
321
  any,
322
322
  Name
323
323
  >;
324
324
  handleGarminWebhookDailies: FunctionReference<
325
325
  "action",
326
326
  "internal",
327
- { autoIngest?: boolean; payload: any },
327
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
328
328
  any,
329
329
  Name
330
330
  >;
331
331
  handleGarminWebhookDeregistration: FunctionReference<
332
332
  "action",
333
333
  "internal",
334
- { autoIngest?: boolean; payload: any },
334
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
335
335
  any,
336
336
  Name
337
337
  >;
338
338
  handleGarminWebhookEpochs: FunctionReference<
339
339
  "action",
340
340
  "internal",
341
- { autoIngest?: boolean; payload: any },
341
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
342
342
  any,
343
343
  Name
344
344
  >;
345
345
  handleGarminWebhookHealthSnapshot: FunctionReference<
346
346
  "action",
347
347
  "internal",
348
- { autoIngest?: boolean; payload: any },
348
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
349
349
  any,
350
350
  Name
351
351
  >;
352
352
  handleGarminWebhookHRVSummary: FunctionReference<
353
353
  "action",
354
354
  "internal",
355
- { autoIngest?: boolean; payload: any },
355
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
356
356
  any,
357
357
  Name
358
358
  >;
359
359
  handleGarminWebhookManuallyUpdatedActivities: FunctionReference<
360
360
  "action",
361
361
  "internal",
362
- { autoIngest?: boolean; payload: any },
362
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
363
363
  any,
364
364
  Name
365
365
  >;
366
366
  handleGarminWebhookMenstrualCycleTracking: FunctionReference<
367
367
  "action",
368
368
  "internal",
369
- { autoIngest?: boolean; payload: any },
369
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
370
370
  any,
371
371
  Name
372
372
  >;
373
373
  handleGarminWebhookMoveIQ: FunctionReference<
374
374
  "action",
375
375
  "internal",
376
- { autoIngest?: boolean; payload: any },
376
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
377
377
  any,
378
378
  Name
379
379
  >;
380
380
  handleGarminWebhookPulseOx: FunctionReference<
381
381
  "action",
382
382
  "internal",
383
- { autoIngest?: boolean; payload: any },
383
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
384
384
  any,
385
385
  Name
386
386
  >;
387
387
  handleGarminWebhookRespiration: FunctionReference<
388
388
  "action",
389
389
  "internal",
390
- { autoIngest?: boolean; payload: any },
390
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
391
391
  any,
392
392
  Name
393
393
  >;
394
394
  handleGarminWebhookSkinTemp: FunctionReference<
395
395
  "action",
396
396
  "internal",
397
- { autoIngest?: boolean; payload: any },
397
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
398
398
  any,
399
399
  Name
400
400
  >;
401
401
  handleGarminWebhookSleeps: FunctionReference<
402
402
  "action",
403
403
  "internal",
404
- { autoIngest?: boolean; payload: any },
404
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
405
405
  any,
406
406
  Name
407
407
  >;
408
408
  handleGarminWebhookStress: FunctionReference<
409
409
  "action",
410
410
  "internal",
411
- { autoIngest?: boolean; payload: any },
411
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
412
412
  any,
413
413
  Name
414
414
  >;
415
415
  handleGarminWebhookUserMetrics: FunctionReference<
416
416
  "action",
417
417
  "internal",
418
- { autoIngest?: boolean; payload: any },
418
+ { autoIngest?: boolean; payload: any; rawPassthrough?: boolean },
419
419
  any,
420
420
  Name
421
421
  >;
@@ -2371,7 +2371,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
2371
2371
  "action",
2372
2372
  "internal",
2373
2373
  {
2374
- autoIngest?: boolean;
2374
+ autoIngestByEvent?: Record<string, boolean>;
2375
2375
  clientId: string;
2376
2376
  clientSecret: string;
2377
2377
  payload: any;