@insforge/sdk 1.4.1 → 1.4.3

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
@@ -833,19 +833,32 @@ var HttpClient = class {
833
833
 
834
834
  // src/modules/auth/helpers.ts
835
835
  var PKCE_VERIFIER_KEY = "insforge_pkce_verifier";
836
+ async function getWebCrypto() {
837
+ const webCrypto = globalThis.crypto;
838
+ if (typeof webCrypto?.getRandomValues === "function" && webCrypto.subtle) {
839
+ return webCrypto;
840
+ }
841
+ if (typeof process !== "undefined" && process.versions?.node) {
842
+ const { webcrypto } = await import("crypto");
843
+ return webcrypto;
844
+ }
845
+ throw new Error("Web Crypto API is not available in this environment");
846
+ }
836
847
  function base64UrlEncode(buffer) {
837
848
  const base64 = btoa(String.fromCharCode(...buffer));
838
849
  return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
839
850
  }
840
- function generateCodeVerifier() {
851
+ async function generateCodeVerifier() {
852
+ const webCrypto = await getWebCrypto();
841
853
  const array = new Uint8Array(32);
842
- crypto.getRandomValues(array);
854
+ webCrypto.getRandomValues(array);
843
855
  return base64UrlEncode(array);
844
856
  }
845
857
  async function generateCodeChallenge(verifier) {
858
+ const webCrypto = await getWebCrypto();
846
859
  const encoder = new TextEncoder();
847
860
  const data = encoder.encode(verifier);
848
- const hash = await crypto.subtle.digest("SHA-256", data);
861
+ const hash = await webCrypto.subtle.digest("SHA-256", data);
849
862
  return base64UrlEncode(new Uint8Array(hash));
850
863
  }
851
864
  function storePkceVerifier(verifier) {
@@ -892,7 +905,7 @@ var Auth = class {
892
905
  this.http = http;
893
906
  this.tokenManager = tokenManager;
894
907
  this.options = options;
895
- this.authCallbackHandled = this.detectAuthCallback();
908
+ this.authCallbackHandled = options.detectOAuthCallback === false ? Promise.resolve() : this.detectAuthCallback();
896
909
  }
897
910
  isServerMode() {
898
911
  return !!this.options.isServerMode;
@@ -989,10 +1002,16 @@ var Auth = class {
989
1002
  async signOut() {
990
1003
  try {
991
1004
  try {
1005
+ const serverMode = this.isServerMode();
1006
+ const csrfToken = !serverMode ? getCsrfToken() : null;
992
1007
  await this.http.post(
993
- this.isServerMode() ? "/api/auth/logout?client_type=mobile" : "/api/auth/logout",
1008
+ serverMode ? "/api/auth/logout?client_type=mobile" : "/api/auth/logout",
994
1009
  void 0,
995
- { credentials: "include", skipAuthRefresh: true }
1010
+ {
1011
+ credentials: "include",
1012
+ skipAuthRefresh: true,
1013
+ ...csrfToken ? { headers: { "X-CSRF-Token": csrfToken } } : {}
1014
+ }
996
1015
  );
997
1016
  } catch {
998
1017
  }
@@ -1038,7 +1057,7 @@ var Auth = class {
1038
1057
  }
1039
1058
  const { provider } = signInOptions;
1040
1059
  const providerKey = encodeURIComponent(provider.toLowerCase());
1041
- const codeVerifier = generateCodeVerifier();
1060
+ const codeVerifier = await generateCodeVerifier();
1042
1061
  const codeChallenge = await generateCodeChallenge(codeVerifier);
1043
1062
  storePkceVerifier(codeVerifier);
1044
1063
  const params = {
@@ -1398,12 +1417,30 @@ function createInsForgePostgrestFetch(httpClient) {
1398
1417
  };
1399
1418
  }
1400
1419
  var Database = class {
1401
- constructor(httpClient) {
1420
+ constructor(httpClient, defaultSchema) {
1402
1421
  this.postgrest = new PostgrestClient("http://dummy", {
1403
1422
  fetch: createInsForgePostgrestFetch(httpClient),
1404
- headers: {}
1423
+ headers: {},
1424
+ ...defaultSchema ? { schema: defaultSchema } : {}
1405
1425
  });
1406
1426
  }
1427
+ /**
1428
+ * Select a non-default Postgres schema for the chained query. Maps to
1429
+ * PostgREST's `Accept-Profile` (reads) / `Content-Profile` (writes) header.
1430
+ * The schema must be exposed by the backend.
1431
+ *
1432
+ * @example
1433
+ * const { data } = await client.database
1434
+ * .schema('analytics')
1435
+ * .from('events')
1436
+ * .select('*');
1437
+ *
1438
+ * @example
1439
+ * await client.database.schema('analytics').rpc('rollup', { day: '2026-01-01' });
1440
+ */
1441
+ schema(schemaName) {
1442
+ return this.postgrest.schema(schemaName);
1443
+ }
1407
1444
  /**
1408
1445
  * Create a query builder for a table
1409
1446
  *
@@ -2760,9 +2797,10 @@ var InsForgeClient = class {
2760
2797
  this.tokenManager.setAccessToken(accessToken);
2761
2798
  }
2762
2799
  this.auth = new Auth(this.http, this.tokenManager, {
2763
- isServerMode: config.isServerMode ?? !!accessToken
2800
+ isServerMode: config.isServerMode ?? !!accessToken,
2801
+ detectOAuthCallback: config.auth?.detectOAuthCallback
2764
2802
  });
2765
- this.database = new Database(this.http);
2803
+ this.database = new Database(this.http, config.db?.schema);
2766
2804
  this.storage = new Storage(this.http);
2767
2805
  this.ai = new AI(this.http);
2768
2806
  this.functions = new Functions(this.http, config.functionsUrl);