@ragable/sdk 0.5.0 → 0.5.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
@@ -9,6 +9,7 @@ function bindFetch(custom) {
9
9
  return f.call(globalThis, input, init);
10
10
  };
11
11
  }
12
+ var DEFAULT_RAGABLE_API_BASE = "https://ragable-341305259977.asia-southeast1.run.app/api";
12
13
  var RagableError = class extends Error {
13
14
  constructor(message, status, body) {
14
15
  super(message);
@@ -40,7 +41,7 @@ var RagableRequestClient = class {
40
41
  __publicField(this, "fetchImpl");
41
42
  __publicField(this, "defaultHeaders");
42
43
  this.apiKey = options.apiKey;
43
- this.baseUrl = options.baseUrl ?? "http://localhost:8080/api";
44
+ this.baseUrl = options.baseUrl ?? DEFAULT_RAGABLE_API_BASE;
44
45
  this.fetchImpl = bindFetch(options.fetch);
45
46
  this.defaultHeaders = options.headers;
46
47
  }
@@ -1107,7 +1108,8 @@ var PostgrestTableApi = class {
1107
1108
 
1108
1109
  // src/browser.ts
1109
1110
  function normalizeBrowserApiBase(baseUrl) {
1110
- return (baseUrl ?? "http://localhost:8080/api").replace(/\/+$/, "");
1111
+ const trimmed = (baseUrl ?? DEFAULT_RAGABLE_API_BASE).trim().replace(/\/+$/, "");
1112
+ return trimmed.endsWith("/api") ? trimmed : `${trimmed}/api`;
1111
1113
  }
1112
1114
  function requireAuthGroupId(options) {
1113
1115
  const id = options.authGroupId?.trim();
@@ -1131,6 +1133,20 @@ async function requireAccessToken(options) {
1131
1133
  }
1132
1134
  return token.trim();
1133
1135
  }
1136
+ async function resolveDatabaseAuthBearer(options) {
1137
+ const mode = options.dataAuth ?? "user";
1138
+ if (mode === "user") {
1139
+ return requireAccessToken(options);
1140
+ }
1141
+ const fromGetter = options.getDataStaticKey ? await options.getDataStaticKey() : null;
1142
+ const key = (fromGetter?.trim() || options.dataStaticKey?.trim()) ?? "";
1143
+ if (!key) {
1144
+ throw new Error(
1145
+ mode === "publicAnon" ? "dataAuth publicAnon requires getDataStaticKey or dataStaticKey (rotate key in dashboard)" : "dataAuth admin requires getDataStaticKey or dataStaticKey (server-side only; rotate in dashboard)"
1146
+ );
1147
+ }
1148
+ return key;
1149
+ }
1134
1150
  async function parseJsonOrThrow(response) {
1135
1151
  const payload = await parseMaybeJsonBody(response);
1136
1152
  if (!response.ok) {
@@ -1300,7 +1316,7 @@ var RagableBrowserDatabaseClient = class {
1300
1316
  }
1301
1317
  async query(params) {
1302
1318
  const gid = requireAuthGroupId(this.options);
1303
- const token = await requireAccessToken(this.options);
1319
+ const token = await resolveDatabaseAuthBearer(this.options);
1304
1320
  const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
1305
1321
  if (!databaseInstanceId) {
1306
1322
  throw new Error(
@@ -1310,6 +1326,7 @@ var RagableBrowserDatabaseClient = class {
1310
1326
  const headers = this.baseHeaders();
1311
1327
  headers.set("Authorization", `Bearer ${token}`);
1312
1328
  headers.set("Content-Type", "application/json");
1329
+ const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
1313
1330
  const response = await this.fetchImpl(
1314
1331
  this.toUrl(`/auth-groups/${gid}/data/query`),
1315
1332
  {
@@ -1319,7 +1336,7 @@ var RagableBrowserDatabaseClient = class {
1319
1336
  databaseInstanceId,
1320
1337
  sql: params.sql,
1321
1338
  ...params.params !== void 0 ? { params: params.params } : {},
1322
- ...params.readOnly === false ? { readOnly: false } : {},
1339
+ readOnly,
1323
1340
  ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
1324
1341
  ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
1325
1342
  })
@@ -1494,6 +1511,7 @@ function createRagableServerClient(options) {
1494
1511
  }
1495
1512
  export {
1496
1513
  AgentsClient,
1514
+ DEFAULT_RAGABLE_API_BASE,
1497
1515
  PostgrestDeleteReturningBuilder,
1498
1516
  PostgrestDeleteRootBuilder,
1499
1517
  PostgrestInsertReturningBuilder,