@dyrected/core 2.5.14 → 2.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app-B2tg7Djj.d.cts +1575 -0
- package/dist/app-B2tg7Djj.d.ts +1575 -0
- package/dist/app-BElen1tP.d.cts +1690 -0
- package/dist/app-BElen1tP.d.ts +1690 -0
- package/dist/app-Bh4_Opv0.d.cts +1522 -0
- package/dist/app-Bh4_Opv0.d.ts +1522 -0
- package/dist/app-Bv9gaDAN.d.cts +561 -0
- package/dist/app-Bv9gaDAN.d.ts +561 -0
- package/dist/app-BvG3bRc8.d.cts +419 -0
- package/dist/app-BvG3bRc8.d.ts +419 -0
- package/dist/app-C3B9N1KR.d.cts +1522 -0
- package/dist/app-C3B9N1KR.d.ts +1522 -0
- package/dist/app-DDJJa0ep.d.cts +1621 -0
- package/dist/app-DDJJa0ep.d.ts +1621 -0
- package/dist/app-DO1s9YW1.d.cts +1621 -0
- package/dist/app-DO1s9YW1.d.ts +1621 -0
- package/dist/app-DTP3-9PJ.d.cts +561 -0
- package/dist/app-DTP3-9PJ.d.ts +561 -0
- package/dist/app-DbKDGYTI.d.cts +566 -0
- package/dist/app-DbKDGYTI.d.ts +566 -0
- package/dist/app-DqRO-CMi.d.cts +1457 -0
- package/dist/app-DqRO-CMi.d.ts +1457 -0
- package/dist/app-DvaFpOtj.d.cts +398 -0
- package/dist/app-DvaFpOtj.d.ts +398 -0
- package/dist/app-FGzip4XM.d.cts +1563 -0
- package/dist/app-FGzip4XM.d.ts +1563 -0
- package/dist/app-T0alZAE0.d.cts +383 -0
- package/dist/app-T0alZAE0.d.ts +383 -0
- package/dist/app-oQt5-9MU.d.cts +1560 -0
- package/dist/app-oQt5-9MU.d.ts +1560 -0
- package/dist/app-rZj1VFer.d.cts +1621 -0
- package/dist/app-rZj1VFer.d.ts +1621 -0
- package/dist/app-wo82JRHl.d.cts +445 -0
- package/dist/app-wo82JRHl.d.ts +445 -0
- package/dist/chunk-23URSKPI.js +2371 -0
- package/dist/chunk-2JMA3M5S.js +2475 -0
- package/dist/chunk-3FZEUK36.js +2470 -0
- package/dist/chunk-DOJHZ7XN.js +2394 -0
- package/dist/chunk-EXXOPW3I.js +2483 -0
- package/dist/chunk-PKNFV7KE.js +2469 -0
- package/dist/chunk-UBTRANFX.js +2476 -0
- package/dist/chunk-W6KURRMW.js +2471 -0
- package/dist/chunk-YNJ3YC4N.js +2483 -0
- package/dist/index.cjs +465 -48
- package/dist/index.d.cts +124 -8
- package/dist/index.d.ts +124 -8
- package/dist/index.js +9 -3
- package/dist/server.cjs +457 -46
- package/dist/server.d.cts +57 -15
- package/dist/server.d.ts +57 -15
- package/dist/server.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,1621 @@
|
|
|
1
|
+
import * as hono_types from 'hono/types';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Minimum HTTP request context passed to every server-side hook and resolver.
|
|
6
|
+
*
|
|
7
|
+
* The full Web Standard `Request` is available as `raw` when you need it, but
|
|
8
|
+
* most hooks only need `query` (URL search parameters).
|
|
9
|
+
*/
|
|
10
|
+
interface HookRequestContext {
|
|
11
|
+
/** Parsed URL query-string parameters, e.g. `{ page: '2', search: 'hello' }`. */
|
|
12
|
+
query: Record<string, string>;
|
|
13
|
+
/** Incoming HTTP headers, lowercased. */
|
|
14
|
+
headers: Record<string, string>;
|
|
15
|
+
/** The raw Web Standard `Request` object. Useful for streaming or advanced header inspection. */
|
|
16
|
+
raw?: Request;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Base shape of an authenticated user as decoded from the JWT.
|
|
20
|
+
*
|
|
21
|
+
* The actual shape will include every field on your auth collection — this
|
|
22
|
+
* interface only guarantees the properties that Dyrected always stamps on the
|
|
23
|
+
* token. Extend it in your own codebase for stronger typing:
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* declare module '@dyrected/core' {
|
|
27
|
+
* interface AuthenticatedUser {
|
|
28
|
+
* role: 'admin' | 'editor'
|
|
29
|
+
* organizationId: string
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
33
|
+
interface AuthenticatedUser {
|
|
34
|
+
/** The user's document ID in the database. */
|
|
35
|
+
sub: string;
|
|
36
|
+
/** The user's email address. */
|
|
37
|
+
email?: string;
|
|
38
|
+
/** Slug of the collection this user was authenticated against. */
|
|
39
|
+
collection: string;
|
|
40
|
+
/** Array of role strings, if your auth collection has a `roles` field. */
|
|
41
|
+
roles?: string[];
|
|
42
|
+
/** Any additional fields from the auth collection document. */
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Every field type supported by Dyrected.
|
|
47
|
+
*
|
|
48
|
+
* - Text group: `text`, `textarea`, `richText`, `email`, `url`, `icon`
|
|
49
|
+
* - Number/Bool: `number`, `boolean`
|
|
50
|
+
* - Date: `date`
|
|
51
|
+
* - Selection: `select`, `multiSelect`, `radio`
|
|
52
|
+
* - Relationship: `relationship`, `join`
|
|
53
|
+
* - Structural: `array`, `object`, `blocks`, `json`
|
|
54
|
+
* - Layout: `row`
|
|
55
|
+
* - Media: `image`
|
|
56
|
+
*/
|
|
57
|
+
type FieldType = "text" | "textarea" | "richText" | "number" | "boolean" | "date" | "select" | "multiSelect" | "radio" | "relationship" | "array" | "object" | "json" | "blocks" | "image" | "email" | "url" | "icon" | "join" | "row";
|
|
58
|
+
/**
|
|
59
|
+
* Arguments passed to a server-side dynamic options resolver.
|
|
60
|
+
*
|
|
61
|
+
* All properties are optional so simple resolvers that fetch from an external
|
|
62
|
+
* API without needing any context (`async () => fetch(...)`) still compile
|
|
63
|
+
* without errors.
|
|
64
|
+
*/
|
|
65
|
+
interface DynamicOptionsResolverArgs {
|
|
66
|
+
/**
|
|
67
|
+
* The configured database adapter. Use it to query any collection.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* options: async ({ db }) => {
|
|
71
|
+
* const result = await db.find({ collection: 'categories' })
|
|
72
|
+
* return result.docs.map(c => ({ label: c.name, value: c.id }))
|
|
73
|
+
* }
|
|
74
|
+
*/
|
|
75
|
+
db?: DatabaseAdapter;
|
|
76
|
+
/**
|
|
77
|
+
* The authenticated user making the request, or `undefined` for
|
|
78
|
+
* unauthenticated requests. Use it to filter options per user.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* options: async ({ user, db }) => {
|
|
82
|
+
* if (!user) return []
|
|
83
|
+
* const teams = await db.find({ collection: 'teams', where: { owner: { equals: user.sub } } })
|
|
84
|
+
* return teams.docs.map(t => ({ label: t.name, value: t.id }))
|
|
85
|
+
* }
|
|
86
|
+
*/
|
|
87
|
+
user?: AuthenticatedUser;
|
|
88
|
+
/**
|
|
89
|
+
* HTTP request context. Sibling field values selected in the Admin UI are
|
|
90
|
+
* forwarded as query parameters and accessible via `req.query`.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* // Cascading dropdown: the Admin UI appends ?country=ca
|
|
94
|
+
* options: async ({ req }) => {
|
|
95
|
+
* const country = req.query.country ?? 'us'
|
|
96
|
+
* const regions = await fetch(`/api/regions?country=${country}`)
|
|
97
|
+
* return regions.json()
|
|
98
|
+
* }
|
|
99
|
+
*/
|
|
100
|
+
req: HookRequestContext;
|
|
101
|
+
}
|
|
102
|
+
/** A function that returns option items for a `select`, `multiSelect`, or `radio` field. */
|
|
103
|
+
type DynamicOptionsResolver = (args: DynamicOptionsResolverArgs) => Promise<DynamicOptionItem[]> | DynamicOptionItem[];
|
|
104
|
+
/**
|
|
105
|
+
* Config-object form for a dynamic options resolver. Use this when you also
|
|
106
|
+
* want server-side caching via `cacheTTL`.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* options: {
|
|
110
|
+
* resolve: async () => fetchCountriesFromApi(),
|
|
111
|
+
* cacheTTL: 300, // cache for 5 minutes
|
|
112
|
+
* }
|
|
113
|
+
*/
|
|
114
|
+
interface DynamicOptionsConfig {
|
|
115
|
+
/** The resolver function. Receives the same args as a bare function resolver. */
|
|
116
|
+
resolve: DynamicOptionsResolver;
|
|
117
|
+
/**
|
|
118
|
+
* Time-to-live in **seconds** for caching the resolver result on the server.
|
|
119
|
+
*
|
|
120
|
+
* Useful for resolvers that call rate-limited external APIs. When set, the
|
|
121
|
+
* result is reused for all identical requests within the TTL window.
|
|
122
|
+
*
|
|
123
|
+
* Not applied when the resolver uses query parameters (`req.query`), since
|
|
124
|
+
* parameterised results are request-specific.
|
|
125
|
+
*/
|
|
126
|
+
cacheTTL?: number;
|
|
127
|
+
}
|
|
128
|
+
/** A single option item — either a plain string or a label/value pair. */
|
|
129
|
+
type DynamicOptionItem = string | {
|
|
130
|
+
label: string;
|
|
131
|
+
value: unknown;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* A single block type for use inside a `blocks` field.
|
|
135
|
+
*
|
|
136
|
+
* Each block has its own field schema. At runtime, every block item in the
|
|
137
|
+
* array carries a `blockType` property that matches the block's `slug`.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* const HeroBlock: Block = {
|
|
141
|
+
* slug: 'hero',
|
|
142
|
+
* labels: { singular: 'Hero', plural: 'Heroes' },
|
|
143
|
+
* fields: [
|
|
144
|
+
* { name: 'heading', type: 'text', required: true },
|
|
145
|
+
* { name: 'image', type: 'relationship', relationTo: 'media' },
|
|
146
|
+
* ],
|
|
147
|
+
* }
|
|
148
|
+
*/
|
|
149
|
+
interface Block {
|
|
150
|
+
/** Unique identifier for this block type. Stored as `blockType` on each item. */
|
|
151
|
+
slug: string;
|
|
152
|
+
/** Human-readable labels shown in the Admin UI block picker. */
|
|
153
|
+
labels?: {
|
|
154
|
+
singular: string;
|
|
155
|
+
plural: string;
|
|
156
|
+
};
|
|
157
|
+
/** Fields that make up this block's data shape. */
|
|
158
|
+
fields: Field[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A hook that runs **before a field value is saved** to the database.
|
|
162
|
+
*
|
|
163
|
+
* Return the transformed value to persist. Return `undefined` to leave the
|
|
164
|
+
* value unchanged (same as returning the original `value`).
|
|
165
|
+
*
|
|
166
|
+
* Field `beforeChange` hooks run recursively inside `array`, `object`, and
|
|
167
|
+
* `blocks` fields — every nested item is processed automatically.
|
|
168
|
+
*
|
|
169
|
+
* @template TValue The TypeScript type of this field's value.
|
|
170
|
+
* @template TDoc The document shape of the parent collection/global.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* // Normalise email to lowercase
|
|
174
|
+
* const normaliseEmail: FieldBeforeChangeHook<string> = ({ value }) =>
|
|
175
|
+
* value?.toLowerCase().trim()
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // Hash a password with bcrypt
|
|
179
|
+
* const hashPassword: FieldBeforeChangeHook<string> = async ({ value }) =>
|
|
180
|
+
* value ? bcrypt.hash(value, 10) : value
|
|
181
|
+
*/
|
|
182
|
+
type FieldBeforeChangeHook<TValue = unknown, TDoc extends object = Record<string, unknown>> = (args: {
|
|
183
|
+
/** The current value of this field (after any previous hooks in the chain). */
|
|
184
|
+
value: TValue;
|
|
185
|
+
/** The full document as it existed before this update. `undefined` on create. */
|
|
186
|
+
originalDoc?: TDoc;
|
|
187
|
+
/** The full incoming data payload being written. */
|
|
188
|
+
data: Partial<TDoc>;
|
|
189
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
190
|
+
user?: AuthenticatedUser;
|
|
191
|
+
}) => TValue | undefined | Promise<TValue | undefined>;
|
|
192
|
+
/**
|
|
193
|
+
* A hook that runs **after a field value is read** from the database, before
|
|
194
|
+
* the response is sent to the client.
|
|
195
|
+
*
|
|
196
|
+
* Return the transformed value to return to the client. Use this for masking,
|
|
197
|
+
* formatting, or adding computed properties.
|
|
198
|
+
*
|
|
199
|
+
* @template TValue The TypeScript type of this field's value.
|
|
200
|
+
* @template TDoc The document shape of the parent collection/global.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* // Mask sensitive value for non-admins
|
|
204
|
+
* const maskForPublic: FieldAfterReadHook<string> = ({ value, user }) =>
|
|
205
|
+
* user?.role === 'admin' ? value : '••••••••'
|
|
206
|
+
*/
|
|
207
|
+
type FieldAfterReadHook<TValue = unknown, TDoc extends object = Record<string, unknown>> = (args: {
|
|
208
|
+
/** The raw field value as stored in the database. */
|
|
209
|
+
value: TValue;
|
|
210
|
+
/** The full document being returned (with defaults applied). */
|
|
211
|
+
doc: TDoc;
|
|
212
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
213
|
+
user?: AuthenticatedUser;
|
|
214
|
+
}) => TValue | undefined | Promise<TValue | undefined>;
|
|
215
|
+
/**
|
|
216
|
+
* @deprecated Use {@link FieldBeforeChangeHook} or {@link FieldAfterReadHook} instead.
|
|
217
|
+
* This alias remains for backwards compatibility.
|
|
218
|
+
*/
|
|
219
|
+
type FieldHook<TDoc extends object = Record<string, unknown>, TValue = unknown> = FieldBeforeChangeHook<TValue, TDoc>;
|
|
220
|
+
/**
|
|
221
|
+
* Runs before Dyrected queries the database for a list or single-document fetch.
|
|
222
|
+
*
|
|
223
|
+
* Return a new `where` query object to override or extend the current filter.
|
|
224
|
+
* Return `undefined` (or nothing) to leave the query unchanged.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* // Scope all reads to the current user's own documents
|
|
228
|
+
* const scopeToUser: CollectionBeforeReadHook = ({ user, query }) => ({
|
|
229
|
+
* ...query,
|
|
230
|
+
* owner: { equals: user?.sub },
|
|
231
|
+
* })
|
|
232
|
+
*/
|
|
233
|
+
type CollectionBeforeReadHook = (args: {
|
|
234
|
+
/** The HTTP request context. */
|
|
235
|
+
req: HookRequestContext;
|
|
236
|
+
/** The current `where` query filter. Modify and return to override. */
|
|
237
|
+
query?: Record<string, unknown>;
|
|
238
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
239
|
+
user?: AuthenticatedUser;
|
|
240
|
+
}) => Record<string, unknown> | void | Promise<Record<string, unknown> | void>;
|
|
241
|
+
/**
|
|
242
|
+
* Runs after a document (or list of documents) is fetched from the database,
|
|
243
|
+
* before the response is sent to the client.
|
|
244
|
+
*
|
|
245
|
+
* Return a modified document to send instead. Useful for adding computed
|
|
246
|
+
* virtual fields or transforming the shape of the response.
|
|
247
|
+
*
|
|
248
|
+
* @template TDoc The shape of the collection's document.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* // Add a computed fullName field
|
|
252
|
+
* const addFullName: CollectionAfterReadHook<User> = ({ doc }) => ({
|
|
253
|
+
* ...doc,
|
|
254
|
+
* fullName: `${doc.firstName} ${doc.lastName}`.trim(),
|
|
255
|
+
* })
|
|
256
|
+
*/
|
|
257
|
+
type CollectionAfterReadHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
258
|
+
/** The document as fetched from the database (with defaults applied). */
|
|
259
|
+
doc: TDoc;
|
|
260
|
+
/** The HTTP request context. */
|
|
261
|
+
req: HookRequestContext;
|
|
262
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
263
|
+
user?: AuthenticatedUser;
|
|
264
|
+
}) => TDoc | Promise<TDoc>;
|
|
265
|
+
/**
|
|
266
|
+
* Runs **before** a document is created or updated in the database.
|
|
267
|
+
*
|
|
268
|
+
* Return a modified data object to write instead of the original. This is the
|
|
269
|
+
* right place for data transformation, normalisation, slug generation, and
|
|
270
|
+
* validation (throw to abort the write).
|
|
271
|
+
*
|
|
272
|
+
* @template TDoc The shape of the collection's document.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* // Auto-generate a slug on create
|
|
276
|
+
* const generateSlug: CollectionBeforeChangeHook<Post> = ({ data, operation }) => {
|
|
277
|
+
* if (operation === 'create' || data.title !== undefined) {
|
|
278
|
+
* return { ...data, slug: slugify(data.title ?? '') }
|
|
279
|
+
* }
|
|
280
|
+
* return data
|
|
281
|
+
* }
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* // Abort the write with a validation error
|
|
285
|
+
* const validateStock: CollectionBeforeChangeHook<Product> = ({ data }) => {
|
|
286
|
+
* if ((data.stock ?? 0) < 0) throw new Error('Stock cannot be negative.')
|
|
287
|
+
* return data
|
|
288
|
+
* }
|
|
289
|
+
*/
|
|
290
|
+
type CollectionBeforeChangeHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
291
|
+
/** The incoming data payload being written. */
|
|
292
|
+
data: Partial<TDoc>;
|
|
293
|
+
/**
|
|
294
|
+
* The existing document before this update. Only present on `'update'`
|
|
295
|
+
* operations; `undefined` on `'create'`.
|
|
296
|
+
*/
|
|
297
|
+
doc?: TDoc;
|
|
298
|
+
/** The HTTP request context. */
|
|
299
|
+
req: HookRequestContext;
|
|
300
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
301
|
+
user?: AuthenticatedUser;
|
|
302
|
+
/** Whether this is a new document or an update to an existing one. */
|
|
303
|
+
operation: "create" | "update";
|
|
304
|
+
}) => Partial<TDoc> | void | Promise<Partial<TDoc> | void>;
|
|
305
|
+
/**
|
|
306
|
+
* Runs **after** a document is created or updated in the database.
|
|
307
|
+
*
|
|
308
|
+
* The return value is ignored — this hook is for side-effects only (webhooks,
|
|
309
|
+
* cache revalidation, notifications, etc.). Throwing here does NOT roll back
|
|
310
|
+
* the already-committed write.
|
|
311
|
+
*
|
|
312
|
+
* @template TDoc The shape of the collection's document.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* // Send a webhook after every save
|
|
316
|
+
* const sendWebhook: CollectionAfterChangeHook<Post> = async ({ doc, operation }) => {
|
|
317
|
+
* await fetch('https://hooks.example.com/content', {
|
|
318
|
+
* method: 'POST',
|
|
319
|
+
* body: JSON.stringify({ event: operation, doc }),
|
|
320
|
+
* })
|
|
321
|
+
* }
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* // Only act when a specific field changed
|
|
325
|
+
* const onStatusChange: CollectionAfterChangeHook<Post> = async ({ doc, previousDoc, operation }) => {
|
|
326
|
+
* if (operation === 'update' && doc.status !== previousDoc?.status) {
|
|
327
|
+
* await notifySubscribers(doc)
|
|
328
|
+
* }
|
|
329
|
+
* }
|
|
330
|
+
*/
|
|
331
|
+
type CollectionAfterChangeHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
332
|
+
/** The document as it was written to the database. */
|
|
333
|
+
doc: TDoc;
|
|
334
|
+
/**
|
|
335
|
+
* Snapshot of the document before the write. Only present on `'update'`
|
|
336
|
+
* operations; `undefined` on `'create'`.
|
|
337
|
+
*/
|
|
338
|
+
previousDoc?: TDoc;
|
|
339
|
+
/** The HTTP request context. */
|
|
340
|
+
req: HookRequestContext;
|
|
341
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
342
|
+
user?: AuthenticatedUser;
|
|
343
|
+
/** Whether this was a new document or an update. */
|
|
344
|
+
operation: "create" | "update";
|
|
345
|
+
}) => void | Promise<void>;
|
|
346
|
+
/**
|
|
347
|
+
* Runs **before** a document is deleted from the database.
|
|
348
|
+
*
|
|
349
|
+
* Throw an error to cancel the deletion — the document will not be removed
|
|
350
|
+
* and the API will return a `500` with your error message.
|
|
351
|
+
*
|
|
352
|
+
* @template TDoc The shape of the collection's document.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* // Prevent deletion when other documents reference this one
|
|
356
|
+
* const guardReferences: CollectionBeforeDeleteHook<Category> = async ({ id, doc }) => {
|
|
357
|
+
* const refs = await db.find({ collection: 'posts', where: { category: { equals: id } } })
|
|
358
|
+
* if (refs.total > 0) throw new Error(`${refs.total} post(s) still reference this category.`)
|
|
359
|
+
* }
|
|
360
|
+
*/
|
|
361
|
+
type CollectionBeforeDeleteHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
362
|
+
/** The ID of the document about to be deleted. */
|
|
363
|
+
id: string;
|
|
364
|
+
/** The full document about to be deleted. */
|
|
365
|
+
doc: TDoc;
|
|
366
|
+
/** The HTTP request context. */
|
|
367
|
+
req: HookRequestContext;
|
|
368
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
369
|
+
user?: AuthenticatedUser;
|
|
370
|
+
}) => void | Promise<void>;
|
|
371
|
+
/**
|
|
372
|
+
* Runs **after** a document has been deleted from the database.
|
|
373
|
+
*
|
|
374
|
+
* Use for cleanup side-effects — removing related media, invalidating caches, etc.
|
|
375
|
+
*
|
|
376
|
+
* @template TDoc The shape of the collection's document.
|
|
377
|
+
*/
|
|
378
|
+
type CollectionAfterDeleteHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
379
|
+
/** The ID of the deleted document. */
|
|
380
|
+
id: string;
|
|
381
|
+
/** The document as it was just before deletion. */
|
|
382
|
+
doc: TDoc;
|
|
383
|
+
/** The HTTP request context. */
|
|
384
|
+
req: HookRequestContext;
|
|
385
|
+
/** The authenticated user, or `undefined` for unauthenticated requests. */
|
|
386
|
+
user?: AuthenticatedUser;
|
|
387
|
+
}) => void | Promise<void>;
|
|
388
|
+
/** @see {@link CollectionBeforeReadHook} */
|
|
389
|
+
type GlobalBeforeReadHook = CollectionBeforeReadHook;
|
|
390
|
+
/**
|
|
391
|
+
* Runs after the global document is fetched, before the response is sent.
|
|
392
|
+
* @see {@link CollectionAfterReadHook}
|
|
393
|
+
*/
|
|
394
|
+
type GlobalAfterReadHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
395
|
+
doc: TDoc;
|
|
396
|
+
req: HookRequestContext;
|
|
397
|
+
user?: AuthenticatedUser;
|
|
398
|
+
}) => TDoc | Promise<TDoc>;
|
|
399
|
+
/**
|
|
400
|
+
* Runs before the global document is updated.
|
|
401
|
+
* Operation is always `'update'` (globals cannot be created or deleted).
|
|
402
|
+
* @see {@link CollectionBeforeChangeHook}
|
|
403
|
+
*/
|
|
404
|
+
type GlobalBeforeChangeHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
405
|
+
data: Partial<TDoc>;
|
|
406
|
+
doc?: TDoc;
|
|
407
|
+
req: HookRequestContext;
|
|
408
|
+
user?: AuthenticatedUser;
|
|
409
|
+
operation: "update";
|
|
410
|
+
}) => Partial<TDoc> | void | Promise<Partial<TDoc> | void>;
|
|
411
|
+
/**
|
|
412
|
+
* Runs after the global document is updated. Side-effects only.
|
|
413
|
+
* @see {@link CollectionAfterChangeHook}
|
|
414
|
+
*/
|
|
415
|
+
type GlobalAfterChangeHook<TDoc extends object = Record<string, unknown>> = (args: {
|
|
416
|
+
doc: TDoc;
|
|
417
|
+
previousDoc?: TDoc;
|
|
418
|
+
req: HookRequestContext;
|
|
419
|
+
user?: AuthenticatedUser;
|
|
420
|
+
operation: "update";
|
|
421
|
+
}) => void | Promise<void>;
|
|
422
|
+
/**
|
|
423
|
+
* @deprecated Use the specific hook types instead:
|
|
424
|
+
* {@link CollectionBeforeChangeHook}, {@link CollectionAfterReadHook}, etc.
|
|
425
|
+
*
|
|
426
|
+
* This broad type remains for backwards compatibility with the internal hook runner.
|
|
427
|
+
*/
|
|
428
|
+
type HookFunction<TDoc extends object = Record<string, unknown>> = (args: {
|
|
429
|
+
data?: Partial<TDoc>;
|
|
430
|
+
doc?: TDoc;
|
|
431
|
+
user?: AuthenticatedUser;
|
|
432
|
+
req?: HookRequestContext;
|
|
433
|
+
operation?: "create" | "update" | "delete";
|
|
434
|
+
[key: string]: unknown;
|
|
435
|
+
}) => unknown | Promise<unknown>;
|
|
436
|
+
/**
|
|
437
|
+
* A function that determines whether the current user can perform an operation.
|
|
438
|
+
*
|
|
439
|
+
* Return `true` to allow, `false` to deny.
|
|
440
|
+
* Return a `where`-style object to allow access only to matching documents
|
|
441
|
+
* (useful for multi-tenant setups where users can only see their own data).
|
|
442
|
+
*
|
|
443
|
+
* Can also be expressed as a Jexl expression **string** for simple role checks
|
|
444
|
+
* that need to be serialised (e.g. stored in the database or sent to the Admin UI).
|
|
445
|
+
*
|
|
446
|
+
* @template TDoc The shape of the collection's document.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* // Simple role check
|
|
450
|
+
* access: {
|
|
451
|
+
* delete: ({ user }) => user?.roles?.includes('admin') ?? false,
|
|
452
|
+
* }
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* // Row-level: users can only read their own documents
|
|
456
|
+
* access: {
|
|
457
|
+
* read: ({ user }) => ({ owner: { equals: user?.sub } }),
|
|
458
|
+
* }
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* // Jexl string — evaluated server-side
|
|
462
|
+
* access: {
|
|
463
|
+
* update: "user.roles contains 'editor'",
|
|
464
|
+
* }
|
|
465
|
+
*/
|
|
466
|
+
type AccessFunction<TDoc extends object = Record<string, unknown>> = (args: {
|
|
467
|
+
user: AuthenticatedUser | undefined;
|
|
468
|
+
doc?: TDoc;
|
|
469
|
+
data?: Partial<TDoc>;
|
|
470
|
+
req: HookRequestContext;
|
|
471
|
+
}) => boolean | Record<string, unknown> | Promise<boolean | Record<string, unknown>>;
|
|
472
|
+
/**
|
|
473
|
+
* Shared base properties present on every field type.
|
|
474
|
+
* The exported `Field` union type extends this with a typed `hooks` block.
|
|
475
|
+
*/
|
|
476
|
+
interface FieldBase {
|
|
477
|
+
/**
|
|
478
|
+
* The database column / JSON key for this field.
|
|
479
|
+
* Use camelCase. Omit for layout-only fields (`row`, `join`).
|
|
480
|
+
*/
|
|
481
|
+
name?: string;
|
|
482
|
+
/**
|
|
483
|
+
* Human-readable label shown next to the field in the Admin UI.
|
|
484
|
+
* Defaults to a title-cased version of `name` if omitted.
|
|
485
|
+
*/
|
|
486
|
+
label?: string;
|
|
487
|
+
/**
|
|
488
|
+
* If `true`, the field must have a non-empty value.
|
|
489
|
+
* Enforced on both client (Admin UI) and server (API).
|
|
490
|
+
*/
|
|
491
|
+
required?: boolean;
|
|
492
|
+
/**
|
|
493
|
+
* If `true`, the database adapter enforces a unique constraint for this
|
|
494
|
+
* field across the collection.
|
|
495
|
+
*/
|
|
496
|
+
unique?: boolean;
|
|
497
|
+
/**
|
|
498
|
+
* The initial value written to new documents when no value is provided.
|
|
499
|
+
* The type should match the field type.
|
|
500
|
+
*/
|
|
501
|
+
defaultValue?: unknown;
|
|
502
|
+
/**
|
|
503
|
+
* Available choices for `select`, `multiSelect`, and `radio` fields.
|
|
504
|
+
*
|
|
505
|
+
* Three forms are accepted:
|
|
506
|
+
*
|
|
507
|
+
* **1. Static array** — fixed list known at build time:
|
|
508
|
+
* ```ts
|
|
509
|
+
* options: [{ label: 'Draft', value: 'draft' }, { label: 'Published', value: 'published' }]
|
|
510
|
+
* ```
|
|
511
|
+
*
|
|
512
|
+
* **2. Server-side resolver** — runs on the server (never in the browser).
|
|
513
|
+
* Use for DB queries, secret API keys, or user-filtered lists.
|
|
514
|
+
* The Admin UI fetches results from `GET /api/dyrected/options/:slug/:field`.
|
|
515
|
+
* ```ts
|
|
516
|
+
* options: async ({ db, user }) => {
|
|
517
|
+
* const cats = await db.find({ collection: 'categories' })
|
|
518
|
+
* return cats.docs.map(c => ({ label: c.name, value: c.id }))
|
|
519
|
+
* }
|
|
520
|
+
* ```
|
|
521
|
+
*
|
|
522
|
+
* **3. With caching** (`{ resolve, cacheTTL }`) — same as above but the
|
|
523
|
+
* result is cached on the server for `cacheTTL` seconds:
|
|
524
|
+
* ```ts
|
|
525
|
+
* options: { resolve: async () => fetchFromSlowApi(), cacheTTL: 300 }
|
|
526
|
+
* ```
|
|
527
|
+
*
|
|
528
|
+
* For **instant client-side cascading dropdowns** driven by another field's
|
|
529
|
+
* value, use `admin.hooks.options` instead (no network round-trip).
|
|
530
|
+
*/
|
|
531
|
+
options?: string[] | {
|
|
532
|
+
label: string;
|
|
533
|
+
value: unknown;
|
|
534
|
+
}[] | DynamicOptionsResolver | DynamicOptionsConfig;
|
|
535
|
+
/** For `relationship` fields — the slug of the collection to link to. */
|
|
536
|
+
relationTo?: string;
|
|
537
|
+
/**
|
|
538
|
+
* For `relationship` and `image` fields — if `true`, stores an array of
|
|
539
|
+
* IDs instead of a single ID.
|
|
540
|
+
*/
|
|
541
|
+
hasMany?: boolean;
|
|
542
|
+
/** Sub-fields for `array` and `object` field types. */
|
|
543
|
+
fields?: Field[];
|
|
544
|
+
/** Block type definitions for `blocks` fields. */
|
|
545
|
+
blocks?: Block[];
|
|
546
|
+
/**
|
|
547
|
+
* For `join` fields — the slug of the target collection to show linked
|
|
548
|
+
* documents from. Paired with `on`.
|
|
549
|
+
*/
|
|
550
|
+
collection?: string;
|
|
551
|
+
/**
|
|
552
|
+
* For `join` fields — the name of the `relationship` field on the target
|
|
553
|
+
* collection that points back to this collection.
|
|
554
|
+
*/
|
|
555
|
+
on?: string;
|
|
556
|
+
/**
|
|
557
|
+
* Field-level read/update access control.
|
|
558
|
+
*
|
|
559
|
+
* - `read`: if it returns `false`, the field is stripped from API responses.
|
|
560
|
+
* - `update`: if it returns `false`, incoming values for this field are silently ignored.
|
|
561
|
+
*/
|
|
562
|
+
access?: {
|
|
563
|
+
read?: AccessFunction | string;
|
|
564
|
+
update?: AccessFunction | string;
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* Admin UI options for this field. None of these properties affect the API
|
|
568
|
+
* or the database — they are purely presentational.
|
|
569
|
+
*/
|
|
570
|
+
admin?: {
|
|
571
|
+
/** Placeholder text shown when the input is empty. */
|
|
572
|
+
placeholder?: string;
|
|
573
|
+
/** Help text rendered below the field in the Admin UI. */
|
|
574
|
+
description?: string;
|
|
575
|
+
/**
|
|
576
|
+
* If `true`, the field is hidden from the Admin form entirely.
|
|
577
|
+
* Its value is preserved in the database and still returned by the API.
|
|
578
|
+
*/
|
|
579
|
+
hidden?: boolean;
|
|
580
|
+
/**
|
|
581
|
+
* If `true`, the field is rendered as non-editable in the Admin UI.
|
|
582
|
+
* The value is still included in form submissions.
|
|
583
|
+
*/
|
|
584
|
+
readOnly?: boolean;
|
|
585
|
+
/**
|
|
586
|
+
* A function (or Jexl expression string) evaluated reactively as the
|
|
587
|
+
* editor types. Return `true` to show the field, `false` to hide it.
|
|
588
|
+
*
|
|
589
|
+
* Receives the current form values as `data` and sibling-level values
|
|
590
|
+
* as `siblingData`.
|
|
591
|
+
*
|
|
592
|
+
* @example
|
|
593
|
+
* // Only show the discount field when a coupon code is entered
|
|
594
|
+
* condition: (data) => !!data.couponCode
|
|
595
|
+
*/
|
|
596
|
+
condition?: ((data: Record<string, unknown>, siblingData: Record<string, unknown>) => boolean) | string;
|
|
597
|
+
/**
|
|
598
|
+
* For `select` fields — render as radio buttons instead of a dropdown.
|
|
599
|
+
* Recommended for 2–5 options.
|
|
600
|
+
*/
|
|
601
|
+
layout?: "radio" | "select" | string;
|
|
602
|
+
/**
|
|
603
|
+
* For `select` fields with `layout: 'radio'` — orientation of the radio buttons.
|
|
604
|
+
* @default 'vertical'
|
|
605
|
+
*/
|
|
606
|
+
direction?: "horizontal" | "vertical";
|
|
607
|
+
/**
|
|
608
|
+
* Assigns this field to a named tab in the Admin form.
|
|
609
|
+
* When any field in the collection has a `tab`, the form switches to a
|
|
610
|
+
* tabbed layout. Fields without a `tab` are grouped under "General".
|
|
611
|
+
*/
|
|
612
|
+
tab?: string;
|
|
613
|
+
/**
|
|
614
|
+
* CSS width of this field when it is a child of a `row` field.
|
|
615
|
+
* Accepts any CSS length value, e.g. `'50%'`, `'200px'`, `'1fr'`.
|
|
616
|
+
*/
|
|
617
|
+
width?: string;
|
|
618
|
+
/**
|
|
619
|
+
* Client-side Admin UI hooks for this field.
|
|
620
|
+
*
|
|
621
|
+
* These run **in the browser** every time any sibling field value changes.
|
|
622
|
+
* They are completely separate from the server-side `hooks` property.
|
|
623
|
+
*
|
|
624
|
+
* `onChange` is typed per-field via the discriminated `Field` union —
|
|
625
|
+
* see `FieldAdminHooks<TValue>` below.
|
|
626
|
+
*/
|
|
627
|
+
hooks?: {
|
|
628
|
+
/**
|
|
629
|
+
* For `select`, `multiSelect`, and `radio` fields only.
|
|
630
|
+
*
|
|
631
|
+
* Computes the **available choices** for this field based on other
|
|
632
|
+
* field values — instant, zero-latency cascading dropdowns.
|
|
633
|
+
*
|
|
634
|
+
* Runs in the browser every time any sibling field changes. When the
|
|
635
|
+
* returned list changes, the field's current value is automatically
|
|
636
|
+
* cleared if it is no longer a valid choice.
|
|
637
|
+
*
|
|
638
|
+
* Use the top-level `options` resolver (server-side) when:
|
|
639
|
+
* - Your option list requires a DB query or a secret API key
|
|
640
|
+
* - You want server-side caching (`cacheTTL`)
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* // Country → State/Province cascading dropdown
|
|
644
|
+
* options: ({ siblingData }) => {
|
|
645
|
+
* if (siblingData.country === 'us') {
|
|
646
|
+
* return [{ label: 'California', value: 'CA' }, { label: 'New York', value: 'NY' }]
|
|
647
|
+
* }
|
|
648
|
+
* return []
|
|
649
|
+
* }
|
|
650
|
+
*/
|
|
651
|
+
options?: (args: {
|
|
652
|
+
/** Current values of all fields at the same nesting level. */
|
|
653
|
+
siblingData: Record<string, unknown>;
|
|
654
|
+
/** Current values of the entire form. */
|
|
655
|
+
data: Record<string, unknown>;
|
|
656
|
+
}) => Array<string | {
|
|
657
|
+
label: string;
|
|
658
|
+
value: unknown;
|
|
659
|
+
}> | Promise<Array<string | {
|
|
660
|
+
label: string;
|
|
661
|
+
value: unknown;
|
|
662
|
+
}>>;
|
|
663
|
+
};
|
|
664
|
+
};
|
|
665
|
+
/**
|
|
666
|
+
* For database migrations: if set, data stored under this key in the database
|
|
667
|
+
* will be migrated to the current field `name` on next schema sync.
|
|
668
|
+
*/
|
|
669
|
+
renameTo?: string;
|
|
670
|
+
/**
|
|
671
|
+
* For SQL adapters: if `true`, this field is extracted to a real column
|
|
672
|
+
* instead of being stored inside a JSON blob. Improves query performance
|
|
673
|
+
* for frequently-filtered fields.
|
|
674
|
+
*/
|
|
675
|
+
promoted?: boolean;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Typed hooks block for a field whose stored value is `TValue`.
|
|
679
|
+
*
|
|
680
|
+
* The return type of the callbacks is kept as `unknown` here so that TypeScript
|
|
681
|
+
* can narrow the contextual type from the discriminated `Field` union without
|
|
682
|
+
* bidirectional inference conflicts. Use the explicit `FieldBeforeChangeHook<T>`
|
|
683
|
+
* and `FieldAfterReadHook<T>` types when you need checked return types.
|
|
684
|
+
*/
|
|
685
|
+
type FieldHooks<TValue> = {
|
|
686
|
+
/**
|
|
687
|
+
* Field-level lifecycle hooks.
|
|
688
|
+
*
|
|
689
|
+
* - `beforeChange` — transform or validate the value before it is saved.
|
|
690
|
+
* `value` is typed to the field's own value type (e.g. `string` for
|
|
691
|
+
* `text`, `number` for `number`, `boolean` for `boolean`).
|
|
692
|
+
* - `afterRead` — transform the value after it is read, before the API response.
|
|
693
|
+
*
|
|
694
|
+
* Both hook types run **recursively** inside `array`, `object`, and `blocks` fields.
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* hooks: {
|
|
698
|
+
* beforeChange: [({ value }) => value.trim()], // value: string ✓
|
|
699
|
+
* afterRead: [({ value, user }) => user?.role === 'admin' ? value : '****'],
|
|
700
|
+
* }
|
|
701
|
+
*/
|
|
702
|
+
hooks?: {
|
|
703
|
+
beforeChange?: Array<(args: {
|
|
704
|
+
value: TValue;
|
|
705
|
+
originalDoc?: Record<string, unknown>;
|
|
706
|
+
data: Record<string, unknown>;
|
|
707
|
+
user?: AuthenticatedUser;
|
|
708
|
+
}) => unknown>;
|
|
709
|
+
afterRead?: Array<(args: {
|
|
710
|
+
value: TValue;
|
|
711
|
+
doc: Record<string, unknown>;
|
|
712
|
+
user?: AuthenticatedUser;
|
|
713
|
+
}) => unknown>;
|
|
714
|
+
};
|
|
715
|
+
};
|
|
716
|
+
/**
|
|
717
|
+
* Typed client-side admin hook block for a field whose value type is `TValue`.
|
|
718
|
+
*
|
|
719
|
+
* Intersected into each discriminated `Field` union member so that
|
|
720
|
+
* `admin.hooks.onChange` receives a `value` typed to the field's own type.
|
|
721
|
+
* The return type is `unknown` for the same reason as `FieldHooks` — to
|
|
722
|
+
* prevent bidirectional inference from conflicting with union discrimination.
|
|
723
|
+
*/
|
|
724
|
+
type FieldAdminHooks<TValue> = {
|
|
725
|
+
admin?: {
|
|
726
|
+
hooks?: {
|
|
727
|
+
/**
|
|
728
|
+
* Derives or computes this field's **value** from other field values in
|
|
729
|
+
* real time — without any network request.
|
|
730
|
+
*
|
|
731
|
+
* Return a new value to update this field. Return `undefined` to leave
|
|
732
|
+
* it unchanged.
|
|
733
|
+
*
|
|
734
|
+
* Use `setValue` for imperative updates inside async callbacks.
|
|
735
|
+
*
|
|
736
|
+
* ⚠️ Never return an options array here — use `admin.hooks.options` for that.
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* // Auto-generate a URL slug from the title field
|
|
740
|
+
* onChange: ({ value, siblingData }) => {
|
|
741
|
+
* const base = (siblingData.title ?? '').toLowerCase().replace(/[^a-z0-9]+/g, '-');
|
|
742
|
+
* return base.includes(value) ? base : value;
|
|
743
|
+
* }
|
|
744
|
+
*/
|
|
745
|
+
onChange?: (args: {
|
|
746
|
+
/** The current value of this field — typed to match the field's `type`. */
|
|
747
|
+
value: TValue;
|
|
748
|
+
/** Current values of all fields at the same nesting level. */
|
|
749
|
+
siblingData: Record<string, unknown>;
|
|
750
|
+
/** Current values of the entire form. */
|
|
751
|
+
data: Record<string, unknown>;
|
|
752
|
+
/** Imperative setter — use inside async callbacks when you can't return a value. */
|
|
753
|
+
setValue: (value: unknown) => void;
|
|
754
|
+
}) => unknown;
|
|
755
|
+
};
|
|
756
|
+
};
|
|
757
|
+
};
|
|
758
|
+
/**
|
|
759
|
+
* Defines a single field on a collection or global.
|
|
760
|
+
*
|
|
761
|
+
* The `hooks.beforeChange` and `hooks.afterRead` callbacks receive a `value`
|
|
762
|
+
* typed to match the field's `type` property — `string` for text fields,
|
|
763
|
+
* `number` for number fields, `boolean` for boolean, `string[]` for
|
|
764
|
+
* `multiSelect`, etc. No manual annotations needed.
|
|
765
|
+
*
|
|
766
|
+
* **Important**: write field `type` values as plain string literals — do **not**
|
|
767
|
+
* use `as const`. TypeScript's `const` generic inference already preserves
|
|
768
|
+
* literal types, and adding `as const` to a discriminant property prevents
|
|
769
|
+
* the contextual type from flowing into the hook callbacks.
|
|
770
|
+
*
|
|
771
|
+
* ```ts
|
|
772
|
+
* // ✓ correct — type inferred as literal 'text', value is string
|
|
773
|
+
* { name: 'slug', type: 'text', hooks: { beforeChange: [({ value }) => value.toLowerCase()] } }
|
|
774
|
+
*
|
|
775
|
+
* // ✗ avoid — `as const` on the discriminant breaks contextual typing
|
|
776
|
+
* { name: 'slug', type: 'text' as const, hooks: { beforeChange: [({ value }) => value.toLowerCase()] } }
|
|
777
|
+
* ```
|
|
778
|
+
*/
|
|
779
|
+
type Field = FieldBase & (({
|
|
780
|
+
type: 'text' | 'textarea' | 'email' | 'url' | 'icon' | 'date' | 'select' | 'radio';
|
|
781
|
+
} & FieldHooks<string> & FieldAdminHooks<string>) | ({
|
|
782
|
+
type: 'number';
|
|
783
|
+
} & FieldHooks<number> & FieldAdminHooks<number>) | ({
|
|
784
|
+
type: 'boolean';
|
|
785
|
+
} & FieldHooks<boolean> & FieldAdminHooks<boolean>) | ({
|
|
786
|
+
type: 'multiSelect';
|
|
787
|
+
} & FieldHooks<string[]> & FieldAdminHooks<string[]>) | ({
|
|
788
|
+
type: 'relationship' | 'image';
|
|
789
|
+
} & FieldHooks<string | string[]> & FieldAdminHooks<string | string[]>) | ({
|
|
790
|
+
type: 'richText' | 'json';
|
|
791
|
+
} & FieldHooks<Record<string, unknown>> & FieldAdminHooks<Record<string, unknown>>) | ({
|
|
792
|
+
type: 'object' | 'array' | 'blocks' | 'join' | 'row';
|
|
793
|
+
} & FieldHooks<unknown> & FieldAdminHooks<unknown>));
|
|
794
|
+
/**
|
|
795
|
+
* Defines a Dyrected collection — a named set of documents with a shared schema.
|
|
796
|
+
*
|
|
797
|
+
* Pass your document's TypeScript type as the generic parameter `TDoc` to get
|
|
798
|
+
* fully typed hooks and access functions:
|
|
799
|
+
*
|
|
800
|
+
* ```ts
|
|
801
|
+
* interface Post {
|
|
802
|
+
* id: string
|
|
803
|
+
* title: string
|
|
804
|
+
* slug: string
|
|
805
|
+
* status: 'draft' | 'published'
|
|
806
|
+
* publishedAt?: string
|
|
807
|
+
* }
|
|
808
|
+
*
|
|
809
|
+
* export const Posts = defineCollection<Post>({
|
|
810
|
+
* slug: 'posts',
|
|
811
|
+
* hooks: {
|
|
812
|
+
* beforeChange: [({ data, operation }) => {
|
|
813
|
+
* // `data` is typed as Partial<Post>
|
|
814
|
+
* if (operation === 'create') return { ...data, status: 'draft' }
|
|
815
|
+
* return data
|
|
816
|
+
* }],
|
|
817
|
+
* afterChange: [({ doc, previousDoc }) => {
|
|
818
|
+
* // `doc` and `previousDoc` are typed as Post
|
|
819
|
+
* if (doc.status !== previousDoc?.status) notifySubscribers(doc)
|
|
820
|
+
* }],
|
|
821
|
+
* },
|
|
822
|
+
* fields: [...],
|
|
823
|
+
* })
|
|
824
|
+
* ```
|
|
825
|
+
*
|
|
826
|
+
* @template TDoc The TypeScript shape of a document in this collection.
|
|
827
|
+
* Defaults to `Record<string, unknown>` for untyped usage.
|
|
828
|
+
*/
|
|
829
|
+
interface CollectionConfig<TDoc extends object = Record<string, unknown>> {
|
|
830
|
+
/**
|
|
831
|
+
* Unique identifier for this collection.
|
|
832
|
+
* Used as the URL segment (`/api/collections/:slug`) and the database table/collection name.
|
|
833
|
+
* Use kebab-case, e.g. `'blog-posts'`.
|
|
834
|
+
*/
|
|
835
|
+
slug: string;
|
|
836
|
+
/**
|
|
837
|
+
* Restricts this collection to a specific site in a multi-tenant deployment.
|
|
838
|
+
* When set, only requests bearing a matching `X-Site-Id` header can access it.
|
|
839
|
+
*/
|
|
840
|
+
siteId?: string;
|
|
841
|
+
/**
|
|
842
|
+
* If `true`, this collection is shared across all sites in a multi-tenant
|
|
843
|
+
* deployment and accessible regardless of the `X-Site-Id` header.
|
|
844
|
+
*/
|
|
845
|
+
shared?: boolean;
|
|
846
|
+
/** Human-readable names for documents in this collection, shown in the Admin UI. */
|
|
847
|
+
labels?: {
|
|
848
|
+
singular: string;
|
|
849
|
+
plural: string;
|
|
850
|
+
};
|
|
851
|
+
/**
|
|
852
|
+
* If `true`, this collection is an **auth collection** — it gains
|
|
853
|
+
* `POST /api/collections/:slug/login` and `POST /api/collections/:slug/logout`
|
|
854
|
+
* endpoints, and documents are expected to have a `password` field.
|
|
855
|
+
*/
|
|
856
|
+
auth?: boolean;
|
|
857
|
+
/**
|
|
858
|
+
* If `true` (or a config object), this collection supports **file uploads**.
|
|
859
|
+
* Documents gain file-related fields (`url`, `filename`, `mimeType`, etc.)
|
|
860
|
+
* and the create endpoint accepts `multipart/form-data`.
|
|
861
|
+
*/
|
|
862
|
+
upload?: boolean | UploadConfig;
|
|
863
|
+
/** Field definitions that make up the document schema for this collection. */
|
|
864
|
+
fields: Field[];
|
|
865
|
+
/**
|
|
866
|
+
* If `true`, Dyrected automatically adds `createdAt` and `updatedAt`
|
|
867
|
+
* timestamp fields to every document. Defaults to `true`.
|
|
868
|
+
*/
|
|
869
|
+
timestamps?: boolean;
|
|
870
|
+
/**
|
|
871
|
+
* Initial documents to seed into this collection the first time it is
|
|
872
|
+
* fetched and found to be empty (e.g. for demo data or defaults).
|
|
873
|
+
*/
|
|
874
|
+
initialData?: Partial<TDoc>[];
|
|
875
|
+
/**
|
|
876
|
+
* If `true`, every create, update, and delete operation on this collection
|
|
877
|
+
* is logged to the `__audit` collection with before/after snapshots and the
|
|
878
|
+
* acting user's identity.
|
|
879
|
+
*/
|
|
880
|
+
audit?: boolean;
|
|
881
|
+
/**
|
|
882
|
+
* Collection-level access control.
|
|
883
|
+
*
|
|
884
|
+
* Each key is an operation; the value is a function (or Jexl string) that
|
|
885
|
+
* returns `true` to allow or `false` to deny. Returning a `where`-style
|
|
886
|
+
* object grants access only to matching documents.
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* access: {
|
|
890
|
+
* read: () => true, // public read
|
|
891
|
+
* create: ({ user }) => !!user, // logged-in users only
|
|
892
|
+
* update: ({ user }) => user?.roles?.includes('editor') ?? false,
|
|
893
|
+
* delete: ({ user }) => user?.roles?.includes('admin') ?? false,
|
|
894
|
+
* }
|
|
895
|
+
*/
|
|
896
|
+
access?: {
|
|
897
|
+
read?: AccessFunction<TDoc> | string;
|
|
898
|
+
create?: AccessFunction<TDoc> | string;
|
|
899
|
+
update?: AccessFunction<TDoc> | string;
|
|
900
|
+
delete?: AccessFunction<TDoc> | string;
|
|
901
|
+
};
|
|
902
|
+
/**
|
|
903
|
+
* Collection-level lifecycle hooks.
|
|
904
|
+
*
|
|
905
|
+
* Hooks run in the order they appear in the array. The return value of each
|
|
906
|
+
* hook is passed as the input to the next. Throwing inside any hook aborts
|
|
907
|
+
* the operation and returns a `500` error.
|
|
908
|
+
*
|
|
909
|
+
* See the [Hooks reference](/docs/concepts/hooks) for the full lifecycle diagram.
|
|
910
|
+
*/
|
|
911
|
+
hooks?: {
|
|
912
|
+
/**
|
|
913
|
+
* Runs before the database is queried. Return a modified `where` object
|
|
914
|
+
* to override the query filter.
|
|
915
|
+
*/
|
|
916
|
+
beforeRead?: CollectionBeforeReadHook[];
|
|
917
|
+
/**
|
|
918
|
+
* Runs after documents are fetched. Return a modified doc to change what
|
|
919
|
+
* the client receives. Runs on every document in a list response.
|
|
920
|
+
*/
|
|
921
|
+
afterRead?: CollectionAfterReadHook<TDoc>[];
|
|
922
|
+
/**
|
|
923
|
+
* Runs before create or update. Return modified data to change what is
|
|
924
|
+
* written to the database. Throw to abort the write entirely.
|
|
925
|
+
*/
|
|
926
|
+
beforeChange?: CollectionBeforeChangeHook<TDoc>[];
|
|
927
|
+
/**
|
|
928
|
+
* Runs after create or update is committed. For side-effects only —
|
|
929
|
+
* webhooks, cache busting, notifications. Return value is ignored.
|
|
930
|
+
*/
|
|
931
|
+
afterChange?: CollectionAfterChangeHook<TDoc>[];
|
|
932
|
+
/**
|
|
933
|
+
* Runs before a document is deleted. Throw to cancel the deletion.
|
|
934
|
+
*/
|
|
935
|
+
beforeDelete?: CollectionBeforeDeleteHook<TDoc>[];
|
|
936
|
+
/**
|
|
937
|
+
* Runs after a document has been deleted. For cleanup side-effects only.
|
|
938
|
+
*/
|
|
939
|
+
afterDelete?: CollectionAfterDeleteHook<TDoc>[];
|
|
940
|
+
};
|
|
941
|
+
/** Admin UI configuration for this collection. */
|
|
942
|
+
admin?: {
|
|
943
|
+
/**
|
|
944
|
+
* The field name used as the document's display title in the Admin list
|
|
945
|
+
* view and breadcrumbs. Defaults to `'title'` if the field exists.
|
|
946
|
+
*/
|
|
947
|
+
useAsTitle?: string;
|
|
948
|
+
/**
|
|
949
|
+
* Field names to show as columns in the Admin list view.
|
|
950
|
+
* Defaults to a sensible set of the first few non-structural fields.
|
|
951
|
+
*/
|
|
952
|
+
defaultColumns?: string[];
|
|
953
|
+
/**
|
|
954
|
+
* Groups this collection under a named section in the Admin sidebar.
|
|
955
|
+
* Collections with the same `group` are visually grouped together.
|
|
956
|
+
*/
|
|
957
|
+
group?: string;
|
|
958
|
+
/** If `true`, this collection is not shown in the Admin UI sidebar. */
|
|
959
|
+
hidden?: boolean;
|
|
960
|
+
/**
|
|
961
|
+
* URL to open in the Live Preview pane when editing a document.
|
|
962
|
+
* Pass a function to derive the URL from the document's fields.
|
|
963
|
+
*
|
|
964
|
+
* @example
|
|
965
|
+
* previewUrl: (doc) => `https://mysite.com/blog/${doc.slug}`
|
|
966
|
+
*/
|
|
967
|
+
previewUrl?: string | ((doc: TDoc, opts: {
|
|
968
|
+
locale?: string;
|
|
969
|
+
}) => string | null);
|
|
970
|
+
/**
|
|
971
|
+
* How the Live Preview pane communicates with the frontend.
|
|
972
|
+
* - `'postMessage'` (default) — sends a `postMessage` with the current doc data.
|
|
973
|
+
* - `'token'` — passes a short-lived preview token as a query parameter.
|
|
974
|
+
*/
|
|
975
|
+
previewMode?: "postMessage" | "token";
|
|
976
|
+
/**
|
|
977
|
+
* Frontend URL pattern for this collection, used by `url` fields to
|
|
978
|
+
* resolve internal links. Use `{fieldName}` placeholders.
|
|
979
|
+
*
|
|
980
|
+
* @example
|
|
981
|
+
* urlPattern: '/blog/{slug}' // → /blog/my-post
|
|
982
|
+
* urlPattern: '/{slug}' // → /about
|
|
983
|
+
*/
|
|
984
|
+
urlPattern?: string;
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Upload configuration for collections that store files.
|
|
989
|
+
* Set `upload: true` on the collection to use all defaults, or pass this
|
|
990
|
+
* object to customise allowed types, size limits, and image processing.
|
|
991
|
+
*/
|
|
992
|
+
interface UploadConfig {
|
|
993
|
+
/**
|
|
994
|
+
* Allowed MIME types. Requests with other MIME types are rejected with `400`.
|
|
995
|
+
* @example ['image/jpeg', 'image/png', 'image/webp', 'application/pdf']
|
|
996
|
+
*/
|
|
997
|
+
allowedMimeTypes?: string[];
|
|
998
|
+
/**
|
|
999
|
+
* Maximum file size in **bytes**.
|
|
1000
|
+
* @example 10 * 1024 * 1024 // 10 MB
|
|
1001
|
+
*/
|
|
1002
|
+
maxFileSize?: number;
|
|
1003
|
+
/**
|
|
1004
|
+
* Local filesystem path where files are stored.
|
|
1005
|
+
* Only used by the `LocalStorage` adapter.
|
|
1006
|
+
*/
|
|
1007
|
+
staticDir?: string;
|
|
1008
|
+
/**
|
|
1009
|
+
* Public URL prefix prepended to filenames when generating download URLs.
|
|
1010
|
+
* Only used by the `LocalStorage` adapter.
|
|
1011
|
+
* @example '/uploads'
|
|
1012
|
+
*/
|
|
1013
|
+
staticURL?: string;
|
|
1014
|
+
/**
|
|
1015
|
+
* The `imageSizes` entry name to use as the thumbnail in the Admin media grid.
|
|
1016
|
+
* @example 'thumbnail'
|
|
1017
|
+
*/
|
|
1018
|
+
adminThumbnail?: string;
|
|
1019
|
+
/**
|
|
1020
|
+
* Additional image sizes to generate when an image is uploaded.
|
|
1021
|
+
* Requires an `ImageService` to be configured (e.g. `@dyrected/image-sharp`).
|
|
1022
|
+
*
|
|
1023
|
+
* @example
|
|
1024
|
+
* imageSizes: [
|
|
1025
|
+
* { name: 'thumbnail', width: 300, height: 300, fit: 'cover' },
|
|
1026
|
+
* { name: 'card', width: 800 },
|
|
1027
|
+
* ]
|
|
1028
|
+
*/
|
|
1029
|
+
imageSizes?: {
|
|
1030
|
+
/** Identifier used to access this size, e.g. `doc.sizes.thumbnail`. */
|
|
1031
|
+
name: string;
|
|
1032
|
+
/** Target width in pixels. */
|
|
1033
|
+
width?: number;
|
|
1034
|
+
/** Target height in pixels. */
|
|
1035
|
+
height?: number;
|
|
1036
|
+
/** sharp crop strategy (`'entropy'`, `'attention'`, etc.). */
|
|
1037
|
+
crop?: string;
|
|
1038
|
+
/**
|
|
1039
|
+
* sharp fit strategy.
|
|
1040
|
+
* @see https://sharp.pixelplumbing.com/api-resize#parameters
|
|
1041
|
+
*/
|
|
1042
|
+
fit?: string;
|
|
1043
|
+
/**
|
|
1044
|
+
* If `true`, images smaller than the target size are not upscaled.
|
|
1045
|
+
* @default true
|
|
1046
|
+
*/
|
|
1047
|
+
withoutEnlargement?: boolean;
|
|
1048
|
+
/** Additional sharp format options forwarded to the output pipeline. */
|
|
1049
|
+
formatOptions?: Record<string, unknown>;
|
|
1050
|
+
}[];
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Defines a Dyrected global — a singleton document without pagination or IDs.
|
|
1054
|
+
*
|
|
1055
|
+
* Globals are ideal for site-wide settings, feature flags, or any data where
|
|
1056
|
+
* there is always exactly one record (e.g. `site-settings`, `navigation`, `theme`).
|
|
1057
|
+
*
|
|
1058
|
+
* Pass your document's TypeScript type as the generic parameter `TDoc` to get
|
|
1059
|
+
* fully typed hooks:
|
|
1060
|
+
*
|
|
1061
|
+
* ```ts
|
|
1062
|
+
* interface SiteSettings {
|
|
1063
|
+
* siteName: string
|
|
1064
|
+
* tagline: string
|
|
1065
|
+
* maintenanceMode: boolean
|
|
1066
|
+
* }
|
|
1067
|
+
*
|
|
1068
|
+
* export const Settings = defineGlobal<SiteSettings>({
|
|
1069
|
+
* slug: 'site-settings',
|
|
1070
|
+
* hooks: {
|
|
1071
|
+
* afterChange: [({ doc }) => {
|
|
1072
|
+
* // `doc` is typed as SiteSettings
|
|
1073
|
+
* if (doc.maintenanceMode) alertOnCall()
|
|
1074
|
+
* }],
|
|
1075
|
+
* },
|
|
1076
|
+
* fields: [...],
|
|
1077
|
+
* })
|
|
1078
|
+
* ```
|
|
1079
|
+
*
|
|
1080
|
+
* @template TDoc The TypeScript shape of this global's document.
|
|
1081
|
+
*/
|
|
1082
|
+
interface GlobalConfig<TDoc extends object = Record<string, unknown>> {
|
|
1083
|
+
/**
|
|
1084
|
+
* Unique identifier for this global.
|
|
1085
|
+
* Used as the URL segment (`/api/globals/:slug`) and the storage key.
|
|
1086
|
+
*/
|
|
1087
|
+
slug: string;
|
|
1088
|
+
/** Restricts this global to a specific site in a multi-tenant deployment. */
|
|
1089
|
+
siteId?: string;
|
|
1090
|
+
/**
|
|
1091
|
+
* If `true`, this global is shared across all sites in a multi-tenant
|
|
1092
|
+
* deployment.
|
|
1093
|
+
*/
|
|
1094
|
+
shared?: boolean;
|
|
1095
|
+
/** Human-readable label shown in the Admin UI sidebar. */
|
|
1096
|
+
label?: string;
|
|
1097
|
+
/** Field definitions for this global's document schema. */
|
|
1098
|
+
fields: Field[];
|
|
1099
|
+
/** Access control for reading and updating this global. */
|
|
1100
|
+
access?: {
|
|
1101
|
+
read?: AccessFunction<TDoc>;
|
|
1102
|
+
update?: AccessFunction<TDoc>;
|
|
1103
|
+
};
|
|
1104
|
+
/**
|
|
1105
|
+
* Global-level lifecycle hooks.
|
|
1106
|
+
* Globals support `beforeRead`, `afterRead`, `beforeChange`, and `afterChange`.
|
|
1107
|
+
* There are no delete hooks since globals cannot be deleted.
|
|
1108
|
+
*/
|
|
1109
|
+
hooks?: {
|
|
1110
|
+
beforeRead?: GlobalBeforeReadHook[];
|
|
1111
|
+
afterRead?: GlobalAfterReadHook<TDoc>[];
|
|
1112
|
+
beforeChange?: GlobalBeforeChangeHook<TDoc>[];
|
|
1113
|
+
afterChange?: GlobalAfterChangeHook<TDoc>[];
|
|
1114
|
+
};
|
|
1115
|
+
/** Admin UI configuration for this global. */
|
|
1116
|
+
admin?: {
|
|
1117
|
+
/** Groups this global under a named section in the Admin sidebar. */
|
|
1118
|
+
group?: string;
|
|
1119
|
+
/** If `true`, this global is not shown in the Admin UI sidebar. */
|
|
1120
|
+
hidden?: boolean;
|
|
1121
|
+
};
|
|
1122
|
+
/**
|
|
1123
|
+
* Initial data to seed this global with the first time it is fetched and
|
|
1124
|
+
* found to be empty.
|
|
1125
|
+
*/
|
|
1126
|
+
initialData?: Partial<TDoc>;
|
|
1127
|
+
}
|
|
1128
|
+
/**
|
|
1129
|
+
* The minimum shape of every document returned by the database layer.
|
|
1130
|
+
*
|
|
1131
|
+
* All documents have an `id` field assigned by the adapter. Additional fields
|
|
1132
|
+
* are stored as `unknown` until you narrow them with your own interface.
|
|
1133
|
+
*
|
|
1134
|
+
* Use this as the base when declaring your collection document types:
|
|
1135
|
+
* ```ts
|
|
1136
|
+
* interface Post extends BaseDocument {
|
|
1137
|
+
* title: string
|
|
1138
|
+
* slug: string
|
|
1139
|
+
* }
|
|
1140
|
+
* ```
|
|
1141
|
+
*/
|
|
1142
|
+
interface BaseDocument {
|
|
1143
|
+
/** The document's unique identifier, assigned by the database adapter. */
|
|
1144
|
+
id: string;
|
|
1145
|
+
[key: string]: unknown;
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* The envelope returned by collection list endpoints (`GET /api/collections/:slug`).
|
|
1149
|
+
*
|
|
1150
|
+
* @template T The document type.
|
|
1151
|
+
*/
|
|
1152
|
+
interface PaginatedResult<T = Record<string, unknown>> {
|
|
1153
|
+
/** The documents on the current page. */
|
|
1154
|
+
docs: T[];
|
|
1155
|
+
/** Total number of documents matching the query (across all pages). */
|
|
1156
|
+
total: number;
|
|
1157
|
+
/** Maximum number of documents per page as requested. */
|
|
1158
|
+
limit: number;
|
|
1159
|
+
/** The current page number (1-indexed). */
|
|
1160
|
+
page: number;
|
|
1161
|
+
/** Total number of pages given the current `limit`. */
|
|
1162
|
+
totalPages: number;
|
|
1163
|
+
/** Whether a next page exists. */
|
|
1164
|
+
hasNextPage: boolean;
|
|
1165
|
+
/** Whether a previous page exists. */
|
|
1166
|
+
hasPrevPage: boolean;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* The interface every database adapter must implement.
|
|
1170
|
+
*
|
|
1171
|
+
* Dyrected ships adapters for PostgreSQL, MySQL, SQLite, and MongoDB.
|
|
1172
|
+
* Implement this interface to connect any other database.
|
|
1173
|
+
*/
|
|
1174
|
+
interface DatabaseAdapter {
|
|
1175
|
+
/** Find a paginated list of documents in a collection. */
|
|
1176
|
+
find(args: {
|
|
1177
|
+
collection: string;
|
|
1178
|
+
where?: Record<string, unknown>;
|
|
1179
|
+
limit?: number;
|
|
1180
|
+
page?: number;
|
|
1181
|
+
sort?: string;
|
|
1182
|
+
}): Promise<PaginatedResult>;
|
|
1183
|
+
/** Find a single document by its ID. Returns `null` if not found. */
|
|
1184
|
+
findOne(args: {
|
|
1185
|
+
collection: string;
|
|
1186
|
+
id: string;
|
|
1187
|
+
}): Promise<BaseDocument | null>;
|
|
1188
|
+
/** Insert a new document and return it with its generated `id`. */
|
|
1189
|
+
create(args: {
|
|
1190
|
+
collection: string;
|
|
1191
|
+
data: Record<string, unknown>;
|
|
1192
|
+
}): Promise<BaseDocument>;
|
|
1193
|
+
/** Update a document by ID and return the updated document. */
|
|
1194
|
+
update(args: {
|
|
1195
|
+
collection: string;
|
|
1196
|
+
id: string;
|
|
1197
|
+
data: Record<string, unknown>;
|
|
1198
|
+
}): Promise<BaseDocument>;
|
|
1199
|
+
/** Delete a document by ID. Return value is intentionally untyped — callers do not use it. */
|
|
1200
|
+
delete(args: {
|
|
1201
|
+
collection: string;
|
|
1202
|
+
id: string;
|
|
1203
|
+
}): Promise<unknown>;
|
|
1204
|
+
/** Fetch the singleton document for a global. Returns `null` if not yet initialised. */
|
|
1205
|
+
getGlobal(args: {
|
|
1206
|
+
slug: string;
|
|
1207
|
+
}): Promise<Record<string, unknown> | null>;
|
|
1208
|
+
/** Create or replace the singleton document for a global. */
|
|
1209
|
+
updateGlobal(args: {
|
|
1210
|
+
slug: string;
|
|
1211
|
+
data: Record<string, unknown>;
|
|
1212
|
+
}): Promise<Record<string, unknown>>;
|
|
1213
|
+
/**
|
|
1214
|
+
* Sync the database schema with the current collection and global configs.
|
|
1215
|
+
* Called on startup to create tables/collections that don't exist yet.
|
|
1216
|
+
* Not all adapters implement this (e.g. MongoDB is schema-less).
|
|
1217
|
+
*/
|
|
1218
|
+
sync?(collections: CollectionConfig[], globals: GlobalConfig[]): Promise<void>;
|
|
1219
|
+
/**
|
|
1220
|
+
* Execute a raw SQL query or database command.
|
|
1221
|
+
* Optional — not all adapters support raw access.
|
|
1222
|
+
*/
|
|
1223
|
+
execute?(query: string, params?: unknown[]): Promise<unknown>;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Metadata returned after a file is uploaded and stored.
|
|
1227
|
+
* Stored on the document in upload collections.
|
|
1228
|
+
*/
|
|
1229
|
+
interface FileData {
|
|
1230
|
+
filename: string;
|
|
1231
|
+
filesize?: number;
|
|
1232
|
+
mimeType: string;
|
|
1233
|
+
/** Public URL of the stored file. */
|
|
1234
|
+
url: string;
|
|
1235
|
+
width?: number;
|
|
1236
|
+
height?: number;
|
|
1237
|
+
focalPoint?: {
|
|
1238
|
+
x: number;
|
|
1239
|
+
y: number;
|
|
1240
|
+
};
|
|
1241
|
+
/** Base64-encoded BlurHash string for progressive image loading. */
|
|
1242
|
+
blurhash?: string;
|
|
1243
|
+
/** `'upload'` for server-stored files; `'external'` for provider-managed files. */
|
|
1244
|
+
type?: "upload" | "external";
|
|
1245
|
+
provider?: string;
|
|
1246
|
+
provider_metadata?: unknown;
|
|
1247
|
+
[key: string]: unknown;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* The interface every storage adapter must implement.
|
|
1251
|
+
*
|
|
1252
|
+
* Dyrected ships adapters for local disk, S3, Cloudflare R2, Cloudinary, and
|
|
1253
|
+
* Backblaze B2. Implement this interface to use any other storage provider.
|
|
1254
|
+
*/
|
|
1255
|
+
interface StorageAdapter {
|
|
1256
|
+
/**
|
|
1257
|
+
* Upload a file and return its metadata (URL, dimensions, etc.).
|
|
1258
|
+
* The `prefix` is a path prefix used for multi-tenant setups.
|
|
1259
|
+
*/
|
|
1260
|
+
upload(args: {
|
|
1261
|
+
filename: string;
|
|
1262
|
+
buffer: Uint8Array;
|
|
1263
|
+
mimeType: string;
|
|
1264
|
+
prefix?: string;
|
|
1265
|
+
}): Promise<FileData>;
|
|
1266
|
+
/** Delete a file by its stored filename. */
|
|
1267
|
+
delete(args: {
|
|
1268
|
+
filename: string;
|
|
1269
|
+
}): Promise<void>;
|
|
1270
|
+
/** Return the public URL for a stored file. */
|
|
1271
|
+
getURL(args: {
|
|
1272
|
+
filename: string;
|
|
1273
|
+
}): string;
|
|
1274
|
+
/**
|
|
1275
|
+
* Retrieve the file's raw bytes and MIME type for serving via the API.
|
|
1276
|
+
* Only needed by adapters that serve files through the Dyrected API
|
|
1277
|
+
* (e.g. `LocalStorage`). Cloud adapters return `null` here and rely on
|
|
1278
|
+
* direct CDN URLs instead.
|
|
1279
|
+
*/
|
|
1280
|
+
resolve?(args: {
|
|
1281
|
+
filename: string;
|
|
1282
|
+
}): Promise<{
|
|
1283
|
+
buffer: Uint8Array;
|
|
1284
|
+
mimeType: string;
|
|
1285
|
+
} | null>;
|
|
1286
|
+
}
|
|
1287
|
+
/**
|
|
1288
|
+
* Processes uploaded images — generates metadata (dimensions, BlurHash) and
|
|
1289
|
+
* produces resized variants defined in `UploadConfig.imageSizes`.
|
|
1290
|
+
*
|
|
1291
|
+
* @example
|
|
1292
|
+
* import { SharpImageService } from '@dyrected/image-sharp'
|
|
1293
|
+
* defineConfig({ image: new SharpImageService(), ... })
|
|
1294
|
+
*/
|
|
1295
|
+
interface ImageService {
|
|
1296
|
+
process(args: {
|
|
1297
|
+
buffer: Uint8Array;
|
|
1298
|
+
mimeType: string;
|
|
1299
|
+
config?: CollectionConfig["upload"];
|
|
1300
|
+
focalPoint?: {
|
|
1301
|
+
x: number;
|
|
1302
|
+
y: number;
|
|
1303
|
+
};
|
|
1304
|
+
}): Promise<{
|
|
1305
|
+
metadata: {
|
|
1306
|
+
width?: number;
|
|
1307
|
+
height?: number;
|
|
1308
|
+
/** Base64-encoded BlurHash for progressive loading. */
|
|
1309
|
+
blurhash?: string;
|
|
1310
|
+
};
|
|
1311
|
+
/** Generated image sizes keyed by their `name`. */
|
|
1312
|
+
sizes?: Record<string, {
|
|
1313
|
+
buffer: Uint8Array;
|
|
1314
|
+
width: number;
|
|
1315
|
+
height: number;
|
|
1316
|
+
filename: string;
|
|
1317
|
+
}>;
|
|
1318
|
+
}>;
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Branding and metadata options for the Dyrected Admin UI.
|
|
1322
|
+
*
|
|
1323
|
+
* @example
|
|
1324
|
+
* admin: {
|
|
1325
|
+
* branding: {
|
|
1326
|
+
* logo: '/logo.svg',
|
|
1327
|
+
* primaryColor: '#6366f1',
|
|
1328
|
+
* },
|
|
1329
|
+
* meta: {
|
|
1330
|
+
* titleSuffix: '- My App',
|
|
1331
|
+
* },
|
|
1332
|
+
* }
|
|
1333
|
+
*/
|
|
1334
|
+
interface AdminConfig {
|
|
1335
|
+
branding?: {
|
|
1336
|
+
/** Full logo image shown in the expanded sidebar. URL or imported image asset. */
|
|
1337
|
+
logo?: string;
|
|
1338
|
+
/** Compact logo mark used in the collapsed sidebar state. */
|
|
1339
|
+
logoMark?: string;
|
|
1340
|
+
/**
|
|
1341
|
+
* Primary accent colour as any CSS colour value.
|
|
1342
|
+
* @example '#6366f1'
|
|
1343
|
+
* @example 'hsl(240 50% 60%)'
|
|
1344
|
+
*/
|
|
1345
|
+
primaryColor?: string;
|
|
1346
|
+
/** Browser tab favicon URL. */
|
|
1347
|
+
favicon?: string;
|
|
1348
|
+
/** Font family for body and UI text. Must be loaded separately. */
|
|
1349
|
+
fontSans?: string;
|
|
1350
|
+
/** Font family for headings. Must be loaded separately. */
|
|
1351
|
+
fontSerif?: string;
|
|
1352
|
+
};
|
|
1353
|
+
meta?: {
|
|
1354
|
+
/**
|
|
1355
|
+
* String appended to every Admin page's `<title>`.
|
|
1356
|
+
* @default '- Dyrected'
|
|
1357
|
+
*/
|
|
1358
|
+
titleSuffix?: string;
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Collapses intersection types into a flat object type for readable IDE tooltips.
|
|
1363
|
+
*
|
|
1364
|
+
* @example
|
|
1365
|
+
* type T = Prettify<{ a: string } & { b: number }>
|
|
1366
|
+
* // → { a: string; b: number }
|
|
1367
|
+
*/
|
|
1368
|
+
type Prettify<T> = {
|
|
1369
|
+
[K in keyof T]: T[K];
|
|
1370
|
+
};
|
|
1371
|
+
/** Maps a field's runtime type tag to its TypeScript value type. @internal */
|
|
1372
|
+
type FieldValueType<F extends Field> = F['type'] extends 'text' | 'textarea' | 'email' | 'url' | 'icon' | 'date' | 'select' | 'radio' ? string : F['type'] extends 'number' ? number : F['type'] extends 'boolean' ? boolean : F['type'] extends 'multiSelect' ? string[] : F['type'] extends 'relationship' ? F extends {
|
|
1373
|
+
hasMany: true;
|
|
1374
|
+
} ? string[] : string : F['type'] extends 'image' ? string : F['type'] extends 'richText' | 'json' ? Record<string, unknown> : F['type'] extends 'object' ? F extends {
|
|
1375
|
+
fields: infer SF extends readonly Field[];
|
|
1376
|
+
} ? Prettify<InferDocShape<SF>> : Record<string, unknown> : F['type'] extends 'array' ? F extends {
|
|
1377
|
+
fields: infer SF extends readonly Field[];
|
|
1378
|
+
} ? Array<Prettify<InferDocShape<SF>>> : unknown[] : F['type'] extends 'blocks' ? F extends {
|
|
1379
|
+
blocks: infer B extends readonly Block[];
|
|
1380
|
+
} ? Array<InferBlocksUnion<B>> : Array<{
|
|
1381
|
+
blockType: string;
|
|
1382
|
+
} & Record<string, unknown>> : unknown;
|
|
1383
|
+
/** Build a discriminated-union type covering all possible block shapes. @internal */
|
|
1384
|
+
type InferBlocksUnion<Blocks extends readonly Block[]> = Blocks extends readonly [infer B extends Block, ...infer Rest extends readonly Block[]] ? B['fields'] extends readonly Field[] ? ({
|
|
1385
|
+
blockType: B['slug'];
|
|
1386
|
+
} & Prettify<InferDocShape<B['fields']>>) | InferBlocksUnion<Rest> : ({
|
|
1387
|
+
blockType: B['slug'];
|
|
1388
|
+
} & Record<string, unknown>) | InferBlocksUnion<Rest> : never;
|
|
1389
|
+
/** Converts one field definition to its corresponding document key/value pair. @internal */
|
|
1390
|
+
type InferFieldEntry<F extends Field> = F extends {
|
|
1391
|
+
type: 'row';
|
|
1392
|
+
fields: infer SF extends readonly Field[];
|
|
1393
|
+
} ? InferDocShape<SF> : F extends {
|
|
1394
|
+
type: 'join';
|
|
1395
|
+
} ? Record<never, never> : F extends {
|
|
1396
|
+
name: infer N extends string;
|
|
1397
|
+
required: true;
|
|
1398
|
+
} ? {
|
|
1399
|
+
[K in N]: FieldValueType<F>;
|
|
1400
|
+
} : F extends {
|
|
1401
|
+
name: infer N extends string;
|
|
1402
|
+
} ? {
|
|
1403
|
+
[K in N]?: FieldValueType<F>;
|
|
1404
|
+
} : Record<never, never>;
|
|
1405
|
+
/**
|
|
1406
|
+
* Infer a document shape from a literal fields tuple.
|
|
1407
|
+
*
|
|
1408
|
+
* This is used automatically by {@link defineCollection} and {@link defineGlobal}
|
|
1409
|
+
* when no explicit `TDoc` type argument is provided — TypeScript derives the
|
|
1410
|
+
* document type straight from the `fields` array you write inline.
|
|
1411
|
+
*
|
|
1412
|
+
* For collections, the inferred type is wrapped with `{ id: string }` (the
|
|
1413
|
+
* database adapter always stamps an `id` on created documents). Globals don't
|
|
1414
|
+
* get an `id`.
|
|
1415
|
+
*
|
|
1416
|
+
* @example
|
|
1417
|
+
* const fields = [
|
|
1418
|
+
* { name: 'title', type: 'text', required: true },
|
|
1419
|
+
* { name: 'published', type: 'boolean' },
|
|
1420
|
+
* ] as const satisfies Field[];
|
|
1421
|
+
*
|
|
1422
|
+
* type Doc = InferDocShape<typeof fields>;
|
|
1423
|
+
* // → { title: string; published?: boolean }
|
|
1424
|
+
*/
|
|
1425
|
+
type InferDocShape<Fields extends readonly Field[]> = Fields extends readonly [] ? Record<never, never> : Fields extends readonly [infer Head extends Field, ...infer Tail extends readonly Field[]] ? InferFieldEntry<Head> & InferDocShape<Tail> : Record<string, unknown>;
|
|
1426
|
+
/**
|
|
1427
|
+
* Audit fields automatically injected into every collection document at runtime.
|
|
1428
|
+
* They are always present in API responses but hidden in the Admin UI by default.
|
|
1429
|
+
*/
|
|
1430
|
+
type SystemDocFields = {
|
|
1431
|
+
createdAt?: string;
|
|
1432
|
+
updatedAt?: string;
|
|
1433
|
+
/** ID of the user who created the document. */
|
|
1434
|
+
createdBy?: string;
|
|
1435
|
+
/** ID of the user who last updated the document. */
|
|
1436
|
+
updatedBy?: string;
|
|
1437
|
+
};
|
|
1438
|
+
/**
|
|
1439
|
+
* Fields automatically injected into collections with `auth: true`.
|
|
1440
|
+
* `password` is part of the schema but is stripped from API read responses.
|
|
1441
|
+
*/
|
|
1442
|
+
type AuthDocFields = {
|
|
1443
|
+
email: string;
|
|
1444
|
+
password?: string;
|
|
1445
|
+
roles?: string;
|
|
1446
|
+
};
|
|
1447
|
+
/**
|
|
1448
|
+
* Fields automatically added to upload/media collection documents.
|
|
1449
|
+
* These mirror the `FileData` interface returned by storage adapters.
|
|
1450
|
+
*/
|
|
1451
|
+
type UploadDocFields = {
|
|
1452
|
+
filename: string;
|
|
1453
|
+
filesize?: number;
|
|
1454
|
+
mimeType: string;
|
|
1455
|
+
/** Public URL of the stored file. */
|
|
1456
|
+
url: string;
|
|
1457
|
+
width?: number;
|
|
1458
|
+
height?: number;
|
|
1459
|
+
focalPoint?: {
|
|
1460
|
+
x: number;
|
|
1461
|
+
y: number;
|
|
1462
|
+
};
|
|
1463
|
+
/** Base64 BlurHash for progressive image loading. */
|
|
1464
|
+
blurhash?: string;
|
|
1465
|
+
sizes?: Record<string, {
|
|
1466
|
+
filename?: string;
|
|
1467
|
+
url?: string;
|
|
1468
|
+
width?: number;
|
|
1469
|
+
height?: number;
|
|
1470
|
+
}>;
|
|
1471
|
+
};
|
|
1472
|
+
/**
|
|
1473
|
+
* The root configuration object passed to `createDyrectedApp`.
|
|
1474
|
+
*
|
|
1475
|
+
* This is the single source of truth for your entire Dyrected instance —
|
|
1476
|
+
* collections, globals, database adapter, storage, email, and more.
|
|
1477
|
+
*
|
|
1478
|
+
* @example
|
|
1479
|
+
* import { defineConfig } from '@dyrected/core'
|
|
1480
|
+
* import { SQLiteAdapter } from '@dyrected/db-sqlite'
|
|
1481
|
+
*
|
|
1482
|
+
* export default defineConfig({
|
|
1483
|
+
* db: new SQLiteAdapter({ filename: './db.sqlite' }),
|
|
1484
|
+
* collections: [Posts, Users],
|
|
1485
|
+
* globals: [SiteSettings],
|
|
1486
|
+
* })
|
|
1487
|
+
*/
|
|
1488
|
+
interface DyrectedConfig {
|
|
1489
|
+
/** Collection definitions. Each collection maps to a database table/collection. */
|
|
1490
|
+
collections: CollectionConfig<any>[];
|
|
1491
|
+
/** Global (singleton) definitions. Each global maps to a single document. */
|
|
1492
|
+
globals: GlobalConfig<any>[];
|
|
1493
|
+
/**
|
|
1494
|
+
* The database adapter. Required for all data operations.
|
|
1495
|
+
* @see {@link DatabaseAdapter}
|
|
1496
|
+
*/
|
|
1497
|
+
db?: DatabaseAdapter;
|
|
1498
|
+
/**
|
|
1499
|
+
* The storage adapter for file uploads.
|
|
1500
|
+
* Required when any collection has `upload: true`.
|
|
1501
|
+
* @see {@link StorageAdapter}
|
|
1502
|
+
*/
|
|
1503
|
+
storage?: StorageAdapter;
|
|
1504
|
+
/**
|
|
1505
|
+
* The image processing service. Required when any upload collection
|
|
1506
|
+
* defines `imageSizes`.
|
|
1507
|
+
* @see {@link ImageService}
|
|
1508
|
+
*/
|
|
1509
|
+
image?: ImageService;
|
|
1510
|
+
/** Admin UI branding and metadata. */
|
|
1511
|
+
admin?: AdminConfig;
|
|
1512
|
+
/**
|
|
1513
|
+
* Email transport configuration. Required for welcome emails, password
|
|
1514
|
+
* resets, and invite links.
|
|
1515
|
+
*
|
|
1516
|
+
* @example
|
|
1517
|
+
* email: {
|
|
1518
|
+
* from: 'no-reply@myapp.com',
|
|
1519
|
+
* send: async ({ to, subject, html }) => {
|
|
1520
|
+
* await resend.emails.send({ from, to, subject, html })
|
|
1521
|
+
* },
|
|
1522
|
+
* }
|
|
1523
|
+
*/
|
|
1524
|
+
email?: {
|
|
1525
|
+
/** The `From` address for all outbound emails. */
|
|
1526
|
+
from: string;
|
|
1527
|
+
/** The send function. Wire in any email provider (Resend, SendGrid, SES, etc.). */
|
|
1528
|
+
send: (args: {
|
|
1529
|
+
to: string;
|
|
1530
|
+
subject: string;
|
|
1531
|
+
html: string;
|
|
1532
|
+
}) => Promise<void>;
|
|
1533
|
+
/** Override the default email templates. */
|
|
1534
|
+
templates?: {
|
|
1535
|
+
welcome?: (args: {
|
|
1536
|
+
email: string;
|
|
1537
|
+
}) => {
|
|
1538
|
+
subject?: string;
|
|
1539
|
+
html: string;
|
|
1540
|
+
};
|
|
1541
|
+
invite?: (args: {
|
|
1542
|
+
token: string;
|
|
1543
|
+
invitedByEmail?: string;
|
|
1544
|
+
}) => {
|
|
1545
|
+
subject?: string;
|
|
1546
|
+
html: string;
|
|
1547
|
+
};
|
|
1548
|
+
resetPassword?: (args: {
|
|
1549
|
+
token: string;
|
|
1550
|
+
}) => {
|
|
1551
|
+
subject?: string;
|
|
1552
|
+
html: string;
|
|
1553
|
+
};
|
|
1554
|
+
passwordChanged?: (args: {
|
|
1555
|
+
email: string;
|
|
1556
|
+
}) => {
|
|
1557
|
+
subject?: string;
|
|
1558
|
+
html: string;
|
|
1559
|
+
};
|
|
1560
|
+
};
|
|
1561
|
+
};
|
|
1562
|
+
/**
|
|
1563
|
+
* Redis connection URL. Required for distributed caching of dynamic option
|
|
1564
|
+
* resolvers and other server-side caches in multi-instance deployments.
|
|
1565
|
+
*
|
|
1566
|
+
* @example
|
|
1567
|
+
* redis: { url: process.env.REDIS_URL }
|
|
1568
|
+
*/
|
|
1569
|
+
redis?: {
|
|
1570
|
+
url: string;
|
|
1571
|
+
};
|
|
1572
|
+
/**
|
|
1573
|
+
* Cross-Origin Resource Sharing (CORS) configuration.
|
|
1574
|
+
* List all origins that are allowed to call the Dyrected API.
|
|
1575
|
+
*
|
|
1576
|
+
* @example
|
|
1577
|
+
* cors: { origins: ['https://myapp.com', 'https://www.myapp.com'] }
|
|
1578
|
+
*/
|
|
1579
|
+
cors?: {
|
|
1580
|
+
origins: string[];
|
|
1581
|
+
};
|
|
1582
|
+
/**
|
|
1583
|
+
* Callback to dynamically fetch additional collections and globals for a
|
|
1584
|
+
* given site ID at request time. Used in multi-tenant deployments where each
|
|
1585
|
+
* site has its own schema stored in the database.
|
|
1586
|
+
*
|
|
1587
|
+
* @param siteId The `X-Site-Id` header value from the incoming request.
|
|
1588
|
+
* @returns Extra collections and globals to merge into the config for this request.
|
|
1589
|
+
*
|
|
1590
|
+
* @example
|
|
1591
|
+
* onSchemaFetch: async (siteId) => {
|
|
1592
|
+
* const site = await db.findOne({ collection: 'sites', id: siteId })
|
|
1593
|
+
* return buildSchemaFromSiteConfig(site)
|
|
1594
|
+
* }
|
|
1595
|
+
*/
|
|
1596
|
+
onSchemaFetch?: (siteId: string) => Promise<{
|
|
1597
|
+
collections?: CollectionConfig<any>[];
|
|
1598
|
+
globals?: GlobalConfig<any>[];
|
|
1599
|
+
}>;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
interface DyrectedContext {
|
|
1603
|
+
Variables: {
|
|
1604
|
+
config: DyrectedConfig;
|
|
1605
|
+
siteId?: string;
|
|
1606
|
+
workspaceId?: string;
|
|
1607
|
+
/** Decoded JWT payload set by requireAuth() or optionalAuth() middleware. */
|
|
1608
|
+
user?: {
|
|
1609
|
+
sub: string;
|
|
1610
|
+
email: string;
|
|
1611
|
+
collection: string;
|
|
1612
|
+
[key: string]: any;
|
|
1613
|
+
};
|
|
1614
|
+
};
|
|
1615
|
+
}
|
|
1616
|
+
/**
|
|
1617
|
+
* Create the main Dyrected Hono application.
|
|
1618
|
+
*/
|
|
1619
|
+
declare function createDyrectedApp(rawConfig: DyrectedConfig): Promise<Hono<DyrectedContext, hono_types.BlankSchema, "/">>;
|
|
1620
|
+
|
|
1621
|
+
export { type AuthDocFields as A, type BaseDocument as B, type CollectionConfig as C, type DyrectedConfig as D, type ImageService as E, type Field as F, type GlobalConfig as G, type HookFunction as H, type InferDocShape as I, type PaginatedResult as J, type StorageAdapter as K, type UploadConfig as L, createDyrectedApp as M, type Prettify as P, type SystemDocFields as S, type UploadDocFields as U, type AccessFunction as a, type AdminConfig as b, type AuthenticatedUser as c, type Block as d, type CollectionAfterChangeHook as e, type CollectionAfterDeleteHook as f, type CollectionAfterReadHook as g, type CollectionBeforeChangeHook as h, type CollectionBeforeDeleteHook as i, type CollectionBeforeReadHook as j, type DatabaseAdapter as k, type DynamicOptionItem as l, type DynamicOptionsConfig as m, type DynamicOptionsResolver as n, type DynamicOptionsResolverArgs as o, type DyrectedContext as p, type FieldAfterReadHook as q, type FieldBeforeChangeHook as r, type FieldHook as s, type FieldType as t, type FileData as u, type GlobalAfterChangeHook as v, type GlobalAfterReadHook as w, type GlobalBeforeChangeHook as x, type GlobalBeforeReadHook as y, type HookRequestContext as z };
|