@dyrected/core 2.5.60 → 2.5.61
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-config-9hs8DRED.d.cts +1718 -0
- package/dist/app-config-9hs8DRED.d.ts +1718 -0
- package/dist/app-config-C0uxEU7Q.d.cts +1874 -0
- package/dist/app-config-C0uxEU7Q.d.ts +1874 -0
- package/dist/app-config-CBOn8IyZ.d.cts +1838 -0
- package/dist/app-config-CBOn8IyZ.d.ts +1838 -0
- package/dist/app-config-CwaU1de2.d.cts +1868 -0
- package/dist/app-config-CwaU1de2.d.ts +1868 -0
- package/dist/app-config-DVdSospO.d.cts +1842 -0
- package/dist/app-config-DVdSospO.d.ts +1842 -0
- package/dist/app-config-ouBRb6Bf.d.cts +1716 -0
- package/dist/app-config-ouBRb6Bf.d.ts +1716 -0
- package/dist/chunk-35NM2WPO.js +1658 -0
- package/dist/chunk-CUOPCOU2.js +1644 -0
- package/dist/index.cjs +68 -5
- package/dist/index.d.cts +46 -4
- package/dist/index.d.ts +46 -4
- package/dist/index.js +35 -4
- package/dist/server.cjs +44 -5
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +13 -4
- package/package.json +1 -1
|
@@ -0,0 +1,1658 @@
|
|
|
1
|
+
// src/workflows.ts
|
|
2
|
+
var WORKFLOW_HISTORY_COLLECTION = "__workflow_history";
|
|
3
|
+
var LIFECYCLE_EVENTS_COLLECTION = "__lifecycle_events";
|
|
4
|
+
function publishingWorkflow() {
|
|
5
|
+
return {
|
|
6
|
+
initialState: "draft",
|
|
7
|
+
draftState: "draft",
|
|
8
|
+
states: [
|
|
9
|
+
{ name: "draft", label: "Draft", color: "neutral" },
|
|
10
|
+
{ name: "in_review", label: "In review", color: "warning" },
|
|
11
|
+
{ name: "published", label: "Published", color: "success", published: true }
|
|
12
|
+
],
|
|
13
|
+
transitions: [
|
|
14
|
+
{ name: "submit", label: "Submit for review", from: "draft", to: "in_review", requiredCapabilities: ["entry.submit"] },
|
|
15
|
+
{ name: "publish", label: "Publish", from: "in_review", to: "published", requiredCapabilities: ["entry.publish"] },
|
|
16
|
+
{ name: "reject", label: "Request changes", from: "in_review", to: "draft", requiredCapabilities: ["entry.publish"], requireComment: true },
|
|
17
|
+
{ name: "unpublish", label: "Unpublish", from: "published", to: "draft", requiredCapabilities: ["entry.unpublish"], unpublish: true }
|
|
18
|
+
],
|
|
19
|
+
roles: [
|
|
20
|
+
{ role: "editor", capabilities: ["entry.edit", "entry.submit"] },
|
|
21
|
+
{ role: "publisher", capabilities: ["entry.edit", "entry.submit", "entry.publish", "entry.unpublish"] },
|
|
22
|
+
{ role: "admin", capabilities: ["entry.edit", "entry.submit", "entry.publish", "entry.unpublish"] }
|
|
23
|
+
]
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function simplePublishingWorkflow() {
|
|
27
|
+
return {
|
|
28
|
+
initialState: "draft",
|
|
29
|
+
draftState: "draft",
|
|
30
|
+
states: [
|
|
31
|
+
{ name: "draft", label: "Draft", color: "neutral" },
|
|
32
|
+
{ name: "published", label: "Published", color: "success", published: true }
|
|
33
|
+
],
|
|
34
|
+
transitions: [
|
|
35
|
+
{ name: "publish", label: "Publish", from: "draft", to: "published" },
|
|
36
|
+
{ name: "unpublish", label: "Unpublish", from: "published", to: "draft", unpublish: true }
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function publicMetadata(meta) {
|
|
41
|
+
const { availableTransitions: _availableTransitions, ...safe } = meta;
|
|
42
|
+
return safe;
|
|
43
|
+
}
|
|
44
|
+
function workflowCapabilities(workflow, user) {
|
|
45
|
+
const capabilities = new Set(Array.isArray(user?.capabilities) ? user.capabilities : []);
|
|
46
|
+
const roles = Array.isArray(user?.roles) ? user.roles : [];
|
|
47
|
+
for (const mapping of workflow.roles ?? []) {
|
|
48
|
+
if (roles.includes(mapping.role)) mapping.capabilities.forEach((capability) => capabilities.add(capability));
|
|
49
|
+
}
|
|
50
|
+
return capabilities;
|
|
51
|
+
}
|
|
52
|
+
function canViewWorkflowDraft(workflow, user) {
|
|
53
|
+
if (!user) return false;
|
|
54
|
+
const capabilities = workflowCapabilities(workflow, user);
|
|
55
|
+
if (capabilities.has("entry.edit")) return true;
|
|
56
|
+
return workflow.transitions.some(
|
|
57
|
+
(transition) => (transition.requiredCapabilities ?? []).some((capability) => capabilities.has(capability))
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function availableWorkflowTransitions(workflow, state, user) {
|
|
61
|
+
const capabilities = workflowCapabilities(workflow, user);
|
|
62
|
+
return workflow.transitions.filter((transition) => {
|
|
63
|
+
const from = Array.isArray(transition.from) ? transition.from : [transition.from];
|
|
64
|
+
return from.includes(state) && (transition.requiredCapabilities ?? []).every((item) => capabilities.has(item));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function initializeWorkflowDocument(data, workflow) {
|
|
68
|
+
return {
|
|
69
|
+
...data,
|
|
70
|
+
__workflow: { state: workflow.initialState, revision: 1 }
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function publishedStateName(workflow) {
|
|
74
|
+
return workflow.states.find((state) => state.published)?.name ?? workflow.states[workflow.states.length - 1]?.name ?? workflow.initialState;
|
|
75
|
+
}
|
|
76
|
+
function materializeWorkflowDocument(doc, workflow, user) {
|
|
77
|
+
const meta = doc.__workflow;
|
|
78
|
+
if (!meta) {
|
|
79
|
+
const { __published: _legacyPublished, __workflow: _legacyWorkflow, ...working2 } = doc;
|
|
80
|
+
const state = publishedStateName(workflow);
|
|
81
|
+
return {
|
|
82
|
+
...working2,
|
|
83
|
+
_workflow: {
|
|
84
|
+
state,
|
|
85
|
+
revision: 1,
|
|
86
|
+
availableTransitions: availableWorkflowTransitions(workflow, state, user).map((item) => item.name)
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const { __published, __workflow, ...working } = doc;
|
|
91
|
+
if (!canViewWorkflowDraft(workflow, user)) {
|
|
92
|
+
if (!__published || typeof __published !== "object") return null;
|
|
93
|
+
return { id: doc.id, ...__published, _workflow: publicMetadata(meta) };
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
...working,
|
|
97
|
+
_workflow: {
|
|
98
|
+
...meta,
|
|
99
|
+
availableTransitions: availableWorkflowTransitions(workflow, meta.state, user).map((item) => item.name)
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function eventId() {
|
|
104
|
+
return globalThis.crypto?.randomUUID?.() ?? `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
105
|
+
}
|
|
106
|
+
function createLifecycleEvent(args) {
|
|
107
|
+
return {
|
|
108
|
+
id: eventId(),
|
|
109
|
+
name: args.name,
|
|
110
|
+
collection: args.collection,
|
|
111
|
+
documentId: args.documentId,
|
|
112
|
+
occurredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
113
|
+
actorId: args.actorId,
|
|
114
|
+
payload: args.payload,
|
|
115
|
+
attempts: 0,
|
|
116
|
+
status: "pending"
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
async function persistEvent(db, event) {
|
|
120
|
+
await db.create({ collection: LIFECYCLE_EVENTS_COLLECTION, data: event });
|
|
121
|
+
}
|
|
122
|
+
async function dispatchLifecycleEvent(config, event) {
|
|
123
|
+
const db = config.db;
|
|
124
|
+
if (!db || !config.events?.handlers.length) return;
|
|
125
|
+
const maxAttempts = config.events.maxAttempts ?? 8;
|
|
126
|
+
const retryDelayMs = config.events.retryDelayMs ?? 1e3;
|
|
127
|
+
const attempts = event.attempts + 1;
|
|
128
|
+
try {
|
|
129
|
+
await db.update({ collection: LIFECYCLE_EVENTS_COLLECTION, id: event.id, data: { status: "processing", attempts } });
|
|
130
|
+
for (const handler of config.events.handlers) await handler({ ...event, status: "processing", attempts });
|
|
131
|
+
await db.update({
|
|
132
|
+
collection: LIFECYCLE_EVENTS_COLLECTION,
|
|
133
|
+
id: event.id,
|
|
134
|
+
data: { status: "delivered", attempts, deliveredAt: (/* @__PURE__ */ new Date()).toISOString(), lastError: null }
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
const exhausted = attempts >= maxAttempts;
|
|
138
|
+
await db.update({
|
|
139
|
+
collection: LIFECYCLE_EVENTS_COLLECTION,
|
|
140
|
+
id: event.id,
|
|
141
|
+
data: {
|
|
142
|
+
status: "failed",
|
|
143
|
+
attempts,
|
|
144
|
+
lastError: error instanceof Error ? error.message : String(error),
|
|
145
|
+
nextAttemptAt: exhausted ? null : new Date(Date.now() + retryDelayMs * 2 ** (attempts - 1)).toISOString()
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function dispatchPendingLifecycleEvents(config, limit = 50) {
|
|
151
|
+
if (!config.db || !config.events?.handlers.length) return 0;
|
|
152
|
+
const maxAttempts = config.events.maxAttempts ?? 8;
|
|
153
|
+
const result = await config.db.find({
|
|
154
|
+
collection: LIFECYCLE_EVENTS_COLLECTION,
|
|
155
|
+
where: { status: { in: ["pending", "failed"] } },
|
|
156
|
+
sort: "occurredAt",
|
|
157
|
+
limit
|
|
158
|
+
});
|
|
159
|
+
const now = Date.now();
|
|
160
|
+
const due = result.docs.filter(
|
|
161
|
+
(doc) => Number(doc.attempts ?? 0) < maxAttempts && (!doc.nextAttemptAt || new Date(doc.nextAttemptAt).getTime() <= now)
|
|
162
|
+
);
|
|
163
|
+
for (const event of due) await dispatchLifecycleEvent(config, event);
|
|
164
|
+
return due.length;
|
|
165
|
+
}
|
|
166
|
+
async function saveWorkflowDraft(args) {
|
|
167
|
+
const { config, collection, id, originalDoc, data, user } = args;
|
|
168
|
+
const db = config.db;
|
|
169
|
+
const workflow = collection.workflow;
|
|
170
|
+
if (!db.transaction) throw new Error(`The configured database adapter does not support workflow transactions.`);
|
|
171
|
+
const previous = originalDoc.__workflow;
|
|
172
|
+
const event = createLifecycleEvent({
|
|
173
|
+
name: "revision.created",
|
|
174
|
+
collection: collection.slug,
|
|
175
|
+
documentId: id,
|
|
176
|
+
actorId: user?.sub,
|
|
177
|
+
payload: { revision: previous.revision + 1, previousRevision: previous.revision }
|
|
178
|
+
});
|
|
179
|
+
const doc = await db.transaction(async (tx) => {
|
|
180
|
+
const nextMeta = {
|
|
181
|
+
...previous,
|
|
182
|
+
state: originalDoc.__published ? workflow.draftState ?? workflow.initialState : previous.state,
|
|
183
|
+
revision: previous.revision + 1
|
|
184
|
+
};
|
|
185
|
+
const updated = await tx.update({ collection: collection.slug, id, data: { ...data, __workflow: nextMeta } });
|
|
186
|
+
await persistEvent(tx, event);
|
|
187
|
+
return updated;
|
|
188
|
+
});
|
|
189
|
+
void dispatchLifecycleEvent(config, event);
|
|
190
|
+
return { doc, event };
|
|
191
|
+
}
|
|
192
|
+
async function createWorkflowDocument(args) {
|
|
193
|
+
const { config, collection, data, user } = args;
|
|
194
|
+
const db = config.db;
|
|
195
|
+
if (!db.transaction) throw new Error(`The configured database adapter does not support workflow transactions.`);
|
|
196
|
+
let event;
|
|
197
|
+
const doc = await db.transaction(async (tx) => {
|
|
198
|
+
const created = await tx.create({ collection: collection.slug, data });
|
|
199
|
+
event = createLifecycleEvent({
|
|
200
|
+
name: "revision.created",
|
|
201
|
+
collection: collection.slug,
|
|
202
|
+
documentId: created.id,
|
|
203
|
+
actorId: user?.sub,
|
|
204
|
+
payload: { revision: 1, previousRevision: null }
|
|
205
|
+
});
|
|
206
|
+
await persistEvent(tx, event);
|
|
207
|
+
return created;
|
|
208
|
+
});
|
|
209
|
+
void dispatchLifecycleEvent(config, event);
|
|
210
|
+
return { doc, event };
|
|
211
|
+
}
|
|
212
|
+
async function transitionWorkflow(args) {
|
|
213
|
+
const { config, collection, id, transitionName, expectedRevision, comment, user, req } = args;
|
|
214
|
+
const db = config.db;
|
|
215
|
+
const workflow = collection.workflow;
|
|
216
|
+
if (!db.transaction) throw new Error(`The configured database adapter does not support workflow transactions.`);
|
|
217
|
+
const original = await db.findOne({ collection: collection.slug, id });
|
|
218
|
+
if (!original) throw Object.assign(new Error("Not Found"), { statusCode: 404 });
|
|
219
|
+
const meta = original.__workflow;
|
|
220
|
+
if (!meta) throw Object.assign(new Error("Entry has no workflow metadata"), { statusCode: 409 });
|
|
221
|
+
if (expectedRevision !== void 0 && expectedRevision !== meta.revision) {
|
|
222
|
+
throw Object.assign(new Error("This entry changed since it was loaded. Refresh before transitioning it."), { statusCode: 409 });
|
|
223
|
+
}
|
|
224
|
+
const transition = workflow.transitions.find((item) => item.name === transitionName);
|
|
225
|
+
const fromStates = transition ? Array.isArray(transition.from) ? transition.from : [transition.from] : [];
|
|
226
|
+
if (!transition || !fromStates.includes(meta.state)) {
|
|
227
|
+
throw Object.assign(new Error(`Transition "${transitionName}" is not valid from "${meta.state}".`), { statusCode: 409 });
|
|
228
|
+
}
|
|
229
|
+
if (!availableWorkflowTransitions(workflow, meta.state, user).some((item) => item.name === transition.name)) {
|
|
230
|
+
throw Object.assign(new Error(`You do not have permission to perform "${transition.label}".`), { statusCode: 403 });
|
|
231
|
+
}
|
|
232
|
+
if (transition.requireComment && !comment?.trim()) {
|
|
233
|
+
throw Object.assign(new Error(`A comment is required for "${transition.label}".`), { statusCode: 400 });
|
|
234
|
+
}
|
|
235
|
+
const hookContext = { transition, from: meta.state, to: transition.to, doc: original, user, comment, req, db };
|
|
236
|
+
for (const hook of workflow.hooks?.beforeTransition ?? []) await hook(hookContext);
|
|
237
|
+
const events = [createLifecycleEvent({
|
|
238
|
+
name: "workflow.transitioned",
|
|
239
|
+
collection: collection.slug,
|
|
240
|
+
documentId: id,
|
|
241
|
+
actorId: user?.sub,
|
|
242
|
+
payload: { transition: transition.name, from: meta.state, to: transition.to, revision: meta.revision, comment }
|
|
243
|
+
})];
|
|
244
|
+
const targetState = workflow.states.find((state) => state.name === transition.to);
|
|
245
|
+
if (targetState.published) {
|
|
246
|
+
events.push(createLifecycleEvent({ name: "entry.published", collection: collection.slug, documentId: id, actorId: user?.sub, payload: { revision: meta.revision } }));
|
|
247
|
+
} else if (transition.unpublish) {
|
|
248
|
+
events.push(createLifecycleEvent({ name: "entry.unpublished", collection: collection.slug, documentId: id, actorId: user?.sub, payload: { revision: meta.publishedRevision } }));
|
|
249
|
+
}
|
|
250
|
+
const updated = await db.transaction(async (tx) => {
|
|
251
|
+
const locked = await tx.findOne({ collection: collection.slug, id });
|
|
252
|
+
if (!locked) throw Object.assign(new Error("Not Found"), { statusCode: 404 });
|
|
253
|
+
const lockedMeta = locked.__workflow;
|
|
254
|
+
if (lockedMeta.revision !== meta.revision || lockedMeta.state !== meta.state || expectedRevision !== void 0 && lockedMeta.revision !== expectedRevision) {
|
|
255
|
+
throw Object.assign(new Error("This entry changed since it was loaded. Refresh before transitioning it."), { statusCode: 409 });
|
|
256
|
+
}
|
|
257
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
258
|
+
const { __published: _published, __workflow: _workflow, id: _id, ...working } = locked;
|
|
259
|
+
const nextMeta = {
|
|
260
|
+
...lockedMeta,
|
|
261
|
+
state: transition.to,
|
|
262
|
+
...targetState.published ? { publishedRevision: lockedMeta.revision, publishedAt: now, publishedBy: user?.sub } : {},
|
|
263
|
+
...transition.unpublish ? { publishedRevision: void 0, publishedAt: void 0, publishedBy: void 0 } : {}
|
|
264
|
+
};
|
|
265
|
+
const data = { __workflow: nextMeta };
|
|
266
|
+
if (targetState.published) data.__published = working;
|
|
267
|
+
if (transition.unpublish) data.__published = null;
|
|
268
|
+
const next = await tx.update({ collection: collection.slug, id, data });
|
|
269
|
+
await tx.create({
|
|
270
|
+
collection: WORKFLOW_HISTORY_COLLECTION,
|
|
271
|
+
data: { collection: collection.slug, documentId: id, transition: transition.name, from: lockedMeta.state, to: transition.to, revision: lockedMeta.revision, comment: comment ?? null, actorId: user?.sub ?? null, createdAt: now }
|
|
272
|
+
});
|
|
273
|
+
for (const event of events) await persistEvent(tx, event);
|
|
274
|
+
if (collection.audit) {
|
|
275
|
+
await tx.create({ collection: "__audit", data: { collection: collection.slug, documentId: id, operation: "workflow.transition", user: user?.sub ?? null, timestamp: now, changes: JSON.stringify({ transition: transition.name, from: lockedMeta.state, to: transition.to, revision: lockedMeta.revision }) } });
|
|
276
|
+
}
|
|
277
|
+
return next;
|
|
278
|
+
});
|
|
279
|
+
for (const event of events) void dispatchLifecycleEvent(config, event);
|
|
280
|
+
for (const hook of workflow.hooks?.afterTransition ?? []) {
|
|
281
|
+
try {
|
|
282
|
+
await hook({ ...hookContext, doc: updated, event: events[0] });
|
|
283
|
+
} catch (error) {
|
|
284
|
+
console.error("[dyrected/workflow] afterTransition hook failed:", error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return updated;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/utils/admin-auth.ts
|
|
291
|
+
function getAdminAuthCollection(config) {
|
|
292
|
+
const adminCollection = resolveAdminAuthCollection(config.collections, config.adminAuth?.collectionSlug);
|
|
293
|
+
return adminCollection ?? null;
|
|
294
|
+
}
|
|
295
|
+
function resolveAdminAuthCollection(collections, collectionSlug) {
|
|
296
|
+
const adminsCollection = collections.find((collection) => collection.slug === "__admins");
|
|
297
|
+
if (adminsCollection) return adminsCollection;
|
|
298
|
+
if (collectionSlug) {
|
|
299
|
+
const requestedCollection = collections.find((collection) => collection.slug === collectionSlug);
|
|
300
|
+
if (requestedCollection) return requestedCollection;
|
|
301
|
+
}
|
|
302
|
+
return collections.find((collection) => collection.auth);
|
|
303
|
+
}
|
|
304
|
+
function getPublicAdminAuthConfig(adminAuth, collections) {
|
|
305
|
+
const providers = (adminAuth?.providers ?? []).map((provider) => ({
|
|
306
|
+
id: provider.id,
|
|
307
|
+
type: provider.type,
|
|
308
|
+
displayName: provider.displayName || humanizeProviderName(provider.id, provider.type),
|
|
309
|
+
autoRedirect: provider.autoRedirect
|
|
310
|
+
}));
|
|
311
|
+
const resolvedCollectionSlug = collections ? resolveAdminAuthCollection(collections, adminAuth?.collectionSlug)?.slug : adminAuth?.collectionSlug;
|
|
312
|
+
return {
|
|
313
|
+
mode: adminAuth?.mode ?? "local",
|
|
314
|
+
collectionSlug: resolvedCollectionSlug,
|
|
315
|
+
provisioningMode: adminAuth?.provisioningMode,
|
|
316
|
+
providers
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function humanizeProviderName(id, type) {
|
|
320
|
+
const cleaned = id.replace(/[-_]+/g, " ").trim();
|
|
321
|
+
if (!cleaned) return type.toUpperCase();
|
|
322
|
+
return cleaned.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/utils/config.ts
|
|
326
|
+
var AUDIT_COLLECTION_SLUG = "__audit";
|
|
327
|
+
var SYSTEM_FIELDS = [
|
|
328
|
+
{
|
|
329
|
+
name: "createdAt",
|
|
330
|
+
type: "date",
|
|
331
|
+
label: "Created At",
|
|
332
|
+
admin: { readOnly: true, hidden: true }
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
name: "updatedAt",
|
|
336
|
+
type: "date",
|
|
337
|
+
label: "Updated At",
|
|
338
|
+
admin: { readOnly: true, hidden: true }
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
name: "createdBy",
|
|
342
|
+
type: "text",
|
|
343
|
+
label: "Created By",
|
|
344
|
+
admin: { readOnly: true, hidden: true }
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
name: "updatedBy",
|
|
348
|
+
type: "text",
|
|
349
|
+
label: "Updated By",
|
|
350
|
+
admin: { readOnly: true, hidden: true }
|
|
351
|
+
}
|
|
352
|
+
];
|
|
353
|
+
var AUDIT_COLLECTION = {
|
|
354
|
+
slug: AUDIT_COLLECTION_SLUG,
|
|
355
|
+
labels: { singular: "Audit Log", plural: "Audit Logs" },
|
|
356
|
+
fields: [
|
|
357
|
+
{ name: "collection", type: "text", label: "Collection", required: true },
|
|
358
|
+
{ name: "documentId", type: "text", label: "Document ID" },
|
|
359
|
+
{ name: "operation", type: "select", label: "Operation", options: ["create", "update", "delete"], required: true },
|
|
360
|
+
{ name: "user", type: "text", label: "User ID" },
|
|
361
|
+
{ name: "timestamp", type: "date", label: "Timestamp", required: true },
|
|
362
|
+
{ name: "changes", type: "json", label: "Changes" }
|
|
363
|
+
],
|
|
364
|
+
access: { read: () => false, create: () => false, update: () => false, delete: () => false },
|
|
365
|
+
admin: { hidden: true }
|
|
366
|
+
};
|
|
367
|
+
var WORKFLOW_HISTORY_COLLECTION_CONFIG = {
|
|
368
|
+
slug: WORKFLOW_HISTORY_COLLECTION,
|
|
369
|
+
labels: { singular: "Workflow transition", plural: "Workflow transitions" },
|
|
370
|
+
fields: [
|
|
371
|
+
{ name: "collection", type: "text", required: true },
|
|
372
|
+
{ name: "documentId", type: "text", required: true },
|
|
373
|
+
{ name: "transition", type: "text", required: true },
|
|
374
|
+
{ name: "from", type: "text", required: true },
|
|
375
|
+
{ name: "to", type: "text", required: true },
|
|
376
|
+
{ name: "revision", type: "number", required: true },
|
|
377
|
+
{ name: "comment", type: "textarea" },
|
|
378
|
+
{ name: "actorId", type: "text" },
|
|
379
|
+
{ name: "createdAt", type: "date", required: true }
|
|
380
|
+
],
|
|
381
|
+
access: { read: ({ user }) => !!user, create: () => false, update: () => false, delete: () => false },
|
|
382
|
+
admin: { hidden: true }
|
|
383
|
+
};
|
|
384
|
+
var LIFECYCLE_EVENTS_COLLECTION_CONFIG = {
|
|
385
|
+
slug: LIFECYCLE_EVENTS_COLLECTION,
|
|
386
|
+
labels: { singular: "Lifecycle event", plural: "Lifecycle events" },
|
|
387
|
+
fields: [
|
|
388
|
+
{ name: "name", type: "text", required: true },
|
|
389
|
+
{ name: "collection", type: "text", required: true },
|
|
390
|
+
{ name: "documentId", type: "text", required: true },
|
|
391
|
+
{ name: "occurredAt", type: "date", required: true },
|
|
392
|
+
{ name: "actorId", type: "text" },
|
|
393
|
+
{ name: "payload", type: "json", required: true },
|
|
394
|
+
{ name: "attempts", type: "number", required: true },
|
|
395
|
+
{ name: "status", type: "select", options: ["pending", "processing", "delivered", "failed"], required: true },
|
|
396
|
+
{ name: "nextAttemptAt", type: "date" },
|
|
397
|
+
{ name: "deliveredAt", type: "date" },
|
|
398
|
+
{ name: "lastError", type: "textarea" }
|
|
399
|
+
],
|
|
400
|
+
access: { read: ({ user }) => !!user?.roles?.includes("admin"), create: () => false, update: () => false, delete: () => false },
|
|
401
|
+
admin: { hidden: true }
|
|
402
|
+
};
|
|
403
|
+
function normalizeConfig(config) {
|
|
404
|
+
const collections = config?.collections || [];
|
|
405
|
+
const globals = config?.globals || [];
|
|
406
|
+
const needsAudit = collections.some((col) => col.audit);
|
|
407
|
+
const needsWorkflow = collections.some((col) => col.workflow || col.drafts);
|
|
408
|
+
const adminAuthCollectionSlug = getAdminAuthCollection({
|
|
409
|
+
collections,
|
|
410
|
+
adminAuth: config.adminAuth
|
|
411
|
+
})?.slug;
|
|
412
|
+
const normalizedCollections = collections.map((col) => {
|
|
413
|
+
let fields = col.fields || [];
|
|
414
|
+
const existingFieldNames = new Set(fields.map((f) => f.name));
|
|
415
|
+
if (col.auth) {
|
|
416
|
+
if (!existingFieldNames.has("email")) {
|
|
417
|
+
fields = [
|
|
418
|
+
...fields,
|
|
419
|
+
{
|
|
420
|
+
name: "email",
|
|
421
|
+
type: "email",
|
|
422
|
+
label: "Email",
|
|
423
|
+
required: true,
|
|
424
|
+
unique: true,
|
|
425
|
+
promoted: true,
|
|
426
|
+
access: {
|
|
427
|
+
update: "!id"
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
];
|
|
431
|
+
}
|
|
432
|
+
if (!existingFieldNames.has("password")) {
|
|
433
|
+
fields = [
|
|
434
|
+
...fields,
|
|
435
|
+
{
|
|
436
|
+
name: "password",
|
|
437
|
+
type: "text",
|
|
438
|
+
label: "Password",
|
|
439
|
+
required: true,
|
|
440
|
+
access: {
|
|
441
|
+
update: "!id || user.id == id"
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
];
|
|
445
|
+
}
|
|
446
|
+
if (!existingFieldNames.has("roles")) {
|
|
447
|
+
fields = [
|
|
448
|
+
...fields,
|
|
449
|
+
{
|
|
450
|
+
name: "roles",
|
|
451
|
+
type: "select",
|
|
452
|
+
label: "Roles",
|
|
453
|
+
defaultValue: [],
|
|
454
|
+
options: [
|
|
455
|
+
{ value: "admin", label: "Admin" },
|
|
456
|
+
{ value: "editor", label: "Editor" },
|
|
457
|
+
{ value: "viewer", label: "Viewer" }
|
|
458
|
+
],
|
|
459
|
+
access: {
|
|
460
|
+
update: "user.roles && 'admin' in user.roles"
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
];
|
|
464
|
+
}
|
|
465
|
+
fields = fields.map((field) => {
|
|
466
|
+
if (field.name === "email") {
|
|
467
|
+
return {
|
|
468
|
+
...field,
|
|
469
|
+
// Email is the login identifier. Keep the integrity constraints
|
|
470
|
+
// auth relies on even when the field is explicitly redefined, so a
|
|
471
|
+
// custom `email` field can never silently drop uniqueness.
|
|
472
|
+
required: true,
|
|
473
|
+
unique: true,
|
|
474
|
+
promoted: true,
|
|
475
|
+
access: {
|
|
476
|
+
...field.access || {},
|
|
477
|
+
update: "!id"
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
if (field.name === "password") {
|
|
482
|
+
return {
|
|
483
|
+
...field,
|
|
484
|
+
// Password is required to authenticate; enforce it regardless of
|
|
485
|
+
// how the field was declared.
|
|
486
|
+
required: true,
|
|
487
|
+
admin: { ...field.admin || {} },
|
|
488
|
+
access: {
|
|
489
|
+
...field.access || {},
|
|
490
|
+
update: "!id || user.id == id"
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
if (field.name === "roles") {
|
|
495
|
+
return {
|
|
496
|
+
...field,
|
|
497
|
+
access: {
|
|
498
|
+
...field.access || {},
|
|
499
|
+
// Must be an admin; cannot edit own roles (no self-elevation).
|
|
500
|
+
update: "user.roles && 'admin' in user.roles && user.id != id"
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
return field;
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
if (adminAuthCollectionSlug && col.slug === adminAuthCollectionSlug && config.adminAuth?.mode === "external") {
|
|
508
|
+
const externalAdminFields = [
|
|
509
|
+
{
|
|
510
|
+
name: "authProvider",
|
|
511
|
+
type: "text",
|
|
512
|
+
label: "Auth Provider",
|
|
513
|
+
admin: { readOnly: true, hidden: true }
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
name: "externalSubject",
|
|
517
|
+
type: "text",
|
|
518
|
+
label: "External Subject",
|
|
519
|
+
admin: { readOnly: true, hidden: true }
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
name: "authSource",
|
|
523
|
+
type: "text",
|
|
524
|
+
label: "Auth Source",
|
|
525
|
+
admin: { readOnly: true, hidden: true }
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
name: "lastLoginAt",
|
|
529
|
+
type: "date",
|
|
530
|
+
label: "Last Login At",
|
|
531
|
+
admin: { readOnly: true, hidden: true }
|
|
532
|
+
}
|
|
533
|
+
];
|
|
534
|
+
for (const field of externalAdminFields) {
|
|
535
|
+
if (!existingFieldNames.has(field.name)) {
|
|
536
|
+
fields = [...fields, field];
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const updatedFieldNames = new Set(fields.map((f) => f.name));
|
|
541
|
+
const fieldsToInject = SYSTEM_FIELDS.filter((f) => !updatedFieldNames.has(f.name));
|
|
542
|
+
const workflow = col.workflow || (col.drafts ? simplePublishingWorkflow() : void 0);
|
|
543
|
+
return {
|
|
544
|
+
...col,
|
|
545
|
+
workflow,
|
|
546
|
+
fields: [...fields, ...fieldsToInject]
|
|
547
|
+
};
|
|
548
|
+
});
|
|
549
|
+
const hasAuditCollection = normalizedCollections.some((col) => col.slug === AUDIT_COLLECTION_SLUG);
|
|
550
|
+
const systemCollections = [];
|
|
551
|
+
if (needsAudit && !hasAuditCollection) systemCollections.push(AUDIT_COLLECTION);
|
|
552
|
+
if (needsWorkflow && !normalizedCollections.some((col) => col.slug === WORKFLOW_HISTORY_COLLECTION)) {
|
|
553
|
+
systemCollections.push(WORKFLOW_HISTORY_COLLECTION_CONFIG);
|
|
554
|
+
}
|
|
555
|
+
if (needsWorkflow && !normalizedCollections.some((col) => col.slug === LIFECYCLE_EVENTS_COLLECTION)) {
|
|
556
|
+
systemCollections.push(LIFECYCLE_EVENTS_COLLECTION_CONFIG);
|
|
557
|
+
}
|
|
558
|
+
return {
|
|
559
|
+
...config,
|
|
560
|
+
collections: [...normalizedCollections, ...systemCollections],
|
|
561
|
+
globals
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/utils/hooks.ts
|
|
566
|
+
async function runCollectionHooks(hooks, args, options = {}) {
|
|
567
|
+
if (!hooks || hooks.length === 0) {
|
|
568
|
+
return args.data ?? args.doc ?? void 0;
|
|
569
|
+
}
|
|
570
|
+
let currentPayload = args.data ?? args.doc ?? void 0;
|
|
571
|
+
for (const hook of hooks) {
|
|
572
|
+
try {
|
|
573
|
+
const result = await hook({
|
|
574
|
+
...args,
|
|
575
|
+
data: args.data !== void 0 ? currentPayload : void 0,
|
|
576
|
+
doc: args.doc !== void 0 ? currentPayload : void 0
|
|
577
|
+
});
|
|
578
|
+
if (result !== void 0) {
|
|
579
|
+
currentPayload = result;
|
|
580
|
+
}
|
|
581
|
+
} catch (err) {
|
|
582
|
+
if (options.isolated) {
|
|
583
|
+
console.error("[dyrected/core] Side-effect hook failed (error isolated \u2014 DB write was successful):", err);
|
|
584
|
+
} else {
|
|
585
|
+
throw err;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return currentPayload;
|
|
590
|
+
}
|
|
591
|
+
async function executeFieldBeforeChange(fields, data, originalDoc, user, db) {
|
|
592
|
+
if (!data || typeof data !== "object") return data;
|
|
593
|
+
const result = { ...data };
|
|
594
|
+
for (const field of fields) {
|
|
595
|
+
if (!field.name) continue;
|
|
596
|
+
const value = result[field.name];
|
|
597
|
+
const origValue = originalDoc?.[field.name];
|
|
598
|
+
let updatedValue = value;
|
|
599
|
+
if (field.hooks?.beforeChange) {
|
|
600
|
+
for (const hook of field.hooks.beforeChange) {
|
|
601
|
+
const typedHook = hook;
|
|
602
|
+
const hookArgs = {
|
|
603
|
+
value: updatedValue,
|
|
604
|
+
originalDoc: originalDoc ?? void 0,
|
|
605
|
+
data: result,
|
|
606
|
+
user,
|
|
607
|
+
db
|
|
608
|
+
};
|
|
609
|
+
updatedValue = await typedHook(hookArgs);
|
|
610
|
+
}
|
|
611
|
+
result[field.name] = updatedValue;
|
|
612
|
+
}
|
|
613
|
+
if (updatedValue !== void 0 && updatedValue !== null) {
|
|
614
|
+
if (field.type === "object" && field.fields) {
|
|
615
|
+
result[field.name] = await executeFieldBeforeChange(
|
|
616
|
+
field.fields,
|
|
617
|
+
updatedValue,
|
|
618
|
+
origValue,
|
|
619
|
+
user,
|
|
620
|
+
db
|
|
621
|
+
);
|
|
622
|
+
} else if (field.type === "array" && field.fields && Array.isArray(updatedValue)) {
|
|
623
|
+
const arrayResult = [];
|
|
624
|
+
for (let i = 0; i < updatedValue.length; i++) {
|
|
625
|
+
const item = updatedValue[i];
|
|
626
|
+
const origItem = Array.isArray(origValue) ? origValue[i] : null;
|
|
627
|
+
arrayResult.push(
|
|
628
|
+
await executeFieldBeforeChange(field.fields, item, origItem, user, db)
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
result[field.name] = arrayResult;
|
|
632
|
+
} else if (field.type === "blocks" && field.blocks && Array.isArray(updatedValue)) {
|
|
633
|
+
const blocksResult = [];
|
|
634
|
+
for (let i = 0; i < updatedValue.length; i++) {
|
|
635
|
+
const blockData = updatedValue[i];
|
|
636
|
+
const origBlock = Array.isArray(origValue) ? origValue[i] : null;
|
|
637
|
+
const blockConfig = field.blocks.find(
|
|
638
|
+
(b) => b.slug === blockData.blockType
|
|
639
|
+
);
|
|
640
|
+
if (blockConfig) {
|
|
641
|
+
blocksResult.push(
|
|
642
|
+
await executeFieldBeforeChange(
|
|
643
|
+
blockConfig.fields,
|
|
644
|
+
blockData,
|
|
645
|
+
origBlock,
|
|
646
|
+
user,
|
|
647
|
+
db
|
|
648
|
+
)
|
|
649
|
+
);
|
|
650
|
+
} else {
|
|
651
|
+
blocksResult.push(blockData);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
result[field.name] = blocksResult;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return result;
|
|
659
|
+
}
|
|
660
|
+
async function executeFieldAfterRead(fields, doc, user, db) {
|
|
661
|
+
if (!doc || typeof doc !== "object") return doc;
|
|
662
|
+
const result = { ...doc };
|
|
663
|
+
for (const field of fields) {
|
|
664
|
+
if (!field.name) continue;
|
|
665
|
+
const value = result[field.name];
|
|
666
|
+
let updatedValue = value;
|
|
667
|
+
if (field.hooks?.afterRead) {
|
|
668
|
+
for (const hook of field.hooks.afterRead) {
|
|
669
|
+
const typedHook = hook;
|
|
670
|
+
const hookArgs = {
|
|
671
|
+
value: updatedValue,
|
|
672
|
+
doc: result,
|
|
673
|
+
user,
|
|
674
|
+
db
|
|
675
|
+
};
|
|
676
|
+
updatedValue = await typedHook(hookArgs);
|
|
677
|
+
}
|
|
678
|
+
result[field.name] = updatedValue;
|
|
679
|
+
}
|
|
680
|
+
if (updatedValue !== void 0 && updatedValue !== null) {
|
|
681
|
+
if (field.type === "object" && field.fields) {
|
|
682
|
+
result[field.name] = await executeFieldAfterRead(
|
|
683
|
+
field.fields,
|
|
684
|
+
updatedValue,
|
|
685
|
+
user,
|
|
686
|
+
db
|
|
687
|
+
);
|
|
688
|
+
} else if (field.type === "array" && field.fields && Array.isArray(updatedValue)) {
|
|
689
|
+
const arrayResult = [];
|
|
690
|
+
for (const item of updatedValue) {
|
|
691
|
+
arrayResult.push(
|
|
692
|
+
await executeFieldAfterRead(
|
|
693
|
+
field.fields,
|
|
694
|
+
item,
|
|
695
|
+
user,
|
|
696
|
+
db
|
|
697
|
+
)
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
result[field.name] = arrayResult;
|
|
701
|
+
} else if (field.type === "blocks" && field.blocks && Array.isArray(updatedValue)) {
|
|
702
|
+
const blocksResult = [];
|
|
703
|
+
for (const blockData of updatedValue) {
|
|
704
|
+
const typedBlock = blockData;
|
|
705
|
+
const blockConfig = field.blocks.find(
|
|
706
|
+
(b) => b.slug === typedBlock.blockType
|
|
707
|
+
);
|
|
708
|
+
if (blockConfig) {
|
|
709
|
+
blocksResult.push(
|
|
710
|
+
await executeFieldAfterRead(
|
|
711
|
+
blockConfig.fields,
|
|
712
|
+
typedBlock,
|
|
713
|
+
user,
|
|
714
|
+
db
|
|
715
|
+
)
|
|
716
|
+
);
|
|
717
|
+
} else {
|
|
718
|
+
blocksResult.push(blockData);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
result[field.name] = blocksResult;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return result;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// src/utils/openapi.ts
|
|
729
|
+
function getCollectionLabels(collection) {
|
|
730
|
+
return collection.labels || { singular: collection.slug, plural: collection.slug };
|
|
731
|
+
}
|
|
732
|
+
function getCollectionTag(collection) {
|
|
733
|
+
return `Collection: ${getCollectionLabels(collection).plural}`;
|
|
734
|
+
}
|
|
735
|
+
function getGlobalTag(global) {
|
|
736
|
+
return `Global: ${global.label || global.slug}`;
|
|
737
|
+
}
|
|
738
|
+
function generateOpenApi(config) {
|
|
739
|
+
const spec = {
|
|
740
|
+
openapi: "3.0.0",
|
|
741
|
+
info: {
|
|
742
|
+
title: "Dyrected API",
|
|
743
|
+
version: "1.0.0",
|
|
744
|
+
description: "Automatically generated OpenAPI specification for the Dyrected project."
|
|
745
|
+
},
|
|
746
|
+
components: {
|
|
747
|
+
schemas: {
|
|
748
|
+
WorkflowMetadata: {
|
|
749
|
+
type: "object",
|
|
750
|
+
required: ["state", "revision"],
|
|
751
|
+
properties: {
|
|
752
|
+
state: { type: "string" },
|
|
753
|
+
revision: { type: "integer", minimum: 1 },
|
|
754
|
+
publishedRevision: { type: "integer", minimum: 1 },
|
|
755
|
+
publishedAt: { type: "string", format: "date-time" },
|
|
756
|
+
publishedBy: { type: "string" },
|
|
757
|
+
availableTransitions: { type: "array", items: { type: "string" } }
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
WorkflowTransitionRequest: {
|
|
761
|
+
type: "object",
|
|
762
|
+
properties: {
|
|
763
|
+
expectedRevision: { type: "integer", minimum: 1 },
|
|
764
|
+
comment: { type: "string" }
|
|
765
|
+
}
|
|
766
|
+
},
|
|
767
|
+
WorkflowHistoryEntry: {
|
|
768
|
+
type: "object",
|
|
769
|
+
required: ["collection", "documentId", "transition", "from", "to", "revision", "createdAt"],
|
|
770
|
+
properties: {
|
|
771
|
+
id: { type: "string" },
|
|
772
|
+
collection: { type: "string" },
|
|
773
|
+
documentId: { type: "string" },
|
|
774
|
+
transition: { type: "string" },
|
|
775
|
+
from: { type: "string" },
|
|
776
|
+
to: { type: "string" },
|
|
777
|
+
revision: { type: "integer" },
|
|
778
|
+
comment: { type: "string", nullable: true },
|
|
779
|
+
actorId: { type: "string", nullable: true },
|
|
780
|
+
createdAt: { type: "string", format: "date-time" }
|
|
781
|
+
}
|
|
782
|
+
},
|
|
783
|
+
AuditEntry: {
|
|
784
|
+
type: "object",
|
|
785
|
+
required: ["collection", "operation", "timestamp"],
|
|
786
|
+
properties: {
|
|
787
|
+
id: { type: "string" },
|
|
788
|
+
collection: { type: "string" },
|
|
789
|
+
documentId: { type: "string", nullable: true },
|
|
790
|
+
operation: { type: "string" },
|
|
791
|
+
user: { type: "string", nullable: true },
|
|
792
|
+
timestamp: { type: "string", format: "date-time" },
|
|
793
|
+
changes: {
|
|
794
|
+
oneOf: [{ type: "string" }, { type: "object", additionalProperties: true }, { type: "null" }]
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
},
|
|
798
|
+
Error: {
|
|
799
|
+
type: "object",
|
|
800
|
+
properties: {
|
|
801
|
+
message: { type: "string" },
|
|
802
|
+
errors: {
|
|
803
|
+
type: "array",
|
|
804
|
+
items: { type: "object", additionalProperties: true }
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
},
|
|
808
|
+
AuthCredentials: {
|
|
809
|
+
type: "object",
|
|
810
|
+
required: ["email", "password"],
|
|
811
|
+
properties: {
|
|
812
|
+
email: { type: "string", format: "email" },
|
|
813
|
+
password: { type: "string" }
|
|
814
|
+
}
|
|
815
|
+
},
|
|
816
|
+
TokenResponse: {
|
|
817
|
+
type: "object",
|
|
818
|
+
properties: {
|
|
819
|
+
token: { type: "string" },
|
|
820
|
+
user: { type: "object", additionalProperties: true }
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
securitySchemes: {
|
|
825
|
+
ApiKeyAuth: {
|
|
826
|
+
type: "apiKey",
|
|
827
|
+
in: "header",
|
|
828
|
+
name: "x-api-key"
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
},
|
|
832
|
+
paths: {},
|
|
833
|
+
security: [{ ApiKeyAuth: [] }]
|
|
834
|
+
};
|
|
835
|
+
spec.paths["/api/schemas"] = {
|
|
836
|
+
get: {
|
|
837
|
+
tags: ["System"],
|
|
838
|
+
summary: "Get the serialized Dyrected schema",
|
|
839
|
+
security: [],
|
|
840
|
+
responses: {
|
|
841
|
+
200: { description: "Collection and global schema definitions" }
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
spec.paths["/api/openapi.json"] = {
|
|
846
|
+
get: {
|
|
847
|
+
tags: ["System"],
|
|
848
|
+
summary: "Get the OpenAPI specification",
|
|
849
|
+
security: [],
|
|
850
|
+
responses: { 200: { description: "OpenAPI 3.0 document" } }
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
spec.paths["/api/docs"] = {
|
|
854
|
+
get: {
|
|
855
|
+
tags: ["System"],
|
|
856
|
+
summary: "Open interactive API documentation",
|
|
857
|
+
security: [],
|
|
858
|
+
responses: { 200: { description: "Swagger UI HTML" } }
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
spec.paths["/api/dyrected/options/{collection}/{field}"] = {
|
|
862
|
+
get: {
|
|
863
|
+
tags: ["System"],
|
|
864
|
+
summary: "Resolve dynamic field options",
|
|
865
|
+
parameters: [
|
|
866
|
+
{
|
|
867
|
+
name: "collection",
|
|
868
|
+
in: "path",
|
|
869
|
+
required: true,
|
|
870
|
+
schema: { type: "string" }
|
|
871
|
+
},
|
|
872
|
+
{
|
|
873
|
+
name: "field",
|
|
874
|
+
in: "path",
|
|
875
|
+
required: true,
|
|
876
|
+
schema: { type: "string" }
|
|
877
|
+
}
|
|
878
|
+
],
|
|
879
|
+
responses: { 200: { description: "Resolved option items" } }
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
spec.paths["/api/preferences/{key}"] = {
|
|
883
|
+
get: {
|
|
884
|
+
tags: ["Preferences"],
|
|
885
|
+
summary: "Get an authenticated user preference",
|
|
886
|
+
parameters: [{ name: "key", in: "path", required: true, schema: { type: "string" } }],
|
|
887
|
+
responses: { 200: { description: "Preference value" } }
|
|
888
|
+
},
|
|
889
|
+
put: {
|
|
890
|
+
tags: ["Preferences"],
|
|
891
|
+
summary: "Set an authenticated user preference",
|
|
892
|
+
parameters: [{ name: "key", in: "path", required: true, schema: { type: "string" } }],
|
|
893
|
+
requestBody: {
|
|
894
|
+
required: true,
|
|
895
|
+
content: { "application/json": { schema: { type: "object" } } }
|
|
896
|
+
},
|
|
897
|
+
responses: { 200: { description: "Updated preference" } }
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
spec.paths["/api/preview-token"] = {
|
|
901
|
+
post: {
|
|
902
|
+
tags: ["Preview"],
|
|
903
|
+
summary: "Create a preview token",
|
|
904
|
+
responses: { 200: { description: "Short-lived preview token" } }
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
spec.paths["/api/preview-data"] = {
|
|
908
|
+
get: {
|
|
909
|
+
tags: ["Preview"],
|
|
910
|
+
summary: "Resolve preview data from a token",
|
|
911
|
+
security: [],
|
|
912
|
+
parameters: [
|
|
913
|
+
{
|
|
914
|
+
name: "token",
|
|
915
|
+
in: "query",
|
|
916
|
+
required: true,
|
|
917
|
+
schema: { type: "string" }
|
|
918
|
+
}
|
|
919
|
+
],
|
|
920
|
+
responses: { 200: { description: "Preview document data" } }
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
spec.paths["/api/audit"] = {
|
|
924
|
+
get: {
|
|
925
|
+
tags: ["Audit"],
|
|
926
|
+
summary: "Get audit entries across all readable audited collections",
|
|
927
|
+
parameters: [
|
|
928
|
+
{ name: "limit", in: "query", schema: { type: "integer", default: 50, maximum: 100 } },
|
|
929
|
+
{ name: "page", in: "query", schema: { type: "integer", default: 1 } },
|
|
930
|
+
{ name: "where", in: "query", schema: { type: "string" }, description: "JSON filter" },
|
|
931
|
+
{ name: "sort", in: "query", schema: { type: "string", default: "-timestamp" } }
|
|
932
|
+
],
|
|
933
|
+
responses: {
|
|
934
|
+
200: {
|
|
935
|
+
description: "Audit entries",
|
|
936
|
+
content: {
|
|
937
|
+
"application/json": {
|
|
938
|
+
schema: {
|
|
939
|
+
type: "object",
|
|
940
|
+
properties: {
|
|
941
|
+
docs: {
|
|
942
|
+
type: "array",
|
|
943
|
+
items: { $ref: "#/components/schemas/AuditEntry" }
|
|
944
|
+
},
|
|
945
|
+
total: { type: "integer" },
|
|
946
|
+
limit: { type: "integer" },
|
|
947
|
+
page: { type: "integer" }
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
for (const collection of config.collections) {
|
|
957
|
+
spec.components.schemas[collection.slug] = collectionToSchema(collection);
|
|
958
|
+
}
|
|
959
|
+
for (const global of config.globals) {
|
|
960
|
+
spec.components.schemas[global.slug] = globalToSchema(global);
|
|
961
|
+
}
|
|
962
|
+
for (const collection of config.collections) {
|
|
963
|
+
const slug = collection.slug;
|
|
964
|
+
const path = `/api/collections/${slug}`;
|
|
965
|
+
const labels = getCollectionLabels(collection);
|
|
966
|
+
const collectionTag = getCollectionTag(collection);
|
|
967
|
+
spec.paths[path] = {
|
|
968
|
+
get: {
|
|
969
|
+
tags: [collectionTag],
|
|
970
|
+
summary: `Find ${labels.plural}`,
|
|
971
|
+
parameters: [
|
|
972
|
+
{
|
|
973
|
+
name: "limit",
|
|
974
|
+
in: "query",
|
|
975
|
+
schema: { type: "integer", default: 10 }
|
|
976
|
+
},
|
|
977
|
+
{
|
|
978
|
+
name: "page",
|
|
979
|
+
in: "query",
|
|
980
|
+
schema: { type: "integer", default: 1 }
|
|
981
|
+
},
|
|
982
|
+
{
|
|
983
|
+
name: "where",
|
|
984
|
+
in: "query",
|
|
985
|
+
schema: { type: "string" },
|
|
986
|
+
description: "JSON filter"
|
|
987
|
+
},
|
|
988
|
+
{
|
|
989
|
+
name: "sort",
|
|
990
|
+
in: "query",
|
|
991
|
+
schema: { type: "string" },
|
|
992
|
+
description: "Sort field (e.g. -createdAt)"
|
|
993
|
+
}
|
|
994
|
+
],
|
|
995
|
+
responses: {
|
|
996
|
+
200: {
|
|
997
|
+
description: "Success",
|
|
998
|
+
content: {
|
|
999
|
+
"application/json": {
|
|
1000
|
+
schema: {
|
|
1001
|
+
type: "object",
|
|
1002
|
+
properties: {
|
|
1003
|
+
docs: {
|
|
1004
|
+
type: "array",
|
|
1005
|
+
items: { $ref: `#/components/schemas/${slug}` }
|
|
1006
|
+
},
|
|
1007
|
+
total: { type: "integer" },
|
|
1008
|
+
limit: { type: "integer" },
|
|
1009
|
+
page: { type: "integer" }
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
},
|
|
1017
|
+
post: {
|
|
1018
|
+
tags: [collectionTag],
|
|
1019
|
+
summary: `Create ${labels.singular}`,
|
|
1020
|
+
requestBody: {
|
|
1021
|
+
required: true,
|
|
1022
|
+
content: {
|
|
1023
|
+
"application/json": {
|
|
1024
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
},
|
|
1028
|
+
responses: {
|
|
1029
|
+
201: {
|
|
1030
|
+
description: "Created",
|
|
1031
|
+
content: {
|
|
1032
|
+
"application/json": {
|
|
1033
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
spec.paths[`${path}/{id}`] = {
|
|
1041
|
+
get: {
|
|
1042
|
+
tags: [collectionTag],
|
|
1043
|
+
summary: `Get a single ${labels.singular}`,
|
|
1044
|
+
parameters: [
|
|
1045
|
+
{
|
|
1046
|
+
name: "id",
|
|
1047
|
+
in: "path",
|
|
1048
|
+
required: true,
|
|
1049
|
+
schema: { type: "string" }
|
|
1050
|
+
}
|
|
1051
|
+
],
|
|
1052
|
+
responses: {
|
|
1053
|
+
200: {
|
|
1054
|
+
description: "Success",
|
|
1055
|
+
content: {
|
|
1056
|
+
"application/json": {
|
|
1057
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
},
|
|
1063
|
+
patch: {
|
|
1064
|
+
tags: [collectionTag],
|
|
1065
|
+
summary: `Update ${labels.singular}`,
|
|
1066
|
+
parameters: [
|
|
1067
|
+
{
|
|
1068
|
+
name: "id",
|
|
1069
|
+
in: "path",
|
|
1070
|
+
required: true,
|
|
1071
|
+
schema: { type: "string" }
|
|
1072
|
+
}
|
|
1073
|
+
],
|
|
1074
|
+
requestBody: {
|
|
1075
|
+
required: true,
|
|
1076
|
+
content: {
|
|
1077
|
+
"application/json": {
|
|
1078
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
},
|
|
1082
|
+
responses: {
|
|
1083
|
+
200: {
|
|
1084
|
+
description: "Updated",
|
|
1085
|
+
content: {
|
|
1086
|
+
"application/json": {
|
|
1087
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
},
|
|
1093
|
+
delete: {
|
|
1094
|
+
tags: [collectionTag],
|
|
1095
|
+
summary: `Delete ${labels.singular}`,
|
|
1096
|
+
parameters: [
|
|
1097
|
+
{
|
|
1098
|
+
name: "id",
|
|
1099
|
+
in: "path",
|
|
1100
|
+
required: true,
|
|
1101
|
+
schema: { type: "string" }
|
|
1102
|
+
}
|
|
1103
|
+
],
|
|
1104
|
+
responses: {
|
|
1105
|
+
204: { description: "Deleted" }
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
spec.paths[`${path}/delete-many`] = {
|
|
1110
|
+
delete: {
|
|
1111
|
+
tags: [collectionTag],
|
|
1112
|
+
summary: `Delete multiple ${labels.plural}`,
|
|
1113
|
+
requestBody: {
|
|
1114
|
+
required: true,
|
|
1115
|
+
content: {
|
|
1116
|
+
"application/json": {
|
|
1117
|
+
schema: {
|
|
1118
|
+
type: "object",
|
|
1119
|
+
required: ["ids"],
|
|
1120
|
+
properties: {
|
|
1121
|
+
ids: { type: "array", items: { type: "string" } }
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
},
|
|
1127
|
+
responses: { 200: { description: "Deleted and failed document IDs" } }
|
|
1128
|
+
}
|
|
1129
|
+
};
|
|
1130
|
+
if (collection.audit) {
|
|
1131
|
+
spec.paths[`${path}/__audit`] = {
|
|
1132
|
+
get: {
|
|
1133
|
+
tags: [collectionTag],
|
|
1134
|
+
summary: `Get ${labels.singular} audit entries`,
|
|
1135
|
+
parameters: [
|
|
1136
|
+
{ name: "limit", in: "query", schema: { type: "integer", default: 50, maximum: 100 } },
|
|
1137
|
+
{ name: "page", in: "query", schema: { type: "integer", default: 1 } },
|
|
1138
|
+
{ name: "where", in: "query", schema: { type: "string" }, description: "JSON filter" },
|
|
1139
|
+
{ name: "sort", in: "query", schema: { type: "string", default: "-timestamp" } }
|
|
1140
|
+
],
|
|
1141
|
+
responses: {
|
|
1142
|
+
200: {
|
|
1143
|
+
description: "Audit entries for this collection",
|
|
1144
|
+
content: {
|
|
1145
|
+
"application/json": {
|
|
1146
|
+
schema: {
|
|
1147
|
+
type: "object",
|
|
1148
|
+
properties: {
|
|
1149
|
+
docs: {
|
|
1150
|
+
type: "array",
|
|
1151
|
+
items: { $ref: "#/components/schemas/AuditEntry" }
|
|
1152
|
+
},
|
|
1153
|
+
total: { type: "integer" },
|
|
1154
|
+
limit: { type: "integer" },
|
|
1155
|
+
page: { type: "integer" }
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
403: { description: "Collection read access denied" },
|
|
1162
|
+
404: { description: "Audit is not enabled for this collection" }
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
spec.paths[`${path}/seed`] = {
|
|
1168
|
+
post: {
|
|
1169
|
+
tags: [collectionTag],
|
|
1170
|
+
summary: `Seed initial ${labels.plural}`,
|
|
1171
|
+
responses: { 200: { description: "Seeded documents" } }
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
if (collection.upload) {
|
|
1175
|
+
spec.paths[`${path}/media`] = {
|
|
1176
|
+
get: {
|
|
1177
|
+
tags: [collectionTag],
|
|
1178
|
+
summary: `List ${labels.plural}`,
|
|
1179
|
+
responses: { 200: { description: "Paginated media documents" } }
|
|
1180
|
+
},
|
|
1181
|
+
post: {
|
|
1182
|
+
tags: [collectionTag],
|
|
1183
|
+
summary: `Upload ${labels.singular}`,
|
|
1184
|
+
requestBody: {
|
|
1185
|
+
required: true,
|
|
1186
|
+
content: {
|
|
1187
|
+
"multipart/form-data": {
|
|
1188
|
+
schema: {
|
|
1189
|
+
type: "object",
|
|
1190
|
+
required: ["file"],
|
|
1191
|
+
properties: { file: { type: "string", format: "binary" } }
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
},
|
|
1196
|
+
responses: { 201: { description: "Uploaded media document" } }
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
spec.paths[`${path}/media/{filename}`] = {
|
|
1200
|
+
get: {
|
|
1201
|
+
tags: [collectionTag],
|
|
1202
|
+
summary: `Serve ${labels.singular} bytes`,
|
|
1203
|
+
parameters: [
|
|
1204
|
+
{
|
|
1205
|
+
name: "filename",
|
|
1206
|
+
in: "path",
|
|
1207
|
+
required: true,
|
|
1208
|
+
schema: { type: "string" }
|
|
1209
|
+
}
|
|
1210
|
+
],
|
|
1211
|
+
security: [],
|
|
1212
|
+
responses: {
|
|
1213
|
+
200: { description: "Stored file" },
|
|
1214
|
+
404: { description: "File not found" }
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
if (collection.auth) {
|
|
1220
|
+
const publicAuthPost = (summary) => ({
|
|
1221
|
+
tags: [collectionTag],
|
|
1222
|
+
summary,
|
|
1223
|
+
security: [],
|
|
1224
|
+
requestBody: {
|
|
1225
|
+
required: true,
|
|
1226
|
+
content: {
|
|
1227
|
+
"application/json": {
|
|
1228
|
+
schema: { type: "object", additionalProperties: true }
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
},
|
|
1232
|
+
responses: {
|
|
1233
|
+
200: { description: "Success" },
|
|
1234
|
+
400: { description: "Invalid request" }
|
|
1235
|
+
}
|
|
1236
|
+
});
|
|
1237
|
+
spec.paths[`${path}/login`] = {
|
|
1238
|
+
post: {
|
|
1239
|
+
...publicAuthPost(`Log in to ${labels.plural}`),
|
|
1240
|
+
requestBody: {
|
|
1241
|
+
required: true,
|
|
1242
|
+
content: {
|
|
1243
|
+
"application/json": {
|
|
1244
|
+
schema: { $ref: "#/components/schemas/AuthCredentials" }
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
},
|
|
1248
|
+
responses: {
|
|
1249
|
+
200: {
|
|
1250
|
+
description: "Authenticated",
|
|
1251
|
+
content: {
|
|
1252
|
+
"application/json": {
|
|
1253
|
+
schema: { $ref: "#/components/schemas/TokenResponse" }
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
},
|
|
1257
|
+
401: { description: "Invalid credentials" }
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
};
|
|
1261
|
+
spec.paths[`${path}/logout`] = {
|
|
1262
|
+
post: {
|
|
1263
|
+
tags: [collectionTag],
|
|
1264
|
+
summary: `Log out of ${labels.plural}`,
|
|
1265
|
+
responses: { 200: { description: "Logged out" } }
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
spec.paths[`${path}/init`] = {
|
|
1269
|
+
get: {
|
|
1270
|
+
tags: [collectionTag],
|
|
1271
|
+
summary: `Get ${labels.plural} initialization state`,
|
|
1272
|
+
security: [],
|
|
1273
|
+
responses: { 200: { description: "Initialization state" } }
|
|
1274
|
+
}
|
|
1275
|
+
};
|
|
1276
|
+
spec.paths[`${path}/first-user`] = {
|
|
1277
|
+
post: publicAuthPost(`Register the first ${labels.singular}`)
|
|
1278
|
+
};
|
|
1279
|
+
spec.paths[`${path}/me`] = {
|
|
1280
|
+
get: {
|
|
1281
|
+
tags: [collectionTag],
|
|
1282
|
+
summary: `Get the current ${labels.singular}`,
|
|
1283
|
+
responses: { 200: { description: "Authenticated user" } }
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
spec.paths[`${path}/refresh-token`] = {
|
|
1287
|
+
post: {
|
|
1288
|
+
tags: [collectionTag],
|
|
1289
|
+
summary: "Refresh an authentication token",
|
|
1290
|
+
responses: { 200: { description: "Refreshed token" } }
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
spec.paths[`${path}/forgot-password`] = {
|
|
1294
|
+
post: publicAuthPost("Request a password reset")
|
|
1295
|
+
};
|
|
1296
|
+
spec.paths[`${path}/reset-password`] = {
|
|
1297
|
+
post: publicAuthPost("Reset a password")
|
|
1298
|
+
};
|
|
1299
|
+
spec.paths[`${path}/invite`] = {
|
|
1300
|
+
post: {
|
|
1301
|
+
tags: [collectionTag],
|
|
1302
|
+
summary: `Invite a ${labels.singular}`,
|
|
1303
|
+
responses: { 200: { description: "Invitation sent" } }
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
spec.paths[`${path}/accept-invite`] = {
|
|
1307
|
+
post: publicAuthPost("Accept an invitation")
|
|
1308
|
+
};
|
|
1309
|
+
spec.paths[`${path}/{id}/change-password`] = {
|
|
1310
|
+
post: {
|
|
1311
|
+
tags: [collectionTag],
|
|
1312
|
+
summary: `Change a ${labels.singular} password`,
|
|
1313
|
+
parameters: [
|
|
1314
|
+
{
|
|
1315
|
+
name: "id",
|
|
1316
|
+
in: "path",
|
|
1317
|
+
required: true,
|
|
1318
|
+
schema: { type: "string" }
|
|
1319
|
+
}
|
|
1320
|
+
],
|
|
1321
|
+
responses: { 200: { description: "Password changed" } }
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
if (collection.workflow) {
|
|
1326
|
+
spec.paths[`${path}/{id}/transitions/{transition}`] = {
|
|
1327
|
+
post: {
|
|
1328
|
+
tags: [collectionTag],
|
|
1329
|
+
summary: `Transition ${labels.singular} workflow`,
|
|
1330
|
+
parameters: [
|
|
1331
|
+
{
|
|
1332
|
+
name: "id",
|
|
1333
|
+
in: "path",
|
|
1334
|
+
required: true,
|
|
1335
|
+
schema: { type: "string" }
|
|
1336
|
+
},
|
|
1337
|
+
{
|
|
1338
|
+
name: "transition",
|
|
1339
|
+
in: "path",
|
|
1340
|
+
required: true,
|
|
1341
|
+
schema: {
|
|
1342
|
+
type: "string",
|
|
1343
|
+
enum: collection.workflow.transitions.map((item) => item.name)
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
],
|
|
1347
|
+
requestBody: {
|
|
1348
|
+
content: {
|
|
1349
|
+
"application/json": {
|
|
1350
|
+
schema: {
|
|
1351
|
+
$ref: "#/components/schemas/WorkflowTransitionRequest"
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
},
|
|
1356
|
+
responses: {
|
|
1357
|
+
200: {
|
|
1358
|
+
description: "Transition applied",
|
|
1359
|
+
content: {
|
|
1360
|
+
"application/json": {
|
|
1361
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
},
|
|
1365
|
+
400: { description: "Required transition input is missing" },
|
|
1366
|
+
403: { description: "Transition capability denied" },
|
|
1367
|
+
409: { description: "Invalid state or stale revision" }
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
spec.paths[`${path}/{id}/workflow-history`] = {
|
|
1372
|
+
get: {
|
|
1373
|
+
tags: [collectionTag],
|
|
1374
|
+
summary: `Get ${labels.singular} workflow history`,
|
|
1375
|
+
parameters: [
|
|
1376
|
+
{
|
|
1377
|
+
name: "id",
|
|
1378
|
+
in: "path",
|
|
1379
|
+
required: true,
|
|
1380
|
+
schema: { type: "string" }
|
|
1381
|
+
},
|
|
1382
|
+
{
|
|
1383
|
+
name: "limit",
|
|
1384
|
+
in: "query",
|
|
1385
|
+
schema: { type: "integer", default: 50, maximum: 100 }
|
|
1386
|
+
}
|
|
1387
|
+
],
|
|
1388
|
+
responses: {
|
|
1389
|
+
200: {
|
|
1390
|
+
description: "Workflow transition history",
|
|
1391
|
+
content: {
|
|
1392
|
+
"application/json": {
|
|
1393
|
+
schema: {
|
|
1394
|
+
type: "object",
|
|
1395
|
+
properties: {
|
|
1396
|
+
docs: {
|
|
1397
|
+
type: "array",
|
|
1398
|
+
items: {
|
|
1399
|
+
$ref: "#/components/schemas/WorkflowHistoryEntry"
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
total: { type: "integer" },
|
|
1403
|
+
limit: { type: "integer" },
|
|
1404
|
+
page: { type: "integer" }
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
},
|
|
1410
|
+
403: { description: "Collection read access denied" }
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
for (const global of config.globals) {
|
|
1417
|
+
const slug = global.slug;
|
|
1418
|
+
const path = `/api/globals/${slug}`;
|
|
1419
|
+
const globalTag = getGlobalTag(global);
|
|
1420
|
+
spec.paths[path] = {
|
|
1421
|
+
get: {
|
|
1422
|
+
tags: [globalTag],
|
|
1423
|
+
summary: `Get ${global.label || slug}`,
|
|
1424
|
+
responses: {
|
|
1425
|
+
200: {
|
|
1426
|
+
description: "Success",
|
|
1427
|
+
content: {
|
|
1428
|
+
"application/json": {
|
|
1429
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
},
|
|
1435
|
+
patch: {
|
|
1436
|
+
tags: [globalTag],
|
|
1437
|
+
summary: `Update ${global.label || slug}`,
|
|
1438
|
+
requestBody: {
|
|
1439
|
+
required: true,
|
|
1440
|
+
content: {
|
|
1441
|
+
"application/json": {
|
|
1442
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
},
|
|
1446
|
+
responses: {
|
|
1447
|
+
200: {
|
|
1448
|
+
description: "Updated",
|
|
1449
|
+
content: {
|
|
1450
|
+
"application/json": {
|
|
1451
|
+
schema: { $ref: `#/components/schemas/${slug}` }
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
};
|
|
1458
|
+
spec.paths[`${path}/seed`] = {
|
|
1459
|
+
post: {
|
|
1460
|
+
tags: [globalTag],
|
|
1461
|
+
summary: `Seed ${global.label || slug}`,
|
|
1462
|
+
responses: { 200: { description: "Seeded global" } }
|
|
1463
|
+
}
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
if (config.storage) {
|
|
1467
|
+
spec.paths["/api/media/{filename}"] = {
|
|
1468
|
+
get: {
|
|
1469
|
+
tags: ["Media"],
|
|
1470
|
+
summary: "Serve a stored file",
|
|
1471
|
+
security: [],
|
|
1472
|
+
parameters: [
|
|
1473
|
+
{
|
|
1474
|
+
name: "filename",
|
|
1475
|
+
in: "path",
|
|
1476
|
+
required: true,
|
|
1477
|
+
schema: { type: "string" }
|
|
1478
|
+
}
|
|
1479
|
+
],
|
|
1480
|
+
responses: {
|
|
1481
|
+
200: { description: "Stored file bytes" },
|
|
1482
|
+
404: { description: "File not found" }
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
return spec;
|
|
1488
|
+
}
|
|
1489
|
+
function collectionToSchema(collection) {
|
|
1490
|
+
const { properties, required } = fieldsToProperties(collection.fields);
|
|
1491
|
+
return {
|
|
1492
|
+
type: "object",
|
|
1493
|
+
properties: {
|
|
1494
|
+
id: { type: "string" },
|
|
1495
|
+
createdAt: { type: "string", format: "date-time" },
|
|
1496
|
+
updatedAt: { type: "string", format: "date-time" },
|
|
1497
|
+
...collection.workflow ? { _workflow: { $ref: "#/components/schemas/WorkflowMetadata" } } : {},
|
|
1498
|
+
...properties
|
|
1499
|
+
},
|
|
1500
|
+
required: ["id", ...required]
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
function globalToSchema(global) {
|
|
1504
|
+
const { properties, required } = fieldsToProperties(global.fields);
|
|
1505
|
+
return {
|
|
1506
|
+
type: "object",
|
|
1507
|
+
properties,
|
|
1508
|
+
required
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1511
|
+
function fieldsToProperties(fields) {
|
|
1512
|
+
const props = {};
|
|
1513
|
+
const required = [];
|
|
1514
|
+
for (const field of fields) {
|
|
1515
|
+
if (field.type === "row") {
|
|
1516
|
+
const nested = fieldsToProperties(field.fields || []);
|
|
1517
|
+
Object.assign(props, nested.properties);
|
|
1518
|
+
required.push(...nested.required);
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
if (!field.name) continue;
|
|
1522
|
+
props[field.name] = fieldToSchema(field);
|
|
1523
|
+
if (field.required) {
|
|
1524
|
+
required.push(field.name);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
return { properties: props, required };
|
|
1528
|
+
}
|
|
1529
|
+
function fieldToSchema(field) {
|
|
1530
|
+
let schema = {};
|
|
1531
|
+
switch (field.type) {
|
|
1532
|
+
case "text":
|
|
1533
|
+
case "textarea":
|
|
1534
|
+
case "email":
|
|
1535
|
+
schema = { type: "string" };
|
|
1536
|
+
break;
|
|
1537
|
+
case "url":
|
|
1538
|
+
schema = {
|
|
1539
|
+
oneOf: [{ type: "string" }, { type: "object", additionalProperties: true }]
|
|
1540
|
+
};
|
|
1541
|
+
break;
|
|
1542
|
+
case "icon":
|
|
1543
|
+
schema = { type: "string" };
|
|
1544
|
+
break;
|
|
1545
|
+
case "number":
|
|
1546
|
+
schema = { type: "number" };
|
|
1547
|
+
break;
|
|
1548
|
+
case "boolean":
|
|
1549
|
+
schema = { type: "boolean" };
|
|
1550
|
+
break;
|
|
1551
|
+
case "date":
|
|
1552
|
+
schema = { type: "string", format: "date" };
|
|
1553
|
+
break;
|
|
1554
|
+
case "datetime":
|
|
1555
|
+
schema = { type: "string", format: "date-time" };
|
|
1556
|
+
break;
|
|
1557
|
+
case "time":
|
|
1558
|
+
schema = { type: "string", format: "time" };
|
|
1559
|
+
break;
|
|
1560
|
+
case "select":
|
|
1561
|
+
case "radio":
|
|
1562
|
+
schema = {
|
|
1563
|
+
type: "string",
|
|
1564
|
+
enum: Array.isArray(field.options) ? field.options.map((o) => typeof o === "string" ? o : o.value) : void 0
|
|
1565
|
+
};
|
|
1566
|
+
break;
|
|
1567
|
+
case "multiSelect":
|
|
1568
|
+
schema = {
|
|
1569
|
+
type: "array",
|
|
1570
|
+
items: {
|
|
1571
|
+
type: "string",
|
|
1572
|
+
enum: Array.isArray(field.options) ? field.options.map((o) => typeof o === "string" ? o : o.value) : void 0
|
|
1573
|
+
}
|
|
1574
|
+
};
|
|
1575
|
+
break;
|
|
1576
|
+
case "relationship":
|
|
1577
|
+
case "image": {
|
|
1578
|
+
const valueSchema = {
|
|
1579
|
+
type: "string",
|
|
1580
|
+
description: `ID of a ${field.relationTo} record`
|
|
1581
|
+
};
|
|
1582
|
+
schema = field.hasMany ? { type: "array", items: valueSchema } : valueSchema;
|
|
1583
|
+
break;
|
|
1584
|
+
}
|
|
1585
|
+
case "join":
|
|
1586
|
+
schema = {
|
|
1587
|
+
type: "array",
|
|
1588
|
+
readOnly: true,
|
|
1589
|
+
items: { type: "object", additionalProperties: true }
|
|
1590
|
+
};
|
|
1591
|
+
break;
|
|
1592
|
+
case "object": {
|
|
1593
|
+
const { properties, required } = fieldsToProperties(field.fields || []);
|
|
1594
|
+
schema = { type: "object", properties, required };
|
|
1595
|
+
break;
|
|
1596
|
+
}
|
|
1597
|
+
case "array": {
|
|
1598
|
+
const { properties, required } = fieldsToProperties(field.fields || []);
|
|
1599
|
+
schema = {
|
|
1600
|
+
type: "array",
|
|
1601
|
+
items: { type: "object", properties, required }
|
|
1602
|
+
};
|
|
1603
|
+
break;
|
|
1604
|
+
}
|
|
1605
|
+
case "json":
|
|
1606
|
+
case "richText":
|
|
1607
|
+
schema = { type: "object", additionalProperties: true };
|
|
1608
|
+
break;
|
|
1609
|
+
case "blocks":
|
|
1610
|
+
schema = {
|
|
1611
|
+
type: "array",
|
|
1612
|
+
items: {
|
|
1613
|
+
oneOf: field.blocks?.map((block) => {
|
|
1614
|
+
const { properties, required } = fieldsToProperties(block.fields);
|
|
1615
|
+
return {
|
|
1616
|
+
type: "object",
|
|
1617
|
+
properties: {
|
|
1618
|
+
blockType: { type: "string", enum: [block.slug] },
|
|
1619
|
+
...properties
|
|
1620
|
+
},
|
|
1621
|
+
required: ["blockType", ...required]
|
|
1622
|
+
};
|
|
1623
|
+
})
|
|
1624
|
+
}
|
|
1625
|
+
};
|
|
1626
|
+
break;
|
|
1627
|
+
default:
|
|
1628
|
+
schema = { type: "string" };
|
|
1629
|
+
}
|
|
1630
|
+
if (field.label) schema.description = field.label;
|
|
1631
|
+
return schema;
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
export {
|
|
1635
|
+
WORKFLOW_HISTORY_COLLECTION,
|
|
1636
|
+
LIFECYCLE_EVENTS_COLLECTION,
|
|
1637
|
+
publishingWorkflow,
|
|
1638
|
+
simplePublishingWorkflow,
|
|
1639
|
+
workflowCapabilities,
|
|
1640
|
+
canViewWorkflowDraft,
|
|
1641
|
+
availableWorkflowTransitions,
|
|
1642
|
+
initializeWorkflowDocument,
|
|
1643
|
+
materializeWorkflowDocument,
|
|
1644
|
+
createLifecycleEvent,
|
|
1645
|
+
dispatchLifecycleEvent,
|
|
1646
|
+
dispatchPendingLifecycleEvents,
|
|
1647
|
+
saveWorkflowDraft,
|
|
1648
|
+
createWorkflowDocument,
|
|
1649
|
+
transitionWorkflow,
|
|
1650
|
+
getAdminAuthCollection,
|
|
1651
|
+
resolveAdminAuthCollection,
|
|
1652
|
+
getPublicAdminAuthConfig,
|
|
1653
|
+
normalizeConfig,
|
|
1654
|
+
runCollectionHooks,
|
|
1655
|
+
executeFieldBeforeChange,
|
|
1656
|
+
executeFieldAfterRead,
|
|
1657
|
+
generateOpenApi
|
|
1658
|
+
};
|