@zveltio/sdk 2.0.8 → 3.0.0-beta.100

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 (157) hide show
  1. package/README.md +17 -4
  2. package/dist/build/index.d.ts +77 -0
  3. package/dist/build/index.d.ts.map +1 -0
  4. package/dist/build/index.js +157 -0
  5. package/dist/client.d.ts +103 -7
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/client.js +129 -28
  8. package/dist/core.d.ts.map +1 -1
  9. package/dist/core.js +21 -3
  10. package/dist/crdt.d.ts.map +1 -1
  11. package/dist/crdt.js +11 -2
  12. package/dist/ddl/index.d.ts +173 -0
  13. package/dist/ddl/index.d.ts.map +1 -0
  14. package/dist/ddl/index.js +33 -0
  15. package/dist/errors.d.ts +58 -0
  16. package/dist/errors.d.ts.map +1 -0
  17. package/dist/errors.js +124 -0
  18. package/dist/extension/action-refusal.d.ts +27 -0
  19. package/dist/extension/action-refusal.d.ts.map +1 -0
  20. package/dist/extension/action-refusal.js +29 -0
  21. package/dist/extension/capabilities.d.ts +71 -0
  22. package/dist/extension/capabilities.d.ts.map +1 -0
  23. package/dist/extension/capabilities.js +79 -0
  24. package/dist/extension/dates.d.ts +7 -0
  25. package/dist/extension/dates.d.ts.map +1 -0
  26. package/dist/extension/dates.js +14 -0
  27. package/dist/extension/index.d.ts +1570 -10
  28. package/dist/extension/index.d.ts.map +1 -1
  29. package/dist/extension/index.js +17 -1
  30. package/dist/extension/jsonb.d.ts +62 -0
  31. package/dist/extension/jsonb.d.ts.map +1 -0
  32. package/dist/extension/jsonb.js +64 -0
  33. package/dist/extension/multipart.d.ts +58 -0
  34. package/dist/extension/multipart.d.ts.map +1 -0
  35. package/dist/extension/multipart.js +61 -0
  36. package/dist/extension/numeric.d.ts +94 -0
  37. package/dist/extension/numeric.d.ts.map +1 -0
  38. package/dist/extension/numeric.js +162 -0
  39. package/dist/extension/permission-gate.d.ts +52 -0
  40. package/dist/extension/permission-gate.d.ts.map +1 -0
  41. package/dist/extension/permission-gate.js +125 -0
  42. package/dist/extension/reserved-paths.d.ts +10 -0
  43. package/dist/extension/reserved-paths.d.ts.map +1 -0
  44. package/dist/extension/reserved-paths.js +23 -0
  45. package/dist/index.d.ts +4 -2
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +1 -0
  48. package/dist/local-store.d.ts +14 -1
  49. package/dist/local-store.d.ts.map +1 -1
  50. package/dist/local-store.js +93 -16
  51. package/dist/offline/index.d.ts +75 -0
  52. package/dist/offline/index.d.ts.map +1 -0
  53. package/dist/offline/index.js +251 -0
  54. package/dist/publish/index.d.ts +94 -0
  55. package/dist/publish/index.d.ts.map +1 -0
  56. package/dist/publish/index.js +153 -0
  57. package/dist/realtime.d.ts +25 -1
  58. package/dist/realtime.d.ts.map +1 -1
  59. package/dist/realtime.js +53 -5
  60. package/dist/rpc/index.d.ts +68 -0
  61. package/dist/rpc/index.d.ts.map +1 -0
  62. package/dist/rpc/index.js +63 -0
  63. package/dist/schema-codegen.d.ts +84 -0
  64. package/dist/schema-codegen.d.ts.map +1 -0
  65. package/dist/schema-codegen.js +503 -0
  66. package/dist/schema-watcher.d.ts +10 -2
  67. package/dist/schema-watcher.d.ts.map +1 -1
  68. package/dist/schema-watcher.js +26 -13
  69. package/dist/studio/index.d.ts +85 -0
  70. package/dist/studio/index.d.ts.map +1 -0
  71. package/dist/studio/index.js +183 -0
  72. package/dist/studio/slot-owner-registry.d.ts +13 -0
  73. package/dist/studio/slot-owner-registry.d.ts.map +1 -0
  74. package/dist/studio/slot-owner-registry.js +21 -0
  75. package/dist/svelte.d.ts +1 -1
  76. package/dist/svelte.d.ts.map +1 -1
  77. package/dist/svelte.js +4 -2
  78. package/dist/sync-manager.d.ts +49 -4
  79. package/dist/sync-manager.d.ts.map +1 -1
  80. package/dist/sync-manager.js +175 -24
  81. package/dist/testing/index.d.ts +121 -0
  82. package/dist/testing/index.d.ts.map +1 -0
  83. package/dist/testing/index.js +305 -0
  84. package/dist/testing/with-test-db.d.ts +119 -0
  85. package/dist/testing/with-test-db.d.ts.map +1 -0
  86. package/dist/testing/with-test-db.js +320 -0
  87. package/dist/tests/action-refusal.test.d.ts +2 -0
  88. package/dist/tests/action-refusal.test.d.ts.map +1 -0
  89. package/dist/tests/action-refusal.test.js +13 -0
  90. package/dist/tests/client.test.d.ts +2 -0
  91. package/dist/tests/client.test.d.ts.map +1 -0
  92. package/dist/tests/client.test.js +164 -0
  93. package/dist/tests/crdt-orset.test.d.ts +2 -0
  94. package/dist/tests/crdt-orset.test.d.ts.map +1 -0
  95. package/dist/tests/crdt-orset.test.js +23 -0
  96. package/dist/tests/local-store.test.js +44 -0
  97. package/dist/tests/migration-parse.test.d.ts +2 -0
  98. package/dist/tests/migration-parse.test.d.ts.map +1 -0
  99. package/dist/tests/migration-parse.test.js +17 -0
  100. package/dist/tests/multipart.test.d.ts +2 -0
  101. package/dist/tests/multipart.test.d.ts.map +1 -0
  102. package/dist/tests/multipart.test.js +37 -0
  103. package/dist/tests/offline-provider-crdt.test.d.ts +2 -0
  104. package/dist/tests/offline-provider-crdt.test.d.ts.map +1 -0
  105. package/dist/tests/offline-provider-crdt.test.js +206 -0
  106. package/dist/tests/realtime.test.d.ts +2 -0
  107. package/dist/tests/realtime.test.d.ts.map +1 -0
  108. package/dist/tests/realtime.test.js +220 -0
  109. package/dist/tests/reserved-paths.test.d.ts +2 -0
  110. package/dist/tests/reserved-paths.test.d.ts.map +1 -0
  111. package/dist/tests/reserved-paths.test.js +21 -0
  112. package/dist/tests/sync-manager.test.d.ts +2 -0
  113. package/dist/tests/sync-manager.test.d.ts.map +1 -0
  114. package/dist/tests/sync-manager.test.js +123 -0
  115. package/dist/tests/today-in.test.d.ts +2 -0
  116. package/dist/tests/today-in.test.d.ts.map +1 -0
  117. package/dist/tests/today-in.test.js +14 -0
  118. package/dist/tests/validate-manifest-actions.test.d.ts +2 -0
  119. package/dist/tests/validate-manifest-actions.test.d.ts.map +1 -0
  120. package/dist/tests/validate-manifest-actions.test.js +82 -0
  121. package/dist/tests/validate-manifest-dependencies.test.d.ts +2 -0
  122. package/dist/tests/validate-manifest-dependencies.test.d.ts.map +1 -0
  123. package/dist/tests/validate-manifest-dependencies.test.js +27 -0
  124. package/dist/tests/validate-manifest-forward-credentials.test.d.ts +2 -0
  125. package/dist/tests/validate-manifest-forward-credentials.test.d.ts.map +1 -0
  126. package/dist/tests/validate-manifest-forward-credentials.test.js +35 -0
  127. package/dist/tests/validate-sdui-data-endpoints.test.d.ts +2 -0
  128. package/dist/tests/validate-sdui-data-endpoints.test.d.ts.map +1 -0
  129. package/dist/tests/validate-sdui-data-endpoints.test.js +41 -0
  130. package/dist/tests/with-test-db.test.d.ts +2 -0
  131. package/dist/tests/with-test-db.test.d.ts.map +1 -0
  132. package/dist/tests/with-test-db.test.js +57 -0
  133. package/dist/types/index.d.ts.map +1 -1
  134. package/dist/utils.d.ts.map +1 -1
  135. package/dist/utils.js +3 -1
  136. package/dist/validate/index.d.ts +148 -0
  137. package/dist/validate/index.d.ts.map +1 -0
  138. package/dist/validate/index.js +887 -0
  139. package/dist/validate/migration-parse.d.ts +22 -0
  140. package/dist/validate/migration-parse.d.ts.map +1 -0
  141. package/dist/validate/migration-parse.js +11 -0
  142. package/dist/validate/shared-message-keys.d.ts +16 -0
  143. package/dist/validate/shared-message-keys.d.ts.map +1 -0
  144. package/dist/validate/shared-message-keys.js +227 -0
  145. package/package.json +56 -4
  146. package/dist/client/Auth.d.ts +0 -19
  147. package/dist/client/Auth.d.ts.map +0 -1
  148. package/dist/client/Auth.js +0 -45
  149. package/dist/client/QueryBuilder.d.ts +0 -17
  150. package/dist/client/QueryBuilder.d.ts.map +0 -1
  151. package/dist/client/QueryBuilder.js +0 -76
  152. package/dist/client/RealtimeClient.d.ts +0 -22
  153. package/dist/client/RealtimeClient.d.ts.map +0 -1
  154. package/dist/client/RealtimeClient.js +0 -96
  155. package/dist/client/ZveltioClient.d.ts +0 -20
  156. package/dist/client/ZveltioClient.d.ts.map +0 -1
  157. package/dist/client/ZveltioClient.js +0 -78
