@pollar/core 0.7.0 → 0.7.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.
package/dist/index.mjs CHANGED
@@ -440,7 +440,7 @@ function createClient(clientOptions) {
440
440
  const {
441
441
  baseUrl: localBaseUrl,
442
442
  fetch: fetch2 = baseFetch,
443
- Request = CustomRequest,
443
+ Request: Request2 = CustomRequest,
444
444
  headers,
445
445
  params = {},
446
446
  parseAs = "json",
@@ -492,7 +492,7 @@ function createClient(clientOptions) {
492
492
  };
493
493
  let id;
494
494
  let options;
495
- let request = new Request(
495
+ let request = new Request2(
496
496
  createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer, pathSerializer }),
497
497
  requestInit
498
498
  );
@@ -522,7 +522,7 @@ function createClient(clientOptions) {
522
522
  id
523
523
  });
524
524
  if (result) {
525
- if (result instanceof Request) {
525
+ if (result instanceof Request2) {
526
526
  request = result;
527
527
  } else if (result instanceof Response) {
528
528
  response = result;
@@ -894,6 +894,22 @@ function createApiClient(baseUrl) {
894
894
  return createClient({ baseUrl });
895
895
  }
896
896
 
897
+ // src/api/endpoints/distribution.ts
898
+ async function listDistributionRules(api) {
899
+ const { data, error } = await api.GET("/distribution/rules");
900
+ if (!data?.content || error) {
901
+ throw new Error(error?.error ?? "Failed to list distribution rules");
902
+ }
903
+ return data.content.rules;
904
+ }
905
+ async function claimDistributionRule(api, body) {
906
+ const { data, error } = await api.POST("/distribution/claim", { body });
907
+ if (!data?.content || error) {
908
+ throw new Error(error?.error ?? "Failed to claim distribution rule");
909
+ }
910
+ return data.content;
911
+ }
912
+
897
913
  // src/api/endpoints/kyc.ts
898
914
  async function getKycStatus(api, providerId) {
899
915
  const { data, error } = await api.GET("/kyc/status", {
@@ -1740,7 +1756,6 @@ async function loginWallet(type, deps) {
1740
1756
 
1741
1757
  // src/client/client.ts
1742
1758
  var isBrowser = typeof window !== "undefined" && typeof localStorage !== "undefined";
1743
- var RETRIED_HEADER = "X-Pollar-Retried";
1744
1759
  function warnServerSide(method) {
1745
1760
  console.warn(
1746
1761
  `[PollarClient] ${method}() called server-side \u2014 browser APIs unavailable. Use PollarClient only in Client Components.`
@@ -1758,6 +1773,13 @@ var PollarClient = class {
1758
1773
  this._profile = null;
1759
1774
  /** Last `DPoP-Nonce` we saw from a server response. Carried into the next proof. */
1760
1775
  this._dpopNonce = null;
1776
+ /**
1777
+ * Snapshot of each in-flight request's body, taken in `onRequest` before
1778
+ * `fetch()` consumes the stream. Needed because `Request.clone()` throws
1779
+ * once the body is disturbed, so the auto-retry path (DPoP nonce challenge
1780
+ * / 401 refresh) must rebuild the request from scratch instead of cloning.
1781
+ */
1782
+ this._requestBodyCache = /* @__PURE__ */ new WeakMap();
1761
1783
  /** Singleton in-flight refresh — concurrent 401s coalesce into one /auth/refresh call. */
1762
1784
  this._refreshPromise = null;
1763
1785
  this._storageEventHandler = null;
@@ -1842,6 +1864,13 @@ var PollarClient = class {
1842
1864
  onRequest: async ({ request }) => {
1843
1865
  request.headers.set("x-pollar-api-key", self.apiKey);
1844
1866
  await self._initialized;
1867
+ if (request.body !== null) {
1868
+ try {
1869
+ self._requestBodyCache.set(request, await request.clone().arrayBuffer());
1870
+ } catch (err) {
1871
+ console.warn("[PollarClient] Could not snapshot request body for retry", err);
1872
+ }
1873
+ }
1845
1874
  const isRefresh = request.url.includes("/auth/refresh");
1846
1875
  if (!isRefresh && self._refreshPromise) await self._refreshPromise;
1847
1876
  if (isRefresh) {
@@ -1864,7 +1893,6 @@ var PollarClient = class {
1864
1893
  const newNonce = response.headers.get("DPoP-Nonce");
1865
1894
  if (newNonce) self._dpopNonce = newNonce;
1866
1895
  if (response.status !== 401) return response;
1867
- if (request.headers.get(RETRIED_HEADER)) return response;
1868
1896
  if (request.url.includes("/auth/refresh")) return response;
1869
1897
  const wwwAuth = response.headers.get("WWW-Authenticate") ?? "";
1870
1898
  const isNonceChallenge = wwwAuth.includes("use_dpop_nonce");
@@ -1897,19 +1925,29 @@ var PollarClient = class {
1897
1925
  }
1898
1926
  }
1899
1927
  async _retryRequest(originalRequest) {
1900
- const clone = originalRequest.clone();
1901
- clone.headers.set(RETRIED_HEADER, "1");
1928
+ const headers = new Headers(originalRequest.headers);
1902
1929
  const accessToken = this._session?.token?.accessToken;
1903
1930
  if (accessToken) {
1904
- const proof = await this._buildProofForRequest(clone, accessToken);
1931
+ const proof = await this._buildProofForRequest(originalRequest, accessToken);
1905
1932
  if (proof) {
1906
- clone.headers.set("Authorization", `DPoP ${accessToken}`);
1907
- clone.headers.set("DPoP", proof);
1933
+ headers.set("Authorization", `DPoP ${accessToken}`);
1934
+ headers.set("DPoP", proof);
1908
1935
  } else {
1909
- clone.headers.set("Authorization", `Bearer ${accessToken}`);
1936
+ headers.set("Authorization", `Bearer ${accessToken}`);
1910
1937
  }
1911
1938
  }
1912
- return fetch(clone);
1939
+ const cachedBody = this._requestBodyCache.get(originalRequest);
1940
+ const retried = new Request(originalRequest.url, {
1941
+ method: originalRequest.method,
1942
+ headers,
1943
+ body: cachedBody ?? null,
1944
+ credentials: originalRequest.credentials,
1945
+ mode: originalRequest.mode,
1946
+ redirect: originalRequest.redirect,
1947
+ referrer: originalRequest.referrer,
1948
+ integrity: originalRequest.integrity
1949
+ });
1950
+ return fetch(retried);
1913
1951
  }
1914
1952
  // ─── Refresh (race-safe singleton) ───────────────────────────────────────
1915
1953
  /**
@@ -2234,7 +2272,8 @@ var PollarClient = class {
2234
2272
  const details = error?.details;
2235
2273
  this._setTransactionState({ step: "error", ...details && { details } });
2236
2274
  }
2237
- } catch {
2275
+ } catch (err) {
2276
+ console.error("[PollarClient] buildTx failed", err);
2238
2277
  this._setTransactionState({ step: "error" });
2239
2278
  }
2240
2279
  }
@@ -2322,6 +2361,13 @@ var PollarClient = class {
2322
2361
  pollRampTransaction(txId, opts) {
2323
2362
  return pollRampTransaction(this._api, txId, opts);
2324
2363
  }
2364
+ // ─── Distribution ─────────────────────────────────────────────────────────
2365
+ listDistributionRules() {
2366
+ return listDistributionRules(this._api);
2367
+ }
2368
+ claimDistributionRule(body) {
2369
+ return claimDistributionRule(this._api, body);
2370
+ }
2325
2371
  _setTxHistoryState(next) {
2326
2372
  this._txHistoryState = next;
2327
2373
  for (const cb of this._txHistoryStateListeners) cb(next);
@@ -2461,6 +2507,6 @@ var PollarClient = class {
2461
2507
  // src/index.ts
2462
2508
  _setDefaultKeyManagerFactory((_storage, apiKey) => new WebCryptoKeyManager(apiKey));
2463
2509
 
2464
- export { AUTH_ERROR_CODES, AlbedoAdapter, FreighterAdapter, PollarClient, StellarClient, WalletType, WebCryptoKeyManager, buildProof, canonicalEcJwk, computeJwkThumbprint, createLocalStorageAdapter, createMemoryAdapter, createOffRamp, createOnRamp, defaultKeyManager, defaultStorage, getKycProviders, getKycStatus, getRampTransaction, getRampsQuote, isValidSession, normalizeHtu, pollKycStatus, pollRampTransaction, resolveKyc, startKyc };
2510
+ export { AUTH_ERROR_CODES, AlbedoAdapter, FreighterAdapter, PollarClient, StellarClient, WalletType, WebCryptoKeyManager, buildProof, canonicalEcJwk, claimDistributionRule, computeJwkThumbprint, createLocalStorageAdapter, createMemoryAdapter, createOffRamp, createOnRamp, defaultKeyManager, defaultStorage, getKycProviders, getKycStatus, getRampTransaction, getRampsQuote, isValidSession, listDistributionRules, normalizeHtu, pollKycStatus, pollRampTransaction, resolveKyc, startKyc };
2465
2511
  //# sourceMappingURL=index.mjs.map
2466
2512
  //# sourceMappingURL=index.mjs.map