@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.mjs CHANGED
@@ -861,24 +861,48 @@ var PostgrestSelectBuilder = class {
861
861
  });
862
862
  }
863
863
  async maybeSingle() {
864
- return asPostgrestResponse(async () => {
865
- const response = await this.pgFetch({
866
- method: "GET",
867
- table: this.table,
868
- searchParams: this.buildSearchParams(),
869
- databaseInstanceId: this.databaseInstanceId,
870
- signal: this._signal
871
- });
872
- const rows = await parsePostgRESTResponse(response);
873
- if (rows.length > 1) {
874
- throw new RagableError(
864
+ const many = await this.executeMany();
865
+ if (many.error) return { data: null, error: many.error };
866
+ const rows = many.data ?? [];
867
+ if (rows.length > 1) {
868
+ return {
869
+ data: null,
870
+ error: new RagableError(
875
871
  "JSON object requested, multiple (or no) rows returned",
876
872
  406,
877
873
  { code: "PGRST116" }
878
- );
879
- }
880
- return rows[0] ?? null;
881
- });
874
+ )
875
+ };
876
+ }
877
+ return { data: rows[0] ?? null, error: null };
878
+ }
879
+ };
880
+ var PostgrestInsertSdkErrorRoot = class {
881
+ constructor(error) {
882
+ this.error = error;
883
+ }
884
+ select(_columns = "*") {
885
+ return new PostgrestInsertSdkErrorReturning(this.error);
886
+ }
887
+ abortSignal(_signal) {
888
+ return this;
889
+ }
890
+ then(onfulfilled, onrejected) {
891
+ return Promise.resolve({ data: null, error: this.error }).then(onfulfilled, onrejected);
892
+ }
893
+ };
894
+ var PostgrestInsertSdkErrorReturning = class {
895
+ constructor(error) {
896
+ this.error = error;
897
+ }
898
+ then(onfulfilled, onrejected) {
899
+ return Promise.resolve({ data: null, error: this.error }).then(onfulfilled, onrejected);
900
+ }
901
+ async single() {
902
+ return { data: null, error: this.error };
903
+ }
904
+ async maybeSingle() {
905
+ return { data: null, error: this.error };
882
906
  }
883
907
  };
884
908
  var PostgrestInsertRootBuilder = class {
@@ -959,34 +983,36 @@ var PostgrestInsertReturningBuilder = class {
959
983
  });
960
984
  }
961
985
  async single() {
962
- return asPostgrestResponse(async () => {
963
- const { data, error } = await this.executeMany();
964
- if (error) throw error;
965
- const rows = data ?? [];
966
- if (rows.length === 0 || rows.length > 1) {
967
- throw new RagableError(
986
+ const many = await this.executeMany();
987
+ if (many.error) return { data: null, error: many.error };
988
+ const rows = many.data ?? [];
989
+ if (rows.length === 0 || rows.length > 1) {
990
+ return {
991
+ data: null,
992
+ error: new RagableError(
968
993
  "JSON object requested, multiple (or no) rows returned",
969
994
  406,
970
995
  { code: "PGRST116" }
971
- );
972
- }
973
- return rows[0];
974
- });
996
+ )
997
+ };
998
+ }
999
+ return { data: rows[0], error: null };
975
1000
  }
976
1001
  async maybeSingle() {
977
- return asPostgrestResponse(async () => {
978
- const { data, error } = await this.executeMany();
979
- if (error) throw error;
980
- const rows = data ?? [];
981
- if (rows.length > 1) {
982
- throw new RagableError(
1002
+ const many = await this.executeMany();
1003
+ if (many.error) return { data: null, error: many.error };
1004
+ const rows = many.data ?? [];
1005
+ if (rows.length > 1) {
1006
+ return {
1007
+ data: null,
1008
+ error: new RagableError(
983
1009
  "JSON object requested, multiple (or no) rows returned",
984
1010
  406,
985
1011
  { code: "PGRST116" }
986
- );
987
- }
988
- return rows[0] ?? null;
989
- });
1012
+ )
1013
+ };
1014
+ }
1015
+ return { data: rows[0] ?? null, error: null };
990
1016
  }
