@rebasepro/types 0.12.1-canary.gf5f1d39 → 0.13.1-canary.g06dbe5b

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.
Files changed (45) hide show
  1. package/dist/call_context.d.ts +70 -4
  2. package/dist/controllers/client.d.ts +72 -70
  3. package/dist/controllers/data.d.ts +379 -36
  4. package/dist/controllers/data_driver.d.ts +82 -5
  5. package/dist/errors.d.ts +30 -4
  6. package/dist/index.es.js +243 -15
  7. package/dist/index.es.js.map +1 -1
  8. package/dist/types/admin_block.d.ts +39 -2
  9. package/dist/types/backend.d.ts +29 -0
  10. package/dist/types/chips.d.ts +12 -1
  11. package/dist/types/collections.d.ts +46 -8
  12. package/dist/types/cron.d.ts +81 -9
  13. package/dist/types/database_adapter.d.ts +35 -1
  14. package/dist/types/entities.d.ts +11 -0
  15. package/dist/types/entity_callbacks.d.ts +2 -1
  16. package/dist/types/index.d.ts +2 -0
  17. package/dist/types/policy.d.ts +46 -14
  18. package/dist/types/project_manifest.d.ts +15 -0
  19. package/dist/types/properties.d.ts +31 -6
  20. package/dist/types/relations.d.ts +51 -0
  21. package/dist/types/rls-functions.d.ts +84 -0
  22. package/dist/types/search.d.ts +231 -0
  23. package/dist/users/user.d.ts +2 -2
  24. package/package.json +5 -5
  25. package/src/call_context.ts +68 -4
  26. package/src/controllers/client.ts +74 -91
  27. package/src/controllers/data.ts +390 -36
  28. package/src/controllers/data_driver.ts +120 -8
  29. package/src/errors.ts +43 -4
  30. package/src/types/admin_block.ts +87 -1
  31. package/src/types/backend.ts +32 -0
  32. package/src/types/chips.ts +14 -1
  33. package/src/types/collections.ts +47 -8
  34. package/src/types/cron.ts +83 -9
  35. package/src/types/database_adapter.ts +41 -0
  36. package/src/types/entities.ts +12 -0
  37. package/src/types/entity_callbacks.ts +2 -1
  38. package/src/types/index.ts +2 -0
  39. package/src/types/policy.ts +50 -14
  40. package/src/types/project_manifest.ts +15 -0
  41. package/src/types/properties.ts +32 -6
  42. package/src/types/relations.ts +51 -0
  43. package/src/types/rls-functions.ts +98 -0
  44. package/src/types/search.ts +247 -0
  45. package/src/users/user.ts +2 -2
@@ -1,3 +1,4 @@
1
+ import type { DataDriver } from "./controllers/data_driver";
1
2
  import type { StorageSource } from "./controllers/storage";
2
3
  import type { RebaseClient } from "./controllers/client";
3
4
  import type { RebaseSdkData } from "./controllers/data";
