@ragable/sdk 0.6.0 → 0.6.1

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
@@ -15,6 +15,13 @@ var RagableSdkError = class extends Error {
15
15
  super(message);
16
16
  this.name = this.constructor.name;
17
17
  }
18
+ toJSON() {
19
+ return {
20
+ name: this.name,
21
+ message: this.message,
22
+ __type: this.__type
23
+ };
24
+ }
18
25
  };
19
26
  var RagableError = class extends RagableSdkError {
20
27
  constructor(message, status, body) {
@@ -26,11 +33,17 @@ var RagableError = class extends RagableSdkError {
26
33
  __publicField(this, "details");
27
34
  this.status = status;
28
35
  this.body = body;
29
- if (body && typeof body === "object") {
30
- const b = body;
31
- this.code = typeof b.code === "string" ? b.code : void 0;
32
- this.details = typeof b.details === "string" ? b.details : void 0;
33
- }
36
+ this.code = body && typeof body === "object" ? typeof body.code === "string" ? body.code : void 0 : void 0;
37
+ this.details = body && typeof body === "object" ? typeof body.details === "string" ? body.details : void 0 : void 0;
38
+ }
39
+ toJSON() {
40
+ return {
41
+ ...super.toJSON(),
42
+ status: this.status,
43
+ body: this.body,
44
+ code: this.code,
45
+ details: this.details
46
+ };
34
47
  }
35
48
  };
36
49
  var RagableNetworkError = class extends RagableSdkError {
@@ -40,6 +53,12 @@ var RagableNetworkError = class extends RagableSdkError {
40
53
  __publicField(this, "cause");
41
54
  this.cause = cause;
42
55
  }
56
+ toJSON() {
57
+ return {
58
+ ...super.toJSON(),
59
+ cause: this.cause instanceof Error ? this.cause.message : this.cause
60
+ };
61
+ }
43
62
  };
44
63
  var RagableAbortError = class extends RagableSdkError {
45
64
  constructor(message = "Request aborted") {
@@ -54,6 +73,12 @@ var RagableTimeoutError = class extends RagableSdkError {
54
73
  __publicField(this, "timeoutMs");
55
74
  this.timeoutMs = timeoutMs;
56
75
  }
76
+ toJSON() {
77
+ return {
78
+ ...super.toJSON(),
79
+ timeoutMs: this.timeoutMs
80
+ };
81
+ }
57
82
  };
58
83
  function extractErrorMessage(payload, fallback) {
59
84
  if (payload && typeof payload === "object") {
@@ -570,7 +595,15 @@ async function asPostgrestResponse(fn) {
570
595
  const data = await fn();
571
596
  return { data, error: null };
572
597
  } catch (e) {
573
- const err = e instanceof RagableError ? e : new RagableError(e.message, 500, null);
598
+ let err;
599
+ if (e instanceof RagableError) {
600
+ err = e;
601
+ } else if (e instanceof RagableSdkError) {
602
+ err = new RagableError(e.message, 0, { originalError: e.__type, cause: e.message });
603
+ } else {
604
+ const message = e instanceof Error ? e.message : typeof e === "string" ? e : "Unknown error";
605
+ err = new RagableError(message, 0, null);
606
+ }
574
607
  return { data: null, error: err };
575
608
  }
576
609
  }
@@ -1917,8 +1950,10 @@ function normalizeBrowserApiBase(baseUrl) {
1917
1950
  function requireAuthGroupId(options) {
1918
1951
  const id = options.authGroupId?.trim();
1919
1952
  if (!id) {
1920
- throw new Error(
1921
- "authGroupId is required for auth and database methods on the browser client"
1953
+ throw new RagableError(
1954
+ "authGroupId is required for auth and database methods on the browser client",
1955
+ 400,
1956
+ { code: "SDK_MISSING_AUTH_GROUP_ID" }
1922
1957
  );
1923
1958
  }
1924
1959
  return id;
@@ -1933,8 +1968,10 @@ async function requireAccessToken(options, ragableAuth) {
1933
1968
  const token = await getter();
1934
1969
  if (token?.trim()) return token.trim();
1935
1970
  }
1936
- throw new Error(
1937
- "No access token available (sign in or provide getAccessToken)"
1971
+ throw new RagableError(
1972
+ "No access token available. Sign in first with auth.signInWithPassword() or provide getAccessToken callback.",
1973
+ 401,
1974
+ { code: "SDK_NO_ACCESS_TOKEN" }
1938
1975
  );
1939
1976
  }
1940
1977
  async function resolveDatabaseAuthBearer(options, ragableAuth) {
@@ -1945,8 +1982,10 @@ async function resolveDatabaseAuthBearer(options, ragableAuth) {
1945
1982
  const fromGetter = options.getDataStaticKey ? await options.getDataStaticKey() : null;
1946
1983
  const key = (fromGetter?.trim() || options.dataStaticKey?.trim()) ?? "";
1947
1984
  if (!key) {
1948
- throw new Error(
1949
- mode === "publicAnon" ? "dataAuth publicAnon requires getDataStaticKey or dataStaticKey" : "dataAuth admin requires getDataStaticKey or dataStaticKey"
1985
+ throw new RagableError(
1986
+ mode === "publicAnon" ? "dataAuth publicAnon requires getDataStaticKey or dataStaticKey" : "dataAuth admin requires getDataStaticKey or dataStaticKey",
1987
+ 400,
1988
+ { code: "SDK_MISSING_STATIC_KEY" }
1950
1989
  );
1951
1990
  }
1952
1991
  return key;
@@ -2179,13 +2218,16 @@ var RagableBrowser = class {
2179
2218
  from(table, databaseInstanceId) {
2180
2219
  const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2181
2220
  if (!id) {
2182
- throw new Error(
2183
- "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument"
2221
+ throw new RagableError(
2222
+ "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument",
2223
+ 400,
2224
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2184
2225
  );
2185
2226
  }
2186
2227
  const gid = requireAuthGroupId(this.options);
2187
2228
  const ragableAuth = this._ragableAuth;
2188
2229
  const opts = this.options;
2230
+ const transport = this.transport;
2189
2231
  const pgFetch = async (params) => {
2190
2232
  const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2191
2233
  const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
@@ -2202,15 +2244,13 @@ var RagableBrowser = class {
2202
2244
  headers.set(k, v);
2203
2245
  }
2204
2246
  }
2205
- if (params.idempotencyKey) {
2206
- headers.set("Idempotency-Key", params.idempotencyKey);
2207
- }
2208
- const fetchImpl = bindFetch(opts.fetch);
2209
- return fetchImpl(url, {
2247
+ return transport.execute({
2248
+ url,
2210
2249
  method: params.method,
2211
2250
  headers,
2212
2251
  body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2213
- signal: params.signal
2252
+ signal: params.signal,
2253
+ idempotencyKey: params.idempotencyKey
2214
2254
  });
2215
2255
  };
2216
2256
  return new PostgrestTableApi(pgFetch, id, table);