kizunacms 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.MD +9 -0
- package/README.md +208 -0
- package/dist/access-4YiqVHXB.d.mts +1251 -0
- package/dist/access-CrrWP0hv.d.cts +1251 -0
- package/dist/db-Cnv0aXba.cjs +1603 -0
- package/dist/db-wUR_ude-.mjs +1274 -0
- package/dist/index.cjs +2716 -0
- package/dist/index.d.cts +545 -0
- package/dist/index.d.mts +545 -0
- package/dist/index.mjs +2701 -0
- package/dist/postgres.cjs +1152 -0
- package/dist/postgres.d.cts +99 -0
- package/dist/postgres.d.mts +99 -0
- package/dist/postgres.mjs +1151 -0
- package/package.json +96 -3
|
@@ -0,0 +1,1251 @@
|
|
|
1
|
+
import { PermissionCatalog, Roles, RouteToolOptions } from "kizunajs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/accounts.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A logged-in account's session, kept by the SHA-256 hash of its token.
|
|
7
|
+
*/
|
|
8
|
+
interface SessionRecord {
|
|
9
|
+
/**
|
|
10
|
+
* The SHA-256 hash of the session's token.
|
|
11
|
+
*/
|
|
12
|
+
tokenHash: string;
|
|
13
|
+
/**
|
|
14
|
+
* The collection the account logs in from.
|
|
15
|
+
*/
|
|
16
|
+
collection: string;
|
|
17
|
+
accountId: string;
|
|
18
|
+
/**
|
|
19
|
+
* When the session ends, as an ISO date-time.
|
|
20
|
+
*/
|
|
21
|
+
expiresAt: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A one-time link to set a password, kept by the SHA-256 hash of its token.
|
|
25
|
+
*/
|
|
26
|
+
interface PasswordTokenRecord {
|
|
27
|
+
tokenHash: string;
|
|
28
|
+
collection: string;
|
|
29
|
+
accountId: string;
|
|
30
|
+
expiresAt: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Where sessions, password links and failed logins are kept, as part of a database adapter.
|
|
34
|
+
*/
|
|
35
|
+
interface AccountStore {
|
|
36
|
+
createSession: (session: SessionRecord) => Promise<void>;
|
|
37
|
+
getSession: (tokenHash: string) => Promise<SessionRecord | undefined>;
|
|
38
|
+
deleteSession: (tokenHash: string) => Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Ends every session of one account except `keep`.
|
|
41
|
+
*/
|
|
42
|
+
deleteSessionsOf: (accountId: string, keep?: string) => Promise<void>;
|
|
43
|
+
createPasswordToken: (token: PasswordTokenRecord) => Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Deletes the token and returns it, or `undefined` when none has the hash.
|
|
46
|
+
*/
|
|
47
|
+
takePasswordToken: (tokenHash: string) => Promise<PasswordTokenRecord | undefined>;
|
|
48
|
+
deletePasswordTokensOf: (accountId: string) => Promise<void>;
|
|
49
|
+
recordFailedLogin: (collection: string, email: string) => Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* How many logins for the email failed since `since`.
|
|
52
|
+
*/
|
|
53
|
+
failedLoginsSince: (collection: string, email: string, since: string) => Promise<number>;
|
|
54
|
+
clearFailedLogins: (collection: string, email: string) => Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Counts a request for a password reset link for the email.
|
|
57
|
+
*/
|
|
58
|
+
recordPasswordResetRequest: (collection: string, email: string) => Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* How many reset links were asked for the email since `since`.
|
|
61
|
+
*/
|
|
62
|
+
passwordResetRequestsSince: (collection: string, email: string, since: string) => Promise<number>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Sends the email with the link an account sets its password with. Build the link to your admin's `/reset-password?token=` page
|
|
66
|
+
* and send it the way your app sends other email.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* passwordReset: {
|
|
70
|
+
* send: async ({ email, token }) => {
|
|
71
|
+
* await sendEmail({
|
|
72
|
+
* to: email,
|
|
73
|
+
* subject: 'Set your password',
|
|
74
|
+
* text: `https://cms.example.com/admin/reset-password?token=${token}`,
|
|
75
|
+
* });
|
|
76
|
+
* },
|
|
77
|
+
* },
|
|
78
|
+
*/
|
|
79
|
+
interface PasswordReset {
|
|
80
|
+
/**
|
|
81
|
+
* Sends one link. `reason` is `'reset'` after a reset request, and `'invite'` when an admin invites the account or
|
|
82
|
+
* creates it without a password.
|
|
83
|
+
*/
|
|
84
|
+
send: (message: {
|
|
85
|
+
email: string;
|
|
86
|
+
name: string;
|
|
87
|
+
token: string;
|
|
88
|
+
reason: 'reset' | 'invite';
|
|
89
|
+
}) => Promise<void>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Hashes a password with scrypt and a random salt.
|
|
93
|
+
*/
|
|
94
|
+
declare const hashPassword: (password: string) => Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Returns whether a password matches a hash from `hashPassword`.
|
|
97
|
+
*/
|
|
98
|
+
declare const verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/fields.d.ts
|
|
101
|
+
/**
|
|
102
|
+
* How a field's value is stored in the database.
|
|
103
|
+
*/
|
|
104
|
+
type FieldColumn = 'text' | 'number' | 'boolean' | 'json';
|
|
105
|
+
/**
|
|
106
|
+
* The options every field type takes.
|
|
107
|
+
*/
|
|
108
|
+
interface FieldBase<Name extends string = string> {
|
|
109
|
+
/**
|
|
110
|
+
* The key the field's value is stored and read under, like `publishedOn`.
|
|
111
|
+
*/
|
|
112
|
+
name: Name;
|
|
113
|
+
/**
|
|
114
|
+
* What the admin calls this field.
|
|
115
|
+
*
|
|
116
|
+
* @default the name in words, so `publishedOn` gives "Published on"
|
|
117
|
+
*/
|
|
118
|
+
label?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Help text shown under the input in the admin, and in the API docs and MCP tools.
|
|
121
|
+
*/
|
|
122
|
+
description?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Makes a document invalid without a value for this field.
|
|
125
|
+
*/
|
|
126
|
+
required?: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Shows the field to editors but takes no value for it from the admin or the API.
|
|
129
|
+
*/
|
|
130
|
+
readOnly?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The schema a slug passes: lowercase letters and digits, with single hyphens between them, at most
|
|
134
|
+
* {@link MAX_SLUG_LENGTH} characters.
|
|
135
|
+
*/
|
|
136
|
+
declare const SlugSchema: z.ZodString;
|
|
137
|
+
/**
|
|
138
|
+
* One option of a `select` field. A plain string is both its value and its label.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* options: [
|
|
142
|
+
* 'draft',
|
|
143
|
+
* {
|
|
144
|
+
* value: 'published',
|
|
145
|
+
* label: 'Published',
|
|
146
|
+
* },
|
|
147
|
+
* ],
|
|
148
|
+
*/
|
|
149
|
+
type SelectOption = string | {
|
|
150
|
+
value: string;
|
|
151
|
+
label: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Short text the admin shows beside the value, inside a one-line input.
|
|
155
|
+
*/
|
|
156
|
+
interface FieldAffixes {
|
|
157
|
+
/**
|
|
158
|
+
* Text shown before the value, like `@`.
|
|
159
|
+
*/
|
|
160
|
+
prefix?: string;
|
|
161
|
+
/**
|
|
162
|
+
* Text shown after the value, like `kr`.
|
|
163
|
+
*/
|
|
164
|
+
suffix?: string;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A field that holds text.
|
|
168
|
+
*/
|
|
169
|
+
interface TextField<Name extends string = string> extends FieldBase<Name>, FieldAffixes {
|
|
170
|
+
type: 'text';
|
|
171
|
+
/**
|
|
172
|
+
* The fewest characters the text may have.
|
|
173
|
+
*/
|
|
174
|
+
minLength?: number;
|
|
175
|
+
/**
|
|
176
|
+
* The most characters the text may have.
|
|
177
|
+
*/
|
|
178
|
+
maxLength?: number;
|
|
179
|
+
/**
|
|
180
|
+
* Gives the text a box of several lines in the admin.
|
|
181
|
+
*/
|
|
182
|
+
multiline?: boolean;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* A field that holds a number.
|
|
186
|
+
*/
|
|
187
|
+
interface NumberField<Name extends string = string> extends FieldBase<Name>, FieldAffixes {
|
|
188
|
+
type: 'number';
|
|
189
|
+
/**
|
|
190
|
+
* The smallest number it takes.
|
|
191
|
+
*/
|
|
192
|
+
min?: number;
|
|
193
|
+
/**
|
|
194
|
+
* The largest number it takes.
|
|
195
|
+
*/
|
|
196
|
+
max?: number;
|
|
197
|
+
/**
|
|
198
|
+
* Takes only whole numbers.
|
|
199
|
+
*/
|
|
200
|
+
integer?: boolean;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* A field that holds `true` or `false`, shown as a checkbox.
|
|
204
|
+
*/
|
|
205
|
+
interface BooleanField<Name extends string = string> extends FieldBase<Name> {
|
|
206
|
+
type: 'boolean';
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A field that holds a URL.
|
|
210
|
+
*/
|
|
211
|
+
interface UrlField<Name extends string = string> extends FieldBase<Name>, FieldAffixes {
|
|
212
|
+
type: 'url';
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A field that holds an email address.
|
|
216
|
+
*/
|
|
217
|
+
interface EmailField<Name extends string = string> extends FieldBase<Name>, FieldAffixes {
|
|
218
|
+
type: 'email';
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* A field that holds a slug: lowercase letters and digits, with single hyphens between them, at most
|
|
222
|
+
* {@link MAX_SLUG_LENGTH} characters.
|
|
223
|
+
*/
|
|
224
|
+
interface SlugField<Name extends string = string> extends FieldBase<Name> {
|
|
225
|
+
type: 'slug';
|
|
226
|
+
/**
|
|
227
|
+
* The text field, or a function, an empty slug is filled in from when a document is created. With a field name,
|
|
228
|
+
* the admin also fills it in as an editor types. A text with no letters or digits makes no slug, and a create whose
|
|
229
|
+
* made slug comes out longer than {@link MAX_SLUG_LENGTH} characters answers 422.
|
|
230
|
+
*
|
|
231
|
+
* @default the `titleField` field
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* generateFrom: (author) => `${author.firstName} ${author.lastName}`,
|
|
235
|
+
*/
|
|
236
|
+
generateFrom?: string | ((values: Record<string, unknown>) => string | undefined);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* A field that holds one of a list of options, or several with `many`.
|
|
240
|
+
*/
|
|
241
|
+
interface SelectField<Name extends string = string, Options extends readonly SelectOption[] = readonly SelectOption[]> extends FieldBase<Name> {
|
|
242
|
+
type: 'select';
|
|
243
|
+
/**
|
|
244
|
+
* The options editors pick from.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* options: ['draft', 'published'],
|
|
248
|
+
*/
|
|
249
|
+
options: Options;
|
|
250
|
+
/**
|
|
251
|
+
* Lets editors pick several options, stored as a list.
|
|
252
|
+
*/
|
|
253
|
+
many?: boolean;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* A field that holds a calendar day with no time zone, as an ISO date like `2026-11-16`, from the year 0001.
|
|
257
|
+
*/
|
|
258
|
+
interface DateField<Name extends string = string> extends FieldBase<Name> {
|
|
259
|
+
type: 'date';
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* A field that holds a moment in time, as an ISO date-time in UTC like `2026-11-16T14:30:00Z`, from the year 0001.
|
|
263
|
+
*/
|
|
264
|
+
interface DateTimeField<Name extends string = string> extends FieldBase<Name> {
|
|
265
|
+
type: 'dateTime';
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* A field that holds the id of a document in another collection. A document another one points at can't be deleted
|
|
269
|
+
* until that pointer is removed.
|
|
270
|
+
*/
|
|
271
|
+
interface RelationshipField<Name extends string = string, To extends string = string> extends FieldBase<Name> {
|
|
272
|
+
type: 'relationship';
|
|
273
|
+
/**
|
|
274
|
+
* The collection the ids point into, like `authors`.
|
|
275
|
+
*/
|
|
276
|
+
to: To;
|
|
277
|
+
/**
|
|
278
|
+
* Holds a list of ids instead of one.
|
|
279
|
+
*/
|
|
280
|
+
many?: boolean;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* A field that holds an image from one of the CMS's stores.
|
|
284
|
+
*/
|
|
285
|
+
interface ImageField<Name extends string = string, StoreName extends string = string> extends FieldBase<Name> {
|
|
286
|
+
type: 'image';
|
|
287
|
+
/**
|
|
288
|
+
* The store the image is uploaded to, like `images`.
|
|
289
|
+
*/
|
|
290
|
+
storage: StoreName;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A field that holds a file from one of the CMS's stores.
|
|
294
|
+
*/
|
|
295
|
+
interface FileField<Name extends string = string, StoreName extends string = string> extends FieldBase<Name> {
|
|
296
|
+
type: 'file';
|
|
297
|
+
/**
|
|
298
|
+
* The store the file is uploaded to, like `documents`.
|
|
299
|
+
*/
|
|
300
|
+
storage: StoreName;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Any field of a built-in type.
|
|
304
|
+
*/
|
|
305
|
+
type BuiltInField<To extends string = string, StoreName extends string = string> = TextField | NumberField | BooleanField | UrlField | EmailField | SlugField | SelectField | DateField | DateTimeField | RelationshipField<string, To> | ImageField<string, StoreName> | FileField<string, StoreName>;
|
|
306
|
+
/**
|
|
307
|
+
* Any field a collection, global or store holds.
|
|
308
|
+
*/
|
|
309
|
+
type Field<To extends string = string, StoreName extends string = string> = BuiltInField<To, StoreName>;
|
|
310
|
+
/**
|
|
311
|
+
* The store's own fields an image or file field carries in its type, from the generated `Config`.
|
|
312
|
+
*/
|
|
313
|
+
type WithStoreFields<StoreFields extends readonly unknown[]> = {
|
|
314
|
+
readonly '~storeFields': StoreFields;
|
|
315
|
+
};
|
|
316
|
+
type SelectValue<Option> = Option extends {
|
|
317
|
+
value: infer Value;
|
|
318
|
+
} ? Value : Option;
|
|
319
|
+
/**
|
|
320
|
+
* The value a field holds, typed from its definition.
|
|
321
|
+
*/
|
|
322
|
+
type FieldValue<F> = F extends {
|
|
323
|
+
type: 'text' | 'url' | 'email' | 'slug' | 'date' | 'dateTime';
|
|
324
|
+
} ? string : F extends {
|
|
325
|
+
type: 'number';
|
|
326
|
+
} ? number : F extends {
|
|
327
|
+
type: 'boolean';
|
|
328
|
+
} ? boolean : F extends {
|
|
329
|
+
type: 'select';
|
|
330
|
+
options: readonly (infer Option)[];
|
|
331
|
+
} ? F extends {
|
|
332
|
+
many: true;
|
|
333
|
+
} ? SelectValue<Option>[] : SelectValue<Option> : F extends {
|
|
334
|
+
type: 'relationship';
|
|
335
|
+
} ? F extends {
|
|
336
|
+
many: true;
|
|
337
|
+
} ? string[] : string : F extends {
|
|
338
|
+
type: 'image';
|
|
339
|
+
} ? ImageValue<StoreFieldsIn<F>> : F extends {
|
|
340
|
+
type: 'file';
|
|
341
|
+
} ? FileValue<StoreFieldsIn<F>> : unknown;
|
|
342
|
+
type StoreFieldsIn<F> = F extends WithStoreFields<infer StoreFields extends readonly Field[]> ? StoreFields : readonly [];
|
|
343
|
+
/**
|
|
344
|
+
* An image file's URL and its size in pixels.
|
|
345
|
+
*/
|
|
346
|
+
interface ImageVersion {
|
|
347
|
+
url: string;
|
|
348
|
+
width: number;
|
|
349
|
+
height: number;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* An image field's value as a document reads it: the original, its focal point and the store's own fields.
|
|
353
|
+
*/
|
|
354
|
+
type ImageValue<StoreFields extends readonly Field[]> = {
|
|
355
|
+
id: string;
|
|
356
|
+
alt?: string;
|
|
357
|
+
filename: string;
|
|
358
|
+
contentType: string;
|
|
359
|
+
fileSize: number;
|
|
360
|
+
/**
|
|
361
|
+
* Where the image stays in view when a site crops it, in percent from its top left corner.
|
|
362
|
+
*/
|
|
363
|
+
focalPoint: {
|
|
364
|
+
x: number;
|
|
365
|
+
y: number;
|
|
366
|
+
};
|
|
367
|
+
original: ImageVersion;
|
|
368
|
+
} & ContentOf<StoreFields>;
|
|
369
|
+
/**
|
|
370
|
+
* A file field's value as a document reads it, with the store's own fields.
|
|
371
|
+
*/
|
|
372
|
+
type FileValue<StoreFields extends readonly Field[]> = {
|
|
373
|
+
id: string;
|
|
374
|
+
url: string;
|
|
375
|
+
filename: string;
|
|
376
|
+
contentType: string;
|
|
377
|
+
fileSize: number;
|
|
378
|
+
} & ContentOf<StoreFields>;
|
|
379
|
+
/**
|
|
380
|
+
* Returns the schema a field's value passes, with a description that says where a relationship's id points.
|
|
381
|
+
*/
|
|
382
|
+
declare const schemaOf: (field: Field, owner?: string) => z.ZodType;
|
|
383
|
+
/**
|
|
384
|
+
* Returns how a field's value is stored in the database.
|
|
385
|
+
*/
|
|
386
|
+
declare const columnOf: (field: Field) => FieldColumn;
|
|
387
|
+
//#endregion
|
|
388
|
+
//#region src/global.d.ts
|
|
389
|
+
/**
|
|
390
|
+
* The options `k.global` takes.
|
|
391
|
+
*/
|
|
392
|
+
interface GlobalDefinition<Name extends string, Fields extends readonly Field[]> {
|
|
393
|
+
/**
|
|
394
|
+
* The global's name in its routes and clients, in camelCase like `settings` or `footer`.
|
|
395
|
+
*/
|
|
396
|
+
name: Name;
|
|
397
|
+
/**
|
|
398
|
+
* What the admin calls this global.
|
|
399
|
+
*
|
|
400
|
+
* @default the global's name
|
|
401
|
+
*/
|
|
402
|
+
label?: string;
|
|
403
|
+
/**
|
|
404
|
+
* The roles that may read and update this global, one list per action. `['public']` lets anyone do it without
|
|
405
|
+
* logging in.
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* auth: {
|
|
409
|
+
* read: ['public'],
|
|
410
|
+
* update: ['admin'],
|
|
411
|
+
* },
|
|
412
|
+
*/
|
|
413
|
+
auth: GlobalAuth;
|
|
414
|
+
/**
|
|
415
|
+
* The actions on this global an AI agent may call as MCP tools.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* tools: {
|
|
419
|
+
* read: true,
|
|
420
|
+
* },
|
|
421
|
+
*/
|
|
422
|
+
tools?: GlobalTools;
|
|
423
|
+
/**
|
|
424
|
+
* The fields this global holds.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* fields: [
|
|
428
|
+
* {
|
|
429
|
+
* name: 'siteName',
|
|
430
|
+
* type: 'text',
|
|
431
|
+
* },
|
|
432
|
+
* ],
|
|
433
|
+
*/
|
|
434
|
+
fields: Fields;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* A single document with no list, as `k.global` returns it.
|
|
438
|
+
*/
|
|
439
|
+
interface Global<Name extends string = string, Fields extends readonly Field[] = readonly Field[]> extends GlobalDefinition<Name, Fields> {
|
|
440
|
+
readonly kind: 'global';
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* The values a global holds, typed from its fields. Every field is optional, since a field nobody saved is absent.
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* type SettingsValues = GlobalOf<typeof Settings>;
|
|
447
|
+
*/
|
|
448
|
+
type GlobalOf<G> = G extends Global<string, infer Fields> ? Partial<ContentOf<Fields>> & {
|
|
449
|
+
updatedAt?: string;
|
|
450
|
+
} : never;
|
|
451
|
+
//#endregion
|
|
452
|
+
//#region src/storage.d.ts
|
|
453
|
+
/**
|
|
454
|
+
* Where a store keeps its files, like `s3Storage()` from `@kizunacms/storage-s3` or `diskStorage()`.
|
|
455
|
+
*/
|
|
456
|
+
interface StorageAdapter {
|
|
457
|
+
/**
|
|
458
|
+
* Returns a signed URL the browser uploads the file to with PUT, and the headers to send with it.
|
|
459
|
+
*/
|
|
460
|
+
presignUpload: (input: {
|
|
461
|
+
key: string;
|
|
462
|
+
contentType: string;
|
|
463
|
+
fileSize: number;
|
|
464
|
+
expiresIn: number;
|
|
465
|
+
}) => Promise<{
|
|
466
|
+
url: string;
|
|
467
|
+
headers: Record<string, string>;
|
|
468
|
+
}>;
|
|
469
|
+
/**
|
|
470
|
+
* Returns the file's size and type, or `undefined` when nothing is uploaded under the key.
|
|
471
|
+
*/
|
|
472
|
+
head: (key: string) => Promise<{
|
|
473
|
+
fileSize: number;
|
|
474
|
+
contentType?: string;
|
|
475
|
+
} | undefined>;
|
|
476
|
+
/**
|
|
477
|
+
* Returns the file's first `length` bytes, or `undefined` when nothing is uploaded under the key.
|
|
478
|
+
*/
|
|
479
|
+
read: (key: string, length: number) => Promise<Uint8Array | undefined>;
|
|
480
|
+
/**
|
|
481
|
+
* Writes the bytes under the key, replacing any file there.
|
|
482
|
+
*/
|
|
483
|
+
put: (key: string, bytes: Uint8Array, contentType: string) => Promise<void>;
|
|
484
|
+
/**
|
|
485
|
+
* Deletes the file under the key.
|
|
486
|
+
*/
|
|
487
|
+
delete: (key: string) => Promise<void>;
|
|
488
|
+
/**
|
|
489
|
+
* Returns the URL the file is read from.
|
|
490
|
+
*/
|
|
491
|
+
url: (key: string) => Promise<string>;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* The options `k.storage` takes.
|
|
495
|
+
*/
|
|
496
|
+
interface StorageDefinition<Name extends string, Fields extends readonly Field[]> {
|
|
497
|
+
/**
|
|
498
|
+
* The store's name, which image and file fields point at, like `images`.
|
|
499
|
+
*/
|
|
500
|
+
name: Name;
|
|
501
|
+
/**
|
|
502
|
+
* Where the files are kept, like `s3Storage()` from `@kizunacms/storage-s3` or `diskStorage()` from `kizunacms`.
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* adapter: s3Storage({
|
|
506
|
+
* bucket: 'example-images',
|
|
507
|
+
* region: 'eu-north-1',
|
|
508
|
+
* }),
|
|
509
|
+
*/
|
|
510
|
+
adapter: StorageAdapter;
|
|
511
|
+
/**
|
|
512
|
+
* The roles that may read, upload, update and delete files in this store, one list per action. `['public']` lets
|
|
513
|
+
* anyone do it without logging in.
|
|
514
|
+
*
|
|
515
|
+
* @example
|
|
516
|
+
* auth: {
|
|
517
|
+
* read: ['public'],
|
|
518
|
+
* create: ['editor'],
|
|
519
|
+
* update: ['editor'],
|
|
520
|
+
* delete: ['admin'],
|
|
521
|
+
* },
|
|
522
|
+
*/
|
|
523
|
+
auth: CollectionAuth;
|
|
524
|
+
/**
|
|
525
|
+
* Fields of your own on every file in this store. The CMS already keeps each file's `filename`, `contentType`,
|
|
526
|
+
* `fileSize`, `width`, `height`, `alt`, `focalPoint` and `uploadedBy`. A store anyone may read leaves `uploadedBy` out.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* fields: [
|
|
530
|
+
* {
|
|
531
|
+
* name: 'credit',
|
|
532
|
+
* type: 'text',
|
|
533
|
+
* },
|
|
534
|
+
* ],
|
|
535
|
+
*/
|
|
536
|
+
fields?: Fields;
|
|
537
|
+
/**
|
|
538
|
+
* Removes the EXIF, XMP and IPTC data from each uploaded JPEG, PNG and WebP, where cameras write the location.
|
|
539
|
+
* That holds for every image in the file, like the second image of an MPO. The photo keeps its orientation.
|
|
540
|
+
*
|
|
541
|
+
* @default true
|
|
542
|
+
*/
|
|
543
|
+
stripExif?: boolean;
|
|
544
|
+
/**
|
|
545
|
+
* The file types this store takes. `'image/*'` takes every image type.
|
|
546
|
+
*
|
|
547
|
+
* @default any type except SVG, HTML, XML and scripts, which no store takes
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* contentTypes: ['image/png', 'image/jpeg', 'image/webp'],
|
|
551
|
+
*/
|
|
552
|
+
contentTypes?: readonly string[];
|
|
553
|
+
/**
|
|
554
|
+
* The largest file this store takes, in bytes.
|
|
555
|
+
*
|
|
556
|
+
* @default 20 MB
|
|
557
|
+
*/
|
|
558
|
+
maxBytes?: number;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* A store of uploaded files, as `k.storage` returns it.
|
|
562
|
+
*/
|
|
563
|
+
interface Storage<Name extends string = string, Fields extends readonly Field[] = readonly Field[]> extends StorageDefinition<Name, Fields> {
|
|
564
|
+
readonly kind: 'storage';
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Creates a storage adapter that keeps files in memory, for tests.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* adapter: memoryStorage(),
|
|
571
|
+
*/
|
|
572
|
+
declare const memoryStorage: (uploadBase?: string) => StorageAdapter & {
|
|
573
|
+
files: Map<string, {
|
|
574
|
+
bytes: Uint8Array;
|
|
575
|
+
contentType: string;
|
|
576
|
+
}>;
|
|
577
|
+
};
|
|
578
|
+
/**
|
|
579
|
+
* Creates a storage adapter that keeps files on disk, for development. Send the requests under `publicUrl` to its
|
|
580
|
+
* `handle`, which answers uploads and downloads.
|
|
581
|
+
*
|
|
582
|
+
* An upload URL is signed for one key, one content type and a largest size, until it expires. Its PUT writes a key
|
|
583
|
+
* once, so a recorded file is never replaced. Every file is served with a sandboxing Content Security Policy, and a
|
|
584
|
+
* file that isn't an image is served as a download.
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* adapter: diskStorage({
|
|
588
|
+
* directory: '.data/uploads',
|
|
589
|
+
* publicUrl: 'http://localhost:3100/uploads',
|
|
590
|
+
* secret: process.env.UPLOADS_SECRET,
|
|
591
|
+
* }),
|
|
592
|
+
*/
|
|
593
|
+
declare const diskStorage: (options: {
|
|
594
|
+
/**
|
|
595
|
+
* The folder the files are kept in.
|
|
596
|
+
*/
|
|
597
|
+
directory: string;
|
|
598
|
+
/**
|
|
599
|
+
* The URL the files are served at, with no trailing slash.
|
|
600
|
+
*/
|
|
601
|
+
publicUrl: string;
|
|
602
|
+
/**
|
|
603
|
+
* The secret that signs upload URLs.
|
|
604
|
+
*/
|
|
605
|
+
secret: string;
|
|
606
|
+
}) => StorageAdapter & {
|
|
607
|
+
/**
|
|
608
|
+
* Answers uploads and downloads under `publicUrl`.
|
|
609
|
+
*/
|
|
610
|
+
handle: (request: Request) => Promise<Response>;
|
|
611
|
+
};
|
|
612
|
+
//#endregion
|
|
613
|
+
//#region src/where.d.ts
|
|
614
|
+
/**
|
|
615
|
+
* What every field can be matched with.
|
|
616
|
+
*/
|
|
617
|
+
interface ValueOperators<Value> {
|
|
618
|
+
equals?: Value;
|
|
619
|
+
not?: Value;
|
|
620
|
+
/**
|
|
621
|
+
* The value is one of these.
|
|
622
|
+
*/
|
|
623
|
+
in?: readonly Value[];
|
|
624
|
+
notIn?: readonly Value[];
|
|
625
|
+
/**
|
|
626
|
+
* `true` for documents that hold a value, `false` for those that don't.
|
|
627
|
+
*/
|
|
628
|
+
exists?: boolean;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* What numbers, dates and moments can also be matched with.
|
|
632
|
+
*/
|
|
633
|
+
interface RangeOperators<Value> {
|
|
634
|
+
greaterThan?: Value;
|
|
635
|
+
atLeast?: Value;
|
|
636
|
+
lessThan?: Value;
|
|
637
|
+
atMost?: Value;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* What the list of values in a `many` field can be matched with.
|
|
641
|
+
*/
|
|
642
|
+
interface ListOperators<Value> {
|
|
643
|
+
/**
|
|
644
|
+
* The list holds this value.
|
|
645
|
+
*/
|
|
646
|
+
contains?: Value;
|
|
647
|
+
/**
|
|
648
|
+
* The list holds every one of these.
|
|
649
|
+
*/
|
|
650
|
+
hasAll?: readonly Value[];
|
|
651
|
+
/**
|
|
652
|
+
* The list holds at least one of these.
|
|
653
|
+
*/
|
|
654
|
+
in?: readonly Value[];
|
|
655
|
+
exists?: boolean;
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* The operators a field offers, from its type.
|
|
659
|
+
*/
|
|
660
|
+
type OperatorsFor<F> = F extends {
|
|
661
|
+
many: true;
|
|
662
|
+
} ? ListOperators<FieldValue<F> extends readonly (infer Element)[] ? Element : never> : F extends {
|
|
663
|
+
type: 'number' | 'date' | 'dateTime';
|
|
664
|
+
} ? ValueOperators<FieldValue<F>> & RangeOperators<FieldValue<F>> : F extends {
|
|
665
|
+
type: 'text' | 'slug' | 'url' | 'email';
|
|
666
|
+
} ? ValueOperators<string> & {
|
|
667
|
+
/**
|
|
668
|
+
* The text holds this, ignoring case.
|
|
669
|
+
*/
|
|
670
|
+
contains?: string;
|
|
671
|
+
} : F extends {
|
|
672
|
+
type: 'boolean';
|
|
673
|
+
} ? Pick<ValueOperators<boolean>, 'equals' | 'not' | 'exists'> : ValueOperators<FieldValue<F>>;
|
|
674
|
+
type FieldsOf<C> = C extends Collection<string, infer Fields> ? Fields : readonly Field[];
|
|
675
|
+
/**
|
|
676
|
+
* Which documents to read, where every field named must match. `and` and `or` take lists of the same shape.
|
|
677
|
+
*
|
|
678
|
+
* @example
|
|
679
|
+
* where: {
|
|
680
|
+
* slug: {
|
|
681
|
+
* equals: 'front-page',
|
|
682
|
+
* },
|
|
683
|
+
* },
|
|
684
|
+
*/
|
|
685
|
+
type WhereOf<C> = { [F in FieldsOf<C>[number] as F['name']]?: OperatorsFor<F> } & {
|
|
686
|
+
and?: readonly WhereOf<C>[];
|
|
687
|
+
or?: readonly WhereOf<C>[];
|
|
688
|
+
};
|
|
689
|
+
/**
|
|
690
|
+
* The order to read documents in, by field name with `-` in front to reverse it. Several apply in turn.
|
|
691
|
+
*
|
|
692
|
+
* @example
|
|
693
|
+
* sort: '-publishedOn',
|
|
694
|
+
*/
|
|
695
|
+
type SortOf<C> = `${'' | '-'}${FieldsOf<C>[number]['name'] | 'createdAt' | 'updatedAt'}` | readonly `${'' | '-'}${FieldsOf<C>[number]['name'] | 'createdAt' | 'updatedAt'}`[];
|
|
696
|
+
/**
|
|
697
|
+
* A `where` with no types from a collection, as a database adapter takes it.
|
|
698
|
+
*/
|
|
699
|
+
type Where = {
|
|
700
|
+
and?: readonly Where[];
|
|
701
|
+
or?: readonly Where[];
|
|
702
|
+
} & {
|
|
703
|
+
[field: string]: Record<string, unknown> | readonly Where[] | undefined;
|
|
704
|
+
};
|
|
705
|
+
//#endregion
|
|
706
|
+
//#region src/db.d.ts
|
|
707
|
+
/**
|
|
708
|
+
* A stored document: its `id`, `createdAt` and `updatedAt`, and its field values.
|
|
709
|
+
*/
|
|
710
|
+
type StoredDocument = DocumentMeta & Record<string, unknown>;
|
|
711
|
+
/**
|
|
712
|
+
* One page of a collection's documents.
|
|
713
|
+
*/
|
|
714
|
+
interface DocumentPage {
|
|
715
|
+
items: StoredDocument[];
|
|
716
|
+
/**
|
|
717
|
+
* How many documents match, across every page.
|
|
718
|
+
*/
|
|
719
|
+
total: number;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Which documents a list holds and in what order.
|
|
723
|
+
*/
|
|
724
|
+
interface ListOptions {
|
|
725
|
+
/**
|
|
726
|
+
* Only the documents with these ids. Unknown ids are skipped.
|
|
727
|
+
*/
|
|
728
|
+
ids?: readonly string[];
|
|
729
|
+
/**
|
|
730
|
+
* The conditions every document must match.
|
|
731
|
+
*/
|
|
732
|
+
where?: Where;
|
|
733
|
+
/**
|
|
734
|
+
* Field names, with `-` in front of one to reverse it.
|
|
735
|
+
*
|
|
736
|
+
* @default the order documents were created in
|
|
737
|
+
*/
|
|
738
|
+
sort?: string | readonly string[];
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* The collection that holds globals, one document each with the global's name as its id.
|
|
742
|
+
*/
|
|
743
|
+
declare const GLOBALS_COLLECTION = "globals";
|
|
744
|
+
/**
|
|
745
|
+
* The collections, globals and stores a database keeps.
|
|
746
|
+
*/
|
|
747
|
+
interface Definitions {
|
|
748
|
+
collections: readonly Collection[];
|
|
749
|
+
globals: readonly Global[];
|
|
750
|
+
storage: readonly Storage[];
|
|
751
|
+
/**
|
|
752
|
+
* The collection editors log in to the admin from.
|
|
753
|
+
*/
|
|
754
|
+
editors?: string;
|
|
755
|
+
/**
|
|
756
|
+
* Every collection whose documents log in, each with a password hash beside its fields.
|
|
757
|
+
*/
|
|
758
|
+
logins?: readonly string[];
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Where the CMS keeps documents, accounts and sessions, passed as the `db` option of `defineConfig`.
|
|
762
|
+
*/
|
|
763
|
+
interface DatabaseAdapter extends AccountStore {
|
|
764
|
+
/**
|
|
765
|
+
* Receives the collections, globals and stores to keep, before the first query.
|
|
766
|
+
*/
|
|
767
|
+
prepare?: (definitions: Definitions) => void;
|
|
768
|
+
/**
|
|
769
|
+
* Returns one page of a collection's documents.
|
|
770
|
+
*/
|
|
771
|
+
list: (collection: string, page: {
|
|
772
|
+
offset: number;
|
|
773
|
+
limit: number;
|
|
774
|
+
}, options?: ListOptions) => Promise<DocumentPage>;
|
|
775
|
+
/**
|
|
776
|
+
* Returns one document, or `undefined` when no document has the id.
|
|
777
|
+
*/
|
|
778
|
+
get: (collection: string, id: string) => Promise<StoredDocument | undefined>;
|
|
779
|
+
/**
|
|
780
|
+
* Creates a document and returns it. `by` is the id of the account that created it.
|
|
781
|
+
*/
|
|
782
|
+
create: (collection: string, values: Record<string, unknown>, by?: string) => Promise<StoredDocument>;
|
|
783
|
+
/**
|
|
784
|
+
* Updates the fields given. `null` clears a field. Returns `undefined` when no document has the id.
|
|
785
|
+
*/
|
|
786
|
+
update: (collection: string, id: string, values: Record<string, unknown>, by?: string) => Promise<StoredDocument | undefined>;
|
|
787
|
+
/**
|
|
788
|
+
* Deletes a document and returns whether one had the id.
|
|
789
|
+
*/
|
|
790
|
+
delete: (collection: string, id: string) => Promise<boolean>;
|
|
791
|
+
/**
|
|
792
|
+
* A global's document, or `undefined` when it was never saved.
|
|
793
|
+
*/
|
|
794
|
+
getGlobal: (name: string) => Promise<StoredDocument | undefined>;
|
|
795
|
+
/**
|
|
796
|
+
* Updates the fields given and saves the global if it never was. `null` clears a field.
|
|
797
|
+
*/
|
|
798
|
+
updateGlobal: (name: string, values: Record<string, unknown>, by?: string) => Promise<StoredDocument>;
|
|
799
|
+
/**
|
|
800
|
+
* Returns the password hash kept beside an account, which no read returns.
|
|
801
|
+
*/
|
|
802
|
+
passwordHashOf: (collection: string, id: string) => Promise<string | undefined>;
|
|
803
|
+
/**
|
|
804
|
+
* Keeps a password hash beside an account.
|
|
805
|
+
*/
|
|
806
|
+
setPasswordHash: (collection: string, id: string, hash: string) => Promise<void>;
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Creates a database adapter that keeps everything in memory, for tests. Nothing survives a restart.
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* export default defineConfig({
|
|
813
|
+
* name: 'Example',
|
|
814
|
+
* db: memoryAdapter(),
|
|
815
|
+
* collections: [Articles, Users],
|
|
816
|
+
* users: Users,
|
|
817
|
+
* roles: ['editor'],
|
|
818
|
+
* });
|
|
819
|
+
*/
|
|
820
|
+
declare const memoryAdapter: () => DatabaseAdapter;
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/meta.d.ts
|
|
823
|
+
/**
|
|
824
|
+
* The fields a document's `meta` values are filled in from. A value an editor writes wins over the field's.
|
|
825
|
+
*
|
|
826
|
+
* @example
|
|
827
|
+
* meta: {
|
|
828
|
+
* description: {
|
|
829
|
+
* generateFrom: 'summary',
|
|
830
|
+
* },
|
|
831
|
+
* image: {
|
|
832
|
+
* generateFrom: 'cover',
|
|
833
|
+
* },
|
|
834
|
+
* },
|
|
835
|
+
*/
|
|
836
|
+
interface MetaOptions {
|
|
837
|
+
/**
|
|
838
|
+
* Where the search title comes from.
|
|
839
|
+
*/
|
|
840
|
+
title?: {
|
|
841
|
+
/**
|
|
842
|
+
* The text field the search title is filled in from.
|
|
843
|
+
*
|
|
844
|
+
* @default the `titleField` field
|
|
845
|
+
*/
|
|
846
|
+
generateFrom?: string;
|
|
847
|
+
};
|
|
848
|
+
/**
|
|
849
|
+
* Where the search description comes from.
|
|
850
|
+
*/
|
|
851
|
+
description?: {
|
|
852
|
+
/**
|
|
853
|
+
* The text field the search description is filled in from, like `'summary'`.
|
|
854
|
+
*/
|
|
855
|
+
generateFrom?: string;
|
|
856
|
+
};
|
|
857
|
+
/**
|
|
858
|
+
* Where the share image comes from. Without `generateFrom` or `storage`, documents have no share image.
|
|
859
|
+
*/
|
|
860
|
+
image?: {
|
|
861
|
+
/**
|
|
862
|
+
* The image field the share image is filled in from, like `'cover'`.
|
|
863
|
+
*/
|
|
864
|
+
generateFrom?: string;
|
|
865
|
+
/**
|
|
866
|
+
* The store editors pick their own share image from.
|
|
867
|
+
*
|
|
868
|
+
* @default the image field's store
|
|
869
|
+
*/
|
|
870
|
+
storage?: string;
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* The text every search title ends with, after a separator.
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* // "Opening hours" reads as "Opening hours - Example"
|
|
878
|
+
* meta: {
|
|
879
|
+
* suffix: 'Example',
|
|
880
|
+
* },
|
|
881
|
+
*/
|
|
882
|
+
interface MetaTitleOptions {
|
|
883
|
+
/**
|
|
884
|
+
* The text every search title ends with.
|
|
885
|
+
*/
|
|
886
|
+
suffix: string;
|
|
887
|
+
/**
|
|
888
|
+
* The text between the title and the suffix.
|
|
889
|
+
*
|
|
890
|
+
* @default '-'
|
|
891
|
+
*/
|
|
892
|
+
separator?: string;
|
|
893
|
+
}
|
|
894
|
+
//#endregion
|
|
895
|
+
//#region src/collection.d.ts
|
|
896
|
+
/**
|
|
897
|
+
* The options `k.collection` takes.
|
|
898
|
+
*/
|
|
899
|
+
interface CollectionDefinition<Name extends string, Fields extends readonly Field[]> {
|
|
900
|
+
/**
|
|
901
|
+
* The collection's name in its routes and clients, in camelCase like `articles` or `blogPosts`.
|
|
902
|
+
*/
|
|
903
|
+
name: Name;
|
|
904
|
+
/**
|
|
905
|
+
* What the admin and the API docs call one document and the whole collection.
|
|
906
|
+
*
|
|
907
|
+
* @default the name in words, so `blogPosts` gives "Blog post" and "Blog posts"
|
|
908
|
+
*
|
|
909
|
+
* @example
|
|
910
|
+
* labels: {
|
|
911
|
+
* singular: 'News article',
|
|
912
|
+
* plural: 'News',
|
|
913
|
+
* },
|
|
914
|
+
*/
|
|
915
|
+
labels?: Partial<Labels>;
|
|
916
|
+
/**
|
|
917
|
+
* The roles that may read, create, update and delete this collection's documents, one list per action.
|
|
918
|
+
* `['public']` lets anyone do it without logging in.
|
|
919
|
+
*
|
|
920
|
+
* @example
|
|
921
|
+
* auth: {
|
|
922
|
+
* read: ['public'],
|
|
923
|
+
* create: ['editor'],
|
|
924
|
+
* update: ['editor', 'author'],
|
|
925
|
+
* delete: ['admin'],
|
|
926
|
+
* },
|
|
927
|
+
*/
|
|
928
|
+
auth: CollectionAuth;
|
|
929
|
+
/**
|
|
930
|
+
* The actions on this collection an AI agent may call as MCP tools.
|
|
931
|
+
*
|
|
932
|
+
* @example
|
|
933
|
+
* tools: {
|
|
934
|
+
* read: true,
|
|
935
|
+
* update: true,
|
|
936
|
+
* },
|
|
937
|
+
*/
|
|
938
|
+
tools?: CollectionTools;
|
|
939
|
+
/**
|
|
940
|
+
* The fields every document in this collection holds.
|
|
941
|
+
*
|
|
942
|
+
* @example
|
|
943
|
+
* fields: [
|
|
944
|
+
* {
|
|
945
|
+
* name: 'title',
|
|
946
|
+
* type: 'text',
|
|
947
|
+
* required: true,
|
|
948
|
+
* },
|
|
949
|
+
* {
|
|
950
|
+
* name: 'author',
|
|
951
|
+
* type: 'relationship',
|
|
952
|
+
* to: 'authors',
|
|
953
|
+
* },
|
|
954
|
+
* ],
|
|
955
|
+
*/
|
|
956
|
+
fields: Fields;
|
|
957
|
+
/**
|
|
958
|
+
* The type name of one document in the API docs and the generated clients, like `Article`.
|
|
959
|
+
*
|
|
960
|
+
* @default the singular of `name` with a capital letter, so `articles` gives `Article`
|
|
961
|
+
*/
|
|
962
|
+
model?: string;
|
|
963
|
+
/**
|
|
964
|
+
* The group from the config's `groups` that holds this collection in the admin menu, the API docs and the clients.
|
|
965
|
+
*
|
|
966
|
+
* @default under Collections, above every group
|
|
967
|
+
*/
|
|
968
|
+
group?: string;
|
|
969
|
+
/**
|
|
970
|
+
* The text field that names each document in the admin. Slugs and the search title are made from it.
|
|
971
|
+
*
|
|
972
|
+
* @default the document's id
|
|
973
|
+
*/
|
|
974
|
+
titleField?: string;
|
|
975
|
+
/**
|
|
976
|
+
* Gives each document a `meta` value with its search title, description and indexing settings. `true` lets editors
|
|
977
|
+
* write them, and an object also fills them in from the document's fields.
|
|
978
|
+
*
|
|
979
|
+
* @example
|
|
980
|
+
* meta: {
|
|
981
|
+
* description: {
|
|
982
|
+
* generateFrom: 'summary',
|
|
983
|
+
* },
|
|
984
|
+
* image: {
|
|
985
|
+
* generateFrom: 'cover',
|
|
986
|
+
* },
|
|
987
|
+
* },
|
|
988
|
+
*/
|
|
989
|
+
meta?: true | MetaOptions;
|
|
990
|
+
/**
|
|
991
|
+
* Makes this collection an account type: its documents log in with an email and a password. They log in as the
|
|
992
|
+
* identity named by the singular of `name`, like `customer`. `{ passwordReset }` also lets accounts reset their
|
|
993
|
+
* password and lets admins invite them.
|
|
994
|
+
*
|
|
995
|
+
* @default false
|
|
996
|
+
*
|
|
997
|
+
* @example
|
|
998
|
+
* export const Customers = k.collection({
|
|
999
|
+
* name: 'customers',
|
|
1000
|
+
* login: true,
|
|
1001
|
+
* auth: {
|
|
1002
|
+
* read: ['admin'],
|
|
1003
|
+
* create: ['public'],
|
|
1004
|
+
* update: ['admin'],
|
|
1005
|
+
* delete: ['admin'],
|
|
1006
|
+
* },
|
|
1007
|
+
* fields: [
|
|
1008
|
+
* {
|
|
1009
|
+
* name: 'name',
|
|
1010
|
+
* type: 'text',
|
|
1011
|
+
* },
|
|
1012
|
+
* ],
|
|
1013
|
+
* });
|
|
1014
|
+
*/
|
|
1015
|
+
login?: true | LoginOptions;
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* What a login collection's accounts can do beside logging in.
|
|
1019
|
+
*
|
|
1020
|
+
* @example
|
|
1021
|
+
* login: {
|
|
1022
|
+
* passwordReset: {
|
|
1023
|
+
* send: async ({ email, token }) => {
|
|
1024
|
+
* await sendEmail({
|
|
1025
|
+
* to: email,
|
|
1026
|
+
* subject: 'Set your password',
|
|
1027
|
+
* text: `https://cms.example.com/admin/reset-password?token=${token}`,
|
|
1028
|
+
* });
|
|
1029
|
+
* },
|
|
1030
|
+
* },
|
|
1031
|
+
* },
|
|
1032
|
+
*/
|
|
1033
|
+
interface LoginOptions {
|
|
1034
|
+
/**
|
|
1035
|
+
* Sends the link an account sets its password with, when it asks for a reset or an admin invites it.
|
|
1036
|
+
*/
|
|
1037
|
+
passwordReset: PasswordReset;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* A collection of documents that share the same fields, as `k.collection` returns it.
|
|
1041
|
+
*/
|
|
1042
|
+
interface Collection<Name extends string = string, Fields extends readonly Field[] = readonly Field[]> extends CollectionDefinition<Name, Fields> {
|
|
1043
|
+
readonly kind: 'collection';
|
|
1044
|
+
/**
|
|
1045
|
+
* The schema of one stored document, for your own routes' `responses`.
|
|
1046
|
+
*
|
|
1047
|
+
* @example
|
|
1048
|
+
* responses: {
|
|
1049
|
+
* 200: z.array(Articles.schema),
|
|
1050
|
+
* },
|
|
1051
|
+
*/
|
|
1052
|
+
readonly schema: z.ZodType;
|
|
1053
|
+
}
|
|
1054
|
+
type FieldSchema<F> = F extends {
|
|
1055
|
+
required: true;
|
|
1056
|
+
} ? z.ZodType<FieldValue<F>> : z.ZodOptional<z.ZodType<FieldValue<F>>>;
|
|
1057
|
+
/**
|
|
1058
|
+
* The schema shape of one stored document: an entry per field, plus `id`, `createdAt` and `updatedAt`.
|
|
1059
|
+
*/
|
|
1060
|
+
type DocumentShapeOf<Fields extends readonly Field[]> = {
|
|
1061
|
+
id: z.ZodString;
|
|
1062
|
+
createdAt: z.ZodType<string>;
|
|
1063
|
+
updatedAt: z.ZodType<string>;
|
|
1064
|
+
} & { [F in Fields[number] as F['name']]: FieldSchema<F> };
|
|
1065
|
+
/**
|
|
1066
|
+
* A collection as `k.collection` returns it, with a `schema` typed field by field for `.pick` and `.omit`.
|
|
1067
|
+
*/
|
|
1068
|
+
type DeclaredCollection<Name extends string, Fields extends readonly Field[]> = Collection<Name, Fields> & {
|
|
1069
|
+
readonly schema: z.ZodObject<DocumentShapeOf<Fields>>;
|
|
1070
|
+
};
|
|
1071
|
+
type RequiredFields<Fields extends readonly Field[]> = Extract<Fields[number], {
|
|
1072
|
+
required: true;
|
|
1073
|
+
}>;
|
|
1074
|
+
type OptionalFields<Fields extends readonly Field[]> = Exclude<Fields[number], {
|
|
1075
|
+
required: true;
|
|
1076
|
+
}>;
|
|
1077
|
+
/**
|
|
1078
|
+
* The values every stored document has besides its fields.
|
|
1079
|
+
*/
|
|
1080
|
+
interface DocumentMeta {
|
|
1081
|
+
id: string;
|
|
1082
|
+
/**
|
|
1083
|
+
* When the document was created, as an ISO date-time in UTC.
|
|
1084
|
+
*/
|
|
1085
|
+
createdAt: string;
|
|
1086
|
+
/**
|
|
1087
|
+
* When the document last changed, as an ISO date-time in UTC.
|
|
1088
|
+
*/
|
|
1089
|
+
updatedAt: string;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* The field values of one document, typed from the fields.
|
|
1093
|
+
*/
|
|
1094
|
+
type ContentOf<Fields extends readonly Field[]> = { [F in RequiredFields<Fields> as F['name']]: FieldValue<F> } & { [F in OptionalFields<Fields> as F['name']]?: FieldValue<F> };
|
|
1095
|
+
/**
|
|
1096
|
+
* One stored document of a collection, typed from its fields.
|
|
1097
|
+
*
|
|
1098
|
+
* @example
|
|
1099
|
+
* type Article = DocumentOf<typeof Articles>;
|
|
1100
|
+
*/
|
|
1101
|
+
type DocumentOf<C> = C extends {
|
|
1102
|
+
readonly kind: 'collection';
|
|
1103
|
+
fields: infer Fields extends readonly Field[];
|
|
1104
|
+
} ? DocumentMeta & ContentOf<Fields> : never;
|
|
1105
|
+
/**
|
|
1106
|
+
* What the admin and the API docs call one document and the whole collection.
|
|
1107
|
+
*/
|
|
1108
|
+
interface Labels {
|
|
1109
|
+
singular: string;
|
|
1110
|
+
plural: string;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Returns what a collection calls one document and the whole collection, from its `labels` or its name.
|
|
1114
|
+
*/
|
|
1115
|
+
declare const labelsOf: (collection: Pick<CollectionDefinition<string, readonly Field[]>, "name" | "labels">) => Labels;
|
|
1116
|
+
/**
|
|
1117
|
+
* Returns the schema of one stored document of a collection.
|
|
1118
|
+
*/
|
|
1119
|
+
declare const documentSchemaOf: (collection: Pick<Collection, "name" | "model" | "fields" | "meta">) => z.ZodObject;
|
|
1120
|
+
//#endregion
|
|
1121
|
+
//#region src/access.d.ts
|
|
1122
|
+
/**
|
|
1123
|
+
* A role an editor holds on each document whose `field` holds their id. Only admins and the roles an operation lists
|
|
1124
|
+
* may change `field` itself.
|
|
1125
|
+
*
|
|
1126
|
+
* @example
|
|
1127
|
+
* author: {
|
|
1128
|
+
* field: 'author',
|
|
1129
|
+
* },
|
|
1130
|
+
*/
|
|
1131
|
+
interface DocumentRole {
|
|
1132
|
+
/**
|
|
1133
|
+
* The field that holds the editor's id. A field with `many: true` gives the role to every editor in its list.
|
|
1134
|
+
*/
|
|
1135
|
+
field: string;
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* The role every CMS has built in. Admins pass every collection's, global's and store's `auth`, and may set other
|
|
1139
|
+
* users' roles.
|
|
1140
|
+
*/
|
|
1141
|
+
declare const ADMIN_ROLE = "admin";
|
|
1142
|
+
/**
|
|
1143
|
+
* The roles users can hold beside `admin`: a list of names, or `Kizuna.roles(permissions, { ... })`.
|
|
1144
|
+
*/
|
|
1145
|
+
type RolesOption<RoleName extends string = string> = readonly RoleName[] | Roles<PermissionCatalog | undefined, RoleName>;
|
|
1146
|
+
/**
|
|
1147
|
+
* Who may do one action: editors holding one of the listed roles and every admin, or whoever a rule lets in.
|
|
1148
|
+
* `['public']` lets anyone do it without logging in.
|
|
1149
|
+
*
|
|
1150
|
+
* @example
|
|
1151
|
+
* update: ['editor', 'author'],
|
|
1152
|
+
* create: {
|
|
1153
|
+
* identity: 'editor',
|
|
1154
|
+
* requires: {
|
|
1155
|
+
* news: ['write'],
|
|
1156
|
+
* },
|
|
1157
|
+
* },
|
|
1158
|
+
*/
|
|
1159
|
+
type AuthValue<RoleName extends string = string, Rule = AuthRule> = readonly ['public'] | readonly RoleName[] | Rule;
|
|
1160
|
+
/**
|
|
1161
|
+
* A rule naming the identities that pass and the roles or permissions they must hold, as a route's `auth` takes it.
|
|
1162
|
+
*/
|
|
1163
|
+
interface AuthRule {
|
|
1164
|
+
identity: string | readonly string[];
|
|
1165
|
+
roles?: string | readonly string[];
|
|
1166
|
+
requires?: object;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Who may read, create, update and delete, one entry per action. `['public']` lets anyone do it without logging in.
|
|
1170
|
+
*
|
|
1171
|
+
* @example
|
|
1172
|
+
* auth: {
|
|
1173
|
+
* read: ['public'],
|
|
1174
|
+
* create: ['editor'],
|
|
1175
|
+
* update: ['editor', 'author'],
|
|
1176
|
+
* delete: ['admin'],
|
|
1177
|
+
* },
|
|
1178
|
+
*/
|
|
1179
|
+
interface CollectionAuth<RoleName extends string = string, Rule = AuthRule> {
|
|
1180
|
+
/**
|
|
1181
|
+
* Who may read. `['public']` lets anyone read without logging in.
|
|
1182
|
+
*/
|
|
1183
|
+
read: AuthValue<RoleName, Rule>;
|
|
1184
|
+
/**
|
|
1185
|
+
* Who may create. On a store, it is who may upload.
|
|
1186
|
+
*/
|
|
1187
|
+
create: AuthValue<RoleName, Rule>;
|
|
1188
|
+
/**
|
|
1189
|
+
* Who may update. A document role here lets its holder update only the documents that name them.
|
|
1190
|
+
*/
|
|
1191
|
+
update: AuthValue<RoleName, Rule>;
|
|
1192
|
+
/**
|
|
1193
|
+
* Who may delete. A document role here lets its holder delete only the documents that name them.
|
|
1194
|
+
*/
|
|
1195
|
+
delete: AuthValue<RoleName, Rule>;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Whether an AI agent may call one action as an MCP tool. `true` publishes it, and an object publishes it with the
|
|
1199
|
+
* fields a route's `tool` takes.
|
|
1200
|
+
*
|
|
1201
|
+
* @example
|
|
1202
|
+
* create: {
|
|
1203
|
+
* description: 'Drafts an article in the voice of the blog.',
|
|
1204
|
+
* needsApproval: true,
|
|
1205
|
+
* },
|
|
1206
|
+
*/
|
|
1207
|
+
type ToolOption = boolean | RouteToolOptions;
|
|
1208
|
+
/**
|
|
1209
|
+
* The actions on a collection an AI agent may call as MCP tools. `true` publishes all four, and a delete asks the
|
|
1210
|
+
* person to approve it unless it sets `needsApproval: false`.
|
|
1211
|
+
*
|
|
1212
|
+
* @example
|
|
1213
|
+
* tools: {
|
|
1214
|
+
* read: true,
|
|
1215
|
+
* create: {
|
|
1216
|
+
* description: 'Drafts an article in the voice of the blog.',
|
|
1217
|
+
* needsApproval: true,
|
|
1218
|
+
* },
|
|
1219
|
+
* update: true,
|
|
1220
|
+
* },
|
|
1221
|
+
*/
|
|
1222
|
+
type CollectionTools = true | {
|
|
1223
|
+
read?: ToolOption;
|
|
1224
|
+
create?: ToolOption;
|
|
1225
|
+
update?: ToolOption;
|
|
1226
|
+
delete?: ToolOption;
|
|
1227
|
+
};
|
|
1228
|
+
/**
|
|
1229
|
+
* The actions on a global an AI agent may call as MCP tools. `true` publishes both.
|
|
1230
|
+
*
|
|
1231
|
+
* @example
|
|
1232
|
+
* tools: {
|
|
1233
|
+
* read: true,
|
|
1234
|
+
* },
|
|
1235
|
+
*/
|
|
1236
|
+
type GlobalTools = true | {
|
|
1237
|
+
read?: ToolOption;
|
|
1238
|
+
update?: ToolOption;
|
|
1239
|
+
};
|
|
1240
|
+
/**
|
|
1241
|
+
* Who may read and update a global, one entry per action. `['public']` lets anyone do it without logging in.
|
|
1242
|
+
*
|
|
1243
|
+
* @example
|
|
1244
|
+
* auth: {
|
|
1245
|
+
* read: ['public'],
|
|
1246
|
+
* update: ['admin'],
|
|
1247
|
+
* },
|
|
1248
|
+
*/
|
|
1249
|
+
type GlobalAuth<RoleName extends string = string, Rule = AuthRule> = Pick<CollectionAuth<RoleName, Rule>, 'read' | 'update'>;
|
|
1250
|
+
//#endregion
|
|
1251
|
+
export { TextField as $, StorageAdapter as A, DateTimeField as B, StoredDocument as C, Where as D, SortOf as E, GlobalDefinition as F, FieldColumn as G, Field as H, GlobalOf as I, RelationshipField as J, FieldValue as K, BooleanField as L, diskStorage as M, memoryStorage as N, WhereOf as O, Global as P, SlugSchema as Q, BuiltInField as R, ListOptions as S, OperatorsFor as T, FieldAffixes as U, EmailField as V, FieldBase as W, SelectOption as X, SelectField as Y, SlugField as Z, MetaOptions as _, GlobalAuth as a, SessionRecord as at, DocumentPage as b, CollectionDefinition as c, DocumentMeta as d, UrlField as et, DocumentOf as f, labelsOf as g, documentSchemaOf as h, DocumentRole as i, PasswordReset as it, StorageDefinition as j, Storage as k, ContentOf as l, LoginOptions as m, AuthRule as n, columnOf as nt, RolesOption as o, hashPassword as ot, Labels as p, NumberField as q, CollectionAuth as r, schemaOf as rt, Collection as s, verifyPassword as st, ADMIN_ROLE as t, WithStoreFields as tt, DeclaredCollection as u, MetaTitleOptions as v, memoryAdapter as w, GLOBALS_COLLECTION as x, DatabaseAdapter as y, DateField as z };
|