@ragable/sdk 0.6.4 → 0.6.6

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.js CHANGED
@@ -32,6 +32,8 @@ __export(index_exports, {
32
32
  PostgrestDeleteRootBuilder: () => PostgrestDeleteRootBuilder,
33
33
  PostgrestInsertReturningBuilder: () => PostgrestInsertReturningBuilder,
34
34
  PostgrestInsertRootBuilder: () => PostgrestInsertRootBuilder,
35
+ PostgrestInsertSdkErrorReturning: () => PostgrestInsertSdkErrorReturning,
36
+ PostgrestInsertSdkErrorRoot: () => PostgrestInsertSdkErrorRoot,
35
37
  PostgrestSelectBuilder: () => PostgrestSelectBuilder,
36
38
  PostgrestTableApi: () => PostgrestTableApi,
37
39
  PostgrestUpdateReturningBuilder: () => PostgrestUpdateReturningBuilder,
@@ -932,24 +934,48 @@ var PostgrestSelectBuilder = class {
932
934
  });
933
935
  }
934
936
  async maybeSingle() {
935
- return asPostgrestResponse(async () => {
936
- const response = await this.pgFetch({
937
- method: "GET",
938
- table: this.table,
939
- searchParams: this.buildSearchParams(),
940
- databaseInstanceId: this.databaseInstanceId,
941
- signal: this._signal
942
- });
943
- const rows = await parsePostgRESTResponse(response);
944
- if (rows.length > 1) {
945
- throw new RagableError(
937
+ const many = await this.executeMany();
938
+ if (many.error) return { data: null, error: many.error };
939
+ const rows = many.data ?? [];
940
+ if (rows.length > 1) {
941
+ return {
942
+ data: null,
943
+ error: new RagableError(
946
944
  "JSON object requested, multiple (or no) rows returned",
947
945
  406,
948
946
  { code: "PGRST116" }
949
- );
950
- }
951
- return rows[0] ?? null;
952
- });
947
+ )
948
+ };
949
+ }
950
+ return { data: rows[0] ?? null, error: null };
951
+ }
952
+ };
953
+ var PostgrestInsertSdkErrorRoot = class {
954
+ constructor(error) {
955
+ this.error = error;
956
+ }
957
+ select(_columns = "*") {
958
+ return new PostgrestInsertSdkErrorReturning(this.error);
959
+ }
960
+ abortSignal(_signal) {
961
+ return this;
962
+ }
963
+ then(onfulfilled, onrejected) {
964
+ return Promise.resolve({ data: null, error: this.error }).then(onfulfilled, onrejected);
965
+ }
966
+ };
967
+ var PostgrestInsertSdkErrorReturning = class {
968
+ constructor(error) {
969
+ this.error = error;
970
+ }
971
+ then(onfulfilled, onrejected) {
972
+ return Promise.resolve({ data: null, error: this.error }).then(onfulfilled, onrejected);
973
+ }
974
+ async single() {
975
+ return { data: null, error: this.error };
976
+ }
977
+ async maybeSingle() {
978
+ return { data: null, error: this.error };
953
979
  }
954
980
  };
955
981
  var PostgrestInsertRootBuilder = class {
@@ -1030,34 +1056,36 @@ var PostgrestInsertReturningBuilder = class {
1030
1056
  });
1031
1057
  }
1032
1058
  async single() {
1033
- return asPostgrestResponse(async () => {
1034
- const { data, error } = await this.executeMany();
1035
- if (error) throw error;
1036
- const rows = data ?? [];
1037
- if (rows.length === 0 || rows.length > 1) {
1038
- throw new RagableError(
1059
+ const many = await this.executeMany();
1060
+ if (many.error) return { data: null, error: many.error };
1061
+ const rows = many.data ?? [];
1062
+ if (rows.length === 0 || rows.length > 1) {
1063
+ return {
1064
+ data: null,
1065
+ error: new RagableError(
1039
1066
  "JSON object requested, multiple (or no) rows returned",
1040
1067
  406,
1041
1068
  { code: "PGRST116" }
1042
- );
1043
- }
1044
- return rows[0];
1045
- });
1069
+ )
1070
+ };
1071
+ }
1072
+ return { data: rows[0], error: null };
1046
1073
  }
