@withgateway/sdk 0.1.7 → 0.1.8

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.
package/dist/index.mjs CHANGED
@@ -2469,6 +2469,58 @@ var UsersClient = class _UsersClient {
2469
2469
  await this.requestJson("POST", "/api/public/user-profiles", body)
2470
2470
  );
2471
2471
  }
2472
+ /**
2473
+ * Record a VALUE EVENT for a user — the per-user ROI stream. `value` is
2474
+ * whatever the event is worth in your terms (minutes saved, revenue…);
2475
+ * Gateway sums it in the hours-saved/ROI rollups, and evals and task-set
2476
+ * replays can assert against the events. Link `traceId`/`sessionId` to tie
2477
+ * the event to the run that produced it.
2478
+ *
2479
+ * @example
2480
+ * ```ts
2481
+ * await client.trackEvent("user-123", "checkout_completed", {
2482
+ * value: 25,
2483
+ * metadata: { cartSize: 3 },
2484
+ * sessionId,
2485
+ * });
2486
+ * ```
2487
+ */
2488
+ async trackEvent(userId, event, options = {}) {
2489
+ const body = {
2490
+ userId,
2491
+ event,
2492
+ metadata: options.metadata ?? {},
2493
+ ...options.value !== void 0 ? { value: options.value } : {},
2494
+ ...options.traceId !== void 0 ? { traceId: options.traceId } : {},
2495
+ ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}
2496
+ };
2497
+ return parseEvent(
2498
+ await this.requestJson("POST", "/api/public/user-events", body)
2499
+ );
2500
+ }
2501
+ /** List a project's user events, optionally filtered by user/event/time. */
2502
+ async listEvents(filter = {}) {
2503
+ const params = new URLSearchParams();
2504
+ if (filter.userId) params.set("userId", filter.userId);
2505
+ if (filter.event) params.set("event", filter.event);
2506
+ if (filter.fromTimestamp)
2507
+ params.set("fromTimestamp", filter.fromTimestamp.toISOString());
2508
+ if (filter.toTimestamp)
2509
+ params.set("toTimestamp", filter.toTimestamp.toISOString());
2510
+ if (filter.page) params.set("page", String(filter.page));
2511
+ if (filter.limit) params.set("limit", String(filter.limit));
2512
+ const qs = params.toString();
2513
+ const payload = await this.requestJson(
2514
+ "GET",
2515
+ `/api/public/user-events${qs ? `?${qs}` : ""}`
2516
+ );
2517
+ const rows = Array.isArray(payload.data) ? payload.data : [];
2518
+ const meta = payload.meta && typeof payload.meta === "object" ? payload.meta : {};
2519
+ return {
2520
+ data: rows.map((row) => parseEvent(row)),
2521
+ totalItems: Number(meta.totalItems ?? rows.length)
2522
+ };
2523
+ }
2472
2524
  async requestJson(method, path, payload) {
2473
2525
  let response;
2474
2526
  try {
@@ -2507,6 +2559,18 @@ var UsersClient = class _UsersClient {
2507
2559
  }
2508
2560
  }
2509
2561
  };
2562
+ function parseEvent(payload) {
2563
+ return {
2564
+ id: String(payload.id ?? ""),
2565
+ userId: String(payload.userId ?? ""),
2566
+ event: String(payload.event ?? ""),
2567
+ value: typeof payload.value === "number" ? payload.value : null,
2568
+ metadata: payload.metadata && typeof payload.metadata === "object" && !Array.isArray(payload.metadata) ? payload.metadata : {},
2569
+ traceId: typeof payload.traceId === "string" ? payload.traceId : null,
2570
+ sessionId: typeof payload.sessionId === "string" ? payload.sessionId : null,
2571
+ createdAt: typeof payload.createdAt === "string" ? new Date(payload.createdAt) : /* @__PURE__ */ new Date(0)
2572
+ };
2573
+ }
2510
2574
  function parseProfile(payload) {
2511
2575
  return {
2512
2576
  userId: String(payload.userId ?? ""),