991
1017
  };
992
1018
  var PostgrestUpdateRootBuilder = class {
@@ -1134,34 +1160,36 @@ var PostgrestUpdateReturningBuilder = class {
1134
1160
  });
1135
1161
  }
1136
1162
  async single() {
1137
- return asPostgrestResponse(async () => {
1138
- const { data, error } = await this.executeMany();
1139
- if (error) throw error;
1140
- const rows = data ?? [];
1141
- if (rows.length === 0 || rows.length > 1) {
1142
- throw new RagableError(
1163
+ const many = await this.executeMany();
1164
+ if (many.error) return { data: null, error: many.error };
1165
+ const rows = many.data ?? [];
1166
+ if (rows.length === 0 || rows.length > 1) {
1167
+ return {
1168
+ data: null,
1169
+ error: new RagableError(
1143
1170
  "JSON object requested, multiple (or no) rows returned",
1144
1171
  406,
1145
1172
  { code: "PGRST116" }
1146
- );
1147
- }
1148
- return rows[0];
1149
- });
1173
+ )
1174
+ };
1175
+ }
1176
+ return { data: rows[0], error: null };
1150
1177
  }
1151
1178
  async maybeSingle() {
1152
- return asPostgrestResponse(async () => {
1153
- const { data, error } = await this.executeMany();
1154
- if (error) throw error;
1155
- const rows = data ?? [];
1156
- if (rows.length > 1) {
1157
- throw new RagableError(
1179
+ const many = await this.executeMany();
1180
+ if (many.error) return { data: null, error: many.error };
1181
+ const rows = many.data ?? [];
1182
+ if (rows.length > 1) {
1183
+ return {
1184
+ data: null,
1185
+ error: new RagableError(
1158
1186
  "JSON object requested, multiple (or no) rows returned",
1159
1187
  406,
1160
1188
  { code: "PGRST116" }
1161
- );
1162
- }
1163
- return rows[0] ?? null;
1164
- });
1189
+ )
1190
+ };
1191
+ }
1192
+ return { data: rows[0] ?? null, error: null };
1165
1193
  }
1166
1194
  };
1167
1195
  var PostgrestDeleteRootBuilder = class {
@@ -1293,34 +1321,36 @@ var PostgrestDeleteReturningBuilder = class {
1293
1321
  });
1294
1322
  }
1295
1323
  async single() {
1296
- return asPostgrestResponse(async () => {
1297
- const { data, error } = await this.executeMany();
1298
- if (error) throw error;
1299
- const rows = data ?? [];
1300
- if (rows.length === 0 || rows.length > 1) {
1301
- throw new RagableError(
1324
+ const many = await this.executeMany();
1325
+ if (many.error) return { data: null, error: many.error };
1326
+ const rows = many.data ?? [];
1327
+ if (rows.length === 0 || rows.length > 1) {
1328
+ return {
1329
+ data: null,
1330
+ error: new RagableError(
1302
1331
  "JSON object requested, multiple (or no) rows returned",
1303
1332
  406,
1304
1333
  { code: "PGRST116" }
1305
- );
1306
- }
1307
- return rows[0];
1308
- });
1334
+ )
1335
+ };
1336
+ }
1337
+ return { data: rows[0], error: null };
1309
1338
  }
1310
1339
  async maybeSingle() {
1311
- return asPostgrestResponse(async () => {
1312
- const { data, error } = await this.executeMany();
1313
- if (error) throw error;
1314
- const rows = data ?? [];
1315
- if (rows.length > 1) {
1316
- throw new RagableError(
1340
+ const many = await this.executeMany();
1341
+ if (many.error) return { data: null, error: many.error };
1342
+ const rows = many.data ?? [];
1343
+ if (rows.length > 1) {
1344
+ return {
1345
+ data: null,
1346
+ error: new RagableError(
1317
1347
  "JSON object requested, multiple (or no) rows returned",
1318
1348
  406,
1319
1349
  { code: "PGRST116" }
1320
- );
1321
- }
1322
- return rows[0] ?? null;
1323
- });
1350
+ )
1351
+ };
1352
+ }
1353
+ return { data: rows[0] ?? null, error: null };
1324
1354
  }
