@ragable/sdk 0.6.8 → 0.6.9

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
@@ -138,7 +138,7 @@ var RagableRequestClient = class {
138
138
  __publicField(this, "fetchImpl");
139
139
  __publicField(this, "defaultHeaders");
140
140
  this.apiKey = options.apiKey;
141
- this.baseUrl = options.baseUrl ?? DEFAULT_RAGABLE_API_BASE;
141
+ this.baseUrl = DEFAULT_RAGABLE_API_BASE.replace(/\/+$/, "");
142
142
  this.fetchImpl = bindFetch(options.fetch);
143
143
  this.defaultHeaders = options.headers;
144
144
  }
@@ -1689,7 +1689,7 @@ var RagableAuth = class {
1689
1689
  __publicField(this, "broadcast", null);
1690
1690
  __publicField(this, "visibilityHandler", null);
1691
1691
  __publicField(this, "initialized", false);
1692
- this.baseUrl = config.baseUrl;
1692
+ this.baseUrl = DEFAULT_RAGABLE_API_BASE.replace(/\/+$/, "");
1693
1693
  this.authGroupId = config.authGroupId;
1694
1694
  this.fetchImpl = bindFetch(config.fetch);
1695
1695
  this.defaultHeaders = config.headers;
@@ -2042,9 +2042,14 @@ function decodeJwtExpiry(jwt) {
2042
2042
  }
2043
2043
 
2044
2044
  // src/browser.ts
2045
- function normalizeBrowserApiBase(baseUrl) {
2046
- const trimmed = (baseUrl ?? DEFAULT_RAGABLE_API_BASE).trim().replace(/\/+$/, "");
2047
- return trimmed.endsWith("/api") ? trimmed : `${trimmed}/api`;
2045
+ function normalizeBrowserApiBase() {
2046
+ return DEFAULT_RAGABLE_API_BASE.replace(/\/+$/, "");
2047
+ }
2048
+ function effectiveDataAuth(options) {
2049
+ if (options.dataAuth) return options.dataAuth;
2050
+ const hasStatic = Boolean(options.dataStaticKey?.trim()) || typeof options.getDataStaticKey === "function";
2051
+ if (hasStatic) return "publicAnon";
2052
+ return "user";
2048
2053
  }
2049
2054
  function requireAuthGroupId(options) {
2050
2055
  const id = options.authGroupId?.trim();
@@ -2074,7 +2079,7 @@ async function requireAccessToken(options, ragableAuth) {
2074
2079
  );
2075
2080
  }
2076
2081
  async function resolveDatabaseAuthBearer(options, ragableAuth) {
2077
- const mode = options.dataAuth ?? "user";
2082
+ const mode = effectiveDataAuth(options);
2078
2083
  if (mode === "user") {
2079
2084
  return requireAccessToken(options, ragableAuth);
2080
2085
  }
@@ -2210,9 +2215,9 @@ var RagableBrowserDatabaseClient = class {
2210
2215
  }
2211
2216
  const gid = requireAuthGroupId(opts);
2212
2217
  const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2213
- const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2218
+ const apiBase = normalizeBrowserApiBase();
2214
2219
  const qs = params.searchParams.toString();
2215
- const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2220
+ const url = `${apiBase}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2216
2221
  const headers = new Headers(opts.headers);
2217
2222
  headers.set("Authorization", `Bearer ${token}`);
2218
2223
  headers.set("X-Database-Instance-Id", params.databaseInstanceId);
@@ -2258,7 +2263,7 @@ var RagableBrowserDatabaseClient = class {
2258
2263
  const headers = this.baseHeaders();
2259
2264
  headers.set("Authorization", `Bearer ${token}`);
2260
2265
  headers.set("Content-Type", "application/json");
2261
- const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2266
+ const readOnly = effectiveDataAuth(this.options) === "publicAnon" ? true : params.readOnly !== false;
2262
2267
  const response = await this.fetchImpl(
2263
2268
  this.toUrl(`/auth-groups/${gid}/data/query`),
2264
2269
  {
@@ -2289,7 +2294,7 @@ var RagableBrowserDatabaseClient = class {
2289
2294
  this._transport = transport;
2290
2295
  }
2291
2296
  toUrl(path) {
2292
- return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2297
+ return `${normalizeBrowserApiBase()}${path.startsWith("/") ? path : `/${path}`}`;
2293
2298
  }
2294
2299
  baseHeaders() {
2295
2300
  return new Headers(this.options.headers);
@@ -2302,7 +2307,7 @@ var RagableBrowserAgentsClient = class {
2302
2307
  this.fetchImpl = bindFetch(options.fetch);
2303
2308
  }
2304
2309
  toUrl(path) {
2305
- return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2310
+ return `${normalizeBrowserApiBase()}${path.startsWith("/") ? path : `/${path}`}`;
2306
2311
  }
2307
2312
  async *chatStream(agentId, params) {
2308
2313
  const orgId = this.options.organizationId;
@@ -2352,19 +2357,19 @@ var RagableBrowser = class {
2352
2357
  });
2353
2358
  if (options.authGroupId) {
2354
2359
  this._ragableAuth = new RagableAuth({
2355
- baseUrl: normalizeBrowserApiBase(options.baseUrl),
2356
2360
  authGroupId: options.authGroupId,
2357
2361
  fetch: options.fetch,
2358
2362
  headers: options.headers,
2359
2363
  auth: options.auth
2360
2364
  });
2361
2365
  this.transport.setRefreshHandler(async () => {
2366
+ if (effectiveDataAuth(options) !== "user") return null;
2362
2367
  const session = await this._ragableAuth.singleFlightRefresh(
2363
2368
  this._ragableAuth.getCurrentSession()?.refresh_token ?? ""
2364
2369
  );
2365
2370
  return session?.access_token ?? null;
2366
2371
  });
2367
- if (!options.getAccessToken) {
2372
+ if (!options.getAccessToken && effectiveDataAuth(options) === "user") {
2368
2373
  this._ragableAuth.initialize().catch(() => {
2369
2374
  });
2370
2375
  }
@@ -2447,31 +2452,18 @@ var Ragable = class {
2447
2452
  function isServerClientOptions(o) {
2448
2453
  return typeof o === "object" && o !== null && "apiKey" in o && typeof o.apiKey === "string" && o.apiKey.length > 0;
2449
2454
  }
2450
- function createClient(urlOrOptions, browserOptions) {
2451
- if (typeof urlOrOptions === "string") {
2452
- if (!browserOptions) {
2453
- throw new Error(
2454
- "createClient(url, options) requires options with at least organizationId"
2455
- );
2456
- }
2457
- const raw = urlOrOptions.trim().replace(/\/+$/, "");
2458
- const baseUrl = raw.endsWith("/api") ? raw : `${raw}/api`;
2459
- return createBrowserClient({
2460
- ...browserOptions,
2461
- baseUrl: normalizeBrowserApiBase(baseUrl)
2462
- });
2463
- }
2464
- if (isServerClientOptions(urlOrOptions)) {
2465
- if (typeof urlOrOptions === "object" && urlOrOptions !== null && "organizationId" in urlOrOptions && typeof urlOrOptions.organizationId === "string") {
2455
+ function createClient(options) {
2456
+ if (isServerClientOptions(options)) {
2457
+ if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
2466
2458
  console.warn(
2467
- "[@ragable/sdk] createClient: `apiKey` is set, so the server client is returned. It has no `database` or `auth` \u2014 only `agents` and `shift`. For `database.from()` / `auth.*`, use the browser client without `apiKey` (e.g. createClient(ragableUrl, { organizationId, authGroupId, databaseInstanceId, ... }))."
2459
+ "[@ragable/sdk] createClient: `apiKey` is set, so the server client is returned. It has no `database` or `auth` \u2014 only `agents` and `shift`. For `database.from()` / `auth.*`, use the browser client without `apiKey` (e.g. createClient({ organizationId, authGroupId, databaseInstanceId, ... }))."
2468
2460
  );
2469
2461
  }
2470
- return new Ragable(urlOrOptions);
2462
+ return new Ragable(options);
2471
2463
  }
2472
- if (typeof urlOrOptions === "object" && urlOrOptions !== null && "organizationId" in urlOrOptions && typeof urlOrOptions.organizationId === "string") {
2464
+ if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
2473
2465
  return createBrowserClient(
2474
- urlOrOptions
2466
+ options
2475
2467
  );
2476
2468
  }
2477
2469
  throw new Error(
@@ -2523,6 +2515,7 @@ export {
2523
2515
  createRagableBrowserClient,
2524
2516
  createRagableServerClient,
2525
2517
  detectStorage,
2518
+ effectiveDataAuth,
2526
2519
  extractErrorMessage,
2527
2520
  formatPostgrestError,
2528
2521
  formatRetrievalContext,