@btst/plugin-cms 0.0.0-stage → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +17 -0
- package/dist/api/getters.cjs +328 -0
- package/dist/api/getters.mjs +314 -0
- package/dist/api/index.cjs +29 -0
- package/dist/api/index.d.cts +209 -0
- package/dist/api/index.d.mts +209 -0
- package/dist/api/index.d.ts +209 -0
- package/dist/api/index.mjs +7 -0
- package/dist/api/lifecycle-migrations.cjs +13 -0
- package/dist/api/lifecycle-migrations.mjs +11 -0
- package/dist/api/mutations.cjs +59 -0
- package/dist/api/mutations.mjs +57 -0
- package/dist/api/operations.cjs +1769 -0
- package/dist/api/operations.mjs +1762 -0
- package/dist/api/plugin.cjs +337 -0
- package/dist/api/plugin.mjs +331 -0
- package/dist/api/query-key-defs.cjs +44 -0
- package/dist/api/query-key-defs.mjs +39 -0
- package/dist/api/relations.cjs +76 -0
- package/dist/api/relations.mjs +70 -0
- package/dist/client/components/forms/content-form.cjs +286 -0
- package/dist/client/components/forms/content-form.mjs +284 -0
- package/dist/client/components/forms/file-upload.cjs +160 -0
- package/dist/client/components/forms/file-upload.mjs +158 -0
- package/dist/client/components/forms/relation-field.cjs +237 -0
- package/dist/client/components/forms/relation-field.mjs +235 -0
- package/dist/client/components/index.cjs +14 -0
- package/dist/client/components/index.d.cts +20 -0
- package/dist/client/components/index.d.mts +20 -0
- package/dist/client/components/index.d.ts +20 -0
- package/dist/client/components/index.mjs +10 -0
- package/dist/client/components/inverse-relations-panel.cjs +272 -0
- package/dist/client/components/inverse-relations-panel.mjs +270 -0
- package/dist/client/components/loading/dashboard-skeleton.cjs +18 -0
- package/dist/client/components/loading/dashboard-skeleton.mjs +16 -0
- package/dist/client/components/loading/editor-skeleton.cjs +32 -0
- package/dist/client/components/loading/editor-skeleton.mjs +30 -0
- package/dist/client/components/loading/list-skeleton.cjs +32 -0
- package/dist/client/components/loading/list-skeleton.mjs +30 -0
- package/dist/client/components/pages/404-page.cjs +26 -0
- package/dist/client/components/pages/404-page.mjs +24 -0
- package/dist/client/components/pages/content-editor-page.cjs +84 -0
- package/dist/client/components/pages/content-editor-page.internal.cjs +170 -0
- package/dist/client/components/pages/content-editor-page.internal.mjs +168 -0
- package/dist/client/components/pages/content-editor-page.mjs +82 -0
- package/dist/client/components/pages/content-list-page.cjs +49 -0
- package/dist/client/components/pages/content-list-page.internal.cjs +334 -0
- package/dist/client/components/pages/content-list-page.internal.mjs +332 -0
- package/dist/client/components/pages/content-list-page.mjs +47 -0
- package/dist/client/components/pages/dashboard-page.cjs +42 -0
- package/dist/client/components/pages/dashboard-page.internal.cjs +102 -0
- package/dist/client/components/pages/dashboard-page.internal.mjs +100 -0
- package/dist/client/components/pages/dashboard-page.mjs +40 -0
- package/dist/client/components/shared/default-error.cjs +17 -0
- package/dist/client/components/shared/default-error.mjs +15 -0
- package/dist/client/components/shared/empty-state.cjs +16 -0
- package/dist/client/components/shared/empty-state.mjs +14 -0
- package/dist/client/components/shared/page-wrapper.cjs +29 -0
- package/dist/client/components/shared/page-wrapper.mjs +27 -0
- package/dist/client/constants.cjs +5 -0
- package/dist/client/constants.mjs +3 -0
- package/dist/client/hooks/cms-hooks.cjs +300 -0
- package/dist/client/hooks/cms-hooks.mjs +280 -0
- package/dist/client/hooks/cms-resource.cjs +13 -0
- package/dist/client/hooks/cms-resource.mjs +11 -0
- package/dist/client/hooks/index.cjs +25 -0
- package/dist/client/hooks/index.d.cts +693 -0
- package/dist/client/hooks/index.d.mts +693 -0
- package/dist/client/hooks/index.d.ts +693 -0
- package/dist/client/hooks/index.mjs +1 -0
- package/dist/client/index.cjs +15 -0
- package/dist/client/index.d.cts +654 -0
- package/dist/client/index.d.mts +654 -0
- package/dist/client/index.d.ts +654 -0
- package/dist/client/index.mjs +3 -0
- package/dist/client/overrides.cjs +26 -0
- package/dist/client/overrides.mjs +24 -0
- package/dist/client/plugin.cjs +316 -0
- package/dist/client/plugin.mjs +314 -0
- package/dist/client.css +3 -0
- package/dist/db.cjs +118 -0
- package/dist/db.mjs +116 -0
- package/dist/permissions.cjs +46 -0
- package/dist/permissions.d.cts +42 -0
- package/dist/permissions.d.mts +42 -0
- package/dist/permissions.d.ts +42 -0
- package/dist/permissions.mjs +44 -0
- package/dist/query-keys.cjs +229 -0
- package/dist/query-keys.d.cts +8 -0
- package/dist/query-keys.d.mts +8 -0
- package/dist/query-keys.d.ts +8 -0
- package/dist/query-keys.mjs +225 -0
- package/dist/schemas.cjs +81 -0
- package/dist/schemas.mjs +72 -0
- package/dist/shared/plugin-cms.BBshPRkb.d.cts +6 -0
- package/dist/shared/plugin-cms.BBshPRkb.d.mts +6 -0
- package/dist/shared/plugin-cms.BBshPRkb.d.ts +6 -0
- package/dist/shared/plugin-cms.Bptwe33B.d.cts +1749 -0
- package/dist/shared/plugin-cms.Csx7tNDO.d.mts +1749 -0
- package/dist/shared/plugin-cms.eNW9_FO5.d.ts +1749 -0
- package/dist/style.css +6 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.cts +406 -0
- package/dist/types.d.mts +406 -0
- package/dist/types.d.ts +406 -0
- package/dist/types.mjs +1 -0
- package/dist/ui/src/components/accordion.cjs +81 -0
- package/dist/ui/src/components/accordion.mjs +62 -0
- package/dist/ui/src/components/alert-dialog.cjs +157 -0
- package/dist/ui/src/components/alert-dialog.mjs +132 -0
- package/dist/ui/src/components/auto-form/common/label.cjs +18 -0
- package/dist/ui/src/components/auto-form/common/label.mjs +16 -0
- package/dist/ui/src/components/auto-form/common/tooltip.cjs +9 -0
- package/dist/ui/src/components/auto-form/common/tooltip.mjs +7 -0
- package/dist/ui/src/components/auto-form/config.cjs +40 -0
- package/dist/ui/src/components/auto-form/config.mjs +37 -0
- package/dist/ui/src/components/auto-form/dependencies.cjs +48 -0
- package/dist/ui/src/components/auto-form/dependencies.mjs +46 -0
- package/dist/ui/src/components/auto-form/fields/array.cjs +266 -0
- package/dist/ui/src/components/auto-form/fields/array.mjs +264 -0
- package/dist/ui/src/components/auto-form/fields/checkbox.cjs +38 -0
- package/dist/ui/src/components/auto-form/fields/checkbox.mjs +36 -0
- package/dist/ui/src/components/auto-form/fields/date.cjs +67 -0
- package/dist/ui/src/components/auto-form/fields/date.mjs +65 -0
- package/dist/ui/src/components/auto-form/fields/enum.cjs +73 -0
- package/dist/ui/src/components/auto-form/fields/enum.mjs +71 -0
- package/dist/ui/src/components/auto-form/fields/input.cjs +32 -0
- package/dist/ui/src/components/auto-form/fields/input.mjs +30 -0
- package/dist/ui/src/components/auto-form/fields/number.cjs +31 -0
- package/dist/ui/src/components/auto-form/fields/number.mjs +29 -0
- package/dist/ui/src/components/auto-form/fields/object.cjs +233 -0
- package/dist/ui/src/components/auto-form/fields/object.mjs +217 -0
- package/dist/ui/src/components/auto-form/fields/radio-group.cjs +67 -0
- package/dist/ui/src/components/auto-form/fields/radio-group.mjs +65 -0
- package/dist/ui/src/components/auto-form/fields/switch.cjs +38 -0
- package/dist/ui/src/components/auto-form/fields/switch.mjs +36 -0
- package/dist/ui/src/components/auto-form/fields/textarea.cjs +31 -0
- package/dist/ui/src/components/auto-form/fields/textarea.mjs +29 -0
- package/dist/ui/src/components/auto-form/helpers.cjs +270 -0
- package/dist/ui/src/components/auto-form/helpers.mjs +259 -0
- package/dist/ui/src/components/auto-form/index.cjs +92 -0
- package/dist/ui/src/components/auto-form/index.mjs +86 -0
- package/dist/ui/src/components/auto-form/stepped-auto-form.cjs +374 -0
- package/dist/ui/src/components/auto-form/stepped-auto-form.mjs +368 -0
- package/dist/ui/src/components/auto-form/types.cjs +11 -0
- package/dist/ui/src/components/auto-form/types.mjs +9 -0
- package/dist/ui/src/components/badge.cjs +42 -0
- package/dist/ui/src/components/badge.mjs +39 -0
- package/dist/ui/src/components/button.cjs +56 -0
- package/dist/ui/src/components/button.mjs +53 -0
- package/dist/ui/src/components/calendar.cjs +207 -0
- package/dist/ui/src/components/calendar.mjs +190 -0
- package/dist/ui/src/components/card.cjs +56 -0
- package/dist/ui/src/components/card.mjs +51 -0
- package/dist/ui/src/components/checkbox.cjs +48 -0
- package/dist/ui/src/components/checkbox.mjs +32 -0
- package/dist/ui/src/components/command.cjs +78 -0
- package/dist/ui/src/components/command.mjs +73 -0
- package/dist/ui/src/components/date-picker.cjs +41 -0
- package/dist/ui/src/components/date-picker.mjs +39 -0
- package/dist/ui/src/components/dialog.cjs +119 -0
- package/dist/ui/src/components/dialog.mjs +97 -0
- package/dist/ui/src/components/form.cjs +120 -0
- package/dist/ui/src/components/form.mjs +98 -0
- package/dist/ui/src/components/input.cjs +23 -0
- package/dist/ui/src/components/input.mjs +21 -0
- package/dist/ui/src/components/label.cjs +39 -0
- package/dist/ui/src/components/label.mjs +23 -0
- package/dist/ui/src/components/multi-select.cjs +555 -0
- package/dist/ui/src/components/multi-select.mjs +540 -0
- package/dist/ui/src/components/page-layout.cjs +25 -0
- package/dist/ui/src/components/page-layout.mjs +23 -0
- package/dist/ui/src/components/page-wrapper.cjs +20 -0
- package/dist/ui/src/components/page-wrapper.mjs +18 -0
- package/dist/ui/src/components/popover.cjs +60 -0
- package/dist/ui/src/components/popover.mjs +42 -0
- package/dist/ui/src/components/radio-group.cjs +62 -0
- package/dist/ui/src/components/radio-group.mjs +45 -0
- package/dist/ui/src/components/select.cjs +161 -0
- package/dist/ui/src/components/select.mjs +139 -0
- package/dist/ui/src/components/separator.cjs +43 -0
- package/dist/ui/src/components/separator.mjs +27 -0
- package/dist/ui/src/components/skeleton.cjs +17 -0
- package/dist/ui/src/components/skeleton.mjs +15 -0
- package/dist/ui/src/components/stack-attribution.cjs +24 -0
- package/dist/ui/src/components/stack-attribution.mjs +22 -0
- package/dist/ui/src/components/switch.cjs +48 -0
- package/dist/ui/src/components/switch.mjs +32 -0
- package/dist/ui/src/components/table.cjs +89 -0
- package/dist/ui/src/components/table.mjs +82 -0
- package/dist/ui/src/components/textarea.cjs +20 -0
- package/dist/ui/src/components/textarea.mjs +18 -0
- package/dist/ui/src/hooks/use-debounce.cjs +16 -0
- package/dist/ui/src/hooks/use-debounce.mjs +14 -0
- package/dist/ui/src/hooks/use-route-lifecycle.cjs +31 -0
- package/dist/ui/src/hooks/use-route-lifecycle.mjs +29 -0
- package/dist/ui/src/lib/schema-converter.cjs +133 -0
- package/dist/ui/src/lib/schema-converter.mjs +127 -0
- package/dist/ui/src/lib/utils.cjs +10 -0
- package/dist/ui/src/lib/utils.mjs +8 -0
- package/dist/utils.cjs +19 -0
- package/dist/utils.mjs +12 -0
- package/package.json +170 -3
- package/README.md +0 -3
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const api = require('@btst/stack/plugins/api');
|
|
4
|
+
const z = require('zod');
|
|
5
|
+
const schemaConverter = require('../ui/src/lib/schema-converter.cjs');
|
|
6
|
+
const db = require('../db.cjs');
|
|
7
|
+
const schemas = require('../schemas.cjs');
|
|
8
|
+
const getters = require('./getters.cjs');
|
|
9
|
+
const operations = require('./operations.cjs');
|
|
10
|
+
const queryKeyDefs = require('./query-key-defs.cjs');
|
|
11
|
+
|
|
12
|
+
function schemasEqual(stored, configured) {
|
|
13
|
+
const normalize = (key, value) => key !== "properties" && value && typeof value === "object" && !Array.isArray(value) ? Object.fromEntries(
|
|
14
|
+
Object.entries(value).sort(([a], [b]) => a.localeCompare(b))
|
|
15
|
+
) : value;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.stringify(JSON.parse(stored), normalize) === JSON.stringify(JSON.parse(configured), normalize);
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function syncContentTypes(adapter, config) {
|
|
23
|
+
for (const definition of config.contentTypes) {
|
|
24
|
+
const jsonSchema = JSON.stringify(schemaConverter.zodToFormSchema(definition.schema));
|
|
25
|
+
const existing = await adapter.findOne({
|
|
26
|
+
model: "contentType",
|
|
27
|
+
where: [
|
|
28
|
+
{ field: "slug", value: definition.slug, operator: "eq" }
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
if (existing) {
|
|
32
|
+
if (existing.name === definition.name && (existing.description ?? null) === (definition.description ?? null) && schemasEqual(existing.jsonSchema, jsonSchema) && existing.fieldConfig == null && existing.autoFormVersion === 2)
|
|
33
|
+
continue;
|
|
34
|
+
await adapter.update({
|
|
35
|
+
model: "contentType",
|
|
36
|
+
where: [{ field: "id", value: existing.id, operator: "eq" }],
|
|
37
|
+
update: {
|
|
38
|
+
name: definition.name,
|
|
39
|
+
description: definition.description ?? null,
|
|
40
|
+
jsonSchema,
|
|
41
|
+
fieldConfig: null,
|
|
42
|
+
autoFormVersion: 2,
|
|
43
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
await adapter.create({
|
|
50
|
+
model: "contentType",
|
|
51
|
+
data: {
|
|
52
|
+
name: definition.name,
|
|
53
|
+
slug: definition.slug,
|
|
54
|
+
description: definition.description ?? null,
|
|
55
|
+
jsonSchema,
|
|
56
|
+
fieldConfig: null,
|
|
57
|
+
autoFormVersion: 2,
|
|
58
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
59
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
} catch (error) {
|
|
63
|
+
const nowExists = await adapter.findOne({
|
|
64
|
+
model: "contentType",
|
|
65
|
+
where: [
|
|
66
|
+
{ field: "slug", value: definition.slug, operator: "eq" }
|
|
67
|
+
]
|
|
68
|
+
});
|
|
69
|
+
if (nowExists) continue;
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Failed to create content type "${definition.slug}": ${error instanceof Error ? error.message : "Unknown database error"}`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const cmsBackendPlugin = (config) => {
|
|
77
|
+
let syncPromise = null;
|
|
78
|
+
const ensureSynced = (adapter) => {
|
|
79
|
+
if (!syncPromise) {
|
|
80
|
+
syncPromise = syncContentTypes(adapter, config).catch((error) => {
|
|
81
|
+
syncPromise = null;
|
|
82
|
+
throw error;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return syncPromise;
|
|
86
|
+
};
|
|
87
|
+
const getContentTypesWithCounts = async (adapter) => {
|
|
88
|
+
const contentTypes = await getters.getAllContentTypes(adapter);
|
|
89
|
+
return Promise.all(
|
|
90
|
+
contentTypes.map(async (contentType) => ({
|
|
91
|
+
...contentType,
|
|
92
|
+
itemCount: await adapter.count({
|
|
93
|
+
model: "contentItem",
|
|
94
|
+
where: [
|
|
95
|
+
{
|
|
96
|
+
field: "contentTypeId",
|
|
97
|
+
value: contentType.id,
|
|
98
|
+
operator: "eq"
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
})
|
|
102
|
+
}))
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
const createCMSPrefetchForRoute = (adapter) => async function prefetchForRoute(key, queryClient, params) {
|
|
106
|
+
await ensureSynced(adapter);
|
|
107
|
+
switch (key) {
|
|
108
|
+
case "dashboard":
|
|
109
|
+
case "newContent":
|
|
110
|
+
queryClient.setQueryData(
|
|
111
|
+
queryKeyDefs.CMS_QUERY_KEYS.typesList(),
|
|
112
|
+
await getContentTypesWithCounts(adapter)
|
|
113
|
+
);
|
|
114
|
+
break;
|
|
115
|
+
case "contentList": {
|
|
116
|
+
const typeSlug = params?.typeSlug ?? "";
|
|
117
|
+
const options = schemas.createListContentQuerySchema(
|
|
118
|
+
config.maxPageSize
|
|
119
|
+
).parse({
|
|
120
|
+
limit: params?.limit,
|
|
121
|
+
search: params?.search,
|
|
122
|
+
view: params?.view,
|
|
123
|
+
fields: params?.fields
|
|
124
|
+
});
|
|
125
|
+
const [contentTypes, contentItems] = await Promise.all([
|
|
126
|
+
getContentTypesWithCounts(adapter),
|
|
127
|
+
getters.getAllContentItems(adapter, typeSlug, options)
|
|
128
|
+
]);
|
|
129
|
+
queryClient.setQueryData(queryKeyDefs.CMS_QUERY_KEYS.typesList(), contentTypes);
|
|
130
|
+
queryClient.setQueryData(
|
|
131
|
+
queryKeyDefs.CMS_QUERY_KEYS.contentList({ typeSlug, ...options }),
|
|
132
|
+
{
|
|
133
|
+
pages: [
|
|
134
|
+
{
|
|
135
|
+
items: contentItems.items,
|
|
136
|
+
total: contentItems.total,
|
|
137
|
+
limit: contentItems.limit ?? 20,
|
|
138
|
+
offset: contentItems.offset ?? 0
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
pageParams: [0]
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case "editContent": {
|
|
147
|
+
const typeSlug = params?.typeSlug ?? "";
|
|
148
|
+
const id = params?.id ?? "";
|
|
149
|
+
const options = schemas.contentReadOptionsSchema.parse({
|
|
150
|
+
view: params?.view,
|
|
151
|
+
fields: params?.fields
|
|
152
|
+
});
|
|
153
|
+
const [contentTypes, item] = await Promise.all([
|
|
154
|
+
getContentTypesWithCounts(adapter),
|
|
155
|
+
id ? getters.getContentItemById(adapter, id, options) : Promise.resolve(null)
|
|
156
|
+
]);
|
|
157
|
+
queryClient.setQueryData(queryKeyDefs.CMS_QUERY_KEYS.typesList(), contentTypes);
|
|
158
|
+
if (id) {
|
|
159
|
+
queryClient.setQueryData(
|
|
160
|
+
queryKeyDefs.CMS_QUERY_KEYS.contentDetail(typeSlug, id, options),
|
|
161
|
+
item
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
return api.defineBackendPlugin({
|
|
169
|
+
id: "cms",
|
|
170
|
+
dbPlugin: db.cmsSchema,
|
|
171
|
+
operations: (adapter) => operations.createCMSOperations(adapter, {
|
|
172
|
+
ensureSynced: () => ensureSynced(adapter),
|
|
173
|
+
getContentTypesWithCounts: () => getContentTypesWithCounts(adapter),
|
|
174
|
+
...config.maxPageSize !== void 0 ? { maxPageSize: config.maxPageSize } : {},
|
|
175
|
+
...config.hooks ? { hooks: config.hooks } : {}
|
|
176
|
+
}),
|
|
177
|
+
/** Lower-level SSG helper that intentionally bypasses auth and hooks. */
|
|
178
|
+
raw: (adapter) => ({
|
|
179
|
+
prefetchForRoute: createCMSPrefetchForRoute(adapter)
|
|
180
|
+
}),
|
|
181
|
+
routes: (_adapter, _context, operations$1) => {
|
|
182
|
+
const listQuerySchema = schemas.createListContentQuerySchema(
|
|
183
|
+
config.maxPageSize
|
|
184
|
+
).extend(schemas.contentReadQuerySchema.shape);
|
|
185
|
+
const paginationSchema = z.z.object({
|
|
186
|
+
limit: z.z.coerce.number().min(1).max(config.maxPageSize ?? schemas.DEFAULT_MAX_PAGE_SIZE).optional().default(20),
|
|
187
|
+
offset: z.z.coerce.number().min(0).optional().default(0)
|
|
188
|
+
});
|
|
189
|
+
const listContentTypes = api.createEndpoint(
|
|
190
|
+
"/content-types",
|
|
191
|
+
{ method: "GET", requireRequest: true },
|
|
192
|
+
operations$1.listContentTypes.route(() => ({}))
|
|
193
|
+
);
|
|
194
|
+
const getContentTypeBySlug = api.createEndpoint(
|
|
195
|
+
"/content-types/:slug",
|
|
196
|
+
{
|
|
197
|
+
method: "GET",
|
|
198
|
+
params: operations.CMSContentTypeParamsSchema,
|
|
199
|
+
requireRequest: true
|
|
200
|
+
},
|
|
201
|
+
operations$1.getContentTypeBySlug.route((ctx) => ctx.params)
|
|
202
|
+
);
|
|
203
|
+
const listContentItems = api.createEndpoint(
|
|
204
|
+
"/content/:typeSlug",
|
|
205
|
+
{
|
|
206
|
+
method: "GET",
|
|
207
|
+
params: z.z.object({ typeSlug: z.z.string().min(1) }),
|
|
208
|
+
query: listQuerySchema,
|
|
209
|
+
requireRequest: true
|
|
210
|
+
},
|
|
211
|
+
operations$1.listContentItems.route((ctx) => ({
|
|
212
|
+
typeSlug: ctx.params.typeSlug,
|
|
213
|
+
query: ctx.query
|
|
214
|
+
}))
|
|
215
|
+
);
|
|
216
|
+
const getContentItem = api.createEndpoint(
|
|
217
|
+
"/content/:typeSlug/:id",
|
|
218
|
+
{
|
|
219
|
+
method: "GET",
|
|
220
|
+
params: operations.CMSContentItemParamsSchema,
|
|
221
|
+
query: schemas.contentReadQuerySchema,
|
|
222
|
+
requireRequest: true
|
|
223
|
+
},
|
|
224
|
+
operations$1.getContentItem.route((ctx) => ({
|
|
225
|
+
...ctx.params,
|
|
226
|
+
...ctx.query
|
|
227
|
+
}))
|
|
228
|
+
);
|
|
229
|
+
const createContentItem = api.createEndpoint(
|
|
230
|
+
"/content/:typeSlug",
|
|
231
|
+
{
|
|
232
|
+
method: "POST",
|
|
233
|
+
params: z.z.object({ typeSlug: z.z.string().min(1) }),
|
|
234
|
+
body: operations.CMSCreateContentItemBodySchema,
|
|
235
|
+
requireRequest: true
|
|
236
|
+
},
|
|
237
|
+
operations$1.createContentItem.route((ctx) => ({
|
|
238
|
+
typeSlug: ctx.params.typeSlug,
|
|
239
|
+
body: ctx.body
|
|
240
|
+
}))
|
|
241
|
+
);
|
|
242
|
+
const updateContentItem = api.createEndpoint(
|
|
243
|
+
"/content/:typeSlug/:id",
|
|
244
|
+
{
|
|
245
|
+
method: "PUT",
|
|
246
|
+
params: operations.CMSContentItemParamsSchema,
|
|
247
|
+
body: operations.CMSUpdateContentItemBodySchema,
|
|
248
|
+
requireRequest: true
|
|
249
|
+
},
|
|
250
|
+
operations$1.updateContentItem.route((ctx) => ({
|
|
251
|
+
...ctx.params,
|
|
252
|
+
body: ctx.body
|
|
253
|
+
}))
|
|
254
|
+
);
|
|
255
|
+
const deleteContentItem = api.createEndpoint(
|
|
256
|
+
"/content/:typeSlug/:id",
|
|
257
|
+
{
|
|
258
|
+
method: "DELETE",
|
|
259
|
+
params: operations.CMSContentItemParamsSchema,
|
|
260
|
+
requireRequest: true
|
|
261
|
+
},
|
|
262
|
+
operations$1.deleteContentItem.route((ctx) => ctx.params)
|
|
263
|
+
);
|
|
264
|
+
const getContentItemPopulated = api.createEndpoint(
|
|
265
|
+
"/content/:typeSlug/:id/populated",
|
|
266
|
+
{
|
|
267
|
+
method: "GET",
|
|
268
|
+
params: operations.CMSContentItemParamsSchema,
|
|
269
|
+
requireRequest: true
|
|
270
|
+
},
|
|
271
|
+
operations$1.getContentItemPopulated.route((ctx) => ctx.params)
|
|
272
|
+
);
|
|
273
|
+
const listContentByRelation = api.createEndpoint(
|
|
274
|
+
"/content/:typeSlug/by-relation",
|
|
275
|
+
{
|
|
276
|
+
method: "GET",
|
|
277
|
+
params: z.z.object({ typeSlug: z.z.string().min(1) }),
|
|
278
|
+
query: z.z.object({ field: z.z.string(), targetId: z.z.string() }).merge(paginationSchema).merge(schemas.contentReadQuerySchema),
|
|
279
|
+
requireRequest: true
|
|
280
|
+
},
|
|
281
|
+
operations$1.listContentByRelation.route((ctx) => ({
|
|
282
|
+
typeSlug: ctx.params.typeSlug,
|
|
283
|
+
query: ctx.query
|
|
284
|
+
}))
|
|
285
|
+
);
|
|
286
|
+
const getInverseRelations = api.createEndpoint(
|
|
287
|
+
"/content-types/:slug/inverse-relations",
|
|
288
|
+
{
|
|
289
|
+
method: "GET",
|
|
290
|
+
params: operations.CMSContentTypeParamsSchema,
|
|
291
|
+
query: z.z.object({ itemId: z.z.string().optional() }),
|
|
292
|
+
requireRequest: true
|
|
293
|
+
},
|
|
294
|
+
operations$1.getInverseRelations.route((ctx) => ({
|
|
295
|
+
slug: ctx.params.slug,
|
|
296
|
+
query: ctx.query
|
|
297
|
+
}))
|
|
298
|
+
);
|
|
299
|
+
const listInverseRelationItems = api.createEndpoint(
|
|
300
|
+
"/content-types/:slug/inverse-relations/:sourceType",
|
|
301
|
+
{
|
|
302
|
+
method: "GET",
|
|
303
|
+
params: z.z.object({
|
|
304
|
+
slug: z.z.string().min(1),
|
|
305
|
+
sourceType: z.z.string().min(1)
|
|
306
|
+
}),
|
|
307
|
+
query: z.z.object({ itemId: z.z.string(), fieldName: z.z.string() }).merge(paginationSchema).merge(schemas.contentReadQuerySchema),
|
|
308
|
+
requireRequest: true
|
|
309
|
+
},
|
|
310
|
+
operations$1.listInverseRelationItems.route((ctx) => ({
|
|
311
|
+
slug: ctx.params.slug,
|
|
312
|
+
sourceType: ctx.params.sourceType,
|
|
313
|
+
query: ctx.query
|
|
314
|
+
}))
|
|
315
|
+
);
|
|
316
|
+
return {
|
|
317
|
+
listContentTypes,
|
|
318
|
+
getContentTypeBySlug,
|
|
319
|
+
listContentItems,
|
|
320
|
+
getContentItem,
|
|
321
|
+
createContentItem,
|
|
322
|
+
updateContentItem,
|
|
323
|
+
deleteContentItem,
|
|
324
|
+
getContentItemPopulated,
|
|
325
|
+
listContentByRelation,
|
|
326
|
+
getInverseRelations,
|
|
327
|
+
listInverseRelationItems
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
exports.CMSContentItemParamsSchema = operations.CMSContentItemParamsSchema;
|
|
334
|
+
exports.CMSContentTypeParamsSchema = operations.CMSContentTypeParamsSchema;
|
|
335
|
+
exports.CMSCreateContentItemBodySchema = operations.CMSCreateContentItemBodySchema;
|
|
336
|
+
exports.CMSUpdateContentItemBodySchema = operations.CMSUpdateContentItemBodySchema;
|
|
337
|
+
exports.cmsBackendPlugin = cmsBackendPlugin;
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { defineBackendPlugin, createEndpoint } from '@btst/stack/plugins/api';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { zodToFormSchema } from '../ui/src/lib/schema-converter.mjs';
|
|
4
|
+
import { cmsSchema } from '../db.mjs';
|
|
5
|
+
import { createListContentQuerySchema, contentReadQuerySchema, DEFAULT_MAX_PAGE_SIZE, contentReadOptionsSchema } from '../schemas.mjs';
|
|
6
|
+
import { getContentItemById, getAllContentItems, getAllContentTypes } from './getters.mjs';
|
|
7
|
+
import { CMSContentTypeParamsSchema, CMSContentItemParamsSchema, CMSCreateContentItemBodySchema, CMSUpdateContentItemBodySchema, createCMSOperations } from './operations.mjs';
|
|
8
|
+
import { CMS_QUERY_KEYS } from './query-key-defs.mjs';
|
|
9
|
+
|
|
10
|
+
function schemasEqual(stored, configured) {
|
|
11
|
+
const normalize = (key, value) => key !== "properties" && value && typeof value === "object" && !Array.isArray(value) ? Object.fromEntries(
|
|
12
|
+
Object.entries(value).sort(([a], [b]) => a.localeCompare(b))
|
|
13
|
+
) : value;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.stringify(JSON.parse(stored), normalize) === JSON.stringify(JSON.parse(configured), normalize);
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function syncContentTypes(adapter, config) {
|
|
21
|
+
for (const definition of config.contentTypes) {
|
|
22
|
+
const jsonSchema = JSON.stringify(zodToFormSchema(definition.schema));
|
|
23
|
+
const existing = await adapter.findOne({
|
|
24
|
+
model: "contentType",
|
|
25
|
+
where: [
|
|
26
|
+
{ field: "slug", value: definition.slug, operator: "eq" }
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
if (existing) {
|
|
30
|
+
if (existing.name === definition.name && (existing.description ?? null) === (definition.description ?? null) && schemasEqual(existing.jsonSchema, jsonSchema) && existing.fieldConfig == null && existing.autoFormVersion === 2)
|
|
31
|
+
continue;
|
|
32
|
+
await adapter.update({
|
|
33
|
+
model: "contentType",
|
|
34
|
+
where: [{ field: "id", value: existing.id, operator: "eq" }],
|
|
35
|
+
update: {
|
|
36
|
+
name: definition.name,
|
|
37
|
+
description: definition.description ?? null,
|
|
38
|
+
jsonSchema,
|
|
39
|
+
fieldConfig: null,
|
|
40
|
+
autoFormVersion: 2,
|
|
41
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
await adapter.create({
|
|
48
|
+
model: "contentType",
|
|
49
|
+
data: {
|
|
50
|
+
name: definition.name,
|
|
51
|
+
slug: definition.slug,
|
|
52
|
+
description: definition.description ?? null,
|
|
53
|
+
jsonSchema,
|
|
54
|
+
fieldConfig: null,
|
|
55
|
+
autoFormVersion: 2,
|
|
56
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
57
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const nowExists = await adapter.findOne({
|
|
62
|
+
model: "contentType",
|
|
63
|
+
where: [
|
|
64
|
+
{ field: "slug", value: definition.slug, operator: "eq" }
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
if (nowExists) continue;
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Failed to create content type "${definition.slug}": ${error instanceof Error ? error.message : "Unknown database error"}`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const cmsBackendPlugin = (config) => {
|
|
75
|
+
let syncPromise = null;
|
|
76
|
+
const ensureSynced = (adapter) => {
|
|
77
|
+
if (!syncPromise) {
|
|
78
|
+
syncPromise = syncContentTypes(adapter, config).catch((error) => {
|
|
79
|
+
syncPromise = null;
|
|
80
|
+
throw error;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return syncPromise;
|
|
84
|
+
};
|
|
85
|
+
const getContentTypesWithCounts = async (adapter) => {
|
|
86
|
+
const contentTypes = await getAllContentTypes(adapter);
|
|
87
|
+
return Promise.all(
|
|
88
|
+
contentTypes.map(async (contentType) => ({
|
|
89
|
+
...contentType,
|
|
90
|
+
itemCount: await adapter.count({
|
|
91
|
+
model: "contentItem",
|
|
92
|
+
where: [
|
|
93
|
+
{
|
|
94
|
+
field: "contentTypeId",
|
|
95
|
+
value: contentType.id,
|
|
96
|
+
operator: "eq"
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
})
|
|
100
|
+
}))
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
const createCMSPrefetchForRoute = (adapter) => async function prefetchForRoute(key, queryClient, params) {
|
|
104
|
+
await ensureSynced(adapter);
|
|
105
|
+
switch (key) {
|
|
106
|
+
case "dashboard":
|
|
107
|
+
case "newContent":
|
|
108
|
+
queryClient.setQueryData(
|
|
109
|
+
CMS_QUERY_KEYS.typesList(),
|
|
110
|
+
await getContentTypesWithCounts(adapter)
|
|
111
|
+
);
|
|
112
|
+
break;
|
|
113
|
+
case "contentList": {
|
|
114
|
+
const typeSlug = params?.typeSlug ?? "";
|
|
115
|
+
const options = createListContentQuerySchema(
|
|
116
|
+
config.maxPageSize
|
|
117
|
+
).parse({
|
|
118
|
+
limit: params?.limit,
|
|
119
|
+
search: params?.search,
|
|
120
|
+
view: params?.view,
|
|
121
|
+
fields: params?.fields
|
|
122
|
+
});
|
|
123
|
+
const [contentTypes, contentItems] = await Promise.all([
|
|
124
|
+
getContentTypesWithCounts(adapter),
|
|
125
|
+
getAllContentItems(adapter, typeSlug, options)
|
|
126
|
+
]);
|
|
127
|
+
queryClient.setQueryData(CMS_QUERY_KEYS.typesList(), contentTypes);
|
|
128
|
+
queryClient.setQueryData(
|
|
129
|
+
CMS_QUERY_KEYS.contentList({ typeSlug, ...options }),
|
|
130
|
+
{
|
|
131
|
+
pages: [
|
|
132
|
+
{
|
|
133
|
+
items: contentItems.items,
|
|
134
|
+
total: contentItems.total,
|
|
135
|
+
limit: contentItems.limit ?? 20,
|
|
136
|
+
offset: contentItems.offset ?? 0
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
pageParams: [0]
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case "editContent": {
|
|
145
|
+
const typeSlug = params?.typeSlug ?? "";
|
|
146
|
+
const id = params?.id ?? "";
|
|
147
|
+
const options = contentReadOptionsSchema.parse({
|
|
148
|
+
view: params?.view,
|
|
149
|
+
fields: params?.fields
|
|
150
|
+
});
|
|
151
|
+
const [contentTypes, item] = await Promise.all([
|
|
152
|
+
getContentTypesWithCounts(adapter),
|
|
153
|
+
id ? getContentItemById(adapter, id, options) : Promise.resolve(null)
|
|
154
|
+
]);
|
|
155
|
+
queryClient.setQueryData(CMS_QUERY_KEYS.typesList(), contentTypes);
|
|
156
|
+
if (id) {
|
|
157
|
+
queryClient.setQueryData(
|
|
158
|
+
CMS_QUERY_KEYS.contentDetail(typeSlug, id, options),
|
|
159
|
+
item
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
return defineBackendPlugin({
|
|
167
|
+
id: "cms",
|
|
168
|
+
dbPlugin: cmsSchema,
|
|
169
|
+
operations: (adapter) => createCMSOperations(adapter, {
|
|
170
|
+
ensureSynced: () => ensureSynced(adapter),
|
|
171
|
+
getContentTypesWithCounts: () => getContentTypesWithCounts(adapter),
|
|
172
|
+
...config.maxPageSize !== void 0 ? { maxPageSize: config.maxPageSize } : {},
|
|
173
|
+
...config.hooks ? { hooks: config.hooks } : {}
|
|
174
|
+
}),
|
|
175
|
+
/** Lower-level SSG helper that intentionally bypasses auth and hooks. */
|
|
176
|
+
raw: (adapter) => ({
|
|
177
|
+
prefetchForRoute: createCMSPrefetchForRoute(adapter)
|
|
178
|
+
}),
|
|
179
|
+
routes: (_adapter, _context, operations) => {
|
|
180
|
+
const listQuerySchema = createListContentQuerySchema(
|
|
181
|
+
config.maxPageSize
|
|
182
|
+
).extend(contentReadQuerySchema.shape);
|
|
183
|
+
const paginationSchema = z.object({
|
|
184
|
+
limit: z.coerce.number().min(1).max(config.maxPageSize ?? DEFAULT_MAX_PAGE_SIZE).optional().default(20),
|
|
185
|
+
offset: z.coerce.number().min(0).optional().default(0)
|
|
186
|
+
});
|
|
187
|
+
const listContentTypes = createEndpoint(
|
|
188
|
+
"/content-types",
|
|
189
|
+
{ method: "GET", requireRequest: true },
|
|
190
|
+
operations.listContentTypes.route(() => ({}))
|
|
191
|
+
);
|
|
192
|
+
const getContentTypeBySlug = createEndpoint(
|
|
193
|
+
"/content-types/:slug",
|
|
194
|
+
{
|
|
195
|
+
method: "GET",
|
|
196
|
+
params: CMSContentTypeParamsSchema,
|
|
197
|
+
requireRequest: true
|
|
198
|
+
},
|
|
199
|
+
operations.getContentTypeBySlug.route((ctx) => ctx.params)
|
|
200
|
+
);
|
|
201
|
+
const listContentItems = createEndpoint(
|
|
202
|
+
"/content/:typeSlug",
|
|
203
|
+
{
|
|
204
|
+
method: "GET",
|
|
205
|
+
params: z.object({ typeSlug: z.string().min(1) }),
|
|
206
|
+
query: listQuerySchema,
|
|
207
|
+
requireRequest: true
|
|
208
|
+
},
|
|
209
|
+
operations.listContentItems.route((ctx) => ({
|
|
210
|
+
typeSlug: ctx.params.typeSlug,
|
|
211
|
+
query: ctx.query
|
|
212
|
+
}))
|
|
213
|
+
);
|
|
214
|
+
const getContentItem = createEndpoint(
|
|
215
|
+
"/content/:typeSlug/:id",
|
|
216
|
+
{
|
|
217
|
+
method: "GET",
|
|
218
|
+
params: CMSContentItemParamsSchema,
|
|
219
|
+
query: contentReadQuerySchema,
|
|
220
|
+
requireRequest: true
|
|
221
|
+
},
|
|
222
|
+
operations.getContentItem.route((ctx) => ({
|
|
223
|
+
...ctx.params,
|
|
224
|
+
...ctx.query
|
|
225
|
+
}))
|
|
226
|
+
);
|
|
227
|
+
const createContentItem = createEndpoint(
|
|
228
|
+
"/content/:typeSlug",
|
|
229
|
+
{
|
|
230
|
+
method: "POST",
|
|
231
|
+
params: z.object({ typeSlug: z.string().min(1) }),
|
|
232
|
+
body: CMSCreateContentItemBodySchema,
|
|
233
|
+
requireRequest: true
|
|
234
|
+
},
|
|
235
|
+
operations.createContentItem.route((ctx) => ({
|
|
236
|
+
typeSlug: ctx.params.typeSlug,
|
|
237
|
+
body: ctx.body
|
|
238
|
+
}))
|
|
239
|
+
);
|
|
240
|
+
const updateContentItem = createEndpoint(
|
|
241
|
+
"/content/:typeSlug/:id",
|
|
242
|
+
{
|
|
243
|
+
method: "PUT",
|
|
244
|
+
params: CMSContentItemParamsSchema,
|
|
245
|
+
body: CMSUpdateContentItemBodySchema,
|
|
246
|
+
requireRequest: true
|
|
247
|
+
},
|
|
248
|
+
operations.updateContentItem.route((ctx) => ({
|
|
249
|
+
...ctx.params,
|
|
250
|
+
body: ctx.body
|
|
251
|
+
}))
|
|
252
|
+
);
|
|
253
|
+
const deleteContentItem = createEndpoint(
|
|
254
|
+
"/content/:typeSlug/:id",
|
|
255
|
+
{
|
|
256
|
+
method: "DELETE",
|
|
257
|
+
params: CMSContentItemParamsSchema,
|
|
258
|
+
requireRequest: true
|
|
259
|
+
},
|
|
260
|
+
operations.deleteContentItem.route((ctx) => ctx.params)
|
|
261
|
+
);
|
|
262
|
+
const getContentItemPopulated = createEndpoint(
|
|
263
|
+
"/content/:typeSlug/:id/populated",
|
|
264
|
+
{
|
|
265
|
+
method: "GET",
|
|
266
|
+
params: CMSContentItemParamsSchema,
|
|
267
|
+
requireRequest: true
|
|
268
|
+
},
|
|
269
|
+
operations.getContentItemPopulated.route((ctx) => ctx.params)
|
|
270
|
+
);
|
|
271
|
+
const listContentByRelation = createEndpoint(
|
|
272
|
+
"/content/:typeSlug/by-relation",
|
|
273
|
+
{
|
|
274
|
+
method: "GET",
|
|
275
|
+
params: z.object({ typeSlug: z.string().min(1) }),
|
|
276
|
+
query: z.object({ field: z.string(), targetId: z.string() }).merge(paginationSchema).merge(contentReadQuerySchema),
|
|
277
|
+
requireRequest: true
|
|
278
|
+
},
|
|
279
|
+
operations.listContentByRelation.route((ctx) => ({
|
|
280
|
+
typeSlug: ctx.params.typeSlug,
|
|
281
|
+
query: ctx.query
|
|
282
|
+
}))
|
|
283
|
+
);
|
|
284
|
+
const getInverseRelations = createEndpoint(
|
|
285
|
+
"/content-types/:slug/inverse-relations",
|
|
286
|
+
{
|
|
287
|
+
method: "GET",
|
|
288
|
+
params: CMSContentTypeParamsSchema,
|
|
289
|
+
query: z.object({ itemId: z.string().optional() }),
|
|
290
|
+
requireRequest: true
|
|
291
|
+
},
|
|
292
|
+
operations.getInverseRelations.route((ctx) => ({
|
|
293
|
+
slug: ctx.params.slug,
|
|
294
|
+
query: ctx.query
|
|
295
|
+
}))
|
|
296
|
+
);
|
|
297
|
+
const listInverseRelationItems = createEndpoint(
|
|
298
|
+
"/content-types/:slug/inverse-relations/:sourceType",
|
|
299
|
+
{
|
|
300
|
+
method: "GET",
|
|
301
|
+
params: z.object({
|
|
302
|
+
slug: z.string().min(1),
|
|
303
|
+
sourceType: z.string().min(1)
|
|
304
|
+
}),
|
|
305
|
+
query: z.object({ itemId: z.string(), fieldName: z.string() }).merge(paginationSchema).merge(contentReadQuerySchema),
|
|
306
|
+
requireRequest: true
|
|
307
|
+
},
|
|
308
|
+
operations.listInverseRelationItems.route((ctx) => ({
|
|
309
|
+
slug: ctx.params.slug,
|
|
310
|
+
sourceType: ctx.params.sourceType,
|
|
311
|
+
query: ctx.query
|
|
312
|
+
}))
|
|
313
|
+
);
|
|
314
|
+
return {
|
|
315
|
+
listContentTypes,
|
|
316
|
+
getContentTypeBySlug,
|
|
317
|
+
listContentItems,
|
|
318
|
+
getContentItem,
|
|
319
|
+
createContentItem,
|
|
320
|
+
updateContentItem,
|
|
321
|
+
deleteContentItem,
|
|
322
|
+
getContentItemPopulated,
|
|
323
|
+
listContentByRelation,
|
|
324
|
+
getInverseRelations,
|
|
325
|
+
listInverseRelationItems
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
export { CMSContentItemParamsSchema, CMSContentTypeParamsSchema, CMSCreateContentItemBodySchema, CMSUpdateContentItemBodySchema, cmsBackendPlugin };
|