@@ -19,7 +20,16 @@ export type RebaseCallContext<USER extends User = User> = {
19
20
  * The Rebase client instance.
20
21
  * Available in all entity callbacks (beforeSave, afterSave, afterRead,
21
22
  * beforeDelete, afterDelete) and in CollectionActionsProps via context.
22
- * Use it to call backend functions, access data, storage, etc.
23
+ * Use it to call backend functions, access storage, send email, etc.
24
+ *
25
+ * ⚠️ **Not the same trust level as {@link data}.** Server-side this is the
26
+ * app singleton, so `client.dataAsAdmin` is **always** the admin-scoped
27
+ * plane — scoped as `{ uid: "service", roles: ["admin"] }`, so policies are
28
+ * evaluated against that identity rather than skipped — while {@link data},
29
+ * one property over, follows whoever triggered the callback. On a user
30
+ * request, reaching for `context.client.dataAsAdmin` silently escalates a
31
+ * user-scoped operation to admin. For queries in a callback use
32
+ * {@link data}; come here for functions, storage and email.
23
33
  *
24
34
  * @example
25
35
  * // In a beforeSave callback:
@@ -35,11 +45,67 @@ export type RebaseCallContext<USER extends User = User> = {
35
45
  * Unified data access — `context.data.products.create(...)`.
36
46
  * Access any collection as a dynamic property.
37
47
  *
38
- * Returns flat rows (`{ id, ...columns }`), identical to the frontend SDK
39
- * client — so `context.data` in a backend callback and `client.data` in the
40
- * frontend behave the same way (`row.title`, never `row.values.title`).
48
+ * **Inherits the privilege of whatever triggered the callback.** This is not
49
+ * a fixed trust level, and it is the one thing to know about this accessor:
50
+ *
51
+ * - Triggered by a **user request** (REST, realtime, an admin-panel edit):
52
+ * user-scoped. The callback runs on the RLS-bound transaction opened for
53
+ * that request, so policies apply to reads *and* writes — a callback
54
+ * cannot see a row its caller could not.
55
+ * - Triggered by **`rebase.dataAsAdmin` or a cron** (the same singleton):
56
+ * admin-scoped, not unscoped. That driver is scoped as
57
+ * `{ uid: "service", roles: ["admin"] }`, so the callback still runs on an
58
+ * RLS-bound transaction — policies are evaluated against that identity.
59
+ * - Triggered by **the base driver** (auth flows, migrations): unscoped, on
60
+ * the owner connection, bypassing RLS.
61
+ *
62
+ * So a callback that reads a sibling row will find it when an admin task
63
+ * saves and may find nothing when an end user saves — without an error,
64
+ * because RLS filters rather than raises. Write callbacks that tolerate
65
+ * that, or reach for {@link client}`.dataAsAdmin` deliberately when the
66
+ * callback genuinely has to see what an admin may see. Note what that does
67
+ * *not* buy you: `policy.serverContext()` (`auth.uid() IS NULL`) is false
68
+ * for the service identity, so a collection whose only rule is
69
+ * `serverContext()` stays closed to it.
70
+ *
71
+ * Verified end-to-end against Postgres rather than asserted — see
72
+ * `"scopes context.data to the caller when a callback runs on a user
73
+ * request"` in `server-postgres`' `rls-enforcement` e2e suite. The
74
+ * documentation previously claimed the opposite (that callbacks always have
75
+ * full access), which is the unsafe direction to be wrong in.
76
+ *
77
+ * Returns flat rows (`{ id, ...columns }`), identical in *shape* to the
78
+ * frontend SDK client — so `context.data` in a backend callback and
79
+ * `client.data` in the frontend are accessed the same way (`row.title`,
80
+ * never `row.values.title`). Shape only: privilege differs as above.
41
81
  */
42
82
  data: RebaseSdkData;
83
+ /**
84
+ * The driver executing the operation this callback is attached to.
85
+ *
86
+ * Present server-side only. Declared here because it is already public in
87
+ * practice — the backend has always passed it, and the callbacks guide
88
+ * documented `context.driver.withAuth(user)` in all six locales. The
89
+ * contract simply did not name it, so `buildCallContext` was cast through
90
+ * `as unknown as RebaseCallContext` and nothing about the object was
91
+ * type-checked at all.
92
+ *
93
+ * The guide no longer recommends `withAuth` — {@link data} is already
94
+ * user-scoped on a user request, so the manual re-scoping it described was
95
+ * answering a problem that did not exist. The field stays declared rather
96
+ * than removed: it is on the runtime object, dropping it would break anyone
97
+ * who found it, and a named optional is better than a silent extra.
98
+ *
99
+ * `withAuth` is not on {@link DataDriver} because not every engine supports
100
+ * RLS scoping; it is narrowed here, and left optional so a driver without it
101
+ * is a compile-time absence rather than a runtime surprise.
102
+ */
103
+ driver?: DataDriver & {
104
+ withAuth?(user: {
105
+ uid: string;
106
+ roles?: string[];
107
+ }): Promise<DataDriver>;
108
+ };
43
109
  /**
44
110
  * Used storage implementation
45
111
  */
@@ -83,6 +83,20 @@ export interface AuthClient {
83
83
  * Manually refresh the session token
84
84
  */
85
85
  refreshSession(): Promise<RebaseSession>;
86
+ /**
87
+ * Whether a session could exist that this client has not loaded yet.
88
+ *
89
+ * `false` means the only way this client can hold a session is an explicit
90
+ * sign-in during this page's lifetime: it neither persists sessions nor
91
+ * carries an httpOnly auth cookie, so there is nothing on disk or in the
92
+ * browser to restore from. A caller that would otherwise probe the server
93
+ * — `getUser()` on mount, say — can skip it, because the answer is already
94
+ * known and the request can only ever fail.
95
+ *
96
+ * Optional so that alternative {@link AuthClient} implementations need not
97
+ * supply it; treat a missing implementation as "unknown, go ahead and ask".
98
+ */
99
+ canRestoreSession?: () => boolean;
86
100
  }
87
101
  /**
88
102
  * User record as returned by the Admin API (`GET /admin/users`, etc.).
@@ -282,18 +296,31 @@ export interface RebaseClient<DB = unknown> {
282
296
  /** Unified Data access layer */
283
297
  data: RebaseSdkData<DB>;
284
298
  /**
285
- * Admin-scoped, **RLS-bypassing** data accessor.
299
+ * Admin-scoped data accessor — **not** an RLS bypass.
286
300
  *
287
301
  * Present on the **server** singleton only (see {@link RebaseServerClient}).
288
- * It runs with `{ uid: "service", roles: ["admin"] }` — every read and write
289
- * bypasses row-level-security policies. This is the correct tool for trusted
290
- * background work (cron jobs, migrations, service-to-service tasks).
302
+ * It runs as the service identity `{ uid: "service", roles: ["admin"] }`,
303
+ * and the driver is scoped with `withAuth()` at boot, so every read and
304
+ * write runs in a transaction that has switched to the restricted
305
+ * `rebase_user` role with `app.uid = 'service'`: policies are evaluated,
306
+ * against that identity. This is the correct tool for trusted background
307
+ * work (cron jobs, migrations, service-to-service tasks).
308
+ *
309
+ * Two consequences the name does not suggest:
310
+ *
311
+ * - `policy.serverContext()` compiles to `auth.uid() IS NULL` and is
312
+ * therefore **false** here. A collection with `disableDefaultPolicies:
313
+ * true` whose only rule is `serverContext()` refuses these writes
314
+ * (`42501`) and returns zero rows — HTTP 200, empty — for these reads.
315
+ * - Its reach equals an `admin`-roled application user's reach. It is not a
316
+ * private channel. The true bypass is {@link sql}, which runs on the
317
+ * owner connection and never goes through `withAuth`.
291
318
  *
292
319
  * ⚠️ **Do NOT use it to serve user-facing data.** Inside a request handler,
293
320
  * user-scoped queries must go through the request-scoped driver
294
- * (`c.var.driver`), which carries the caller's identity so RLS applies.
295
- * Reaching for `dataAsAdmin` (or its alias {@link data}) in a request handler
296
- * silently exposes every row to every caller.
321
+ * (`c.var.driver`), which carries the caller's identity. Reaching for
322
+ * `dataAsAdmin` (or its alias {@link data}) in a request handler serves
323
+ * every caller whatever an admin may see.
297
324
  *
298
325
  * Undefined in the browser SDK.
299
326
  */
@@ -337,6 +364,16 @@ export interface RebaseClient<DB = unknown> {
337
364
  apiKeys?: ApiKeysAPI;
338
365
  /** Base HTTP URL of the backend server */
339
366
  baseUrl?: string;
367
+ /**
368
+ * The path every API route is mounted under, appended to {@link baseUrl}.
369
+ *
370
+ * `"/api"` unless the backend was configured with a different `basePath`
371
+ * and the client told to match. Exposed because code that builds a URL by
372
+ * hand — rather than going through the client's own methods — otherwise has
373
+ * to guess, and guessing `/api` is wrong for exactly the projects that set
374
+ * the option.
375
+ */
376
+ apiPath?: string;
340
377
  /** WebSocket client for realtime subscriptions */
341
378
  ws?: RebaseWebSocket;
342
379
  /** Set the auth token for subsequent requests */
@@ -347,7 +384,22 @@ export interface RebaseClient<DB = unknown> {
347
384
  setOnUnauthorized?(handler: () => Promise<boolean>): void;
348
385
  /** Resolve the current auth token */
349
386
  resolveToken?(): Promise<string | null>;
350
- /** Make a raw HTTP call to the backend */
387
+ /**
388
+ * POST to an arbitrary path on the backend — the escape hatch, not the way
389
+ * to call a function.
390
+ *
391
+ * For a custom function use {@link functions}`.invoke(name, payload)`: it
392
+ * targets `/functions/<name>`, takes a method and sub-path, and returns the
393
+ * response body as sent. This posts wherever you point it and **unwraps**:
394
+ * it returns `res.data` when the response has a `data` property and the
395
+ * whole envelope otherwise — so an endpoint that legitimately answers
396
+ * `{ data: null }` hands back the envelope rather than `null`. Two ways to
397
+ * reach a function with two different response contracts is a trap; this is
398
+ * the one that exists for paths `invoke` cannot express.
399
+ *
400
+ * @internal Prefer `functions.invoke()`. Kept public because a backend can
401
+ * mount routes outside `/functions`, and nothing else reaches those.
402
+ */
351
403
  call?<T = unknown>(endpoint: string, payload?: unknown): Promise<T>;
352
404
  /**
353
405
  * Execute raw SQL against the database.
@@ -366,22 +418,20 @@ export interface RebaseClient<DB = unknown> {
366
418
  * the admin-scoped {@link dataAsAdmin} accessor, raw {@link sql}, and the
367
419
  * {@link email} service are all present (non-optional).
368
420
  *
369
- * **Trust levels.** Both {@link dataAsAdmin} and the deprecated {@link data}
370
- * alias point at the same admin-scoped, **RLS-bypassing** driver. Prefer
371
- * `dataAsAdmin` so the privilege is explicit at every call site. For user-scoped
372
- * queries inside a request handler use the request-scoped driver
373
- * (`c.var.driver`) instead — never `dataAsAdmin`/`data`.
421
+ * **Trust levels.** {@link dataAsAdmin} is the admin-scoped driver — scoped as
422
+ * `{ uid: "service", roles: ["admin"] }`, so policies are still evaluated
423
+ * against that identity rather than skipped — and it is the only name for it
424
+ * here: the `data` alias that used to sit beside it is deliberately `Omit`ted
425
+ * from {@link RebaseClient} so the privilege has to be spelled out at every
426
+ * call site. {@link sql} is the unconditional bypass: raw SQL on the owner
427
+ * connection, no policies. For user-scoped queries inside a request handler use
428
+ * the request-scoped driver (`c.var.driver`) instead — never `dataAsAdmin`.
374
429
  */
375
- export interface RebaseServerClient<DB = unknown> extends RebaseClient<DB> {
430
+ export interface RebaseServerClient<DB = unknown> extends Omit<RebaseClient<DB>, "data"> {
376
431
  /**
377
- * @deprecated On the server, prefer {@link dataAsAdmin} for admin scope or
378
- * the request-scoped driver (`c.var.driver`) for user scope. This alias
379
- * points at the same admin-scoped, RLS-bypassing accessor as `dataAsAdmin`.
380
- */
381
- data: RebaseSdkData<DB>;
382
- /**
383
- * Admin-scoped, **RLS-bypassing** data accessor. Always present server-side.
384
- * See {@link RebaseClient.dataAsAdmin} for the full safety contract.
432
+ * Admin-scoped data accessor (RLS is evaluated as the service identity, not
433
+ * skipped). Always present server-side. See {@link RebaseClient.dataAsAdmin}
434
+ * for the full safety contract.
385
435
  */
386
436
  dataAsAdmin: RebaseSdkData<DB>;
387
437
  /**
@@ -400,54 +450,6 @@ export interface RebaseServerClient<DB = unknown> extends RebaseClient<DB> {
400
450
  params?: unknown[];
401
451
  }): Promise<Record<string, unknown>[]>;
402
452
  }
403
- /**
404
- * The browser-side Rebase surface — the shape produced by
405
- * `createRebaseClient()` in `@rebasepro/client`.
406
- *
407
- * Its {@link data} accessor is **user-scoped**: every call carries the signed-in
408
- * user's token, so backend RLS policies apply. It deliberately omits the
409
- * server-only members — there is no `sql`, no `email`, and no
410
- * `dataAsAdmin`, so the RLS-bypassing accessor can never be reached from
411
- * browser code.
412
- */
413
- export interface RebaseBrowserClient<DB = unknown> {
414
- /** User-scoped data access layer (carries the signed-in user's token). */
415
- data: RebaseSdkData<DB>;
416
- /** Unified Authentication layer */
417
- auth: AuthClient;
418
- /** Unified Storage layer (default storage source, backward-compatible) */
419
- storage?: StorageSource;
420
- /** Registry of all named storage sources for multi-backend support */
421
- storageRegistry?: StorageSourceRegistry;
422
- /** Build a server-backed {@link StorageSource} for a named storage source. */
423
- createStorageSource?(storageId: string): StorageSource;
424
- /** Discover the storage sources declared on the backend. */
425
- fetchStorageSources?(): Promise<StorageSourceDefinition[]>;
426
- /** Admin API for user management */
427
- admin?: AdminAPI;
428
- /** Cron job management API */
429
- cron?: CronAPI;
430
- /** Database backup management API */
431
- backups?: BackupsAPI;
432
- /** Custom backend functions API */
433
- functions?: FunctionsAPI;
434
- /** Service API keys management API */
435
- apiKeys?: ApiKeysAPI;
436
- /** Base HTTP URL of the backend server */
437
- baseUrl?: string;
438
- /** WebSocket client for realtime subscriptions */
439
- ws?: RebaseWebSocket;
440
- /** Set the auth token for subsequent requests */
441
- setToken?(token: string | null): void;
442
- /** Set a function that lazily resolves the auth token */
443
- setAuthTokenGetter?(getter: () => Promise<string | null>): void;
444
- /** Set handler called when a request returns 401 */
445
- setOnUnauthorized?(handler: () => Promise<boolean>): void;
446
- /** Resolve the current auth token */
447
- resolveToken?(): Promise<string | null>;
448
- /** Make a raw HTTP call to the backend */
449
- call?<T = unknown>(endpoint: string, payload?: unknown): Promise<T>;
450
- }
451
453
  /**
452
454
  * Client-side registry for managing multiple storage sources.
453
455
  *