@ragable/sdk 0.6.0 → 0.6.2

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;
@@ -2055,12 +2094,27 @@ var RagableBrowserAuthClient = class {
2055
2094
  }
2056
2095
  };
2057
2096
  var RagableBrowserDatabaseClient = class {
2058
- constructor(options, ragableAuth = null) {
2097
+ constructor(options, ragableAuth = null, postgrestFrom) {
2059
2098
  this.options = options;
2060
2099
  this.ragableAuth = ragableAuth;
2100
+ this.postgrestFrom = postgrestFrom;
2061
2101
  __publicField(this, "fetchImpl");
2062
2102
  this.fetchImpl = bindFetch(options.fetch);
2063
2103
  }
2104
+ /**
2105
+ * PostgREST fluent API — same as `client.from('table')`.
2106
+ * Use either the root client or `database` for Supabase-style table access; `database.query()` is raw SQL only.
2107
+ */
2108
+ from(table, databaseInstanceId) {
2109
+ if (!this.postgrestFrom) {
2110
+ throw new RagableError(
2111
+ "database.from() is only available on the client from createBrowserClient().",
2112
+ 500,
2113
+ { code: "SDK_DATABASE_FROM_UNAVAILABLE" }
2114
+ );
2115
+ }
2116
+ return this.postgrestFrom(table, databaseInstanceId);
2117
+ }
2064
2118
  toUrl(path) {
2065
2119
  return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
2066
2120
  }
@@ -2174,18 +2228,25 @@ var RagableBrowser = class {
2174
2228
  }
2175
2229
  this.agents = new RagableBrowserAgentsClient(options);
2176
2230
  this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
2177
- this.database = new RagableBrowserDatabaseClient(options, this._ragableAuth);
2231
+ this.database = new RagableBrowserDatabaseClient(
2232
+ options,
2233
+ this._ragableAuth,
2234
+ (table, databaseInstanceId) => this.from(table, databaseInstanceId)
2235
+ );
2178
2236
  }
2179
2237
  from(table, databaseInstanceId) {
2180
2238
  const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2181
2239
  if (!id) {
2182
- throw new Error(
2183
- "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument"
2240
+ throw new RagableError(
2241
+ "RagableBrowser.from() requires databaseInstanceId in client options or as the second argument",
2242
+ 400,
2243
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2184
2244
  );
2185
2245
  }
2186
2246
  const gid = requireAuthGroupId(this.options);
2187
2247
  const ragableAuth = this._ragableAuth;
2188
2248
  const opts = this.options;
2249
+ const transport = this.transport;
2189
2250
  const pgFetch = async (params) => {
2190
2251
  const token = await resolveDatabaseAuthBearer(opts, ragableAuth);
2191
2252
  const baseUrl = normalizeBrowserApiBase(opts.baseUrl);
@@ -2202,15 +2263,13 @@ var RagableBrowser = class {
2202
2263
  headers.set(k, v);
2203
2264
  }
2204
2265
  }
2205
- if (params.idempotencyKey) {
2206
- headers.set("Idempotency-Key", params.idempotencyKey);
2207
- }
2208
- const fetchImpl = bindFetch(opts.fetch);
2209
- return fetchImpl(url, {
2266
+ return transport.execute({
2267
+ url,
2210
2268
  method: params.method,
2211
2269
  headers,
2212
2270
  body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
2213
- signal: params.signal
2271
+ signal: params.signal,
2272
+ idempotencyKey: params.idempotencyKey
2214
2273
  });
2215
2274
  };
2216
2275
  return new PostgrestTableApi(pgFetch, id, table);