@ragable/sdk 0.6.2 → 0.6.4

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() {
@@ -45,7 +51,38 @@ var RagableError = class extends RagableSdkError {
45
51
  details: this.details
46
52
  };
47
53
  }
54
+ /** Stable string for logs — avoids `{}` when coercing or stringifying. */
55
+ toString() {
56
+ const bits = [`${this.name}: ${this.message}`];
57
+ if (this.status) bits.push(`status=${this.status}`);
58
+ if (this.code) bits.push(`code=${this.code}`);
59
+ return bits.join(" \xB7 ");
60
+ }
48
61
  };
62
+ function formatSdkError(err) {
63
+ if (err instanceof RagableError) {
64
+ return `${err.message} (HTTP ${err.status}${err.code ? `, ${err.code}` : ""})`;
65
+ }
66
+ if (err instanceof RagableSdkError) {
67
+ return err.message;
68
+ }
69
+ if (err instanceof Error) {
70
+ return err.message || err.name;
71
+ }
72
+ if (typeof err === "string") return err;
73
+ if (err && typeof err === "object") {
74
+ try {
75
+ const s = JSON.stringify(err);
76
+ if (s !== "{}") return s;
77
+ return "Unknown error (empty object \u2014 avoid `{...error}` spread; use error.message or formatSdkError)";
78
+ } catch {
79
+ }
80
+ }
81
+ return String(err);
82
+ }
83
+ function formatPostgrestError(error) {
84
+ return formatSdkError(error);
85
+ }
49
86
  var RagableNetworkError = class extends RagableSdkError {
50
87
  constructor(message, cause) {
51
88
  super(message);
@@ -615,6 +652,27 @@ function encodeFilterValue(op, value) {
615
652
  }
616
653
  return `${op}.${value}`;
617
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
+ }
618
676
  async function parsePostgRESTResponse(response) {
619
677
  if (response.status === 204) return null;
620
678
  const text = await response.text();
@@ -630,9 +688,8 @@ async function parsePostgRESTResponse(response) {
630
688
  );
631
689
  }
632
690
  if (!response.ok) {
633
- const msg = typeof payload === "object" && payload !== null ? payload.message ?? payload.error ?? response.statusText : response.statusText;
634
- const code = typeof payload === "object" && payload !== null ? payload.code : void 0;
635
- 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);
636
693
  }
637
694
  return payload;
638
695
  }
@@ -1376,7 +1433,14 @@ var PostgrestTableApi = class {
1376
1433
  columns
1377
1434
  );
1378
1435
  }
1379
- insert(values) {
1436
+ insert(values, ...rest) {
1437
+ if (rest.length > 0) {
1438
+ throw new RagableError(
1439
+ ".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
+ 400,
1441
+ { code: "SDK_INSERT_EXTRA_ARGS" }
1442
+ );
1443
+ }
1380
1444
  const rows = Array.isArray(values) ? values : [values];
1381
1445
  return new PostgrestInsertRootBuilder(
1382
1446
  this.pgFetch,
@@ -2412,7 +2476,9 @@ export {
2412
2476
  createRagableServerClient,
2413
2477
  detectStorage,
2414
2478
  extractErrorMessage,
2479
+ formatPostgrestError,
2415
2480
  formatRetrievalContext,
2481
+ formatSdkError,
2416
2482
  generateIdempotencyKey,
2417
2483
  normalizeBrowserApiBase,
2418
2484
  parseSseDataLine,