@spacelr/sdk 0.17.0 → 0.18.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.
package/dist/index.mjs CHANGED
@@ -2429,6 +2429,45 @@ var AuthModule = class _AuthModule {
2429
2429
  query: { token: linkToken }
2430
2430
  });
2431
2431
  }
2432
+ /**
2433
+ * Long-poll a device link once (new device B). UNAUTHENTICATED. The server
2434
+ * holds the request until the link leaves `pending` or ~25s pass, then returns
2435
+ * the current state — a returned `pending` just means "call again". Prefer
2436
+ * {@link waitForDeviceLinkApproval}, which loops for you.
2437
+ */
2438
+ async waitDeviceLinkStatus(linkToken, options = {}) {
2439
+ return this.http.request({
2440
+ method: "GET",
2441
+ path: "/auth/device-link/wait",
2442
+ query: { token: linkToken },
2443
+ signal: options.signal
2444
+ });
2445
+ }
2446
+ /**
2447
+ * Wait until the signed-in device decides (new device B). UNAUTHENTICATED.
2448
+ * Replaces a hand-written poll loop on {@link pollDeviceLinkStatus}: resolves
2449
+ * with the first non-`pending` state (`approved` → continue with
2450
+ * {@link getDeviceLinkOptions}; `rejected` → start over). Rejects when the link
2451
+ * expires (404 after its ~5 min TTL), on a throttle (429), or when `signal`
2452
+ * aborts. A client-side request timeout shorter than the server's ~25s hold is
2453
+ * treated as one more `pending` round, not as a failure.
2454
+ */
2455
+ async waitForDeviceLinkApproval(linkToken, options = {}) {
2456
+ const minRoundMs = 1e3;
2457
+ for (; ; ) {
2458
+ const startedAt = Date.now();
2459
+ try {
2460
+ const status = await this.waitDeviceLinkStatus(linkToken, options);
2461
+ if (status.state !== "pending") return status;
2462
+ } catch (error) {
2463
+ if (!(error instanceof SpacelrTimeoutError)) throw error;
2464
+ }
2465
+ const remainingMs = minRoundMs - (Date.now() - startedAt);
2466
+ if (remainingMs > 0) {
2467
+ await new Promise((resolve) => setTimeout(resolve, remainingMs));
2468
+ }
2469
+ }
2470
+ }
2432
2471
  /**
2433
2472
  * Fetch WebAuthn registration options to complete the device link (new
2434
2473
  * device B). UNAUTHENTICATED — requires BOTH the displayed `linkToken` AND
@@ -2481,6 +2520,18 @@ var AuthModule = class _AuthModule {
2481
2520
  body: { code }
2482
2521
  });
2483
2522
  }
2523
+ /**
2524
+ * Reject the pending device link (signed-in device A). Authenticated. The new
2525
+ * device sees `rejected` immediately instead of waiting for the link to expire.
2526
+ */
2527
+ async rejectDeviceLink(code) {
2528
+ return this.http.request({
2529
+ method: "POST",
2530
+ path: "/auth/device-link/reject",
2531
+ authenticated: true,
2532
+ body: { code }
2533
+ });
2534
+ }
2484
2535
  async storeTokensFromRegister(response) {
2485
2536
  if (!response.access_token) return;
2486
2537
  const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;