@insforge/sdk 1.4.0 → 1.4.2

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.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { I as InsForgeClient, a as InsForgeConfig, b as InsForgeAdminConfig } from './client-CqfpCc8Z.js';
2
- export { i as AI, c as ApiError, f as Auth, A as AuthSession, C as ConnectionState, D as Database, E as Emails, l as EventCallback, j as FunctionInvokeOptions, F as Functions, H as HttpClient, e as InsForgeError, d as InsForgeErrorCode, L as Logger, P as Payments, k as PaymentsResponse, R as Realtime, S as Storage, g as StorageBucket, h as StorageResponse, T as TokenManager } from './client-CqfpCc8Z.js';
1
+ import { I as InsForgeClient } from './client-BR9o-WUm.js';
2
+ export { c as AI, A as Auth, C as ConnectionState, D as Database, E as Emails, f as EventCallback, d as FunctionInvokeOptions, F as Functions, H as HttpClient, L as Logger, P as Payments, e as PaymentsResponse, R as Realtime, S as Storage, a as StorageBucket, b as StorageResponse, T as TokenManager } from './client-BR9o-WUm.js';
3
+ import { I as InsForgeConfig, a as InsForgeAdminConfig } from './types-Dk-44JJf.js';
4
+ export { b as ApiError, A as AuthSession, d as InsForgeError, c as InsForgeErrorCode } from './types-Dk-44JJf.js';
3
5
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
4
6
  import '@supabase/postgrest-js';
5
7
 
package/dist/index.js CHANGED
@@ -28,8 +28,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
33
  AI: () => AI,
34
34
  Auth: () => Auth,
35
35
  Database: () => Database,
@@ -46,9 +46,9 @@ __export(index_exports, {
46
46
  TokenManager: () => TokenManager,
47
47
  createAdminClient: () => createAdminClient,
48
48
  createClient: () => createClient,
49
- default: () => index_default
49
+ default: () => src_default
50
50
  });
51
- module.exports = __toCommonJS(index_exports);
51
+ module.exports = __toCommonJS(src_exports);
52
52
 
53
53
  // src/types.ts
54
54
  var InsForgeError = class _InsForgeError extends Error {
@@ -449,7 +449,7 @@ var HttpClient = class {
449
449
  return Math.round(jitter);
450
450
  }
451
451
  shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
452
- return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
452
+ return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.accessToken && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
453
453
  }
