@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.
- package/README.md +17 -4
- package/dist/build/index.d.ts +77 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +157 -0
- package/dist/client.d.ts +103 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +129 -28
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +21 -3
- package/dist/crdt.d.ts.map +1 -1
- package/dist/crdt.js +11 -2
- package/dist/ddl/index.d.ts +173 -0
- package/dist/ddl/index.d.ts.map +1 -0
- package/dist/ddl/index.js +33 -0
- package/dist/errors.d.ts +58 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +124 -0
- package/dist/extension/action-refusal.d.ts +27 -0
- package/dist/extension/action-refusal.d.ts.map +1 -0
- package/dist/extension/action-refusal.js +29 -0
- package/dist/extension/capabilities.d.ts +71 -0
- package/dist/extension/capabilities.d.ts.map +1 -0
- package/dist/extension/capabilities.js +79 -0
- package/dist/extension/dates.d.ts +7 -0
- package/dist/extension/dates.d.ts.map +1 -0
- package/dist/extension/dates.js +14 -0
- package/dist/extension/index.d.ts +1570 -10
- package/dist/extension/index.d.ts.map +1 -1
- package/dist/extension/index.js +17 -1
- package/dist/extension/jsonb.d.ts +62 -0
- package/dist/extension/jsonb.d.ts.map +1 -0
- package/dist/extension/jsonb.js +64 -0
- package/dist/extension/multipart.d.ts +58 -0
- package/dist/extension/multipart.d.ts.map +1 -0
- package/dist/extension/multipart.js +61 -0
- package/dist/extension/numeric.d.ts +94 -0
- package/dist/extension/numeric.d.ts.map +1 -0
- package/dist/extension/numeric.js +162 -0
- package/dist/extension/permission-gate.d.ts +52 -0
- package/dist/extension/permission-gate.d.ts.map +1 -0
- package/dist/extension/permission-gate.js +125 -0
- package/dist/extension/reserved-paths.d.ts +10 -0
- package/dist/extension/reserved-paths.d.ts.map +1 -0
- package/dist/extension/reserved-paths.js +23 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/local-store.d.ts +14 -1
- package/dist/local-store.d.ts.map +1 -1
- package/dist/local-store.js +93 -16
- package/dist/offline/index.d.ts +75 -0
- package/dist/offline/index.d.ts.map +1 -0
- package/dist/offline/index.js +251 -0
- package/dist/publish/index.d.ts +94 -0
- package/dist/publish/index.d.ts.map +1 -0
- package/dist/publish/index.js +153 -0
- package/dist/realtime.d.ts +25 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +53 -5
- package/dist/rpc/index.d.ts +68 -0
- package/dist/rpc/index.d.ts.map +1 -0
- package/dist/rpc/index.js +63 -0
- package/dist/schema-codegen.d.ts +84 -0
- package/dist/schema-codegen.d.ts.map +1 -0
- package/dist/schema-codegen.js +503 -0
- package/dist/schema-watcher.d.ts +10 -2
- package/dist/schema-watcher.d.ts.map +1 -1
- package/dist/schema-watcher.js +26 -13
- package/dist/studio/index.d.ts +85 -0
- package/dist/studio/index.d.ts.map +1 -0
- package/dist/studio/index.js +183 -0
- package/dist/studio/slot-owner-registry.d.ts +13 -0
- package/dist/studio/slot-owner-registry.d.ts.map +1 -0
- package/dist/studio/slot-owner-registry.js +21 -0
- package/dist/svelte.d.ts +1 -1
- package/dist/svelte.d.ts.map +1 -1
- package/dist/svelte.js +4 -2
- package/dist/sync-manager.d.ts +49 -4
- package/dist/sync-manager.d.ts.map +1 -1
- package/dist/sync-manager.js +175 -24
- package/dist/testing/index.d.ts +121 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +305 -0
- package/dist/testing/with-test-db.d.ts +119 -0
- package/dist/testing/with-test-db.d.ts.map +1 -0
- package/dist/testing/with-test-db.js +320 -0
- package/dist/tests/action-refusal.test.d.ts +2 -0
- package/dist/tests/action-refusal.test.d.ts.map +1 -0
- package/dist/tests/action-refusal.test.js +13 -0
- package/dist/tests/client.test.d.ts +2 -0
- package/dist/tests/client.test.d.ts.map +1 -0
- package/dist/tests/client.test.js +164 -0
- package/dist/tests/crdt-orset.test.d.ts +2 -0
- package/dist/tests/crdt-orset.test.d.ts.map +1 -0
- package/dist/tests/crdt-orset.test.js +23 -0
- package/dist/tests/local-store.test.js +44 -0
- package/dist/tests/migration-parse.test.d.ts +2 -0
- package/dist/tests/migration-parse.test.d.ts.map +1 -0
- package/dist/tests/migration-parse.test.js +17 -0
- package/dist/tests/multipart.test.d.ts +2 -0
- package/dist/tests/multipart.test.d.ts.map +1 -0
- package/dist/tests/multipart.test.js +37 -0
- package/dist/tests/offline-provider-crdt.test.d.ts +2 -0
- package/dist/tests/offline-provider-crdt.test.d.ts.map +1 -0
- package/dist/tests/offline-provider-crdt.test.js +206 -0
- package/dist/tests/realtime.test.d.ts +2 -0
- package/dist/tests/realtime.test.d.ts.map +1 -0
- package/dist/tests/realtime.test.js +220 -0
- package/dist/tests/reserved-paths.test.d.ts +2 -0
- package/dist/tests/reserved-paths.test.d.ts.map +1 -0
- package/dist/tests/reserved-paths.test.js +21 -0
- package/dist/tests/sync-manager.test.d.ts +2 -0
- package/dist/tests/sync-manager.test.d.ts.map +1 -0
- package/dist/tests/sync-manager.test.js +123 -0
- package/dist/tests/today-in.test.d.ts +2 -0
- package/dist/tests/today-in.test.d.ts.map +1 -0
- package/dist/tests/today-in.test.js +14 -0
- package/dist/tests/validate-manifest-actions.test.d.ts +2 -0
- package/dist/tests/validate-manifest-actions.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-actions.test.js +82 -0
- package/dist/tests/validate-manifest-dependencies.test.d.ts +2 -0
- package/dist/tests/validate-manifest-dependencies.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-dependencies.test.js +27 -0
- package/dist/tests/validate-manifest-forward-credentials.test.d.ts +2 -0
- package/dist/tests/validate-manifest-forward-credentials.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-forward-credentials.test.js +35 -0
- package/dist/tests/validate-sdui-data-endpoints.test.d.ts +2 -0
- package/dist/tests/validate-sdui-data-endpoints.test.d.ts.map +1 -0
- package/dist/tests/validate-sdui-data-endpoints.test.js +41 -0
- package/dist/tests/with-test-db.test.d.ts +2 -0
- package/dist/tests/with-test-db.test.d.ts.map +1 -0
- package/dist/tests/with-test-db.test.js +57 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -1
- package/dist/validate/index.d.ts +148 -0
- package/dist/validate/index.d.ts.map +1 -0
- package/dist/validate/index.js +887 -0
- package/dist/validate/migration-parse.d.ts +22 -0
- package/dist/validate/migration-parse.d.ts.map +1 -0
- package/dist/validate/migration-parse.js +11 -0
- package/dist/validate/shared-message-keys.d.ts +16 -0
- package/dist/validate/shared-message-keys.d.ts.map +1 -0
- package/dist/validate/shared-message-keys.js +227 -0
- package/package.json +56 -4
- package/dist/client/Auth.d.ts +0 -19
- package/dist/client/Auth.d.ts.map +0 -1
- package/dist/client/Auth.js +0 -45
- package/dist/client/QueryBuilder.d.ts +0 -17
- package/dist/client/QueryBuilder.d.ts.map +0 -1
- package/dist/client/QueryBuilder.js +0 -76
- package/dist/client/RealtimeClient.d.ts +0 -22
- package/dist/client/RealtimeClient.d.ts.map +0 -1
- package/dist/client/RealtimeClient.js +0 -96
- package/dist/client/ZveltioClient.d.ts +0 -20
- package/dist/client/ZveltioClient.d.ts.map +0 -1
- package/dist/client/ZveltioClient.js +0 -78
package/dist/client.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
import { ZveltioApiError } from './errors.js';
|
|
2
|
+
/** The header a step-up token rides in (engine lib/step-up.ts). */
|
|
3
|
+
const STEP_UP_HEADER = 'X-Zveltio-Step-Up';
|
|
4
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
1
5
|
class CollectionRef {
|
|
2
|
-
name;
|
|
3
6
|
client;
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
/** URL-encoded once, here: a record id is caller data (a slug, an email, a
|
|
8
|
+
* composite key), and an unencoded `/`, `?` or `#` in one silently routes
|
|
9
|
+
* the request somewhere else. */
|
|
10
|
+
path;
|
|
11
|
+
constructor(name,
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
13
|
+
client) {
|
|
6
14
|
this.client = client;
|
|
15
|
+
this.path = `/api/data/${encodeURIComponent(name)}`;
|
|
7
16
|
}
|
|
8
17
|
list(params) {
|
|
9
18
|
const qs = new URLSearchParams();
|
|
@@ -22,25 +31,25 @@ class CollectionRef {
|
|
|
22
31
|
if (params?.cursor)
|
|
23
32
|
qs.set('cursor', params.cursor);
|
|
24
33
|
const q = qs.toString();
|
|
25
|
-
return this.client['request']('GET',
|
|
34
|
+
return this.client['request']('GET', `${this.path}${q ? `?${q}` : ''}`);
|
|
26
35
|
}
|
|
27
36
|
getMany(params) {
|
|
28
37
|
return this.list(params);
|
|
29
38
|
}
|
|
30
39
|
get(id) {
|
|
31
|
-
return this.client['request']('GET',
|
|
40
|
+
return this.client['request']('GET', `${this.path}/${encodeURIComponent(id)}`);
|
|
32
41
|
}
|
|
33
42
|
getOne(id) {
|
|
34
43
|
return this.get(id);
|
|
35
44
|
}
|
|
36
45
|
create(data) {
|
|
37
|
-
return this.client['request']('POST',
|
|
46
|
+
return this.client['request']('POST', this.path, data);
|
|
38
47
|
}
|
|
39
48
|
update(id, data) {
|
|
40
|
-
return this.client['request']('PATCH',
|
|
49
|
+
return this.client['request']('PATCH', `${this.path}/${encodeURIComponent(id)}`, data);
|
|
41
50
|
}
|
|
42
51
|
delete(id) {
|
|
43
|
-
return this.client['request']('DELETE',
|
|
52
|
+
return this.client['request']('DELETE', `${this.path}/${encodeURIComponent(id)}`);
|
|
44
53
|
}
|
|
45
54
|
}
|
|
46
55
|
/**
|
|
@@ -50,15 +59,20 @@ class CollectionRef {
|
|
|
50
59
|
*
|
|
51
60
|
* import type { ZveltioSchema } from './zveltio-types';
|
|
52
61
|
* const client = createZveltioClient<ZveltioSchema>({ baseUrl: '...' });
|
|
53
|
-
* const {
|
|
62
|
+
* const { records } = await client.collection('products').list();
|
|
54
63
|
* // ^-- typed as your Products interface
|
|
55
64
|
*
|
|
56
65
|
* Usage without types (untyped, same as before):
|
|
57
66
|
* const client = createZveltioClient({ baseUrl: '...' });
|
|
58
67
|
*/
|
|
68
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
59
69
|
export class ZveltioClient {
|
|
60
70
|
baseUrl;
|
|
61
71
|
headers;
|
|
72
|
+
bearer;
|
|
73
|
+
sessionToken;
|
|
74
|
+
onSessionToken;
|
|
75
|
+
onStepUpRequired;
|
|
62
76
|
constructor(config) {
|
|
63
77
|
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
64
78
|
this.headers = {
|
|
@@ -66,34 +80,90 @@ export class ZveltioClient {
|
|
|
66
80
|
...(config.apiKey ? { 'X-API-Key': config.apiKey } : {}),
|
|
67
81
|
...config.headers,
|
|
68
82
|
};
|
|
83
|
+
this.bearer = config.sessionToken !== undefined || config.onSessionToken !== undefined;
|
|
84
|
+
this.sessionToken = config.sessionToken ?? null;
|
|
85
|
+
this.onSessionToken = config.onSessionToken;
|
|
86
|
+
this.onStepUpRequired = config.onStepUpRequired;
|
|
69
87
|
}
|
|
70
|
-
|
|
88
|
+
/** Switch to bearer mode with `token` (or sign this client out of it with `null`). */
|
|
89
|
+
setSessionToken(token) {
|
|
90
|
+
this.bearer = true;
|
|
91
|
+
this.sessionToken = token;
|
|
92
|
+
this.onSessionToken?.(token);
|
|
93
|
+
}
|
|
94
|
+
/** The token bearer mode sends; null in cookie mode or when signed out. */
|
|
95
|
+
getSessionToken() {
|
|
96
|
+
return this.sessionToken;
|
|
97
|
+
}
|
|
98
|
+
/** Every call goes through here: the bearer out, a newly issued token in. */
|
|
99
|
+
async send(path, init) {
|
|
100
|
+
const res = await this.sendOnce(path, init);
|
|
101
|
+
if (res.status !== 403 || !this.onStepUpRequired)
|
|
102
|
+
return this.checked(res);
|
|
103
|
+
const err = await ZveltioApiError.fromResponse(res);
|
|
104
|
+
if (err.code !== 'step_up_required')
|
|
105
|
+
throw err;
|
|
106
|
+
const action = err.errors?.action;
|
|
107
|
+
const token = typeof action === 'string' ? await this.onStepUpRequired(action) : null;
|
|
108
|
+
if (!token)
|
|
109
|
+
throw err;
|
|
110
|
+
return this.checked(await this.sendOnce(path, {
|
|
111
|
+
...init,
|
|
112
|
+
headers: { ...init.headers, [STEP_UP_HEADER]: token },
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
async checked(res) {
|
|
116
|
+
if (!res.ok)
|
|
117
|
+
throw await ZveltioApiError.fromResponse(res);
|
|
118
|
+
return res;
|
|
119
|
+
}
|
|
120
|
+
async sendOnce(path, init) {
|
|
71
121
|
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
122
|
+
...init,
|
|
123
|
+
headers: this.bearer && this.sessionToken
|
|
124
|
+
? { ...init.headers, Authorization: `Bearer ${this.sessionToken}` }
|
|
125
|
+
: init.headers,
|
|
126
|
+
// A cookie jar beside the bearer would carry a session the bearer then overrides.
|
|
127
|
+
credentials: this.bearer ? 'omit' : 'include',
|
|
128
|
+
});
|
|
129
|
+
const issued = this.bearer ? res.headers.get('set-auth-token') : null;
|
|
130
|
+
if (issued && issued !== this.sessionToken)
|
|
131
|
+
this.setSessionToken(issued);
|
|
132
|
+
return res;
|
|
133
|
+
}
|
|
134
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
135
|
+
async request(method, path, body) {
|
|
136
|
+
const res = await this.send(path, {
|
|
72
137
|
method,
|
|
73
138
|
headers: this.headers,
|
|
74
|
-
credentials: 'include',
|
|
75
139
|
body: body ? JSON.stringify(body) : undefined,
|
|
76
140
|
});
|
|
77
|
-
if (!res.ok) {
|
|
78
|
-
const text = await res.text().catch(() => '');
|
|
79
|
-
throw new Error(`${method} ${path} failed: ${res.status} ${text}`);
|
|
80
|
-
}
|
|
81
141
|
return res.json();
|
|
82
142
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
143
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
144
|
+
get(path) {
|
|
145
|
+
return this.request('GET', path);
|
|
146
|
+
}
|
|
147
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
148
|
+
post(path, body) {
|
|
149
|
+
return this.request('POST', path, body);
|
|
150
|
+
}
|
|
151
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
152
|
+
patch(path, body) {
|
|
153
|
+
return this.request('PATCH', path, body);
|
|
154
|
+
}
|
|
155
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
156
|
+
delete(path) {
|
|
157
|
+
return this.request('DELETE', path);
|
|
158
|
+
}
|
|
159
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
87
160
|
async upload(path, formData) {
|
|
88
161
|
const headers = { ...this.headers };
|
|
89
162
|
delete headers['Content-Type'];
|
|
90
|
-
const res = await
|
|
91
|
-
method: 'POST', headers, credentials: 'include', body: formData,
|
|
92
|
-
});
|
|
93
|
-
if (!res.ok)
|
|
94
|
-
throw new Error(`Upload ${path} failed: ${res.status}`);
|
|
163
|
+
const res = await this.send(path, { method: 'POST', headers, body: formData });
|
|
95
164
|
return res.json();
|
|
96
165
|
}
|
|
166
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
97
167
|
collection(name) {
|
|
98
168
|
return new CollectionRef(name, this);
|
|
99
169
|
}
|
|
@@ -101,22 +171,53 @@ export class ZveltioClient {
|
|
|
101
171
|
auth = {
|
|
102
172
|
login: (email, password) => this.request('POST', '/api/auth/sign-in/email', { email, password }),
|
|
103
173
|
signup: (email, password, name) => this.request('POST', '/api/auth/sign-up/email', { email, password, name }),
|
|
104
|
-
logout: () =>
|
|
174
|
+
logout: async () => {
|
|
175
|
+
try {
|
|
176
|
+
return await this.request('POST', '/api/auth/sign-out');
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
if (this.bearer)
|
|
180
|
+
this.setSessionToken(null);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
105
183
|
session: () => this.request('GET', '/api/auth/get-session'),
|
|
184
|
+
/**
|
|
185
|
+
* The API key this client was made with: its scopes, tenant, expiry and RLS
|
|
186
|
+
* exemption — never the hash. 403 for a session, 401 for a key that no
|
|
187
|
+
* longer authenticates.
|
|
188
|
+
*/
|
|
189
|
+
apiKey: () => this.request('GET', '/api/api-keys/self'),
|
|
190
|
+
/**
|
|
191
|
+
* The signed-in member's own keys (`/api/me/api-keys`, ADR 0010), when the
|
|
192
|
+
* instance lets members issue them. A session only; creating one needs a
|
|
193
|
+
* step-up, which `onStepUpRequired` answers. The raw `key` is returned once.
|
|
194
|
+
*/
|
|
195
|
+
apiKeys: {
|
|
196
|
+
list: () => this.request('GET', '/api/me/api-keys'),
|
|
197
|
+
create: (input) => this.request('POST', '/api/me/api-keys', input),
|
|
198
|
+
revoke: (id) => this.request('DELETE', `/api/me/api-keys/${encodeURIComponent(id)}`),
|
|
199
|
+
},
|
|
106
200
|
};
|
|
107
|
-
/**
|
|
201
|
+
/**
|
|
202
|
+
* Storage helpers. A client made with `apiKey` needs a key holding the `$storage`
|
|
203
|
+
* scope — `{ collection: '$storage', actions: ['read', 'create', 'delete'] }`; a
|
|
204
|
+
* `*` collection does not include it, and the engine answers 403 without it.
|
|
205
|
+
*/
|
|
108
206
|
storage = {
|
|
109
207
|
upload: (file, folder) => {
|
|
110
208
|
const fd = new FormData();
|
|
111
209
|
fd.append('file', file);
|
|
210
|
+
// The engine reads `folder_id`; a `folder` field is ignored and the file
|
|
211
|
+
// lands in the root.
|
|
112
212
|
if (folder)
|
|
113
|
-
fd.append('
|
|
213
|
+
fd.append('folder_id', folder);
|
|
114
214
|
return this.upload('/api/storage/upload', fd);
|
|
115
215
|
},
|
|
116
|
-
list: (folder) => this.get(`/api/storage${folder ? `?
|
|
216
|
+
list: (folder) => this.get(`/api/storage${folder ? `?folder_id=${encodeURIComponent(folder)}` : ''}`),
|
|
117
217
|
delete: (key) => this.delete(`/api/storage/${encodeURIComponent(key)}`),
|
|
118
218
|
};
|
|
119
219
|
}
|
|
220
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
120
221
|
export function createZveltioClient(config) {
|
|
121
222
|
return new ZveltioClient(config);
|
|
122
223
|
}
|
package/dist/core.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIrD,MAAM,WAAW,iBAAiB;IAChC,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;
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIrD,MAAM,WAAW,iBAAiB;IAChC,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;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AAID,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,eAAe,EACzB,UAAU,EAAE,MAAM,EAElB,QAAQ,EAAE,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE,KAAK,IAAI,GACrD,MAAM,IAAI,CAGZ"}
|
package/dist/core.js
CHANGED
|
@@ -6,20 +6,31 @@
|
|
|
6
6
|
* state primitives (useState/ref) and lifecycle hooks (useEffect/onMounted).
|
|
7
7
|
*/
|
|
8
8
|
// ── Collection ────────────────────────────────────────────────────────────────
|
|
9
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
9
10
|
export async function fetchCollection(client, collectionName, options) {
|
|
11
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
10
12
|
const result = await client.collection(collectionName).list(options);
|
|
13
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
11
14
|
return result?.data ?? result?.records ?? result ?? [];
|
|
12
15
|
}
|
|
16
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
13
17
|
export async function fetchRecord(client, collectionName, id) {
|
|
14
18
|
const result = await client.collection(collectionName).get(id);
|
|
19
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
15
20
|
return result?.data ?? result?.record ?? result ?? null;
|
|
16
21
|
}
|
|
22
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
17
23
|
export async function createRecord(client, collectionName, data) {
|
|
24
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
18
25
|
const result = await client.collection(collectionName).create(data);
|
|
26
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
19
27
|
return result?.data ?? result?.record ?? result;
|
|
20
28
|
}
|
|
29
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
21
30
|
export async function updateRecord(client, collectionName, id, data) {
|
|
31
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
22
32
|
const result = await client.collection(collectionName).update(id, data);
|
|
33
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
23
34
|
return result?.data ?? result?.record ?? result;
|
|
24
35
|
}
|
|
25
36
|
export async function deleteRecord(client, collectionName, id) {
|
|
@@ -28,8 +39,10 @@ export async function deleteRecord(client, collectionName, id) {
|
|
|
28
39
|
// ── Auth ──────────────────────────────────────────────────────────────────────
|
|
29
40
|
export async function fetchSession(client) {
|
|
30
41
|
try {
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
// `/api/auth/get-session` answers `{ session, user }` (or null): `session`
|
|
43
|
+
// is the envelope's own member, not the envelope.
|
|
44
|
+
const res = await client.auth.session();
|
|
45
|
+
return { user: res?.user ?? null, session: res?.session ?? null };
|
|
33
46
|
}
|
|
34
47
|
catch {
|
|
35
48
|
return { user: null, session: null };
|
|
@@ -50,16 +63,21 @@ export async function signupUser(client, email, password, name) {
|
|
|
50
63
|
// ── Storage ───────────────────────────────────────────────────────────────────
|
|
51
64
|
export async function uploadFile(client, file, folder) {
|
|
52
65
|
const result = await client.storage.upload(file, folder);
|
|
66
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
53
67
|
return result?.data ?? result?.file ?? result;
|
|
54
68
|
}
|
|
55
69
|
export async function listFiles(client, folder) {
|
|
56
70
|
const result = await client.storage.list(folder);
|
|
71
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
57
72
|
return result?.data ?? result?.files ?? result ?? [];
|
|
58
73
|
}
|
|
59
74
|
export async function removeFile(client, fileId) {
|
|
60
75
|
await client.storage.delete(fileId);
|
|
61
76
|
}
|
|
62
77
|
// ── Realtime ──────────────────────────────────────────────────────────────────
|
|
63
|
-
export function subscribeToCollection(realtime, collection,
|
|
78
|
+
export function subscribeToCollection(realtime, collection,
|
|
79
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
80
|
+
callback) {
|
|
81
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
64
82
|
return realtime.subscribe(collection, callback);
|
|
65
83
|
}
|
package/dist/crdt.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crdt.d.ts","sourceRoot":"","sources":["../src/crdt.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crdt.d.ts","sourceRoot":"","sources":["../src/crdt.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,GAAG;IAC/B,KAAK,EAAE,CAAC,CAAC;IACT,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAEnD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAG,WAAW,CAkB7E;AAED,gDAAgD;AAChD,wBAAgB,UAAU,CAExB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACf,WAAW,CAMb;AAED,8CAA8C;AAE9C,wBAAgB,YAAY,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAOlE;AAKD,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,GAAG;IACnC,KAAK,EAAE,CAAC,CAAC;IACT,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,OAAO,CAAC;CAClB;AAGD,MAAM,MAAM,KAAK,CAAC,CAAC,GAAG,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;AAE/C,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAE7D;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,CAEnE;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAezE;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAEjD;AAID,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;gBAEtB,QAAQ,EAAE,MAAM,EAAE,OAAO,SAAI;IAKzC,yCAAyC;IACzC,IAAI,IAAI,MAAM;IAId,uDAAuD;IACvD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAKhC,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,EAAE,IAAI,MAAM,CAEf;CACF"}
|
package/dist/crdt.js
CHANGED
|
@@ -27,7 +27,9 @@ export function mergeLWW(local, remote) {
|
|
|
27
27
|
return result;
|
|
28
28
|
}
|
|
29
29
|
/** Convert a plain record to an LWW document */
|
|
30
|
-
export function toDocument(
|
|
30
|
+
export function toDocument(
|
|
31
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
32
|
+
data, lamport, clientId) {
|
|
31
33
|
const doc = {};
|
|
32
34
|
for (const [key, value] of Object.entries(data)) {
|
|
33
35
|
doc[key] = { value, lamport, clientId };
|
|
@@ -35,7 +37,9 @@ export function toDocument(data, lamport, clientId) {
|
|
|
35
37
|
return doc;
|
|
36
38
|
}
|
|
37
39
|
/** Extract plain data from an LWW document */
|
|
40
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
38
41
|
export function fromDocument(doc) {
|
|
42
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
39
43
|
const data = {};
|
|
40
44
|
for (const [key, field] of Object.entries(doc)) {
|
|
41
45
|
data[key] = field.value;
|
|
@@ -57,7 +61,12 @@ export function orSetMerge(local, remote) {
|
|
|
57
61
|
merged.set(el.uid, el);
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
|
-
|
|
64
|
+
// Tombstones stay in the set. Dropping them loses the only record that the
|
|
65
|
+
// element was ever removed, so the next merge against a replica that still
|
|
66
|
+
// holds it as present reads it as a new addition and resurrects it — the exact
|
|
67
|
+
// outcome an observed-remove set exists to prevent. `orSetValues` is what
|
|
68
|
+
// hides removed elements from callers.
|
|
69
|
+
return Array.from(merged.values());
|
|
61
70
|
}
|
|
62
71
|
export function orSetValues(set) {
|
|
63
72
|
return set.filter((el) => !el.removed).map((el) => el.value);
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DDL types + DDLManager interface for extension authors (S4-08).
|
|
3
|
+
*
|
|
4
|
+
* Replaces the previous tsconfig path-alias `@zveltio/engine-ddl` that
|
|
5
|
+
* pointed extensions directly at `engine/src/lib/ddl-manager.ts`. That
|
|
6
|
+
* alias broke the SDK ↔ engine boundary: extensions had a hard
|
|
7
|
+
* structural dependency on engine internals, and `bun extension publish`
|
|
8
|
+
* couldn't bundle them cleanly.
|
|
9
|
+
*
|
|
10
|
+
* What lives here:
|
|
11
|
+
* - Pure TYPE definitions for collections, fields, and relations.
|
|
12
|
+
* - The `DDLManager` INTERFACE — the subset of the engine's
|
|
13
|
+
* DDLManager class that extensions actually call via `ctx.DDLManager`.
|
|
14
|
+
*
|
|
15
|
+
* What does NOT live here (intentionally):
|
|
16
|
+
* - The runtime DDLManager class itself. It depends on
|
|
17
|
+
* `fieldTypeRegistry`, lock-timeout transactions, and other
|
|
18
|
+
* engine-internal plumbing. The engine continues to own the
|
|
19
|
+
* implementation and hands an instance into every extension via
|
|
20
|
+
* `ctx.DDLManager`.
|
|
21
|
+
*
|
|
22
|
+
* Usage in an extension:
|
|
23
|
+
* import type { DDLManager, CollectionDefinition } from '@zveltio/sdk/ddl';
|
|
24
|
+
*
|
|
25
|
+
* export const ext: ZveltioExtension = {
|
|
26
|
+
* async register(_app, ctx) {
|
|
27
|
+
* const ddl: DDLManager = ctx.DDLManager;
|
|
28
|
+
* const col = await ddl.getCollection(ctx.db, 'contacts');
|
|
29
|
+
* // ...
|
|
30
|
+
* },
|
|
31
|
+
* };
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Definition of one field in a collection. Mirrors the Zod schema in the
|
|
35
|
+
* engine's ddl-manager.ts but as a plain TypeScript interface so the SDK
|
|
36
|
+
* has no Zod dependency.
|
|
37
|
+
*/
|
|
38
|
+
export interface FieldDefinition {
|
|
39
|
+
/** Lowercase, snake_case identifier. PostgreSQL column name. */
|
|
40
|
+
name: string;
|
|
41
|
+
/**
|
|
42
|
+
* Field type id. Built-ins: `text`, `number`, `boolean`, `date`,
|
|
43
|
+
* `datetime`, `json`, `uuid`, `email`, `url`, `richtext`, `m2o`,
|
|
44
|
+
* `m2m`, `o2m`, `reference`, `select`, `multi-select`, `relation`,
|
|
45
|
+
* `geolocation`, etc. Extensions can register custom types via
|
|
46
|
+
* `ctx.fieldTypeRegistry`.
|
|
47
|
+
*/
|
|
48
|
+
type: string;
|
|
49
|
+
required?: boolean;
|
|
50
|
+
unique?: boolean;
|
|
51
|
+
indexed?: boolean;
|
|
52
|
+
/** Default value for new rows. Type depends on `type`. */
|
|
53
|
+
defaultValue?: unknown;
|
|
54
|
+
/** Type-specific options. Shape depends on `type` — see field-type docs. */
|
|
55
|
+
options?: Record<string, string | number | boolean | null | unknown[]>;
|
|
56
|
+
label?: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
/** Store the field's value encrypted at rest (AES-256-GCM). */
|
|
59
|
+
encrypted?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A collection definition — the unit DDL methods operate on. `name`
|
|
63
|
+
* becomes the suffix of the underlying PG table (`zvd_<name>`).
|
|
64
|
+
*/
|
|
65
|
+
export interface CollectionDefinition {
|
|
66
|
+
/** Lowercase, snake_case. Max 63 chars (PG identifier limit). */
|
|
67
|
+
name: string;
|
|
68
|
+
displayName?: string;
|
|
69
|
+
icon?: string;
|
|
70
|
+
/** Which zone the collection's records belong to. */
|
|
71
|
+
routeGroup?: 'public' | 'partners' | 'private' | 'admin';
|
|
72
|
+
/** When true, RLS policies + row ownership are enforced for this table. */
|
|
73
|
+
isPermissioned?: boolean;
|
|
74
|
+
sort?: number;
|
|
75
|
+
/** At least one field; required by createCollection. */
|
|
76
|
+
fields: FieldDefinition[];
|
|
77
|
+
description?: string;
|
|
78
|
+
singularName?: string;
|
|
79
|
+
aiSearchEnabled?: boolean;
|
|
80
|
+
aiSearchField?: string | null;
|
|
81
|
+
/** True for collections owned by an extension's migrations
|
|
82
|
+
* (i.e. ones Studio should not let users alter). */
|
|
83
|
+
isManaged?: boolean;
|
|
84
|
+
isSystem?: boolean;
|
|
85
|
+
schemaLocked?: boolean;
|
|
86
|
+
/** Create even though access rules already name `name`; without it the
|
|
87
|
+
* create throws (`code: 'existing_rules'`) and the rules take effect on
|
|
88
|
+
* nothing. With it they take effect on the new collection. */
|
|
89
|
+
adoptRules?: boolean;
|
|
90
|
+
}
|
|
91
|
+
/** A row from `zvd_relations`, as `getRelations` returns it. */
|
|
92
|
+
export interface RelationRecord {
|
|
93
|
+
id: string;
|
|
94
|
+
name: string;
|
|
95
|
+
type: string;
|
|
96
|
+
source_collection: string;
|
|
97
|
+
source_field: string;
|
|
98
|
+
target_collection: string;
|
|
99
|
+
target_field: string | null;
|
|
100
|
+
junction_table: string | null;
|
|
101
|
+
}
|
|
102
|
+
/** A row from `zv_collections` / `getCollection` / `getCollections`. The
|
|
103
|
+
* exact shape is engine-internal; this interface only fixes the fields
|
|
104
|
+
* extensions are documented to read. Treat unknown keys as best-effort. */
|
|
105
|
+
export interface CollectionRecord {
|
|
106
|
+
id?: string;
|
|
107
|
+
name: string;
|
|
108
|
+
displayName?: string;
|
|
109
|
+
fields?: FieldDefinition[];
|
|
110
|
+
routeGroup?: string;
|
|
111
|
+
isPermissioned?: boolean;
|
|
112
|
+
isManaged?: boolean;
|
|
113
|
+
isSystem?: boolean;
|
|
114
|
+
schemaLocked?: boolean;
|
|
115
|
+
[k: string]: unknown;
|
|
116
|
+
}
|
|
117
|
+
import type { Kysely } from 'kysely';
|
|
118
|
+
export type DDLManagerDb = Kysely<any>;
|
|
119
|
+
/**
|
|
120
|
+
* Subset of `DDLManager` available to extensions via `ctx.DDLManager`.
|
|
121
|
+
*
|
|
122
|
+
* The engine ships the implementation as a class with `static` methods.
|
|
123
|
+
* From an extension's perspective they're indistinguishable from instance
|
|
124
|
+
* methods, hence this interface uses the call shape (no `this` requirement).
|
|
125
|
+
*
|
|
126
|
+
* Stability note: this is part of the v1.0 extension SDK contract. New
|
|
127
|
+
* methods may be added; existing signatures will not change in a breaking
|
|
128
|
+
* way without a major version bump.
|
|
129
|
+
*/
|
|
130
|
+
export interface DDLManager {
|
|
131
|
+
/** Translate a logical collection name to its PG table (`zvd_<name>`). */
|
|
132
|
+
getTableName(collectionName: string): string;
|
|
133
|
+
/** Invalidate the in-engine collection cache. Pass a name to scope the
|
|
134
|
+
* invalidation, or omit to flush everything. */
|
|
135
|
+
invalidateCache(name?: string): void;
|
|
136
|
+
/** True when `zvd_<collectionName>` exists in the public schema. */
|
|
137
|
+
tableExists(db: DDLManagerDb, collectionName: string): Promise<boolean>;
|
|
138
|
+
/** Create a new collection + its underlying table + indices. Throws if
|
|
139
|
+
* the collection already exists. */
|
|
140
|
+
createCollection(db: DDLManagerDb, definition: CollectionDefinition): Promise<void>;
|
|
141
|
+
/** Drop a collection. Refuses by default if any table references it via
|
|
142
|
+
* FK — set `force: true` to cascade. */
|
|
143
|
+
dropCollection(db: DDLManagerDb, name: string, opts?: {
|
|
144
|
+
force?: boolean;
|
|
145
|
+
}): Promise<void>;
|
|
146
|
+
/** Add a single field to an existing collection. */
|
|
147
|
+
addField(db: DDLManagerDb, collectionName: string, field: FieldDefinition): Promise<void>;
|
|
148
|
+
/** Remove a single field. Fails if any zvd_* row still has a non-null
|
|
149
|
+
* value unless the underlying field type allows DROP COLUMN. */
|
|
150
|
+
removeField(db: DDLManagerDb, collectionName: string, fieldName: string): Promise<void>;
|
|
151
|
+
/** List every collection registered in `zv_collections`. */
|
|
152
|
+
getCollections(db: DDLManagerDb): Promise<CollectionRecord[]>;
|
|
153
|
+
/** Every registered relation, or only those whose source or target is `collection`. */
|
|
154
|
+
getRelations(db: DDLManagerDb, collection?: string): Promise<RelationRecord[]>;
|
|
155
|
+
/** Read a single collection by name. Returns `null` when missing. */
|
|
156
|
+
getCollection(db: DDLManagerDb, name: string): Promise<CollectionRecord | null>;
|
|
157
|
+
/** Update display-only metadata (label, description, icon, sort, etc.). */
|
|
158
|
+
updateCollectionMetadata(db: DDLManagerDb, name: string, metadata: Partial<CollectionDefinition>): Promise<void>;
|
|
159
|
+
/** Introspect the live PG table and return its field configs. Useful for
|
|
160
|
+
* extensions that auto-discover schema changes. */
|
|
161
|
+
introspectTable(db: DDLManagerDb, collectionName: string): Promise<FieldDefinition[]>;
|
|
162
|
+
/** Every physical column of the collection's table, system columns included,
|
|
163
|
+
* in table order; `[]` when the table does not exist. */
|
|
164
|
+
columnNames(db: DDLManagerDb, collectionName: string): Promise<string[]>;
|
|
165
|
+
/** Re-read PG and write the result back to `zv_collections.fields`. Returns
|
|
166
|
+
* the number of changes detected. */
|
|
167
|
+
syncFieldsFromDB(db: DDLManagerDb, collectionName: string): Promise<number>;
|
|
168
|
+
/** Render the SQL that `createCollection` would emit, without running it. */
|
|
169
|
+
previewCollection(definition: CollectionDefinition): Promise<{
|
|
170
|
+
sql: string[];
|
|
171
|
+
}>;
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ddl/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAIH;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb;;;;;;OAMG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,0DAA0D;IAC1D,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,OAAO,EAAE,CAAC,CAAC;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,UAAU,CAAC,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC;IACzD,2EAA2E;IAC3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AAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC;IAEhF,2EAA2E;IAC3E,wBAAwB,CACtB,EAAE,EAAE,YAAY,EAChB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,OAAO,CAAC,oBAAoB,CAAC,GACtC,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;wDACoD;IACpD,eAAe,CAAC,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;IAEtF;8DAC0D;IAC1D,WAAW,CAAC,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEzE;0CACsC;IACtC,gBAAgB,CAAC,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE5E,6EAA6E;IAC7E,iBAAiB,CAAC,UAAU,EAAE,oBAAoB,GAAG,OAAO,CAAC;QAAE,GAAG,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CACjF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DDL types + DDLManager interface for extension authors (S4-08).
|
|
3
|
+
*
|
|
4
|
+
* Replaces the previous tsconfig path-alias `@zveltio/engine-ddl` that
|
|
5
|
+
* pointed extensions directly at `engine/src/lib/ddl-manager.ts`. That
|
|
6
|
+
* alias broke the SDK ↔ engine boundary: extensions had a hard
|
|
7
|
+
* structural dependency on engine internals, and `bun extension publish`
|
|
8
|
+
* couldn't bundle them cleanly.
|
|
9
|
+
*
|
|
10
|
+
* What lives here:
|
|
11
|
+
* - Pure TYPE definitions for collections, fields, and relations.
|
|
12
|
+
* - The `DDLManager` INTERFACE — the subset of the engine's
|
|
13
|
+
* DDLManager class that extensions actually call via `ctx.DDLManager`.
|
|
14
|
+
*
|
|
15
|
+
* What does NOT live here (intentionally):
|
|
16
|
+
* - The runtime DDLManager class itself. It depends on
|
|
17
|
+
* `fieldTypeRegistry`, lock-timeout transactions, and other
|
|
18
|
+
* engine-internal plumbing. The engine continues to own the
|
|
19
|
+
* implementation and hands an instance into every extension via
|
|
20
|
+
* `ctx.DDLManager`.
|
|
21
|
+
*
|
|
22
|
+
* Usage in an extension:
|
|
23
|
+
* import type { DDLManager, CollectionDefinition } from '@zveltio/sdk/ddl';
|
|
24
|
+
*
|
|
25
|
+
* export const ext: ZveltioExtension = {
|
|
26
|
+
* async register(_app, ctx) {
|
|
27
|
+
* const ddl: DDLManager = ctx.DDLManager;
|
|
28
|
+
* const col = await ddl.getCollection(ctx.db, 'contacts');
|
|
29
|
+
* // ...
|
|
30
|
+
* },
|
|
31
|
+
* };
|
|
32
|
+
*/
|
|
33
|
+
export {};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified error envelope (H-13) — the SDK is the contract source of truth for
|
|
3
|
+
* error shapes, exactly as it is for `ZveltioExtension`.
|
|
4
|
+
*
|
|
5
|
+
* Every non-2xx response from the engine carries an RFC 9457 `problem+json`
|
|
6
|
+
* body: the standard fields (`type`, `title`, `status`, `detail`, `instance`)
|
|
7
|
+
* plus two Zveltio extensions:
|
|
8
|
+
* - `code` — a STABLE machine-readable string (e.g. `tenant.membership_required`)
|
|
9
|
+
* that clients can switch on without parsing prose.
|
|
10
|
+
* - `traceId` — correlates with server logs / the W3C trace, for support.
|
|
11
|
+
*
|
|
12
|
+
* The engine mirrors this shape (it can't import the SDK root at build time);
|
|
13
|
+
* keep the two in sync — this file is the canonical definition.
|
|
14
|
+
*/
|
|
15
|
+
/** RFC 9457 problem-details object + Zveltio `code`/`traceId` extensions. */
|
|
16
|
+
export interface ProblemDetails {
|
|
17
|
+
/** URI reference identifying the problem type. `about:blank` when the HTTP
|
|
18
|
+
* status is the only semantics. */
|
|
19
|
+
type: string;
|
|
20
|
+
/** Short, human-readable summary — stable for a given `code`/status. */
|
|
21
|
+
title: string;
|
|
22
|
+
/** HTTP status code, duplicated in the body per RFC 9457. */
|
|
23
|
+
status: number;
|
|
24
|
+
/** Human-readable explanation specific to this occurrence. */
|
|
25
|
+
detail?: string;
|
|
26
|
+
/** URI reference for this specific occurrence — the request path. */
|
|
27
|
+
instance?: string;
|
|
28
|
+
/** Stable machine-readable error code, e.g. `tenant.membership_required`. */
|
|
29
|
+
code: string;
|
|
30
|
+
/** Correlation id for server logs / distributed trace. */
|
|
31
|
+
traceId?: string;
|
|
32
|
+
/** Optional structured details (e.g. per-field validation errors). */
|
|
33
|
+
errors?: unknown;
|
|
34
|
+
}
|
|
35
|
+
/** MIME type the engine sets on every error response. */
|
|
36
|
+
export declare const PROBLEM_CONTENT_TYPE = "application/problem+json";
|
|
37
|
+
/**
|
|
38
|
+
* Typed error thrown by the SDK client for any non-2xx response. Carries the
|
|
39
|
+
* parsed envelope; tolerant of legacy `{ error }` shapes during the beta.
|
|
40
|
+
*/
|
|
41
|
+
export declare class ZveltioApiError extends Error {
|
|
42
|
+
readonly code: string;
|
|
43
|
+
readonly status: number;
|
|
44
|
+
readonly title: string;
|
|
45
|
+
readonly type: string;
|
|
46
|
+
readonly detail?: string;
|
|
47
|
+
readonly instance?: string;
|
|
48
|
+
readonly traceId?: string;
|
|
49
|
+
readonly errors?: unknown;
|
|
50
|
+
/** The full parsed problem object. */
|
|
51
|
+
readonly problem: ProblemDetails;
|
|
52
|
+
constructor(problem: ProblemDetails);
|
|
53
|
+
/** Parse a failed `fetch` Response into a typed error (tolerant fallback). */
|
|
54
|
+
static fromResponse(res: Response): Promise<ZveltioApiError>;
|
|
55
|
+
/** Build from raw parts — handles problem+json, legacy `{ error }`, or prose. */
|
|
56
|
+
static fromParts(status: number, bodyText: string, contentType?: string | null, url?: string): ZveltioApiError;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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