1325
1355
  };
1326
1356
  var PostgrestUpsertRootBuilder = class {
@@ -1389,34 +1419,36 @@ var PostgrestUpsertReturningBuilder = class {
1389
1419
  });
1390
1420
  }
1391
1421
  async single() {
1392
- return asPostgrestResponse(async () => {
1393
- const { data, error } = await this.executeMany();
1394
- if (error) throw error;
1395
- const rows = data ?? [];
1396
- if (rows.length === 0 || rows.length > 1) {
1397
- throw new RagableError(
1422
+ const many = await this.executeMany();
1423
+ if (many.error) return { data: null, error: many.error };
1424
+ const rows = many.data ?? [];
1425
+ if (rows.length === 0 || rows.length > 1) {
1426
+ return {
1427
+ data: null,
1428
+ error: new RagableError(
1398
1429
  "JSON object requested, multiple (or no) rows returned",
1399
1430
  406,
1400
1431
  { code: "PGRST116" }
1401
- );
1402
- }
1403
- return rows[0];
1404
- });
1432
+ )
1433
+ };
1434
+ }
1435
+ return { data: rows[0], error: null };
1405
1436
  }
1406
1437
  async maybeSingle() {
1407
- return asPostgrestResponse(async () => {
1408
- const { data, error } = await this.executeMany();
1409
- if (error) throw error;
1410
- const rows = data ?? [];
1411
- if (rows.length > 1) {
1412
- throw new RagableError(
1438
+ const many = await this.executeMany();
1439
+ if (many.error) return { data: null, error: many.error };
1440
+ const rows = many.data ?? [];
1441
+ if (rows.length > 1) {
1442
+ return {
1443
+ data: null,
1444
+ error: new RagableError(
1413
1445
  "JSON object requested, multiple (or no) rows returned",
1414
1446
  406,
1415
1447
  { code: "PGRST116" }
1416
- );
1417
- }
1418
- return rows[0] ?? null;
1419
- });
1448
+ )
1449
+ };
1450
+ }
1451
+ return { data: rows[0] ?? null, error: null };
1420
1452
  }
1421
1453
  };
