@insforge/sdk 1.3.2-razorpay.1 → 1.4.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/ssr.mjs CHANGED
@@ -397,7 +397,7 @@ var HttpClient = class {
397
397
  return Math.round(jitter);
398
398
  }
399
399
  shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
400
- return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
400
+ return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.accessToken && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
401
401
  }
402
402
  async fetchWithRetry(args) {
403
403
  const {
@@ -1609,7 +1609,7 @@ var StorageBucket = class {
1609
1609
  size: file.size,
1610
1610
  mimeType: file.type || "application/octet-stream",
1611
1611
  uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
1612
- url: this.getPublicUrl(strategy.key)
1612
+ url: this.getPublicUrl(strategy.key).data.publicUrl
1613
1613
  },
1614
1614
  error: null
1615
1615
  };
@@ -1681,11 +1681,101 @@ var StorageBucket = class {
1681
1681
  }
1682
1682
  }
1683
1683
  /**
1684
- * Get public URL for a file
1684
+ * Get the public URL for an object in a public bucket.
1685
+ *
1686
+ * Pure string construction — no network call, no auth. The URL only resolves
1687
+ * if the bucket is public; for private objects use {@link createSignedUrl}.
1688
+ *
1685
1689
  * @param path - The object key/path
1690
+ * @returns `{ data: { publicUrl }, error }` — matches the external SDK pattern,
1691
+ * so `const { data } = getPublicUrl(path)` then `data.publicUrl`.
1686
1692
  */
1687
1693
  getPublicUrl(path) {
1688
- return `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
1694
+ const publicUrl = `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
1695
+ return { data: { publicUrl }, error: null };
1696
+ }
1697
+ /**
1698
+ * Resolve a download strategy (signed or direct URL) for an object with a
1699
+ * caller-supplied TTL. Prefers the canonical GET route and falls back to the
1700
+ * legacy POST alias so signed-URL creation still works against older backends
1701
+ * that predate the GET route (they return 404/405 for it). A genuine
1702
+ * "object not found" (STORAGE_NOT_FOUND) is not retried.
1703
+ */
1704
+ async requestDownloadStrategy(path, expiresIn) {
1705
+ const encoded = encodeURIComponent(path);
1706
+ try {
1707
+ return await this.http.get(
1708
+ `/api/storage/buckets/${this.bucketName}/download-strategy/objects/${encoded}`,
1709
+ { params: { expiresIn: expiresIn.toString() } }
1710
+ );
1711
+ } catch (error) {
1712
+ const status = error instanceof InsForgeError ? error.statusCode : void 0;
1713
+ const isMissingRoute = (status === 404 || status === 405) && !(error instanceof InsForgeError && error.error === "STORAGE_NOT_FOUND");
1714
+ if (!isMissingRoute) throw error;
1715
+ return await this.http.post(
1716
+ `/api/storage/buckets/${this.bucketName}/objects/${encoded}/download-strategy`,
1717
+ { expiresIn }
1718
+ );
1719
+ }
1720
+ }
1721
+ /**
1722
+ * Create a signed URL for an object.
1723
+ *
1724
+ * Returns a time-limited, credential-free URL that can be handed directly to
1725
+ * a browser (`<img src>`), an email, or a third party — no SDK or session is
1726
+ * needed to fetch it. Authorization is enforced when the URL is minted (the
1727
+ * caller must be allowed to read the object), so the resulting link is a
1728
+ * pre-authorized capability scoped to this one object until it expires.
1729
+ *
1730
+ * @param path - The object key/path
1731
+ * @param expiresIn - Lifetime in seconds (default 3600 = 1h, max 604800 = 7d).
1732
+ * Honored for private buckets; public buckets return their long-lived URL.
1733
+ */
1734
+ async createSignedUrl(path, expiresIn = 3600) {
1735
+ try {
1736
+ const strategy = await this.requestDownloadStrategy(path, expiresIn);
1737
+ return {
1738
+ data: {
1739
+ signedUrl: strategy.url,
1740
+ expiresAt: strategy.expiresAt ? new Date(strategy.expiresAt).toISOString() : null
1741
+ },
1742
+ error: null
1743
+ };
1744
+ } catch (error) {
1745
+ return {
1746
+ data: null,
1747
+ error: error instanceof InsForgeError ? error : new InsForgeError("Failed to create signed URL", 500, "STORAGE_ERROR")
1748
+ };
1749
+ }
1750
+ }
1751
+ /**
1752
+ * Create signed URLs for multiple objects in a single call.
1753
+ *
1754
+ * Each entry resolves independently: a failure on one key (not found / not
1755
+ * permitted) is reported on that entry's `error` without failing the rest.
1756
+ *
1757
+ * @param paths - The object keys/paths
1758
+ * @param expiresIn - Lifetime in seconds (default 3600 = 1h, max 604800 = 7d)
1759
+ */
1760
+ async createSignedUrls(paths, expiresIn = 3600) {
1761
+ try {
1762
+ const data = await Promise.all(
1763
+ paths.map(async (path) => {
1764
+ const { data: signed, error } = await this.createSignedUrl(path, expiresIn);
1765
+ return {
1766
+ path,
1767
+ signedUrl: signed?.signedUrl ?? null,
1768
+ error: error ? error.message : null
1769
+ };
1770
+ })
1771
+ );
1772
+ return { data, error: null };
1773
+ } catch (error) {
1774
+ return {
1775
+ data: null,
1776
+ error: error instanceof InsForgeError ? error : new InsForgeError("Failed to create signed URLs", 500, "STORAGE_ERROR")
1777
+ };
1778
+ }
1689
1779
  }
