@rebasepro/client 0.29.2 → 0.30.0

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/auth.d.ts CHANGED
@@ -63,8 +63,9 @@ export interface CreateAuthOptions {
63
63
  /**
64
64
  * The flow mode a client uses when `authFlowMode` is not set.
65
65
  *
66
- * Cookie in a browser. A backend run by the Rebase runtime always keeps the
67
- * refresh token in a cookie and answers the body's with `""`, so a browser
66
+ * Cookie in a browser. A Rebase backend keeps the refresh token in a cookie
67
+ * and answers the body's with `""` (the runtime always; one booted with
68
+ * `initializeRebaseBackend` unless it sets `cookieAuth: false`), so a browser
68
69
  * client defaulting to json held nothing to renew with, and was signed out
69
70
  * without a word once its access token expired, about an hour after sign-in.
70
71
  *
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Compile-time assertion that what `createRebaseClient()` returns is a
3
+ * `RebaseClient` — the type `<Rebase client={…}>` takes.
4
+ *
5
+ * A plain module under `src`, imported by nothing, for the reason
6
+ * `query-contract.types.ts` gives: `pnpm typecheck` reads `src`, and a type
7
+ * assertion in a jest test is checked by nothing.
8
+ *
9
+ * ## What went wrong that this exists to prevent
10
+ *
11
+ * 0.29.2 added `batch` to `RebaseSdkData`, and the typed data layer kept
12
+ * picking `collection` alone. A `createRebaseClient<Database>()` stopped being
13
+ * a `RebaseClient<Database>`, and every app passing one to `<Rebase>` — the
14
+ * Cloud console among them — stopped compiling. Nothing in this repo passes a
15
+ * typed client anywhere that wants a `RebaseClient`, so nothing here noticed.
16
+ */
17
+ import type { RebaseClient } from "@rebasepro/types";
18
+ /**
19
+ * Shaped the way the generator writes one: an `interface` (no implicit index
20
+ * signature, so the typed branch of `TypedDataLayer` is taken) whose entries
21
+ * carry `Row`, `Insert`, `Update` and `Slug`.
22
+ */
23
+ interface ContractDatabase {
24
+ orders: {
25
+ Row: {
26
+ id: string;
27
+ total: number;
28
+ };
29
+ Insert: {
30
+ total: number;
31
+ };
32
+ Update: {
33
+ total?: number;
34
+ };
35
+ Slug: "orders";
36
+ };
37
+ orderItems: {
38
+ Row: {
39
+ id: string;
40
+ order_id: string;
41
+ sku: string;
42
+ };
43
+ Insert: {
44
+ order_id: string;
45
+ sku: string;
46
+ };
47
+ Update: {
48
+ sku?: string;
49
+ };
50
+ Slug: "order_items";
51
+ };
52
+ }
53
+ export declare const typedIsARebaseClient: RebaseClient<ContractDatabase>;
54
+ export declare const untypedIsARebaseClient: RebaseClient;
55
+ /** `data.batch` takes the accessor names, as `client.batch` does. */
56
+ export declare const typedDataBatch: () => Promise<import("@rebasepro/types").BatchResult<Record<string, unknown>>>;
57
+ export {};
package/dist/index.d.ts CHANGED
@@ -113,7 +113,7 @@ type TypedDataLayer<DB> = {
113
113
  collection<S extends string>(slug: S): CollectionClient<RowOf<DBEntry<DB, S>>, InsertOf<DBEntry<DB, S>>, UpdateOf<DBEntry<DB, S>>, IncludeOf<DBEntry<DB, S>, DB>>;
114
114
  } & {
115
115
  [K in keyof DB]: CollectionClient<RowOf<DB[K]>, InsertOf<DB[K]>, UpdateOf<DB[K]>, IncludeOf<DB[K], DB>>;
116
- } & (string extends keyof DB ? RebaseSdkData : Pick<RebaseSdkData, "collection">);
116
+ } & (string extends keyof DB ? RebaseSdkData : Pick<RebaseSdkData, "collection"> & Pick<RebaseSdkData<DB>, "batch">);
117
117
  /**
118
118
  * The return type of `createRebaseClient<DB>()`.
119
119
  *
package/dist/index.es.js CHANGED
@@ -166,8 +166,9 @@ function detectStorage() {
166
166
  /**
167
167
  * The flow mode a client uses when `authFlowMode` is not set.
168
168
  *
169
- * Cookie in a browser. A backend run by the Rebase runtime always keeps the
170
- * refresh token in a cookie and answers the body's with `""`, so a browser
169
+ * Cookie in a browser. A Rebase backend keeps the refresh token in a cookie
170
+ * and answers the body's with `""` (the runtime always; one booted with
171
+ * `initializeRebaseBackend` unless it sets `cookieAuth: false`), so a browser
171
172
  * client defaulting to json held nothing to renew with, and was signed out
172
173
  * without a word once its access token expired, about an hour after sign-in.
173
174
  *
@@ -5363,7 +5364,39 @@ function createRebaseClient(options) {
5363
5364
  }
5364
5365
  return collectionClients.get(slug);
5365
5366
  }
5366
- const dataTarget = { collection };
5367
+ /**
5368
+ * `client.batch()`, and `client.data.batch()` — the same function, so the
5369
+ * HTTP client's data layer is the `RebaseSdkData` that `<Rebase>` and the
5370
+ * in-process `context.data` take.
5371
+ */
5372
+ const batch = async (operations, batchOptions) => {
5373
+ if (!Array.isArray(operations)) throw invalidBulkBody("batch expects an array of operations.");
5374
+ if (operations.length === 0) return {
5375
+ data: [],
5376
+ meta: { operations: 0 }
5377
+ };
5378
+ const headers = {};
5379
+ if (batchOptions?.idempotencyKey) headers["Idempotency-Key"] = batchOptions.idempotencyKey;
5380
+ if (batchOptions?.returning === false) headers.Prefer = "return=minimal";
5381
+ const dictionary = options.collections;
5382
+ const wire = dictionary ? operations.map((operation) => {
5383
+ if (!operation || typeof operation !== "object") return operation;
5384
+ const name = operation.collection;
5385
+ return typeof name === "string" && Object.hasOwn(dictionary, name) ? {
5386
+ ...operation,
5387
+ collection: dictionary[name]
5388
+ } : operation;
5389
+ }) : operations;
5390
+ return transport.request("/data/_batch", {
5391
+ method: "POST",
5392
+ body: JSON.stringify({ operations: wire }),
5393
+ ...Object.keys(headers).length > 0 ? { headers } : {}
5394
+ });
5395
+ };
5396
+ const dataTarget = {
5397
+ collection,
5398
+ batch
5399
+ };
5367
5400
  /**
5368
5401
  * Names the proxy never serves as a collection: the ones promise and
5369
5402
  * serialisation machinery probe for (`then`, `toJSON`, React's
@@ -5378,6 +5411,7 @@ function createRebaseClient(options) {
5378
5411
  const dataProxy = new Proxy(dataTarget, {
5379
5412
  get(_target, prop) {
5380
5413
  if (prop === "collection") return collection;
5414
+ if (prop === "batch") return batch;
5381
5415
  if (typeof prop === "symbol") return void 0;
5382
5416
  if (isProbedName(prop)) return void 0;
5383
5417
  if (options.collections && Object.hasOwn(options.collections, prop)) return collection(options.collections[prop]);
@@ -5404,7 +5438,7 @@ function createRebaseClient(options) {
5404
5438
  return collection(slug);
5405
5439
  },
5406
5440
  has(_target, prop) {
5407
- if (prop === "collection") return true;
5441
+ if (prop === "collection" || prop === "batch") return true;
5408
5442
  if (typeof prop === "symbol") return false;
5409
5443
  if (isProbedName(prop)) return false;
5410
5444
  if (options.collections) return Object.hasOwn(options.collections, prop);
@@ -5502,30 +5536,7 @@ function createRebaseClient(options) {
5502
5536
  body: payload !== void 0 ? JSON.stringify(payload) : void 0
5503
5537
  });
5504
5538
  },
5505
- batch: async (operations, batchOptions) => {
5506
- if (!Array.isArray(operations)) throw invalidBulkBody("batch expects an array of operations.");
5507
- if (operations.length === 0) return {
5508
- data: [],
5509
- meta: { operations: 0 }
5510
- };
5511
- const headers = {};
5512
- if (batchOptions?.idempotencyKey) headers["Idempotency-Key"] = batchOptions.idempotencyKey;
5513
- if (batchOptions?.returning === false) headers.Prefer = "return=minimal";
5514
- const dictionary = options.collections;
5515
- const wire = dictionary ? operations.map((operation) => {
5516
- if (!operation || typeof operation !== "object") return operation;
5517
- const name = operation.collection;
5518
- return typeof name === "string" && Object.hasOwn(dictionary, name) ? {
5519
- ...operation,
5520
- collection: dictionary[name]
5521
- } : operation;
5522
- }) : operations;
5523
- return transport.request("/data/_batch", {
5524
- method: "POST",
5525
- body: JSON.stringify({ operations: wire }),
5526
- ...Object.keys(headers).length > 0 ? { headers } : {}
5527
- });
5528
- },
5539
+ batch,
5529
5540
  data: dataProxy,
5530
5541
  ...offlineManager ? { offline: offlineManager.api } : {}
5531
5542
  };