454
454
  async fetchWithRetry(args) {
455
455
  const {
@@ -885,19 +885,32 @@ var HttpClient = class {
885
885
 
886
886
  // src/modules/auth/helpers.ts
887
887
  var PKCE_VERIFIER_KEY = "insforge_pkce_verifier";
888
+ async function getWebCrypto() {
889
+ const webCrypto = globalThis.crypto;
890
+ if (typeof webCrypto?.getRandomValues === "function" && webCrypto.subtle) {
891
+ return webCrypto;
892
+ }
893
+ if (typeof process !== "undefined" && process.versions?.node) {
894
+ const { webcrypto } = await import("crypto");
895
+ return webcrypto;
896
+ }
897
+ throw new Error("Web Crypto API is not available in this environment");
898
+ }
888
899
  function base64UrlEncode(buffer) {
889
900
  const base64 = btoa(String.fromCharCode(...buffer));
890
901
  return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
891
902
  }
892
- function generateCodeVerifier() {
903
+ async function generateCodeVerifier() {
904
+ const webCrypto = await getWebCrypto();
893
905
  const array = new Uint8Array(32);
894
- crypto.getRandomValues(array);
906
+ webCrypto.getRandomValues(array);
895
907
  return base64UrlEncode(array);
896
908
  }
897
909
  async function generateCodeChallenge(verifier) {
910
+ const webCrypto = await getWebCrypto();
898
911
  const encoder = new TextEncoder();
899
912
  const data = encoder.encode(verifier);
900
- const hash = await crypto.subtle.digest("SHA-256", data);
913
+ const hash = await webCrypto.subtle.digest("SHA-256", data);
901
914
  return base64UrlEncode(new Uint8Array(hash));
902
915
  }
903
916
  function storePkceVerifier(verifier) {
@@ -944,7 +957,7 @@ var Auth = class {
944
957
  this.http = http;
945
958
  this.tokenManager = tokenManager;
946
959
  this.options = options;
947
- this.authCallbackHandled = this.detectAuthCallback();
960
+ this.authCallbackHandled = options.detectOAuthCallback === false ? Promise.resolve() : this.detectAuthCallback();
948
961
  }
949
962
  isServerMode() {
950
963
  return !!this.options.isServerMode;
@@ -1090,7 +1103,7 @@ var Auth = class {
1090
1103
  }
1091
1104
  const { provider } = signInOptions;
1092
1105
  const providerKey = encodeURIComponent(provider.toLowerCase());
1093
- const codeVerifier = generateCodeVerifier();
1106
+ const codeVerifier = await generateCodeVerifier();
1094
1107
  const codeChallenge = await generateCodeChallenge(codeVerifier);
1095
1108
  storePkceVerifier(codeVerifier);
1096
1109
  const params = {
@@ -1661,7 +1674,7 @@ var StorageBucket = class {
1661
1674
  size: file.size,
1662
1675
  mimeType: file.type || "application/octet-stream",
1663
1676
  uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
1664
- url: this.getPublicUrl(strategy.key)
1677
+ url: this.getPublicUrl(strategy.key).data.publicUrl
1665
1678
  },
1666
1679
  error: null
1667
1680
  };
@@ -1733,11 +1746,101 @@ var StorageBucket = class {
1733
1746
  }
1734
1747
  }
1735
1748
  /**
1736
- * Get public URL for a file
1749
+ * Get the public URL for an object in a public bucket.
1750
+ *
1751
+ * Pure string construction — no network call, no auth. The URL only resolves
1752
+ * if the bucket is public; for private objects use {@link createSignedUrl}.
1753
+ *
1737
1754
  * @param path - The object key/path
1755
+ * @returns `{ data: { publicUrl }, error }` — matches the external SDK pattern,
1756
+ * so `const { data } = getPublicUrl(path)` then `data.publicUrl`.
1738
1757
  */
1739
1758
  getPublicUrl(path) {
1740
- return `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
1759
+ const publicUrl = `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
1760
+ return { data: { publicUrl }, error: null };
1761
+ }
1762
+ /**
1763
+ * Resolve a download strategy (signed or direct URL) for an object with a
1764
+ * caller-supplied TTL. Prefers the canonical GET route and falls back to the
1765
+ * legacy POST alias so signed-URL creation still works against older backends
1766
+ * that predate the GET route (they return 404/405 for it). A genuine
1767
+ * "object not found" (STORAGE_NOT_FOUND) is not retried.
1768
+ */
1769
+ async requestDownloadStrategy(path, expiresIn) {
1770
+ const encoded = encodeURIComponent(path);
1771
+ try {
1772
+ return await this.http.get(
1773
+ `/api/storage/buckets/${this.bucketName}/download-strategy/objects/${encoded}`,
1774
+ { params: { expiresIn: expiresIn.toString() } }
1775
+ );
1776
+ } catch (error) {
1777
+ const status = error instanceof InsForgeError ? error.statusCode : void 0;
1778
+ const isMissingRoute = (status === 404 || status === 405) && !(error instanceof InsForgeError && error.error === "STORAGE_NOT_FOUND");
1779
+ if (!isMissingRoute) throw error;
1780
+ return await this.http.post(
1781
+ `/api/storage/buckets/${this.bucketName}/objects/${encoded}/download-strategy`,
1782
+ { expiresIn }
1783
+ );
1784
+ }
1785
+ }
1786
+ /**
1787
+ * Create a signed URL for an object.
1788
+ *
1789
+ * Returns a time-limited, credential-free URL that can be handed directly to
1790
+ * a browser (`<img src>`), an email, or a third party — no SDK or session is
1791
+ * needed to fetch it. Authorization is enforced when the URL is minted (the
1792
+ * caller must be allowed to read the object), so the resulting link is a
1793
+ * pre-authorized capability scoped to this one object until it expires.
1794
+ *
1795
+ * @param path - The object key/path
1796
+ * @param expiresIn - Lifetime in seconds (default 3600 = 1h, max 604800 = 7d).
1797
+ * Honored for private buckets; public buckets return their long-lived URL.
1798
+ */
1799
+ async createSignedUrl(path, expiresIn = 3600) {
1800
+ try {
1801
+ const strategy = await this.requestDownloadStrategy(path, expiresIn);
1802
+ return {
1803
+ data: {
1804
+ signedUrl: strategy.url,
1805
+ expiresAt: strategy.expiresAt ? new Date(strategy.expiresAt).toISOString() : null
1806
+ },
1807
+ error: null
1808
+ };
1809
+ } catch (error) {
1810
+ return {
1811
+ data: null,
1812
+ error: error instanceof InsForgeError ? error : new InsForgeError("Failed to create signed URL", 500, "STORAGE_ERROR")
1813
+ };
1814
+ }
1815
+ }
1816
+ /**
1817
+ * Create signed URLs for multiple objects in a single call.
1818
+ *
1819
+ * Each entry resolves independently: a failure on one key (not found / not
1820
+ * permitted) is reported on that entry's `error` without failing the rest.
1821
+ *
1822
+ * @param paths - The object keys/paths
1823
+ * @param expiresIn - Lifetime in seconds (default 3600 = 1h, max 604800 = 7d)
1824
+ */
1825
+ async createSignedUrls(paths, expiresIn = 3600) {
1826
+ try {
1827
+ const data = await Promise.all(
1828
+ paths.map(async (path) => {
1829
+ const { data: signed, error } = await this.createSignedUrl(path, expiresIn);
1830
+ return {
1831
+ path,
1832
+ signedUrl: signed?.signedUrl ?? null,
1833
+ error: error ? error.message : null
1834
+ };
1835
+ })
1836
+ );
1837
+ return { data, error: null };
1838
+ } catch (error) {
1839
+ return {
1840
+ data: null,
1841
+ error: error instanceof InsForgeError ? error : new InsForgeError("Failed to create signed URLs", 500, "STORAGE_ERROR")
1842
+ };
1843
+ }
1741
1844
  }
1742
1845
  /**
1743
1846
  * List objects in the bucket
@@ -2716,12 +2819,14 @@ var InsForgeClient = class {
2716
2819
  const logger = new Logger(config.debug);
2717
2820
  this.tokenManager = new TokenManager();
2718
2821
  this.http = new HttpClient(config, this.tokenManager, logger);
2719
- if (config.edgeFunctionToken) {
2720
- this.http.setAuthToken(config.edgeFunctionToken);
2721
- this.tokenManager.setAccessToken(config.edgeFunctionToken);
2822
+ const accessToken = config.accessToken ?? config.edgeFunctionToken;
2823
+ if (accessToken) {
2824
+ this.http.setAuthToken(accessToken);
2825
+ this.tokenManager.setAccessToken(accessToken);
2722
2826
  }
2723
2827
  this.auth = new Auth(this.http, this.tokenManager, {
2724
- isServerMode: config.isServerMode ?? !!config.edgeFunctionToken
2828
+ isServerMode: config.isServerMode ?? !!accessToken,
2829
+ detectOAuthCallback: config.auth?.detectOAuthCallback
2725
2830
  });
2726
2831
  this.database = new Database(this.http);
2727
2832
  this.storage = new Storage(this.http);
@@ -2800,11 +2905,11 @@ function createAdminClient(config) {
2800
2905
  }
2801
2906
  return new InsForgeClient({
2802
2907
  ...clientConfig,
2803
- edgeFunctionToken: apiKey,
2908
+ accessToken: apiKey,
2804
2909
  isServerMode: true
2805
2910
  });
2806
2911
  }
2807
- var index_default = InsForgeClient;
2912
+ var src_default = InsForgeClient;
2808
2913
  // Annotate the CommonJS export names for ESM import in node:
2809
2914
  0 && (module.exports = {
2810
2915
  AI,