@ragable/sdk 0.6.19 → 0.6.21

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
@@ -627,6 +627,17 @@ async function parseTransportResponse(response) {
627
627
  }
628
628
 
629
629
  // src/browser-postgrest.ts
630
+ function toRagableResult(r) {
631
+ if (r.error) return { ok: false, error: r.error };
632
+ return { ok: true, value: r.data };
633
+ }
634
+ function assertPostgrestSuccess(r) {
635
+ if (r.error) throw r.error;
636
+ }
637
+ function unwrapPostgrest(r) {
638
+ if (r.error) throw r.error;
639
+ return r.data;
640
+ }
630
641
  async function asPostgrestResponse(fn) {
631
642
  try {
632
643
  const data = await fn();
@@ -1746,6 +1757,8 @@ var RagableAuth = class {
1746
1757
  const refreshed = await this._doRefresh(session.refresh_token);
1747
1758
  if (refreshed) {
1748
1759
  this.currentSession = refreshed;
1760
+ } else {
1761
+ await this.storage.removeItem(this.storageKey);
1749
1762
  }
1750
1763
  }
1751
1764
  }
@@ -1763,7 +1776,8 @@ var RagableAuth = class {
1763
1776
  const raw = await this.fetchAuth("/register", "POST", {
1764
1777
  email: credentials.email,
1765
1778
  password: credentials.password,
1766
- ...name !== void 0 ? { name } : {}
1779
+ ...name !== void 0 ? { name } : {},
1780
+ ...credentials.options?.data !== void 0 ? { data: credentials.options.data } : {}
1767
1781
  });
1768
1782
  const session = this.rawToSession(raw);
1769
1783
  await this.setSessionInternal(session, "SIGNED_IN");
@@ -1833,7 +1847,9 @@ var RagableAuth = class {
1833
1847
  const token = this.currentSession?.access_token;
1834
1848
  if (!token) throw new RagableError("Not authenticated", 401, null);
1835
1849
  const result = await this.fetchAuthWithBearer("/me", "PATCH", token, {
1850
+ ...attributes.email !== void 0 ? { email: attributes.email } : {},
1836
1851
  ...attributes.password !== void 0 ? { password: attributes.password } : {},
1852
+ ...attributes.data !== void 0 ? { data: attributes.data } : {},
1837
1853
  ...attributes.data?.name !== void 0 ? { name: attributes.data.name } : {}
1838
1854
  });
1839
1855
  if (this.currentSession) {
@@ -1858,6 +1874,17 @@ var RagableAuth = class {
1858
1874
  getAccessToken() {
1859
1875
  return this.currentSession?.access_token ?? null;
1860
1876
  }
1877
+ async getValidAccessToken() {
1878
+ if (!this.initialized) await this.initialize();
1879
+ const session = this.currentSession;
1880
+ if (!session) return null;
1881
+ const secondsUntilExpiry = session.expires_at - nowSeconds();
1882
+ if (secondsUntilExpiry <= this.refreshSkewSeconds) {
1883
+ const refreshed = await this.singleFlightRefresh(session.refresh_token);
1884
+ return refreshed?.access_token ?? null;
1885
+ }
1886
+ return session.access_token;
1887
+ }
1861
1888
  getCurrentSession() {
1862
1889
  return this.currentSession;
1863
1890
  }
@@ -2071,7 +2098,7 @@ function requireAuthGroupId(options) {
2071
2098
  }
2072
2099
  async function requireAccessToken(options, ragableAuth) {
2073
2100
  if (ragableAuth) {
2074
- const token = ragableAuth.getAccessToken();
2101
+ const token = await ragableAuth.getValidAccessToken();
2075
2102
  if (token) return token;
2076
2103
  }
2077
2104
  const getter = options.getAccessToken;
@@ -2196,24 +2223,71 @@ var RagableBrowserAuthClient = class {
2196
2223
  return this.auth.getSession();
2197
2224
  }
2198
2225
  };
2226
+ function collectionRecordToRowWithMeta(record) {
2227
+ const { data, id, createdAt, updatedAt } = record;
2228
+ return { ...data, meta: { id, createdAt, updatedAt } };
2229
+ }
2230
+ function collectionRecordsToRowWithMeta(records) {
2231
+ return records.map(collectionRecordToRowWithMeta);
2232
+ }
2233
+ var FIND_QUERY_KEYS = [
2234
+ "where",
2235
+ "filters",
2236
+ "limit",
2237
+ "offset",
2238
+ "orderBy",
2239
+ "orderDirection",
2240
+ "return"
2241
+ ];
2199
2242
  var BrowserCollectionApi = class {
2200
2243
  constructor(database, name, databaseInstanceId) {
2201
2244
  this.database = database;
2202
2245
  this.name = name;
2203
2246
  this.databaseInstanceId = databaseInstanceId;
2204
- __publicField(this, "find", (whereOrParams = {}) => {
2205
- const hasQueryKeys = ["where", "filters", "limit", "offset", "orderBy", "orderDirection"].some(
2206
- (key) => Object.prototype.hasOwnProperty.call(whereOrParams, key)
2207
- );
2208
- const body = hasQueryKeys ? whereOrParams : { where: whereOrParams };
2209
- return asPostgrestResponse(
2210
- () => this.database._requestCollection(
2211
- "POST",
2212
- `/${encodeURIComponent(this.name)}/find`,
2213
- body,
2214
- this.databaseInstanceId
2215
- )
2216
- );
2247
+ __publicField(this, "requestFind", (body) => asPostgrestResponse(
2248
+ () => this.database._requestCollection(
2249
+ "POST",
2250
+ `/${encodeURIComponent(this.name)}/find`,
2251
+ body,
2252
+ this.databaseInstanceId
2253
+ )
2254
+ ));
2255
+ /**
2256
+ * Query collection rows. Prefer this over the deprecated `find` alias.
2257
+ * Use `return: "flat"` to get {@link CollectionRowWithMeta} without nested `.data`.
2258
+ */
2259
+ __publicField(this, "findMany", async (whereOrParams = {}) => {
2260
+ const { returnMode, body } = this.normalizeFindArgs(whereOrParams);
2261
+ const res = await this.requestFind(body);
2262
+ if (res.error) return res;
2263
+ if (returnMode === "flat") {
2264
+ return {
2265
+ data: collectionRecordsToRowWithMeta(res.data),
2266
+ error: null
2267
+ };
2268
+ }
2269
+ return res;
2270
+ });
2271
+ /**
2272
+ * @deprecated Use {@link BrowserCollectionApi.findMany} — same behavior.
2273
+ */
2274
+ __publicField(this, "find", (whereOrParams = {}) => this.findMany(whereOrParams));
2275
+ /**
2276
+ * At most one row, `data` is the record or `null` if none match (not an error).
2277
+ */
2278
+ __publicField(this, "findFirst", async (whereOrParams = {}) => {
2279
+ const { body } = this.normalizeFindArgs(whereOrParams);
2280
+ const withCap = { ...body, limit: 1, offset: body["offset"] ?? 0 };
2281
+ const res = await this.requestFind(withCap);
2282
+ if (res.error) return res;
2283
+ return { data: res.data[0] ?? null, error: null };
2284
+ });
2285
+ /**
2286
+ * Lookup by primary key `id` (envelope). Equivalent to
2287
+ * `findFirst({ where: { id }, limit: 1 })` with a typed `where.id`.
2288
+ */
2289
+ __publicField(this, "findUnique", async (args) => {
2290
+ return this.findFirst({ where: args.where });
2217
2291
  });
2218
2292
  __publicField(this, "insert", (data) => asPostgrestResponse(
2219
2293
  () => this.database._requestCollection(
@@ -2223,6 +2297,9 @@ var BrowserCollectionApi = class {
2223
2297
  this.databaseInstanceId
2224
2298
  )
2225
2299
  ));
2300
+ /**
2301
+ * Update rows matching `where` (JSON fields, plus envelope `id` / `createdAt` / `updatedAt`).
2302
+ */
2226
2303
  __publicField(this, "update", (where, patch, options) => asPostgrestResponse(
2227
2304
  () => this.database._requestCollection(
2228
2305
  "PATCH",
@@ -2231,6 +2308,15 @@ var BrowserCollectionApi = class {
2231
2308
  this.databaseInstanceId
2232
2309
  )
2233
2310
  ));
2311
+ /**
2312
+ * Like {@link BrowserCollectionApi.update} but the success payload includes
2313
+ * `meta.count` (number of rows returned from the update, bounded by `limit`).
2314
+ */
2315
+ __publicField(this, "updateMany", async (where, patch, options) => {
2316
+ const r = await this.update(where, patch, options);
2317
+ if (r.error) return r;
2318
+ return { data: { records: r.data, meta: { count: r.data.length } }, error: null };
2319
+ });
2234
2320
  __publicField(this, "delete", (where, options) => asPostgrestResponse(
2235
2321
  () => this.database._requestCollection(
2236
2322
  "DELETE",
@@ -2240,6 +2326,15 @@ var BrowserCollectionApi = class {
2240
2326
  )
2241
2327
  ));
2242
2328
  }
2329
+ normalizeFindArgs(whereOrParams) {
2330
+ const hasQueryKeys = typeof whereOrParams === "object" && whereOrParams !== null && FIND_QUERY_KEYS.some(
2331
+ (key) => Object.prototype.hasOwnProperty.call(whereOrParams, key)
2332
+ );
2333
+ const raw = hasQueryKeys ? { ...whereOrParams } : { where: whereOrParams };
2334
+ const returnMode = raw["return"] === "flat" ? "flat" : "envelope";
2335
+ delete raw["return"];
2336
+ return { returnMode, body: raw };
2337
+ }
2243
2338
  };
2244
2339
  var RagableBrowserDatabaseClient = class {
2245
2340
  constructor(options, ragableAuth = null) {
@@ -2690,10 +2785,7 @@ var RagableBrowser = class {
2690
2785
  });
2691
2786
  this.transport.setRefreshHandler(async () => {
2692
2787
  if (effectiveDataAuth(options) !== "user") return null;
2693
- const session = await this._ragableAuth.singleFlightRefresh(
2694
- this._ragableAuth.getCurrentSession()?.refresh_token ?? ""
2695
- );
2696
- return session?.access_token ?? null;
2788
+ return this._ragableAuth.getValidAccessToken();
2697
2789
  });
2698
2790
  if (!options.getAccessToken && effectiveDataAuth(options) === "user") {
2699
2791
  this._ragableAuth.initialize().catch(() => {
@@ -2835,7 +2927,10 @@ export {
2835
2927
  ShiftClient,
2836
2928
  Transport,
2837
2929
  asPostgrestResponse,
2930
+ assertPostgrestSuccess,
2838
2931
  bindFetch,
2932
+ collectionRecordToRowWithMeta,
2933
+ collectionRecordsToRowWithMeta,
2839
2934
  createBrowserClient,
2840
2935
  createClient,
2841
2936
  createRagPipeline,
@@ -2851,6 +2946,8 @@ export {
2851
2946
  normalizeBrowserApiBase,
2852
2947
  parseSseDataLine,
2853
2948
  parseTransportResponse,
2854
- readSseStream
2949
+ readSseStream,
2950
+ toRagableResult,
2951
+ unwrapPostgrest
2855
2952
  };
2856
2953
  //# sourceMappingURL=index.mjs.map