@ragable/sdk 0.6.5 → 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;
@@ -2170,20 +2197,20 @@ var RagableBrowserDatabaseClient = class {
2170
2197
  this._transport = transport;
2171
2198
  }
2172
2199
  from(table, databaseInstanceId) {
2173
- const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2174
- if (!id) {
2175
- throw new RagableError(
2176
- "database.from() requires databaseInstanceId in client options or as the second argument",
2177
- 400,
2178
- { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2179
- );
2180
- }
2181
- const gid = requireAuthGroupId(this.options);
2200
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim() || "";
2182
2201
  const ragableAuth = this.ragableAuth;
2183
2202
  const opts = this.options;
2184
2203
  const transport = this._transport;
2185
2204
  const fetchImpl = this.fetchImpl;
2186
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);
2187
2214
  const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2188
2215
  const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2189
2216
  const qs = params.searchParams.toString();
@@ -2222,36 +2249,43 @@ var RagableBrowserDatabaseClient = class {
2222
2249
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2223
2250
  }
2224
2251
  async query(params) {
2225
- const gid = requireAuthGroupId(this.options);
2226
- const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2227
- const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2228
- if (!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" }
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
+ }
2233
2281
  );
2234
- }
2235
- const headers = this.baseHeaders();
2236
- headers.set("Authorization", `Bearer ${token}`);
2237
- headers.set("Content-Type", "application/json");
2238
- const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2239
- const response = await this.fetchImpl(
2240
- this.toUrl(`/auth-groups/${gid}/data/query`),
2241
- {
2242
- method: "POST",
2243
- headers,
2244
- body: JSON.stringify({
2245
- databaseInstanceId,
2246
- sql: params.sql,
2247
- ...params.params !== void 0 ? { params: params.params } : {},
2248
- readOnly,
2249
- ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2250
- ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2251
- })
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);
2252
2286
  }
2253
- );
2254
- return parseJsonOrThrow2(response);
2287
+ return payload;
2288
+ });
2255
2289
  }
2256
2290
  baseHeaders() {
2257
2291
  return new Headers(this.options.headers);
@@ -2449,6 +2483,8 @@ export {
2449
2483
  PostgrestDeleteRootBuilder,
2450
2484
  PostgrestInsertReturningBuilder,
2451
2485
  PostgrestInsertRootBuilder,
2486
+ PostgrestInsertSdkErrorReturning,
2487
+ PostgrestInsertSdkErrorRoot,
2452
2488
  PostgrestSelectBuilder,
2453
2489
  PostgrestTableApi,
2454
2490
  PostgrestUpdateReturningBuilder,