1047
1074
  async maybeSingle() {
1048
- return asPostgrestResponse(async () => {
1049
- const { data, error } = await this.executeMany();
1050
- if (error) throw error;
1051
- const rows = data ?? [];
1052
- if (rows.length > 1) {
1053
- throw new RagableError(
1075
+ const many = await this.executeMany();
1076
+ if (many.error) return { data: null, error: many.error };
1077
+ const rows = many.data ?? [];
1078
+ if (rows.length > 1) {
1079
+ return {
1080
+ data: null,
1081
+ error: new RagableError(
1054
1082
  "JSON object requested, multiple (or no) rows returned",
1055
1083
  406,
1056
1084
  { code: "PGRST116" }
1057
- );
1058
- }
1059
- return rows[0] ?? null;
1060
- });
1085
+ )
1086
+ };
1087
+ }
1088
+ return { data: rows[0] ?? null, error: null };
1061
1089
  }
1062
1090
  };
1063
1091
  var PostgrestUpdateRootBuilder = class {
@@ -1205,34 +1233,36 @@ var PostgrestUpdateReturningBuilder = class {
1205
1233
  });
1206
1234
  }
1207
1235
  async single() {
1208
- return asPostgrestResponse(async () => {
1209
- const { data, error } = await this.executeMany();
1210
- if (error) throw error;
1211
- const rows = data ?? [];
1212
- if (rows.length === 0 || rows.length > 1) {
1213
- throw new RagableError(
1236
+ const many = await this.executeMany();
1237
+ if (many.error) return { data: null, error: many.error };
1238
+ const rows = many.data ?? [];
1239
+ if (rows.length === 0 || rows.length > 1) {
1240
+ return {
1241
+ data: null,
1242
+ error: new RagableError(
1214
1243
  "JSON object requested, multiple (or no) rows returned",
1215
1244
  406,
1216
1245
  { code: "PGRST116" }
1217
- );
1218
- }
1219
- return rows[0];
1220
- });
1246
+ )
1247
+ };
1248
+ }
1249
+ return { data: rows[0], error: null };
1221
1250
  }
1222
1251
  async maybeSingle() {
1223
- return asPostgrestResponse(async () => {
1224
- const { data, error } = await this.executeMany();
1225
- if (error) throw error;
1226
- const rows = data ?? [];
1227
- if (rows.length > 1) {
1228
- throw new RagableError(
1252
+ const many = await this.executeMany();
1253
+ if (many.error) return { data: null, error: many.error };
1254
+ const rows = many.data ?? [];
1255
+ if (rows.length > 1) {
1256
+ return {
1257
+ data: null,
1258
+ error: new RagableError(
1229
1259
  "JSON object requested, multiple (or no) rows returned",
1230
1260
  406,
1231
1261
  { code: "PGRST116" }
1232
- );
1233
- }
1234
- return rows[0] ?? null;
1235
- });
1262
+ )
1263
+ };
1264
+ }
1265
+ return { data: rows[0] ?? null, error: null };
1236
1266
  }
1237
1267
  };
1238
1268
  var PostgrestDeleteRootBuilder = class {
@@ -1364,34 +1394,36 @@ var PostgrestDeleteReturningBuilder = class {
1364
1394
  });
1365
1395
  }
1366
1396
  async single() {
1367
- return asPostgrestResponse(async () => {
1368
- const { data, error } = await this.executeMany();
1369
- if (error) throw error;
1370
- const rows = data ?? [];
1371
- if (rows.length === 0 || rows.length > 1) {
1372
- throw new RagableError(
1397
+ const many = await this.executeMany();
1398
+ if (many.error) return { data: null, error: many.error };
1399
+ const rows = many.data ?? [];
1400
+ if (rows.length === 0 || rows.length > 1) {
1401
+ return {
1402
+ data: null,
1403
+ error: new RagableError(
1373
1404
  "JSON object requested, multiple (or no) rows returned",
1374
1405
  406,
1375
1406
  { code: "PGRST116" }
1376
- );
1377
- }
1378
- return rows[0];
1379
- });
1407
+ )
1408
+ };
1409
+ }
1410
+ return { data: rows[0], error: null };
1380
1411
  }