package/README.md CHANGED
@@ -57,7 +57,7 @@ const client = createZveltioClient<ZveltioSchema>({
57
57
 
58
58
  ```typescript
59
59
  // TypeScript knows the shape of each record automatically
60
- const { data: products } = await client.collection('products').list();
60
+ const { records: products } = await client.collection('products').list();
61
61
  // ^-- typed as Products[] (from your generated CollectionTypeMap)
62
62
 
63
63
  const order = await client.collection('orders').getOne('ord-123');
@@ -186,6 +186,19 @@ await sync.stop();
186
186
  The engine exposes two endpoints for batch sync:
187
187
 
188
188
  - `POST /api/sync/push` — send offline operations to server
189
- - `POST /api/sync/pull` — pull changes since a timestamp
190
-
191
- These are used automatically by `SyncManager`. You can also call them directly for custom sync logic.
189
+ - `POST /api/sync/pull` — pull changes after your cursors
190
+
191
+ `SyncManager.pull()` uses `/api/sync/pull` and keeps its cursors in the local store. Both endpoints take a
192
+ session only: a client authenticated by an API key gets 403, and `pull()` then falls back to paging the
193
+ collections API, which cannot report deletes.
194
+
195
+ A pull returns at most 1000 rows per collection. Send `{ collections, cursors }`, merge the
196
+ response's `cursors` into yours, and pull again while `hasMore` is true. Keep `cursors` for the next
197
+ sync; a collection without one is read from its start. Cursors are opaque: there is no timestamp
198
+ position, because a timestamp cannot page through rows sharing one `updated_at` (a bulk import).
199
+
200
+ A row deleted on the server comes back as `{ operation: 'delete', data: null }`; drop your copy. Deletes are
201
+ kept for 30 days: a cursor last caught up longer ago than that, or one the engine cannot open (an older engine's, or sealed
202
+ under a rotated `BETTER_AUTH_SECRET`), gets `resync[collection] === true`, and the collection restarts
203
+ from the first row — drop your synced copies of it before applying the page. A first pull (no cursor) gets
204
+ no deletes, so start it from an empty copy.
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @zveltio/sdk/build
3
+ *
4
+ * Public API for authors who want to compile their extension bundle
5
+ * from a custom build script (not via `zveltio extension pack`).
6
+ *
7
+ * The canonical pipeline is still `zveltio extension pack` — it
8
+ * understands the manifest, computes the hash, and patches the
9
+ * integrity block. This module exposes the underlying `Bun.build`
10
+ * plugin so authors with non-standard build flows (monorepo
11
+ * orchestration, custom entrypoints, IDE integration) can produce
12
+ * the SAME artifact bytes.
13
+ *
14
+ * Usage:
15
+ *
16
+ * ```ts
17
+ * import { createExtensionBuildConfig } from '@zveltio/sdk/build';
18
+ *
19
+ * const result = await Bun.build({
20
+ * ...createExtensionBuildConfig({
21
+ * entry: './engine/index.ts',
22
+ * outdir: './engine',
23
+ * resolveDir: process.cwd(),
24
+ * }),
25
+ * });
26
+ * ```
27
+ *
28
+ * After build, compute SHA-256 of `engine/index.js` and write it to
29
+ * `manifest.integrity.engineSha256` — the engine refuses to load any
30
+ * bundled extension whose on-disk bytes don't match the declared hash.
31
+ */
32
+ import type { BunPlugin } from 'bun';
33
+ /**
34
+ * Packages that the engine ships in its compiled binary AND that
35
+ * Bun's bundler resolves to `.d.ts` (not `.js`) due to an exports-
36
+ * condition quirk. The plugin below forces them to resolve to their
37
+ * ESM JavaScript entrypoints so the bundle actually runs.
38
+ */
39
+ export declare const EXTENSION_BUNDLE_CORE_DEPS: readonly ["hono", "zod", "kysely", "@hono/zod-validator"];
40
+ /**
41
+ * Bun plugin that forces the core extension deps (hono / zod / kysely
42
+ * / @hono/zod-validator) to resolve to their ESM `.js` entrypoints
43
+ * during build. Works around Bun's exports-condition matching picking
44
+ * `.d.ts` for hono-style packages.
45
+ */
46
+ export declare function createExtensionBundleResolvePlugin(resolveFromDir: string): BunPlugin;
47
+ export interface ExtensionBuildConfigOptions {
48
+ /** Absolute path to the extension's `engine/index.ts`. */
49
+ entry: string;
50
+ /** Directory the build output lands in (typically `<ext>/engine`). */
51
+ outdir: string;
52
+ /** Extension root — used to walk node_modules for peer-dep resolution. */
53
+ resolveDir: string;
54
+ /** External packages to leave as bare imports. Use sparingly — Bun's
55
+ * compiled binary cannot resolve bare specifiers from dynamically-
56
+ * imported disk files. Default: []. */
57
+ external?: string[];
58
+ /** Emit a `.map` next to the bundle. Default: false. */
59
+ sourcemap?: boolean;
60
+ }
61
+ /**
62
+ * Build a `Bun.build` config object the SAME way `zveltio extension
63
+ * pack` does. Returns the full options object including the resolve
64
+ * plugin and bundle conditions — spread it into your own `Bun.build`
65
+ * call.
66
+ */
67
+ export declare function createExtensionBuildConfig(opts: ExtensionBuildConfigOptions): {
68
+ entrypoints: string[];
69
+ outdir: string;
70
+ target: "bun";
71
+ format: "esm";
72
+ sourcemap: "linked" | "none";
73
+ external: string[];
74
+ conditions: ("default" | "import" | "bun" | "node")[];
75
+ plugins: BunPlugin[];
76
+ };
77
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/build/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAIrC;;;;;GAKG;AACH,eAAO,MAAM,0BAA0B,2DAK7B,CAAC;AAiFX;;;;;GAKG;AACH,wBAAgB,kCAAkC,CAAC,cAAc,EAAE,MAAM,GAAG,SAAS,CAYpF;AAED,MAAM,WAAW,2BAA2B;IAC1C,0DAA0D;IAC1D,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;IACnB;;4CAEwC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,wDAAwD;IACxD,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,2BAA2B;;;;;eAMrB,QAAQ,GAAG,MAAM;;;;EAKvE"}
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @zveltio/sdk/build
3
+ *
4
+ * Public API for authors who want to compile their extension bundle
5
+ * from a custom build script (not via `zveltio extension pack`).
6
+ *
7
+ * The canonical pipeline is still `zveltio extension pack` — it
8
+ * understands the manifest, computes the hash, and patches the
9
+ * integrity block. This module exposes the underlying `Bun.build`
10
+ * plugin so authors with non-standard build flows (monorepo
11
+ * orchestration, custom entrypoints, IDE integration) can produce
12
+ * the SAME artifact bytes.
13
+ *
14
+ * Usage:
15
+ *
16
+ * ```ts
17
+ * import { createExtensionBuildConfig } from '@zveltio/sdk/build';
18
+ *
19
+ * const result = await Bun.build({
20
+ * ...createExtensionBuildConfig({
21
+ * entry: './engine/index.ts',
22
+ * outdir: './engine',
23
+ * resolveDir: process.cwd(),
24
+ * }),
25
+ * });
26
+ * ```
27
+ *
28
+ * After build, compute SHA-256 of `engine/index.js` and write it to
29
+ * `manifest.integrity.engineSha256` — the engine refuses to load any
30
+ * bundled extension whose on-disk bytes don't match the declared hash.
31
+ */
32
+ import { createRequire } from 'node:module';
33
+ import { existsSync, readFileSync } from 'node:fs';
34
+ import { dirname, join } from 'node:path';
35
+ const require = createRequire(import.meta.url);
36
+ /**
37
+ * Packages that the engine ships in its compiled binary AND that
38
+ * Bun's bundler resolves to `.d.ts` (not `.js`) due to an exports-
39
+ * condition quirk. The plugin below forces them to resolve to their
40
+ * ESM JavaScript entrypoints so the bundle actually runs.
41
+ */
42
+ export const EXTENSION_BUNDLE_CORE_DEPS = [
43
+ 'hono',
44
+ 'zod',
45
+ 'kysely',
46
+ '@hono/zod-validator',
47
+ ];
48
+ const BUNDLE_CONDITIONS = ['import', 'bun', 'node', 'default'];
49
+ function escapeRegExp(s) {
50
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
51
+ }
52
+ function pickExportTarget(target) {
53
+ if (typeof target === 'string')
54
+ return target;
55
+ if (target && typeof target === 'object') {
56
+ const o = target;
57
+ if (typeof o.import === 'string')
58
+ return o.import;
59
+ if (typeof o.default === 'string')
60
+ return o.default;
61
+ if (o.import)
62
+ return pickExportTarget(o.import);
63
+ }
64
+ return undefined;
65
+ }
66
+ function resolveSearchPaths(resolveFromDir) {
67
+ const paths = [resolveFromDir];
68
+ let cur = resolveFromDir;
69
+ for (let i = 0; i < 6; i++) {
70
+ cur = dirname(cur);
71
+ paths.push(cur);
72
+ if (existsSync(join(cur, 'node_modules')))
73
+ break;
74
+ }
75
+ return [...new Set(paths)];
76
+ }
77
+ function resolvePackageRoot(pkg, searchPaths) {
78
+ let candidate;
79
+ try {
80
+ candidate = dirname(require.resolve(pkg, { paths: searchPaths }));
81
+ }
82
+ catch {
83
+ throw new Error(`Cannot find package "${pkg}" from ${searchPaths[0]}. ` +
84
+ `Run \`bun install\` in the extension directory or monorepo root.`);
85
+ }
86
+ for (;;) {
87
+ const pkgJsonPath = join(candidate, 'package.json');
88
+ if (existsSync(pkgJsonPath)) {
89
+ const manifest = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
90
+ if (manifest.name === pkg)
91
+ return candidate;
92
+ }
93
+ const parent = dirname(candidate);
94
+ if (parent === candidate)
95
+ break;
96
+ candidate = parent;
97
+ }
98
+ throw new Error(`Cannot find package root for "${pkg}"`);
99
+ }
100
+ function resolvePackageEsmEntry(pkg, resolveFromDir) {
101
+ const searchPaths = resolveSearchPaths(resolveFromDir);
102
+ const root = resolvePackageRoot(pkg, searchPaths);
103
+ const pkgJsonPath = join(root, 'package.json');
104
+ const manifest = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
105
+ const dot = manifest.exports?.['.'];
106
+ let rel = pickExportTarget(dot);
107
+ rel ??= manifest.module ?? manifest.main;
108
+ if (!rel || typeof rel !== 'string') {
109
+ throw new Error(`Cannot determine ESM entry for package "${pkg}"`);
110
+ }
111
+ const abs = join(root, rel.replace(/^\.\//, ''));
112
+ if (!existsSync(abs)) {
113
+ throw new Error(`ESM entry missing for "${pkg}": ${abs}`);
114
+ }
115
+ if (abs.endsWith('.d.ts')) {
116
+ throw new Error(`Package "${pkg}" resolved to a declaration file (${abs}). ` +
117
+ `Expected exports.import or module field to point at JavaScript.`);
118
+ }
119
+ return abs;
120
+ }
121
+ /**
122
+ * Bun plugin that forces the core extension deps (hono / zod / kysely
123
+ * / @hono/zod-validator) to resolve to their ESM `.js` entrypoints
124
+ * during build. Works around Bun's exports-condition matching picking
125
+ * `.d.ts` for hono-style packages.
126
+ */
127
+ export function createExtensionBundleResolvePlugin(resolveFromDir) {
128
+ return {
129
+ name: 'zveltio-extension-bundle-resolve',
130
+ setup(build) {
131
+ for (const pkg of EXTENSION_BUNDLE_CORE_DEPS) {
132
+ const filter = new RegExp(`^${escapeRegExp(pkg)}$`);
133
+ build.onResolve({ filter }, () => ({
134
+ path: resolvePackageEsmEntry(pkg, resolveFromDir),
135
+ }));
136
+ }
137
+ },
138
+ };
139
+ }
140
+ /**
141
+ * Build a `Bun.build` config object the SAME way `zveltio extension
142
+ * pack` does. Returns the full options object including the resolve
143
+ * plugin and bundle conditions — spread it into your own `Bun.build`
144
+ * call.
145
+ */
146
+ export function createExtensionBuildConfig(opts) {
147
+ return {
148
+ entrypoints: [opts.entry],
149
+ outdir: opts.outdir,
150
+ target: 'bun',
151
+ format: 'esm',
152
+ sourcemap: (opts.sourcemap ? 'linked' : 'none'),
153
+ external: opts.external ?? [],
154
+ conditions: [...BUNDLE_CONDITIONS],
155
+ plugins: [createExtensionBundleResolvePlugin(opts.resolveDir)],
156
+ };
157
+ }
package/dist/client.d.ts CHANGED
@@ -1,8 +1,55 @@
1
1
  export interface ZveltioClientConfig {
2
2
  baseUrl: string;
3
3
  apiKey?: string;
4
+ /**
5
+ * A session token for a client with no cookie jar (native app, server): sent as
6
+ * `Authorization: Bearer`, with cookies off. Giving this or `onSessionToken`
7
+ * turns that mode on; a browser leaves both unset and keeps the httpOnly cookie.
8
+ */
9
+ sessionToken?: string;
10
+ /**
11
+ * Called with each token the engine issues (sign-in, a session it replaces) and
12
+ * with `null` on logout: persist it here and pass it back as `sessionToken`.
13
+ */
14
+ onSessionToken?: (token: string | null) => void;
15
+ /**
16
+ * Called when the engine answers 403 `step_up_required` (a configuration change,
17
+ * or an action that wants a fresh second factor). Confirm the user's passkey or
18
+ * TOTP for `action` and resolve with the step-up token; the call is then sent
19
+ * once more with it. Resolve `null` to give up: the 403 is thrown as before.
20
+ */
21
+ onStepUpRequired?: (action: string) => Promise<string | null>;
4
22
  headers?: Record<string, string>;
5
23
  }
24
+ /** `GET /api/api-keys/self` — the key a client authenticates with, as the engine holds it. */
25
+ export interface ApiKeySelf {
26
+ id: string;
27
+ name: string;
28
+ key_prefix: string;
29
+ scopes: Array<{
30
+ collection: string;
31
+ actions: string[];
32
+ }>;
33
+ tenant_id: string | null;
34
+ expires_at: string | null;
35
+ rls_bypass: boolean;
36
+ }
37
+ /** A key in `GET /api/me/api-keys` — never the hash or the raw key. */
38
+ export interface MemberApiKey extends ApiKeySelf {
39
+ last_used_at: string | null;
40
+ created_at: string;
41
+ is_active: boolean;
42
+ }
43
+ /** `POST /api/me/api-keys`: explicit scopes and an expiry at most one year out. */
44
+ export interface MemberApiKeyInput {
45
+ name: string;
46
+ scopes: Array<{
47
+ collection: string;
48
+ actions: string[];
49
+ }>;
50
+ expires_at: string;
51
+ rls_bypass?: boolean;
52
+ }
6
53
  export interface ListParams {
7
54
  page?: number;
8
55
  limit?: number;
@@ -12,16 +59,29 @@ export interface ListParams {
12
59
  filter?: Record<string, any>;
13
60
  cursor?: string;
14
61
  }
62
+ /**
63
+ * What `GET /api/data/:collection` answers (`listRecords` in the engine). This
64
+ * declared `{ data, total, page, limit }`, a shape the engine has never sent, so
65
+ * every typed reader of `.data` got `undefined` — the sync manager's paged pull
66
+ * among them, which applied no row at all.
67
+ */
15
68
  export interface ListResult<T> {
16
- data: T[];
17
- total: number;
18
- page: number;
19
- limit: number;
69
+ records: T[];
70
+ pagination: {
71
+ /** Absent when the count was skipped. */
72
+ total?: number;
73
+ page: number;
74
+ limit: number;
75
+ pages?: number;
76
+ };
20
77
  next_cursor?: string;
21
78
  }
22
79
  declare class CollectionRef<T extends Record<string, any>> {
23
- private readonly name;
24
80
  private readonly client;
81
+ /** URL-encoded once, here: a record id is caller data (a slug, an email, a
82
+ * composite key), and an unencoded `/`, `?` or `#` in one silently routes
83
+ * the request somewhere else. */
84
+ private readonly path;
25
85
  constructor(name: string, client: ZveltioClient<any>);
26
86
  list(params?: ListParams): Promise<ListResult<T>>;
27
87
  getMany(params?: ListParams): Promise<ListResult<T>>;
@@ -40,7 +100,7 @@ declare class CollectionRef<T extends Record<string, any>> {
40
100
  *
41
101
  * import type { ZveltioSchema } from './zveltio-types';
42
102
  * const client = createZveltioClient<ZveltioSchema>({ baseUrl: '...' });
43
- * const { data } = await client.collection('products').list();
103
+ * const { records } = await client.collection('products').list();
44
104
  * // ^-- typed as your Products interface
45
105
  *
46
106
  * Usage without types (untyped, same as before):
@@ -49,7 +109,19 @@ declare class CollectionRef<T extends Record<string, any>> {
49
109
  export declare class ZveltioClient<Schema extends Record<string, any> = Record<string, any>> {
50
110
  private baseUrl;
51
111
  private headers;
112
+ private bearer;
113
+ private sessionToken;
114
+ private onSessionToken?;
115
+ private onStepUpRequired?;
52
116
  constructor(config: ZveltioClientConfig);
117
+ /** Switch to bearer mode with `token` (or sign this client out of it with `null`). */
118
+ setSessionToken(token: string | null): void;
119
+ /** The token bearer mode sends; null in cookie mode or when signed out. */
120
+ getSessionToken(): string | null;
121
+ /** Every call goes through here: the bearer out, a newly issued token in. */
122
+ private send;
123
+ private checked;
124
+ private sendOnce;
53
125
  private request;
54
126
  get<T = any>(path: string): Promise<T>;
55
127
  post<T = any>(path: string, body?: unknown): Promise<T>;
@@ -69,8 +141,32 @@ export declare class ZveltioClient<Schema extends Record<string, any> = Record<s
69
141
  readonly signup: (email: string, password: string, name: string) => Promise<any>;
70
142
  readonly logout: () => Promise<any>;
71
143
  readonly session: () => Promise<any>;
144
+ /**
145
+ * The API key this client was made with: its scopes, tenant, expiry and RLS
146
+ * exemption — never the hash. 403 for a session, 401 for a key that no
147
+ * longer authenticates.
148
+ */
149
+ readonly apiKey: () => Promise<ApiKeySelf>;
150
+ /**
151
+ * The signed-in member's own keys (`/api/me/api-keys`, ADR 0010), when the
152
+ * instance lets members issue them. A session only; creating one needs a
153
+ * step-up, which `onStepUpRequired` answers. The raw `key` is returned once.
154
+ */
155
+ readonly apiKeys: {
156
+ readonly list: () => Promise<{
157
+ api_keys: MemberApiKey[];
158
+ }>;
159
+ readonly create: (input: MemberApiKeyInput) => Promise<MemberApiKey & {
160
+ key: string;
161
+ }>;
162
+ readonly revoke: (id: string) => Promise<any>;
163
+ };
72
164
  };
73
- /** Storage helpers */
165
+ /**
166
+ * Storage helpers. A client made with `apiKey` needs a key holding the `$storage`
167
+ * scope — `{ collection: '$storage', actions: ['read', 'create', 'delete'] }`; a
168
+ * `*` collection does not include it, and the engine answers 403 without it.
169
+ */
74
170
  readonly storage: {
75
171
  readonly upload: (file: File, folder?: string) => Promise<any>;
76
172
  readonly list: (folder?: string) => Promise<any>;
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CA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