1422
1454
  var PostgrestTableApi = class {
@@ -1435,11 +1467,14 @@ var PostgrestTableApi = class {
1435
1467
  }
1436
1468
  insert(values, ...rest) {
1437
1469
  if (rest.length > 0) {
1438
- throw new RagableError(
1470
+ const err = new RagableError(
1439
1471
  ".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.",
1440
1472
  400,
1441
1473
  { code: "SDK_INSERT_EXTRA_ARGS" }
1442
1474
  );
1475
+ return new PostgrestInsertSdkErrorRoot(
1476
+ err
1477
+ );
1443
1478
  }
1444
1479
  const rows = Array.isArray(values) ? values : [values];
1445
1480
  return new PostgrestInsertRootBuilder(
@@ -2054,14 +2089,6 @@ async function resolveDatabaseAuthBearer(options, ragableAuth) {
2054
2089
  }
2055
2090
  return key;
2056
2091
  }
2057
- async function parseJsonOrThrow2(response) {
2058
- const payload = await parseMaybeJsonBody(response);
2059
- if (!response.ok) {
2060
- const message = extractErrorMessage(payload, response.statusText);
2061
- throw new RagableError(message, response.status, payload);
2062
- }
2063
- return payload;
2064
- }
2065
2092
  var RagableBrowserAuthClient = class {
2066
2093
  constructor(_options, ragableAuth = null) {
2067
2094
  this.ragableAuth = ragableAuth;
@@ -2158,59 +2185,107 @@ var RagableBrowserAuthClient = class {
2158
2185
  }
2159
2186
  };
2160
2187
  var RagableBrowserDatabaseClient = class {
2161
- constructor(options, ragableAuth = null, postgrestFrom) {
2188
+ constructor(options, ragableAuth = null) {
2162
2189
  this.options = options;
2163
2190
  this.ragableAuth = ragableAuth;
2164
- this.postgrestFrom = postgrestFrom;
2165
2191
  __publicField(this, "fetchImpl");
2192
+ __publicField(this, "_transport", null);
2166
2193
  this.fetchImpl = bindFetch(options.fetch);
2167
2194
  }
2168
- /**
2169
- * PostgREST fluent API — same as `client.from('table')`.
2170
- * Use either the root client or `database` for Supabase-style table access; `database.query()` is raw SQL only.
2171
- */
2195
+ /** @internal Called by RagableBrowser to share the Transport instance. */
2196
+ _setTransport(transport) {
2197
+ this._transport = transport;
2198
+ }
2172
2199
  from(table, databaseInstanceId) {
2173
- if (!this.postgrestFrom) {
2174
- throw new RagableError(
2175
- "database.from() is only available on the client from createBrowserClient().",
2176
- 500,
2177
- { code: "SDK_DATABASE_FROM_UNAVAILABLE" }
2178
- );
2179
- }
2180
- return this.postgrestFrom(table, databaseInstanceId);
2200
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim() || "";
2201
+ const ragableAuth = this.ragableAuth;
2202
+ const opts = this.options;
2203
+ const transport = this._transport;
2204
+ const fetchImpl = this.fetchImpl;
2205
+ const pgFetch = async (params) => {
2206
+ if (!params.databaseInstanceId?.trim()) {
2207
+ throw new RagableError(
2208
+ "database.from() requires databaseInstanceId in client options or as the second argument",
2209
+ 400,
2210
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2211
+ );
2212
+ }
2213
+ const gid = requireAuthGroupId(opts);
2214
+ const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2215
+ const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2216
+ const qs = params.searchParams.toString();
2217
+ const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2218
+ const headers = new Headers(opts.headers);
2219
+ headers.set("Authorization", `Bearer ${token}`);
2220
+ headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2221
+ if (params.body !== void 0) {
2222
+ headers.set("Content-Type", "application/json");
2223
+ }
2224
+ if (params.headers) {
2225
+ for (const [k, v] of Object.entries(params.headers)) {
2226
+ headers.set(k, v);
2227
+ }
2228
+ }
2229
+ if (transport) {
2230
+ return transport.execute({
2231
+ url,
2232
+ method: params.method,
2233
+ headers,
2234
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2235
+ signal: params.signal,
2236
+ idempotencyKey: params.idempotencyKey
2237
+ });
2238
+ }
2239
+ return fetchImpl(url, {
2240
+ method: params.method,
2241
+ headers,
2242
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2243
+ signal: params.signal
2244
+ });
2245
+ };
2246
+ return new PostgrestTableApi(pgFetch, id, table);
2181
2247
  }
2182
2248
  toUrl(path) {
2183
2249
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2184
2250
  }
2185
2251
  async query(params) {
2186
- const gid = requireAuthGroupId(this.options);
2187
- const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2188
- const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2189
- if (!databaseInstanceId) {
2190
- throw new Error(
2191
- "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })"
2252
+ return asPostgrestResponse(async () => {
2253
+ const gid = requireAuthGroupId(this.options);
2254
+ const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2255
+ const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2256
+ if (!databaseInstanceId) {
2257
+ throw new RagableError(
2258
+ "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })",
2259
+ 400,
2260
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2261
+ );
2262
+ }
2263
+ const headers = this.baseHeaders();
2264
+ headers.set("Authorization", `Bearer ${token}`);
2265
+ headers.set("Content-Type", "application/json");
2266
+ const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2267
+ const response = await this.fetchImpl(
2268
+ this.toUrl(`/auth-groups/${gid}/data/query`),
2269
+ {
2270
+ method: "POST",
2271
+ headers,
2272
+ body: JSON.stringify({
2273
+ databaseInstanceId,
2274
+ sql: params.sql,
2275
+ ...params.params !== void 0 ? { params: params.params } : {},
2276
+ readOnly,
2277
+ ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2278
+ ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2279
+ })
2280
+ }
2192
2281
  );
2193
- }
2194
- const headers = this.baseHeaders();
2195
- headers.set("Authorization", `Bearer ${token}`);
2196
- headers.set("Content-Type", "application/json");
2197
- const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2198
- const response = await this.fetchImpl(
2199
- this.toUrl(`/auth-groups/${gid}/data/query`),
2200
- {
2201
- method: "POST",
2202
- headers,
2203
- body: JSON.stringify({
2204
- databaseInstanceId,
2205
- sql: params.sql,
2206
- ...params.params !== void 0 ? { params: params.params } : {},
2207
- readOnly,
2208
- ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2209
- ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2210
- })
2282
+ const payload = await parseMaybeJsonBody(response);
2283
+ if (!response.ok) {
2284
+ const message = extractErrorMessage(payload, response.statusText);
2285
+ throw new RagableError(message, response.status, payload);
2211
2286
  }
2212
- );
2213
- return parseJsonOrThrow2(response);
2287
+ return payload;
2288
+ });
2214
2289
  }
2215
2290
  baseHeaders() {
2216
2291
  return new Headers(this.options.headers);
@@ -2261,9 +2336,7 @@ var RagableBrowser = class {
2261
2336
  __publicField(this, "auth");
2262
2337
  __publicField(this, "database");
2263
2338
  __publicField(this, "transport");
2264
- __publicField(this, "options");
2265
2339
  __publicField(this, "_ragableAuth");
2266
- this.options = options;
2267
2340
  this.transport = new Transport({
2268
2341
  fetch: options.fetch,
2269
2342
  headers: options.headers,
@@ -2294,49 +2367,13 @@ var RagableBrowser = class {
2294
2367
  this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
2295
2368
  this.database = new RagableBrowserDatabaseClient(
2296
2369
  options,
2297
- this._ragableAuth,
2298
- (table, databaseInstanceId) => this.from(table, databaseInstanceId)
2370
+ this._ragableAuth
2299
2371
  );
2372
+ this.database._setTransport(this.transport);
2300
2373
  }
2374
+ /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
2301
2375
  from(table, databaseInstanceId) {
2302
- const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2303
- if (!id) {
2304
- throw new RagableError(
2305
- "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument",
2306
- 400,
2307
- { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2308
- );
2309
- }
2310
- const gid = requireAuthGroupId(this.options);
2311
- const ragableAuth = this._ragableAuth;
2312
- const opts = this.options;
2313
- const transport = this.transport;
2314
- const pgFetch = async (params) => {
2315
- const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2316
- const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2317
- const qs = params.searchParams.toString();
2318
- const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2319
- const headers = new Headers(opts.headers);
2320
- headers.set("Authorization", `Bearer ${token}`);
2321
- headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2322
- if (params.body !== void 0) {
2323
- headers.set("Content-Type", "application/json");
2324
- }
2325
- if (params.headers) {
2326
- for (const [k, v] of Object.entries(params.headers)) {
2327
- headers.set(k, v);
2328
- }
2329
- }
2330
- return transport.execute({
2331
- url,
2332
- method: params.method,
2333
- headers,
2334
- body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2335
- signal: params.signal,
2336
- idempotencyKey: params.idempotencyKey
2337
- });
2338
- };
2339
- return new PostgrestTableApi(pgFetch, id, table);
2376
+ return this.database.from(table, databaseInstanceId);
2340
2377
  }
2341
2378
  destroy() {
2342
2379
  this._ragableAuth?.destroy();
@@ -2446,6 +2483,8 @@ export {
2446
2483
  PostgrestDeleteRootBuilder,
2447
2484
  PostgrestInsertReturningBuilder,
2448
2485
  PostgrestInsertRootBuilder,
2486
+ PostgrestInsertSdkErrorReturning,
2487
+ PostgrestInsertSdkErrorRoot,
2449
2488
  PostgrestSelectBuilder,
2450
2489
  PostgrestTableApi,
2451
2490
  PostgrestUpdateReturningBuilder,