1381
1412
  async maybeSingle() {
1382
- return asPostgrestResponse(async () => {
1383
- const { data, error } = await this.executeMany();
1384
- if (error) throw error;
1385
- const rows = data ?? [];
1386
- if (rows.length > 1) {
1387
- throw new RagableError(
1413
+ const many = await this.executeMany();
1414
+ if (many.error) return { data: null, error: many.error };
1415
+ const rows = many.data ?? [];
1416
+ if (rows.length > 1) {
1417
+ return {
1418
+ data: null,
1419
+ error: new RagableError(
1388
1420
  "JSON object requested, multiple (or no) rows returned",
1389
1421
  406,
1390
1422
  { code: "PGRST116" }
1391
- );
1392
- }
1393
- return rows[0] ?? null;
1394
- });
1423
+ )
1424
+ };
1425
+ }
1426
+ return { data: rows[0] ?? null, error: null };
1395
1427
  }
1396
1428
  };
1397
1429
  var PostgrestUpsertRootBuilder = class {
@@ -1460,34 +1492,36 @@ var PostgrestUpsertReturningBuilder = class {
1460
1492
  });
1461
1493
  }
1462
1494
  async single() {
1463
- return asPostgrestResponse(async () => {
1464
- const { data, error } = await this.executeMany();
1465
- if (error) throw error;
1466
- const rows = data ?? [];
1467
- if (rows.length === 0 || rows.length > 1) {
1468
- throw new RagableError(
1495
+ const many = await this.executeMany();
1496
+ if (many.error) return { data: null, error: many.error };
1497
+ const rows = many.data ?? [];
1498
+ if (rows.length === 0 || rows.length > 1) {
1499
+ return {
1500
+ data: null,
1501
+ error: new RagableError(
1469
1502
  "JSON object requested, multiple (or no) rows returned",
1470
1503
  406,
1471
1504
  { code: "PGRST116" }
1472
- );
1473
- }
1474
- return rows[0];
1475
- });
1505
+ )
1506
+ };
1507
+ }
1508
+ return { data: rows[0], error: null };
1476
1509
  }
1477
1510
  async maybeSingle() {
1478
- return asPostgrestResponse(async () => {
1479
- const { data, error } = await this.executeMany();
1480
- if (error) throw error;
1481
- const rows = data ?? [];
1482
- if (rows.length > 1) {
1483
- throw new RagableError(
1511
+ const many = await this.executeMany();
1512
+ if (many.error) return { data: null, error: many.error };
1513
+ const rows = many.data ?? [];
1514
+ if (rows.length > 1) {
1515
+ return {
1516
+ data: null,
1517
+ error: new RagableError(
1484
1518
  "JSON object requested, multiple (or no) rows returned",
1485
1519
  406,
1486
1520
  { code: "PGRST116" }
1487
- );
1488
- }
1489
- return rows[0] ?? null;
1490
- });
1521
+ )
1522
+ };
1523
+ }
1524
+ return { data: rows[0] ?? null, error: null };
1491
1525
  }
1492
1526
  };
