@ragable/sdk 0.6.5 → 0.6.7

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;
@@ -2163,96 +2190,107 @@ var RagableBrowserDatabaseClient = class {
2163
2190
  this.ragableAuth = ragableAuth;
2164
2191
  __publicField(this, "fetchImpl");
2165
2192
  __publicField(this, "_transport", null);
2166
- this.fetchImpl = bindFetch(options.fetch);
2167
- }
2168
- /** @internal Called by RagableBrowser to share the Transport instance. */
2169
- _setTransport(transport) {
2170
- this._transport = transport;
2171
- }
2172
- 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);
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);
2193
+ /**
2194
+ * PostgREST table access. Instance field so `client.database.from` is always an own,
2195
+ * enumerable function (avoids rare prototype/bundler issues).
2196
+ */
2197
+ __publicField(this, "from", (table, databaseInstanceId) => {
2198
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim() || "";
2199
+ const ragableAuth = this.ragableAuth;
2200
+ const opts = this.options;
2201
+ const transport = this._transport;
2202
+ const fetchImpl = this.fetchImpl;
2203
+ const pgFetch = async (params) => {
2204
+ if (!params.databaseInstanceId?.trim()) {
2205
+ throw new RagableError(
2206
+ "database.from() requires databaseInstanceId in client options or as the second argument",
2207
+ 400,
2208
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2209
+ );
2200
2210
  }
2201
- }
2202
- if (transport) {
2203
- return transport.execute({
2204
- url,
2211
+ const gid = requireAuthGroupId(opts);
2212
+ const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2213
+ const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
2214
+ const qs = params.searchParams.toString();
2215
+ const url = `${baseUrl}/auth-groups/${gid}/data/rest/${params.table}${qs ? `?${qs}` : ""}`;
2216
+ const headers = new Headers(opts.headers);
2217
+ headers.set("Authorization", `Bearer ${token}`);
2218
+ headers.set("X-Database-Instance-Id", params.databaseInstanceId);
2219
+ if (params.body !== void 0) {
2220
+ headers.set("Content-Type", "application/json");
2221
+ }
2222
+ if (params.headers) {
2223
+ for (const [k, v] of Object.entries(params.headers)) {
2224
+ headers.set(k, v);
2225
+ }
2226
+ }
2227
+ if (transport) {
2228
+ return transport.execute({
2229
+ url,
2230
+ method: params.method,
2231
+ headers,
2232
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2233
+ signal: params.signal,
2234
+ idempotencyKey: params.idempotencyKey
2235
+ });
2236
+ }
2237
+ return fetchImpl(url, {
2205
2238
  method: params.method,
2206
2239
  headers,
2207
2240
  body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2208
- signal: params.signal,
2209
- idempotencyKey: params.idempotencyKey
2241
+ signal: params.signal
2210
2242
  });
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
2243
+ };
2244
+ return new PostgrestTableApi(pgFetch, id, table);
2245
+ });
2246
+ __publicField(this, "query", async (params) => {
2247
+ return asPostgrestResponse(async () => {
2248
+ const gid = requireAuthGroupId(this.options);
2249
+ const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2250
+ const databaseInstanceId = params.databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2251
+ if (!databaseInstanceId) {
2252
+ throw new RagableError(
2253
+ "database.query requires databaseInstanceId in the request or on createBrowserClient({ databaseInstanceId })",
2254
+ 400,
2255
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2256
+ );
2257
+ }
2258
+ const headers = this.baseHeaders();
2259
+ headers.set("Authorization", `Bearer ${token}`);
2260
+ headers.set("Content-Type", "application/json");
2261
+ const readOnly = (this.options.dataAuth ?? "user") === "publicAnon" ? true : params.readOnly !== false;
2262
+ const response = await this.fetchImpl(
2263
+ this.toUrl(`/auth-groups/${gid}/data/query`),
2264
+ {
2265
+ method: "POST",
2266
+ headers,
2267
+ body: JSON.stringify({
2268
+ databaseInstanceId,
2269
+ sql: params.sql,
2270
+ ...params.params !== void 0 ? { params: params.params } : {},
2271
+ readOnly,
2272
+ ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
2273
+ ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
2274
+ })
2275
+ }
2276
+ );
2277
+ const payload = await parseMaybeJsonBody(response);
2278
+ if (!response.ok) {
2279
+ const message = extractErrorMessage(payload, response.statusText);
2280
+ throw new RagableError(message, response.status, payload);
2281
+ }
2282
+ return payload;
2217
2283
  });
2218
- };
2219
- return new PostgrestTableApi(pgFetch, id, table);
2284
+ });
2285
+ this.fetchImpl = bindFetch(options.fetch);
2286
+ }
2287
+ /** @internal Called by RagableBrowser to share the Transport instance. */
2288
+ _setTransport(transport) {
2289
+ this._transport = transport;
2220
2290
  }
2221
2291
  toUrl(path) {
2222
2292
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2223
2293
  }
2224
- 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" }
2233
- );
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
- })
2252
- }
2253
- );
2254
- return parseJsonOrThrow2(response);
2255
- }
2256
2294
  baseHeaders() {
2257
2295
  return new Headers(this.options.headers);
2258
2296
  }
@@ -2303,6 +2341,10 @@ var RagableBrowser = class {
2303
2341
  __publicField(this, "database");
2304
2342
  __publicField(this, "transport");
2305
2343
  __publicField(this, "_ragableAuth");
2344
+ /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
2345
+ __publicField(this, "from", (table, databaseInstanceId) => {
2346
+ return this.database.from(table, databaseInstanceId);
2347
+ });
2306
2348
  this.transport = new Transport({
2307
2349
  fetch: options.fetch,
2308
2350
  headers: options.headers,
@@ -2337,10 +2379,6 @@ var RagableBrowser = class {
2337
2379
  );
2338
2380
  this.database._setTransport(this.transport);
2339
2381
  }
2340
- /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
2341
- from(table, databaseInstanceId) {
2342
- return this.database.from(table, databaseInstanceId);
2343
- }
2344
2382
  destroy() {
2345
2383
  this._ragableAuth?.destroy();
2346
2384
  }
@@ -2449,6 +2487,8 @@ export {
2449
2487
  PostgrestDeleteRootBuilder,
2450
2488
  PostgrestInsertReturningBuilder,
2451
2489
  PostgrestInsertRootBuilder,
2490
+ PostgrestInsertSdkErrorReturning,
2491
+ PostgrestInsertSdkErrorRoot,
2452
2492
  PostgrestSelectBuilder,
2453
2493
  PostgrestTableApi,
2454
2494
  PostgrestUpdateReturningBuilder,