@ragable/sdk 0.6.3 → 0.6.5

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
@@ -14,6 +14,12 @@ var RagableSdkError = class extends Error {
14
14
  constructor(message) {
15
15
  super(message);
16
16
  this.name = this.constructor.name;
17
+ Object.defineProperty(this, "message", {
18
+ configurable: true,
19
+ enumerable: true,
20
+ writable: true,
21
+ value: message
22
+ });
17
23
  }
18
24
  toJSON() {
19
25
  return {
@@ -33,7 +39,7 @@ var RagableError = class extends RagableSdkError {
33
39
  __publicField(this, "details");
34
40
  this.status = status;
35
41
  this.body = body;
36
- this.code = body && typeof body === "object" ? typeof body.code === "string" ? body.code : void 0 : void 0;
42
+ this.code = body && typeof body === "object" ? typeof body.code === "string" ? body.code : typeof body.code === "number" ? String(body.code) : void 0 : void 0;
37
43
  this.details = body && typeof body === "object" ? typeof body.details === "string" ? body.details : void 0 : void 0;
38
44
  }
39
45
  toJSON() {
@@ -68,11 +74,15 @@ function formatSdkError(err) {
68
74
  try {
69
75
  const s = JSON.stringify(err);
70
76
  if (s !== "{}") return s;
77
+ return "Unknown error (empty object \u2014 avoid `{...error}` spread; use error.message or formatSdkError)";
71
78
  } catch {
72
79
  }
73
80
  }
74
81
  return String(err);
75
82
  }
83
+ function formatPostgrestError(error) {
84
+ return formatSdkError(error);
85
+ }
76
86
  var RagableNetworkError = class extends RagableSdkError {
77
87
  constructor(message, cause) {
78
88
  super(message);
@@ -642,6 +652,27 @@ function encodeFilterValue(op, value) {
642
652
  }
643
653
  return `${op}.${value}`;
644
654
  }
655
+ function extractPostgRESTErrorMessage(payload, status, statusText) {
656
+ const st = (statusText ?? "").trim();
657
+ if (typeof payload !== "object" || payload === null) {
658
+ return st || `HTTP ${status}`;
659
+ }
660
+ const p = payload;
661
+ const raw = p.message ?? p.error ?? p.hint;
662
+ let msg;
663
+ if (typeof raw === "string") {
664
+ msg = raw;
665
+ } else if (typeof raw === "number" || typeof raw === "boolean") {
666
+ msg = String(raw);
667
+ } else if (raw !== null && raw !== void 0 && typeof raw === "object") {
668
+ msg = JSON.stringify(raw);
669
+ } else {
670
+ msg = st || `HTTP ${status}`;
671
+ }
672
+ msg = msg.trim();
673
+ if (!msg) return st || `HTTP ${status}`;
674
+ return msg;
675
+ }
645
676
  async function parsePostgRESTResponse(response) {
646
677
  if (response.status === 204) return null;
647
678
  const text = await response.text();
@@ -657,9 +688,8 @@ async function parsePostgRESTResponse(response) {
657
688
  );
658
689
  }
659
690
  if (!response.ok) {
660
- const msg = typeof payload === "object" && payload !== null ? payload.message ?? payload.error ?? response.statusText : response.statusText;
661
- const code = typeof payload === "object" && payload !== null ? payload.code : void 0;
662
- throw new RagableError(String(msg), response.status, { code });
691
+ const msg = extractPostgRESTErrorMessage(payload, response.status, response.statusText);
692
+ throw new RagableError(msg, response.status, payload);
663
693
  }
664
694
  return payload;
665
695
  }
@@ -2128,26 +2158,65 @@ var RagableBrowserAuthClient = class {
2128
2158
  }
2129
2159
  };
2130
2160
  var RagableBrowserDatabaseClient = class {
2131
- constructor(options, ragableAuth = null, postgrestFrom) {
2161
+ constructor(options, ragableAuth = null) {
2132
2162
  this.options = options;
2133
2163
  this.ragableAuth = ragableAuth;
2134
- this.postgrestFrom = postgrestFrom;
2135
2164
  __publicField(this, "fetchImpl");
2165
+ __publicField(this, "_transport", null);
2136
2166
  this.fetchImpl = bindFetch(options.fetch);
2137
2167
  }
2138
- /**
2139
- * PostgREST fluent API — same as `client.from('table')`.
2140
- * Use either the root client or `database` for Supabase-style table access; `database.query()` is raw SQL only.
2141
- */
2168
+ /** @internal Called by RagableBrowser to share the Transport instance. */
2169
+ _setTransport(transport) {
2170
+ this._transport = transport;
2171
+ }
2142
2172
  from(table, databaseInstanceId) {
2143
- if (!this.postgrestFrom) {
2173
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2174
+ if (!id) {
2144
2175
  throw new RagableError(
2145
- "database.from() is only available on the client from createBrowserClient().",
2146
- 500,
2147
- { code: "SDK_DATABASE_FROM_UNAVAILABLE" }
2176
+ "database.from() requires databaseInstanceId in client options or as the second argument",
2177
+ 400,
2178
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2148
2179
  );
2149
2180
  }
2150
- return this.postgrestFrom(table, databaseInstanceId);
2181
+ const gid = requireAuthGroupId(this.options);
2182
+ const ragableAuth = this.ragableAuth;
2183
+ const opts = this.options;
2184
+ const transport = this._transport;
2185
+ const fetchImpl = this.fetchImpl;
2186
+ const pgFetch = async (params) => {
2187
+ const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2188
+ const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2189
+ const qs = params.searchParams.toString();
2190
+ const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2191
+ const headers = new Headers(opts.headers);
2192
+ headers.set("Authorization", `Bearer ${token}`);
2193
+ headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2194
+ if (params.body !== void 0) {
2195
+ headers.set("Content-Type", "application/json");
2196
+ }
2197
+ if (params.headers) {
2198
+ for (const [k, v] of Object.entries(params.headers)) {
2199
+ headers.set(k, v);
2200
+ }
2201
+ }
2202
+ if (transport) {
2203
+ return transport.execute({
2204
+ url,
2205
+ method: params.method,
2206
+ headers,
2207
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2208
+ signal: params.signal,
2209
+ idempotencyKey: params.idempotencyKey
2210
+ });
2211
+ }
2212
+ return fetchImpl(url, {
2213
+ method: params.method,
2214
+ headers,
2215
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2216
+ signal: params.signal
2217
+ });
2218
+ };
2219
+ return new PostgrestTableApi(pgFetch, id, table);
2151
2220
  }
2152
2221
  toUrl(path) {
2153
2222
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
@@ -2157,8 +2226,10 @@ var RagableBrowserDatabaseClient = class {
2157
2226
  const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2158
2227
  const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2159
2228
  if (!databaseInstanceId) {
2160
- throw new Error(
2161
- "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })"
2229
+ throw new RagableError(
2230
+ "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })",
2231
+ 400,
2232
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2162
2233
  );
2163
2234
  }
2164
2235
  const headers = this.baseHeaders();
@@ -2231,9 +2302,7 @@ var RagableBrowser = class {
2231
2302
  __publicField(this, "auth");
2232
2303
  __publicField(this, "database");
2233
2304
  __publicField(this, "transport");
2234
- __publicField(this, "options");
2235
2305
  __publicField(this, "_ragableAuth");
2236
- this.options = options;
2237
2306
  this.transport = new Transport({
2238
2307
  fetch: options.fetch,
2239
2308
  headers: options.headers,
@@ -2264,49 +2333,13 @@ var RagableBrowser = class {
2264
2333
  this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
2265
2334
  this.database = new RagableBrowserDatabaseClient(
2266
2335
  options,
2267
- this._ragableAuth,
2268
- (table, databaseInstanceId) => this.from(table, databaseInstanceId)
2336
+ this._ragableAuth
2269
2337
  );
2338
+ this.database._setTransport(this.transport);
2270
2339
  }
2340
+ /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
2271
2341
  from(table, databaseInstanceId) {
2272
- const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2273
- if (!id) {
2274
- throw new RagableError(
2275
- "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument",
2276
- 400,
2277
- { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2278
- );
2279
- }
2280
- const gid = requireAuthGroupId(this.options);
2281
- const ragableAuth = this._ragableAuth;
2282
- const opts = this.options;
2283
- const transport = this.transport;
2284
- const pgFetch = async (params) => {
2285
- const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2286
- const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2287
- const qs = params.searchParams.toString();
2288
- const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2289
- const headers = new Headers(opts.headers);
2290
- headers.set("Authorization", `Bearer ${token}`);
2291
- headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2292
- if (params.body !== void 0) {
2293
- headers.set("Content-Type", "application/json");
2294
- }
2295
- if (params.headers) {
2296
- for (const [k, v] of Object.entries(params.headers)) {
2297
- headers.set(k, v);
2298
- }
2299
- }
2300
- return transport.execute({
2301
- url,
2302
- method: params.method,
2303
- headers,
2304
- body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2305
- signal: params.signal,
2306
- idempotencyKey: params.idempotencyKey
2307
- });
2308
- };
2309
- return new PostgrestTableApi(pgFetch, id, table);
2342
+ return this.database.from(table, databaseInstanceId);
2310
2343
  }
2311
2344
  destroy() {
2312
2345
  this._ragableAuth?.destroy();
@@ -2446,6 +2479,7 @@ export {
2446
2479
  createRagableServerClient,
2447
2480
  detectStorage,
2448
2481
  extractErrorMessage,
2482
+ formatPostgrestError,
2449
2483
  formatRetrievalContext,
2450
2484
  formatSdkError,
2451
2485
  generateIdempotencyKey,