1493
1527
  var PostgrestTableApi = class {
@@ -1506,11 +1540,14 @@ var PostgrestTableApi = class {
1506
1540
  }
1507
1541
  insert(values, ...rest) {
1508
1542
  if (rest.length > 0) {
1509
- throw new RagableError(
1543
+ const err = new RagableError(
1510
1544
  ".insert() accepts only one argument: the row object or an array of rows. Do not pass readOnly, options, or a second object \u2014 those apply only to database.query({ sql, readOnly }). PostgREST inserts are writes by default.",
1511
1545
  400,
1512
1546
  { code: "SDK_INSERT_EXTRA_ARGS" }
1513
1547
  );
1548
+ return new PostgrestInsertSdkErrorRoot(
1549
+ err
1550
+ );
1514
1551
  }
1515
1552
  const rows = Array.isArray(values) ? values : [values];
1516
1553
  return new PostgrestInsertRootBuilder(
@@ -2125,14 +2162,6 @@ async function resolveDatabaseAuthBearer(options, ragableAuth) {
2125
2162
  }
2126
2163
  return key;
2127
2164
  }
2128
- async function parseJsonOrThrow2(response) {
2129
- const payload = await parseMaybeJsonBody(response);
2130
- if (!response.ok) {
2131
- const message = extractErrorMessage(payload, response.statusText);
2132
- throw new RagableError(message, response.status, payload);
2133
- }
2134
- return payload;
2135
- }
2136
2165
  var RagableBrowserAuthClient = class {
2137
2166
  constructor(_options, ragableAuth = null) {
2138
2167
  this.ragableAuth = ragableAuth;
@@ -2229,59 +2258,107 @@ var RagableBrowserAuthClient = class {
2229
2258
  }
2230
2259
  };
2231
2260
  var RagableBrowserDatabaseClient = class {
2232
- constructor(options, ragableAuth = null, postgrestFrom) {
2261
+ constructor(options, ragableAuth = null) {
2233
2262
  this.options = options;
2234
2263
  this.ragableAuth = ragableAuth;
2235
- this.postgrestFrom = postgrestFrom;
2236
2264
  __publicField(this, "fetchImpl");
2265
+ __publicField(this, "_transport", null);
2237
2266
  this.fetchImpl = bindFetch(options.fetch);
2238
2267
  }
2239
- /**
2240
- * PostgREST fluent API — same as `client.from('table')`.
2241
- * Use either the root client or `database` for Supabase-style table access; `database.query()` is raw SQL only.
2242
- */
2268
+ /** @internal Called by RagableBrowser to share the Transport instance. */
2269
+ _setTransport(transport) {
2270
+ this._transport = transport;
2271
+ }
2243
2272
  from(table, databaseInstanceId) {
2244
- if (!this.postgrestFrom) {
2245
- throw new RagableError(
2246
- "database.from() is only available on the client from createBrowserClient().",
2247
- 500,
2248
- { code: "SDK_DATABASE_FROM_UNAVAILABLE" }
2249
- );
2250
- }
2251
- return this.postgrestFrom(table, databaseInstanceId);
2273
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim() || "";
2274
+ const ragableAuth = this.ragableAuth;
2275
+ const opts = this.options;
2276
+ const transport = this._transport;
2277
+ const fetchImpl = this.fetchImpl;
2278
+ const pgFetch = async (params) => {
2279
+ if (!params.databaseInstanceId?.trim()) {
2280
+ throw new RagableError(
2281
+ "database.from() requires databaseInstanceId in client options or as the second argument",
2282
+ 400,
2283
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2284
+ );
2285
+ }
2286
+ const gid = requireAuthGroupId(opts);
2287
+ const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2288
+ const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2289
+ const qs = params.searchParams.toString();
2290
+ const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2291
+ const headers = new Headers(opts.headers);
2292
+ headers.set("Authorization", `Bearer ${token}`);
2293
+ headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2294
+ if (params.body !== void 0) {
2295
+ headers.set("Content-Type", "application/json");
2296
+ }
2297
+ if (params.headers) {
2298
+ for (const [k, v] of Object.entries(params.headers)) {
2299
+ headers.set(k, v);
2300
+ }
2301
+ }
2302
+ if (transport) {
2303
+ return transport.execute({
2304
+ url,
2305
+ method: params.method,
2306
+ headers,
2307
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2308
+ signal: params.signal,
2309
+ idempotencyKey: params.idempotencyKey
2310
+ });
2311
+ }
2312
+ return fetchImpl(url, {
2313
+ method: params.method,
2314
+ headers,
2315
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2316
+ signal: params.signal
2317
+ });
2318
+ };
2319
+ return new PostgrestTableApi(pgFetch, id, table);
2252
2320
  }
2253
2321
  toUrl(path) {
2254
2322
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2255
2323
  }
2256
2324
  async query(params) {
2257
- const gid = requireAuthGroupId(this.options);
2258
- const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2259
- const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2260
- if (!databaseInstanceId) {
2261
- throw new Error(
2262
- "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })"
2325
+ return asPostgrestResponse(async () => {
2326
+ const gid = requireAuthGroupId(this.options);
2327
+ const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2328
+ const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2329
+ if (!databaseInstanceId) {
2330
+ throw new RagableError(
2331
+ "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })",
2332
+ 400,
2333
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2334
+ );
2335
+ }
2336
+ const headers = this.baseHeaders();
2337
+ headers.set("Authorization", `Bearer ${token}`);
2338
+ headers.set("Content-Type", "application/json");
2339
+ const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2340
+ const response = await this.fetchImpl(
2341
+ this.toUrl(`/auth-groups/${gid}/data/query`),
2342
+ {
2343
+ method: "POST",
2344
+ headers,
2345
+ body: JSON.stringify({
2346
+ databaseInstanceId,
2347
+ sql: params.sql,
2348
+ ...params.params !== void 0 ? { params: params.params } : {},
2349
+ readOnly,
2350
+ ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2351
+ ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2352
+ })
2353
+ }
2263
2354
  );
2264
- }
2265
- const headers = this.baseHeaders();
2266
- headers.set("Authorization", `Bearer ${token}`);
2267
- headers.set("Content-Type", "application/json");
2268
- const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2269
- const response = await this.fetchImpl(
2270
- this.toUrl(`/auth-groups/${gid}/data/query`),
2271
- {
2272
- method: "POST",
2273
- headers,
2274
- body: JSON.stringify({
2275
- databaseInstanceId,
2276
- sql: params.sql,
2277
- ...params.params !== void 0 ? { params: params.params } : {},
2278
- readOnly,
2279
- ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2280
- ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2281
- })
2355
+ const payload = await parseMaybeJsonBody(response);
2356
+ if (!response.ok) {
2357
+ const message = extractErrorMessage(payload, response.statusText);
2358
+ throw new RagableError(message, response.status, payload);
2282
2359
  }
2283
- );
2284
- return parseJsonOrThrow2(response);
2360
+ return payload;
2361
+ });
2285
2362
  }
2286
2363
  baseHeaders() {
2287
2364
  return new Headers(this.options.headers);
@@ -2332,9 +2409,7 @@ var RagableBrowser = class {
2332
2409
  __publicField(this, "auth");
2333
2410
  __publicField(this, "database");
2334
2411
  __publicField(this, "transport");
2335
- __publicField(this, "options");
2336
2412
  __publicField(this, "_ragableAuth");
2337
- this.options = options;
2338
2413
  this.transport = new Transport({
2339
2414
  fetch: options.fetch,
2340
2415
  headers: options.headers,
@@ -2365,49 +2440,13 @@ var RagableBrowser = class {
2365
2440
  this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
2366
2441
  this.database = new RagableBrowserDatabaseClient(
2367
2442
  options,
2368
- this._ragableAuth,
2369
- (table, databaseInstanceId) => this.from(table, databaseInstanceId)
2443
+ this._ragableAuth
2370
2444
  );
2445
+ this.database._setTransport(this.transport);
2371
2446
  }
2447
+ /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
2372
2448
  from(table, databaseInstanceId) {
2373
- const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2374
- if (!id) {
2375
- throw new RagableError(
2376
- "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument",
2377
- 400,
2378
- { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2379
- );
2380
- }
2381
- const gid = requireAuthGroupId(this.options);
2382
- const ragableAuth = this._ragableAuth;
2383
- const opts = this.options;
2384
- const transport = this.transport;
2385
- const pgFetch = async (params) => {
2386
- const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2387
- const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2388
- const qs = params.searchParams.toString();
2389
- const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2390
- const headers = new Headers(opts.headers);
2391
- headers.set("Authorization", `Bearer ${token}`);
2392
- headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2393
- if (params.body !== void 0) {
2394
- headers.set("Content-Type", "application/json");
2395
- }
2396
- if (params.headers) {
2397
- for (const [k, v] of Object.entries(params.headers)) {
2398
- headers.set(k, v);
2399
- }
2400
- }
2401
- return transport.execute({
2402
- url,
2403
- method: params.method,
2404
- headers,
2405
- body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2406
- signal: params.signal,
2407
- idempotencyKey: params.idempotencyKey
2408
- });
2409
- };
2410
- return new PostgrestTableApi(pgFetch, id, table);
2449
+ return this.database.from(table, databaseInstanceId);
2411
2450
  }
2412
2451
  destroy() {
2413
2452
  this._ragableAuth?.destroy();
@@ -2518,6 +2557,8 @@ function createRagableServerClient(options) {
2518
2557
  PostgrestDeleteRootBuilder,
2519
2558
  PostgrestInsertReturningBuilder,
2520
2559
  PostgrestInsertRootBuilder,
2560
+ PostgrestInsertSdkErrorReturning,
2561
+ PostgrestInsertSdkErrorRoot,
2521
2562
  PostgrestSelectBuilder,
2522
2563
  PostgrestTableApi,
2523
2564
  PostgrestUpdateReturningBuilder,