1690
1780
  /**
1691
1781
  * List objects in the bucket
@@ -2664,12 +2754,13 @@ var InsForgeClient = class {
2664
2754
  const logger = new Logger(config.debug);
2665
2755
  this.tokenManager = new TokenManager();
2666
2756
  this.http = new HttpClient(config, this.tokenManager, logger);
2667
- if (config.edgeFunctionToken) {
2668
- this.http.setAuthToken(config.edgeFunctionToken);
2669
- this.tokenManager.setAccessToken(config.edgeFunctionToken);
2757
+ const accessToken = config.accessToken ?? config.edgeFunctionToken;
2758
+ if (accessToken) {
2759
+ this.http.setAuthToken(accessToken);
2760
+ this.tokenManager.setAccessToken(accessToken);
2670
2761
  }
2671
2762
  this.auth = new Auth(this.http, this.tokenManager, {
2672
- isServerMode: config.isServerMode ?? !!config.edgeFunctionToken
2763
+ isServerMode: config.isServerMode ?? !!accessToken
2673
2764
  });
2674
2765
  this.database = new Database(this.http);
2675
2766
  this.storage = new Storage(this.http);
@@ -3075,7 +3166,10 @@ function createBrowserClient(options = {}) {
3075
3166
  ...options,
3076
3167
  baseUrl,
3077
3168
  anonKey,
3078
- fetch: ssrFetch
3169
+ fetch: ssrFetch,
3170
+ // Browser clients manage tokens via the refresh route, not a static
3171
+ // config token; shadow any untyped accessToken in the options spread.
3172
+ accessToken: void 0
3079
3173
  });
3080
3174
  const setAccessToken = client.setAccessToken.bind(client);
3081
3175
  client.setAccessToken = (token) => {
@@ -3113,7 +3207,10 @@ function createServerClient(options = {}) {
3113
3207
  baseUrl,
3114
3208
  anonKey,
3115
3209
  isServerMode: true,
3116
- edgeFunctionToken: accessToken ?? void 0
3210
+ accessToken: accessToken ?? void 0,
3211
+ // The cookie/option token is the only credential source here; shadow any
3212
+ // untyped edgeFunctionToken smuggled through the options spread.
3213
+ edgeFunctionToken: void 0
3117
3214
  });
3118
3215
  }
3119
3216