@rebasepro/client 0.29.2 → 0.29.3
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/client-contract.types.d.ts +57 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +36 -26
- package/dist/index.es.js.map +1 -1
- package/package.json +4 -4
|
@@ -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
|
@@ -5363,7 +5363,39 @@ function createRebaseClient(options) {
|
|
|
5363
5363
|
}
|
|
5364
5364
|
return collectionClients.get(slug);
|
|
5365
5365
|
}
|
|
5366
|
-
|
|
5366
|
+
/**
|
|
5367
|
+
* `client.batch()`, and `client.data.batch()` — the same function, so the
|
|
5368
|
+
* HTTP client's data layer is the `RebaseSdkData` that `<Rebase>` and the
|
|
5369
|
+
* in-process `context.data` take.
|
|
5370
|
+
*/
|
|
5371
|
+
const batch = async (operations, batchOptions) => {
|
|
5372
|
+
if (!Array.isArray(operations)) throw invalidBulkBody("batch expects an array of operations.");
|
|
5373
|
+
if (operations.length === 0) return {
|
|
5374
|
+
data: [],
|
|
5375
|
+
meta: { operations: 0 }
|
|
5376
|
+
};
|
|
5377
|
+
const headers = {};
|
|
5378
|
+
if (batchOptions?.idempotencyKey) headers["Idempotency-Key"] = batchOptions.idempotencyKey;
|
|
5379
|
+
if (batchOptions?.returning === false) headers.Prefer = "return=minimal";
|
|
5380
|
+
const dictionary = options.collections;
|
|
5381
|
+
const wire = dictionary ? operations.map((operation) => {
|
|
5382
|
+
if (!operation || typeof operation !== "object") return operation;
|
|
5383
|
+
const name = operation.collection;
|
|
5384
|
+
return typeof name === "string" && Object.hasOwn(dictionary, name) ? {
|
|
5385
|
+
...operation,
|
|
5386
|
+
collection: dictionary[name]
|
|
5387
|
+
} : operation;
|
|
5388
|
+
}) : operations;
|
|
5389
|
+
return transport.request("/data/_batch", {
|
|
5390
|
+
method: "POST",
|
|
5391
|
+
body: JSON.stringify({ operations: wire }),
|
|
5392
|
+
...Object.keys(headers).length > 0 ? { headers } : {}
|
|
5393
|
+
});
|
|
5394
|
+
};
|
|
5395
|
+
const dataTarget = {
|
|
5396
|
+
collection,
|
|
5397
|
+
batch
|
|
5398
|
+
};
|
|
5367
5399
|
/**
|
|
5368
5400
|
* Names the proxy never serves as a collection: the ones promise and
|
|
5369
5401
|
* serialisation machinery probe for (`then`, `toJSON`, React's
|
|
@@ -5378,6 +5410,7 @@ function createRebaseClient(options) {
|
|
|
5378
5410
|
const dataProxy = new Proxy(dataTarget, {
|
|
5379
5411
|
get(_target, prop) {
|
|
5380
5412
|
if (prop === "collection") return collection;
|
|
5413
|
+
if (prop === "batch") return batch;
|
|
5381
5414
|
if (typeof prop === "symbol") return void 0;
|
|
5382
5415
|
if (isProbedName(prop)) return void 0;
|
|
5383
5416
|
if (options.collections && Object.hasOwn(options.collections, prop)) return collection(options.collections[prop]);
|
|
@@ -5404,7 +5437,7 @@ function createRebaseClient(options) {
|
|
|
5404
5437
|
return collection(slug);
|
|
5405
5438
|
},
|
|
5406
5439
|
has(_target, prop) {
|
|
5407
|
-
if (prop === "collection") return true;
|
|
5440
|
+
if (prop === "collection" || prop === "batch") return true;
|
|
5408
5441
|
if (typeof prop === "symbol") return false;
|
|
5409
5442
|
if (isProbedName(prop)) return false;
|
|
5410
5443
|
if (options.collections) return Object.hasOwn(options.collections, prop);
|
|
@@ -5502,30 +5535,7 @@ function createRebaseClient(options) {
|
|
|
5502
5535
|
body: payload !== void 0 ? JSON.stringify(payload) : void 0
|
|
5503
5536
|
});
|
|
5504
5537
|
},
|
|
5505
|
-
batch
|
|
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
|
-
},
|
|
5538
|
+
batch,
|
|
5529
5539
|
data: dataProxy,
|
|
5530
5540
|
...offlineManager ? { offline: offlineManager.api } : {}
|
|
5531
5541
|
};
|