qarpeo 0.2.0 → 0.3.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.
Files changed (260) hide show
  1. package/dist/chunks/tools.mjs +993 -31
  2. package/dist/cli/index.mjs +1102 -31
  3. package/dist/fate/index.d.ts +9 -1
  4. package/dist/fate/index.mjs +20 -3
  5. package/dist/module.mjs +296 -753
  6. package/dist/runtime/browser.mjs +2 -2
  7. package/dist/runtime/comments-server.d.ts +3 -0
  8. package/dist/runtime/comments-server.mjs +2 -0
  9. package/dist/runtime/comments.d.ts +0 -2
  10. package/dist/runtime/comments.mjs +0 -2
  11. package/dist/runtime/db.d.ts +10 -9
  12. package/dist/runtime/db.mjs +79 -8
  13. package/dist/runtime/drizzle.mjs +21 -6
  14. package/dist/runtime/index.d.ts +4 -0
  15. package/dist/runtime/realtime-server.d.ts +4 -0
  16. package/dist/runtime/realtime-server.mjs +13 -0
  17. package/dist/runtime/realtime.d.ts +1 -4
  18. package/dist/runtime/realtime.mjs +0 -13
  19. package/dist/runtime/replica.d.ts +27 -0
  20. package/dist/runtime/replica.mjs +229 -0
  21. package/dist/runtime/server/durable-objects/agent.mjs +14 -2
  22. package/dist/runtime/server/durable-objects/room.d.ts +10 -0
  23. package/dist/runtime/server/durable-objects/room.mjs +119 -28
  24. package/dist/runtime/server/middleware/devtools.mjs +1 -1
  25. package/dist/runtime/server/middleware/tenancy.mjs +7 -4
  26. package/dist/runtime/server/plugins/store.mjs +1 -1
  27. package/dist/runtime/server/routes/__qarpeo/mail/track/click/[id].get.mjs +57 -17
  28. package/dist/runtime/server/routes/agents/[agent]/chat.post.mjs +19 -12
  29. package/dist/runtime/server/routes/agents/[agent]/clear.post.mjs +8 -9
  30. package/dist/runtime/server/routes/agents/[agent]/history.get.mjs +8 -9
  31. package/dist/runtime/server/routes/analytics/events.get.mjs +12 -2
  32. package/dist/runtime/server/routes/analytics/pageviews.get.mjs +10 -1
  33. package/dist/runtime/server/routes/analytics/summary.get.mjs +10 -1
  34. package/dist/runtime/server/routes/auth/2fa/disable.post.mjs +11 -2
  35. package/dist/runtime/server/routes/auth/2fa/setup.post.mjs +7 -0
  36. package/dist/runtime/server/routes/auth/2fa/verify.post.mjs +11 -2
  37. package/dist/runtime/server/routes/auth/account.delete.mjs +7 -0
  38. package/dist/runtime/server/routes/auth/magic-link/verify.get.mjs +5 -3
  39. package/dist/runtime/server/routes/auth/organization/sso/index.post.mjs +10 -0
  40. package/dist/runtime/server/routes/auth/otp/verify.post.mjs +5 -3
  41. package/dist/runtime/server/routes/auth/sso/saml/acs.post.mjs +18 -0
  42. package/dist/runtime/server/routes/auth/webauthn/login/options.post.d.ts +1 -1
  43. package/dist/runtime/server/routes/auth/webauthn/login/options.post.mjs +11 -2
  44. package/dist/runtime/server/routes/auth/webauthn/login/verify.post.mjs +10 -1
  45. package/dist/runtime/server/routes/auth/webauthn/register/options.post.d.ts +1 -1
  46. package/dist/runtime/server/routes/auth/webauthn/register/options.post.mjs +10 -1
  47. package/dist/runtime/server/routes/auth/webauthn/register/verify.post.mjs +10 -1
  48. package/dist/runtime/server/routes/browser/crawl.post.mjs +13 -0
  49. package/dist/runtime/server/routes/browser/invoice.post.mjs +15 -1
  50. package/dist/runtime/server/routes/browser/og-image.post.mjs +13 -0
  51. package/dist/runtime/server/routes/browser/pdf.post.mjs +15 -1
  52. package/dist/runtime/server/routes/browser/screenshot.post.mjs +13 -0
  53. package/dist/runtime/server/routes/cms/[collection]/[id]/preview-token.post.mjs +23 -8
  54. package/dist/runtime/server/routes/cms/[collection]/[id]/publish.post.mjs +2 -1
  55. package/dist/runtime/server/routes/cms/[collection]/[id]/review.post.mjs +2 -1
  56. package/dist/runtime/server/routes/cms/[collection]/[id]/revisions/[revId]/restore.post.mjs +2 -1
  57. package/dist/runtime/server/routes/cms/[collection]/[id].delete.mjs +2 -1
  58. package/dist/runtime/server/routes/cms/[collection]/[id].put.mjs +2 -1
  59. package/dist/runtime/server/routes/cms/[collection]/index.post.mjs +2 -1
  60. package/dist/runtime/server/routes/cms/categories/[id].delete.mjs +2 -1
  61. package/dist/runtime/server/routes/cms/categories/[id].put.mjs +2 -1
  62. package/dist/runtime/server/routes/cms/categories.post.mjs +2 -1
  63. package/dist/runtime/server/routes/cms/collections/[id].delete.mjs +2 -1
  64. package/dist/runtime/server/routes/cms/collections/[id].put.mjs +2 -1
  65. package/dist/runtime/server/routes/cms/collections/index.post.mjs +2 -1
  66. package/dist/runtime/server/routes/cms/export.post.mjs +1 -1
  67. package/dist/runtime/server/routes/cms/subscribers/[id].delete.mjs +2 -1
  68. package/dist/runtime/server/routes/cms/subscribers/[id].patch.mjs +2 -1
  69. package/dist/runtime/server/routes/cms/tags/[id].delete.mjs +2 -1
  70. package/dist/runtime/server/routes/cms/tags.post.mjs +2 -1
  71. package/dist/runtime/server/routes/comments/[id]/react.post.mjs +7 -3
  72. package/dist/runtime/server/routes/comments/[id]/report.post.mjs +3 -0
  73. package/dist/runtime/server/routes/comments/[id].delete.mjs +1 -1
  74. package/dist/runtime/server/routes/comments/[id].get.mjs +13 -0
  75. package/dist/runtime/server/routes/comments/[id].patch.mjs +1 -1
  76. package/dist/runtime/server/routes/fate.d.ts +6 -0
  77. package/dist/runtime/server/routes/fate.mjs +32 -3
  78. package/dist/runtime/server/routes/mail/bounce.post.mjs +14 -2
  79. package/dist/runtime/server/routes/mail/unsubscribe.mjs +2 -1
  80. package/dist/runtime/server/routes/mail/webhooks.post.mjs +18 -4
  81. package/dist/runtime/server/routes/notifications/[id].get.mjs +3 -4
  82. package/dist/runtime/server/routes/notifications/index.get.mjs +4 -2
  83. package/dist/runtime/server/routes/notifications/push/subscribe.post.mjs +3 -1
  84. package/dist/runtime/server/routes/notifications/push/unsubscribe.post.mjs +5 -3
  85. package/dist/runtime/server/routes/notifications/read-all.post.mjs +6 -2
  86. package/dist/runtime/server/routes/notifications/send.post.mjs +13 -2
  87. package/dist/runtime/server/routes/payments/cancel.post.mjs +1 -1
  88. package/dist/runtime/server/routes/payments/change-plan.post.mjs +1 -1
  89. package/dist/runtime/server/routes/payments/checkout.post.mjs +1 -1
  90. package/dist/runtime/server/routes/payments/pause.post.mjs +1 -1
  91. package/dist/runtime/server/routes/payments/portal.post.mjs +1 -1
  92. package/dist/runtime/server/routes/payments/subscription.get.mjs +1 -1
  93. package/dist/runtime/server/routes/payments/usage.get.mjs +1 -1
  94. package/dist/runtime/server/routes/payments/usage.post.mjs +1 -1
  95. package/dist/runtime/server/routes/payments/webhook.post.mjs +8 -1
  96. package/dist/runtime/server/routes/realtime/broadcast.post.mjs +3 -2
  97. package/dist/runtime/server/routes/realtime/history.delete.mjs +2 -2
  98. package/dist/runtime/server/routes/realtime/history.get.mjs +2 -2
  99. package/dist/runtime/server/routes/realtime/presence.get.mjs +3 -3
  100. package/dist/runtime/server/routes/realtime/state.get.mjs +3 -3
  101. package/dist/runtime/server/routes/realtime/state.post.mjs +13 -4
  102. package/dist/runtime/server/routes/realtime/ws.get.mjs +2 -2
  103. package/dist/runtime/server/routes/storage/file/[...key].delete.mjs +1 -1
  104. package/dist/runtime/server/routes/storage/file/[...key].get.mjs +7 -0
  105. package/dist/runtime/server/routes/storage/files.get.d.ts +1 -1
  106. package/dist/runtime/server/routes/storage/files.get.mjs +24 -4
  107. package/dist/runtime/server/routes/storage/multipart/abort.post.mjs +16 -1
  108. package/dist/runtime/server/routes/storage/multipart/complete.post.d.ts +1 -1
  109. package/dist/runtime/server/routes/storage/multipart/complete.post.mjs +15 -1
  110. package/dist/runtime/server/routes/storage/multipart/create.post.mjs +7 -0
  111. package/dist/runtime/server/routes/storage/multipart/part.put.mjs +15 -1
  112. package/dist/runtime/server/routes/storage/presigned-r2.post.mjs +8 -1
  113. package/dist/runtime/server/routes/storage/presigned.post.d.ts +1 -1
  114. package/dist/runtime/server/routes/storage/presigned.post.mjs +8 -1
  115. package/dist/runtime/server/routes/storage/sign.post.mjs +7 -0
  116. package/dist/runtime/server/routes/storage/upload.post.d.ts +2 -2
  117. package/dist/runtime/server/routes/store/cart/index.delete.mjs +3 -4
  118. package/dist/runtime/server/routes/store/cart/index.get.mjs +3 -10
  119. package/dist/runtime/server/routes/store/cart/items/[id].delete.mjs +6 -4
  120. package/dist/runtime/server/routes/store/cart/items/[id].patch.mjs +12 -4
  121. package/dist/runtime/server/routes/store/cart/items.post.mjs +9 -10
  122. package/dist/runtime/server/routes/store/categories/index.get.mjs +4 -2
  123. package/dist/runtime/server/routes/store/checkout.post.mjs +13 -3
  124. package/dist/runtime/server/routes/store/coupons/apply.post.mjs +6 -5
  125. package/dist/runtime/server/routes/store/orders/[id].get.mjs +2 -2
  126. package/dist/runtime/server/routes/store/orders/download/[token].get.mjs +6 -3
  127. package/dist/runtime/server/routes/store/orders/index.get.mjs +1 -1
  128. package/dist/runtime/server/routes/store/products/[slug].get.mjs +2 -2
  129. package/dist/runtime/server/routes/store/products/index.get.mjs +1 -1
  130. package/dist/runtime/server/routes/store/tax/calculate.post.mjs +10 -0
  131. package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/index.delete.mjs +26 -25
  132. package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/verify.post.mjs +26 -25
  133. package/dist/runtime/server/utils/ai.d.ts +6 -0
  134. package/dist/runtime/server/utils/ai.mjs +12 -2
  135. package/dist/runtime/server/utils/analytics-query.mjs +8 -4
  136. package/dist/runtime/server/utils/browser-utils.d.ts +9 -0
  137. package/dist/runtime/server/utils/browser-utils.mjs +135 -0
  138. package/dist/runtime/server/utils/cms/cache.d.ts +13 -0
  139. package/dist/runtime/server/utils/cms/cache.mjs +95 -0
  140. package/dist/runtime/server/utils/cms/common.mjs +1 -1
  141. package/dist/runtime/server/utils/cms/markdown.mjs +38 -8
  142. package/dist/runtime/server/utils/cms/mutations.d.ts +26 -0
  143. package/dist/runtime/server/utils/cms/mutations.mjs +569 -0
  144. package/dist/runtime/server/utils/cms/path.d.ts +8 -0
  145. package/dist/runtime/server/utils/cms/path.mjs +23 -0
  146. package/dist/runtime/server/utils/cms/queries.d.ts +27 -0
  147. package/dist/runtime/server/utils/cms/queries.mjs +366 -0
  148. package/dist/runtime/server/utils/cms/references.d.ts +48 -0
  149. package/dist/runtime/server/utils/cms/references.mjs +309 -0
  150. package/dist/runtime/server/utils/cms/revisions.d.ts +7 -0
  151. package/dist/runtime/server/utils/cms/revisions.mjs +60 -0
  152. package/dist/runtime/server/utils/cms/sync.mjs +3 -3
  153. package/dist/runtime/server/utils/cms.d.ts +5 -82
  154. package/dist/runtime/server/utils/cms.mjs +28 -1367
  155. package/dist/runtime/server/utils/comments.d.ts +11 -0
  156. package/dist/runtime/server/utils/comments.mjs +65 -17
  157. package/dist/runtime/server/utils/crypto.mjs +1 -1
  158. package/dist/runtime/server/utils/domains.mjs +9 -0
  159. package/dist/runtime/server/utils/drizzle.d.ts +26 -1
  160. package/dist/runtime/server/utils/drizzle.mjs +78 -19
  161. package/dist/runtime/server/utils/email-inbound.mjs +9 -1
  162. package/dist/runtime/server/utils/impersonation.mjs +58 -1
  163. package/dist/runtime/server/utils/lock.d.ts +11 -0
  164. package/dist/runtime/server/utils/lock.mjs +4 -0
  165. package/dist/runtime/server/utils/mail-suppression.mjs +48 -13
  166. package/dist/runtime/server/utils/mail.d.ts +2 -1
  167. package/dist/runtime/server/utils/mail.mjs +17 -11
  168. package/dist/runtime/server/utils/notifications.d.ts +13 -4
  169. package/dist/runtime/server/utils/notifications.mjs +20 -4
  170. package/dist/runtime/server/utils/organization.mjs +36 -1
  171. package/dist/runtime/server/utils/passwordless.mjs +8 -0
  172. package/dist/runtime/server/utils/payments/checkout.d.ts +34 -0
  173. package/dist/runtime/server/utils/payments/checkout.mjs +158 -0
  174. package/dist/runtime/server/utils/payments/common.d.ts +45 -0
  175. package/dist/runtime/server/utils/payments/common.mjs +234 -0
  176. package/dist/runtime/server/utils/payments/index.d.ts +9 -0
  177. package/dist/runtime/server/utils/payments/index.mjs +21 -0
  178. package/dist/runtime/server/utils/payments/lemonsqueezy.d.ts +7 -0
  179. package/dist/runtime/server/utils/payments/lemonsqueezy.mjs +55 -0
  180. package/dist/runtime/server/utils/payments/paddle.d.ts +7 -0
  181. package/dist/runtime/server/utils/payments/paddle.mjs +92 -0
  182. package/dist/runtime/server/utils/payments/polar.d.ts +11 -0
  183. package/dist/runtime/server/utils/payments/polar.mjs +66 -0
  184. package/dist/runtime/server/utils/payments/stripe.d.ts +7 -0
  185. package/dist/runtime/server/utils/payments/stripe.mjs +159 -0
  186. package/dist/runtime/server/utils/payments/subscriptions.d.ts +89 -0
  187. package/dist/runtime/server/utils/payments/subscriptions.mjs +236 -0
  188. package/dist/runtime/server/utils/payments/usage.d.ts +12 -0
  189. package/dist/runtime/server/utils/payments/usage.mjs +143 -0
  190. package/dist/runtime/server/utils/payments/webhooks.d.ts +10 -0
  191. package/dist/runtime/server/utils/payments/webhooks.mjs +200 -0
  192. package/dist/runtime/server/utils/payments.d.ts +1 -158
  193. package/dist/runtime/server/utils/payments.mjs +1 -1289
  194. package/dist/runtime/server/utils/queue.d.ts +2 -0
  195. package/dist/runtime/server/utils/queue.mjs +34 -21
  196. package/dist/runtime/server/utils/rag.mjs +30 -17
  197. package/dist/runtime/server/utils/realtime.d.ts +5 -1
  198. package/dist/runtime/server/utils/realtime.mjs +43 -7
  199. package/dist/runtime/server/utils/session.mjs +16 -3
  200. package/dist/runtime/server/utils/sso.d.ts +3 -0
  201. package/dist/runtime/server/utils/sso.mjs +43 -0
  202. package/dist/runtime/server/utils/storage.mjs +12 -1
  203. package/dist/runtime/server/utils/store/cart.d.ts +63 -0
  204. package/dist/runtime/server/utils/store/cart.mjs +542 -0
  205. package/dist/runtime/server/utils/store/checkout.d.ts +14 -0
  206. package/dist/runtime/server/utils/store/checkout.mjs +118 -0
  207. package/dist/runtime/server/utils/store/common.d.ts +29 -0
  208. package/dist/runtime/server/utils/store/common.mjs +479 -0
  209. package/dist/runtime/server/utils/store/index.d.ts +7 -0
  210. package/dist/runtime/server/utils/store/index.mjs +30 -0
  211. package/dist/runtime/server/utils/store/inventory.d.ts +25 -0
  212. package/dist/runtime/server/utils/store/inventory.mjs +190 -0
  213. package/dist/runtime/server/utils/store/orders.d.ts +60 -0
  214. package/dist/runtime/server/utils/store/orders.mjs +590 -0
  215. package/dist/runtime/server/utils/store/products.d.ts +35 -0
  216. package/dist/runtime/server/utils/store/products.mjs +461 -0
  217. package/dist/runtime/server/utils/store/vendor.d.ts +18 -0
  218. package/dist/runtime/server/utils/store/vendor.mjs +68 -0
  219. package/dist/runtime/server/utils/store.d.ts +1 -207
  220. package/dist/runtime/server/utils/store.mjs +1 -2227
  221. package/dist/runtime/server/utils/string.d.ts +1 -1
  222. package/dist/runtime/server/utils/string.mjs +12 -4
  223. package/dist/runtime/server/utils/tenant.mjs +19 -5
  224. package/dist/runtime/server/utils/tracker.mjs +2 -0
  225. package/dist/runtime/server.d.ts +3 -3
  226. package/dist/runtime/server.mjs +4 -4
  227. package/dist/runtime/storage-server.d.ts +2 -0
  228. package/dist/runtime/storage-server.mjs +1 -0
  229. package/dist/runtime/storage.d.ts +1 -1
  230. package/dist/runtime/storage.mjs +0 -1
  231. package/dist/runtime/store.d.ts +0 -1
  232. package/dist/runtime/store.mjs +0 -1
  233. package/dist/runtime/test-utils.d.ts +4 -0
  234. package/dist/runtime/test-utils.mjs +54 -0
  235. package/dist/runtime/types/d1.d.ts +43 -0
  236. package/dist/runtime/types/drizzle.d.ts +6 -0
  237. package/dist/runtime/types/store.d.ts +1 -1
  238. package/dist/types/analytics.d.ts +80 -0
  239. package/dist/types/auth.d.ts +282 -0
  240. package/dist/types/cache.d.ts +55 -0
  241. package/dist/types/comments.d.ts +130 -0
  242. package/dist/types/db.d.ts +29 -0
  243. package/dist/types/email.d.ts +182 -0
  244. package/dist/types/index.d.ts +22 -0
  245. package/dist/types/jobs.d.ts +58 -0
  246. package/dist/types/notifications.d.ts +77 -0
  247. package/dist/types/options.d.ts +363 -0
  248. package/dist/types/payments.d.ts +145 -0
  249. package/dist/types/ratelimit.d.ts +71 -0
  250. package/dist/types/realtime.d.ts +136 -0
  251. package/dist/types/site.d.ts +121 -0
  252. package/dist/types/storage.d.ts +164 -0
  253. package/dist/types/store-options.d.ts +199 -0
  254. package/dist/types/tenant.d.ts +100 -0
  255. package/dist/types.d.ts +1 -2174
  256. package/dist/utils/migration-lock.d.ts +139 -0
  257. package/dist/utils/pkg.d.ts +14 -1
  258. package/dist/utils/schema-diff.d.ts +39 -0
  259. package/package.json +55 -17
  260. package/dist/chunks/auth-schema.mjs +0 -364
@@ -0,0 +1,309 @@
1
+ import { createError } from "nuxt/server";
2
+ import { resolveTableName } from "../table-names.mjs";
3
+ import {
4
+ getCMSDb,
5
+ getCMSConfig,
6
+ getCMSTenantId,
7
+ cmsCollectionTbl,
8
+ cmsEntryTbl
9
+ } from "./common.mjs";
10
+ const userTbl = (e) => resolveTableName("user", e);
11
+ export function isReferenceValue(v) {
12
+ return !!v && typeof v === "object" && !Array.isArray(v) && typeof v.collectionId === "string" && typeof v.slug === "string";
13
+ }
14
+ export async function getCollectionFields(event, collectionId) {
15
+ const config = getCMSConfig(event);
16
+ const configFields = config.collections?.[collectionId]?.fields;
17
+ let dbFields;
18
+ try {
19
+ const db = getCMSDb(event);
20
+ const row = await db.prepare(`SELECT schema_json FROM ${cmsCollectionTbl(event)} WHERE id = ?`).bind(collectionId).first();
21
+ if (row?.schema_json) {
22
+ dbFields = JSON.parse(row.schema_json);
23
+ }
24
+ } catch {
25
+ }
26
+ return { ...configFields, ...dbFields };
27
+ }
28
+ async function fetchEntryRef(event, collectionId, slugOrId) {
29
+ const db = getCMSDb(event);
30
+ const tenantId = getCMSTenantId(event);
31
+ const row = await db.prepare(
32
+ `SELECT id, collection_id, slug, title FROM ${cmsEntryTbl(event)}
33
+ WHERE collection_id = ? AND (slug = ? OR id = ?)
34
+ ${tenantId ? "AND (tenant_id = ? OR tenant_id IS NULL)" : ""}
35
+ LIMIT 1`
36
+ ).bind(
37
+ ...tenantId ? [collectionId, slugOrId, slugOrId, tenantId] : [collectionId, slugOrId, slugOrId]
38
+ ).first();
39
+ if (!row) return null;
40
+ return {
41
+ id: row.id,
42
+ collectionId: row.collection_id,
43
+ slug: row.slug,
44
+ title: row.title
45
+ };
46
+ }
47
+ async function resolveReferenceInput(event, input, defaultTargetCollectionId, filter, fieldName) {
48
+ if (input == null || input === "") {
49
+ throw createError({ statusCode: 400, message: "Reference value is required" });
50
+ }
51
+ let collectionId;
52
+ let slugOrId;
53
+ if (typeof input === "string") {
54
+ const trimmed = input.trim();
55
+ if (!trimmed) {
56
+ throw createError({ statusCode: 400, message: "Reference value is required" });
57
+ }
58
+ const slashIdx = trimmed.indexOf("/");
59
+ if (slashIdx > 0) {
60
+ collectionId = trimmed.slice(0, slashIdx);
61
+ slugOrId = trimmed.slice(slashIdx + 1);
62
+ } else {
63
+ collectionId = defaultTargetCollectionId;
64
+ slugOrId = trimmed;
65
+ }
66
+ } else if (isReferenceValue(input)) {
67
+ collectionId = input.collectionId;
68
+ slugOrId = input.id || input.slug;
69
+ } else {
70
+ throw createError({
71
+ statusCode: 400,
72
+ message: 'Invalid reference value: expected an id, "collection/slug", or a reference object'
73
+ });
74
+ }
75
+ if (!collectionId || !slugOrId) {
76
+ throw createError({
77
+ statusCode: 400,
78
+ message: "Reference value is missing collection or slug/id"
79
+ });
80
+ }
81
+ const resolved = await fetchEntryRef(event, collectionId, slugOrId);
82
+ if (!resolved) {
83
+ throw createError({
84
+ statusCode: 400,
85
+ message: `Referenced entry "${slugOrId}" not found in collection "${collectionId}"`
86
+ });
87
+ }
88
+ if (filter && Object.keys(filter).length > 0) {
89
+ const { getCMSEntry } = await import("./queries.mjs");
90
+ const fullEntry = await getCMSEntry(event, collectionId, resolved.id, { preview: true });
91
+ if (fullEntry) {
92
+ for (const [key, expected] of Object.entries(filter)) {
93
+ const actual = fullEntry.metadata?.[key];
94
+ if (actual !== expected) {
95
+ const label = fieldName ? `reference field "${fieldName}"` : "reference";
96
+ throw createError({
97
+ statusCode: 400,
98
+ message: `The ${label} target "${resolved.slug}" does not match the required filter (${key} must be ${String(expected)})`
99
+ });
100
+ }
101
+ }
102
+ }
103
+ }
104
+ return resolved;
105
+ }
106
+ export async function validateAndNormalizeReferenceFields(event, collectionId, metadata) {
107
+ const fields = await getCollectionFields(event, collectionId);
108
+ for (const [fieldName, fieldDef] of Object.entries(fields)) {
109
+ if (fieldDef.type !== "reference" || !fieldDef.reference) continue;
110
+ const refOpts = fieldDef.reference;
111
+ const multiple = refOpts.multiple === true;
112
+ const value = metadata[fieldName];
113
+ if (value == null || value === "") {
114
+ if (fieldDef.required) {
115
+ throw createError({
116
+ statusCode: 400,
117
+ message: `Reference field "${fieldName}" is required`
118
+ });
119
+ }
120
+ if (multiple) metadata[fieldName] = [];
121
+ continue;
122
+ }
123
+ if (multiple) {
124
+ const items = Array.isArray(value) ? value : [value];
125
+ const resolved = [];
126
+ for (const item of items) {
127
+ if (item == null || item === "") continue;
128
+ resolved.push(
129
+ await resolveReferenceInput(event, item, refOpts.to, refOpts.filter, fieldName)
130
+ );
131
+ }
132
+ metadata[fieldName] = resolved;
133
+ } else {
134
+ metadata[fieldName] = await resolveReferenceInput(
135
+ event,
136
+ value,
137
+ refOpts.to,
138
+ refOpts.filter,
139
+ fieldName
140
+ );
141
+ }
142
+ }
143
+ return metadata;
144
+ }
145
+ function resolveComponentDefinition(fieldDef, config) {
146
+ if (fieldDef.type !== "component") return null;
147
+ const compOpts = fieldDef.component;
148
+ if (!compOpts) return null;
149
+ if (typeof compOpts === "string") {
150
+ return config.components?.[compOpts] ?? null;
151
+ }
152
+ if (compOpts.name) {
153
+ return config.components?.[compOpts.name] ?? null;
154
+ }
155
+ if (compOpts.fields) {
156
+ return { fields: compOpts.fields };
157
+ }
158
+ return null;
159
+ }
160
+ export async function validateAndNormalizeComponentFields(event, collectionId, metadata) {
161
+ const config = getCMSConfig(event);
162
+ const fields = await getCollectionFields(event, collectionId);
163
+ for (const [fieldName, fieldDef] of Object.entries(fields)) {
164
+ if (fieldDef.type !== "component") continue;
165
+ const compDef = resolveComponentDefinition(fieldDef, config);
166
+ if (!compDef) continue;
167
+ const compOpts = typeof fieldDef.component === "object" ? fieldDef.component : null;
168
+ const isRepeatable = fieldDef.repeatable || (compOpts?.repeatable ?? false);
169
+ const value = metadata[fieldName];
170
+ if (value == null || value === "") {
171
+ if (fieldDef.required) {
172
+ throw createError({
173
+ statusCode: 400,
174
+ message: `Component field "${fieldName}" is required`
175
+ });
176
+ }
177
+ if (isRepeatable) metadata[fieldName] = [];
178
+ continue;
179
+ }
180
+ if (isRepeatable && !Array.isArray(value)) {
181
+ metadata[fieldName] = [value];
182
+ }
183
+ if (isRepeatable && compOpts?.max != null && Array.isArray(metadata[fieldName])) {
184
+ const arr = metadata[fieldName];
185
+ if (arr.length > compOpts.max) {
186
+ throw createError({
187
+ statusCode: 400,
188
+ message: `Component field "${fieldName}" exceeds maximum of ${compOpts.max} items`
189
+ });
190
+ }
191
+ }
192
+ if (isRepeatable && compOpts?.min != null && Array.isArray(metadata[fieldName])) {
193
+ const arr = metadata[fieldName];
194
+ if (arr.length < compOpts.min) {
195
+ throw createError({
196
+ statusCode: 400,
197
+ message: `Component field "${fieldName}" requires at least ${compOpts.min} items`
198
+ });
199
+ }
200
+ }
201
+ }
202
+ return metadata;
203
+ }
204
+ export function defineCMSComponent(def) {
205
+ return def;
206
+ }
207
+ export async function resolveReference(event, ref) {
208
+ if (!ref || !ref.id && !ref.slug) return null;
209
+ const collectionId = ref.collectionId;
210
+ if (!collectionId) return null;
211
+ const slugOrId = ref.id || ref.slug;
212
+ if (!slugOrId) return null;
213
+ const { getCMSEntry } = await import("./queries.mjs");
214
+ return getCMSEntry(event, collectionId, slugOrId);
215
+ }
216
+ export async function populateReferenceFields(event, entry, populate) {
217
+ const resEntry = { ...entry };
218
+ if (resEntry.authorId && (populate === true || Array.isArray(populate) && (populate.includes("author") || populate.includes("authorId")))) {
219
+ try {
220
+ const db = getCMSDb(event);
221
+ if (db) {
222
+ const userRow = await db.prepare(
223
+ `SELECT id, name, email, image, username, avatar, role, bio FROM ${userTbl(event)} WHERE id = ? LIMIT 1`
224
+ ).bind(resEntry.authorId).first();
225
+ if (userRow) {
226
+ resEntry.author = {
227
+ id: userRow.id,
228
+ name: userRow.name || userRow.username || (userRow.email ? userRow.email.split("@")[0] : "User"),
229
+ username: userRow.username || null,
230
+ email: userRow.email || null,
231
+ avatar: userRow.avatar || userRow.image || null,
232
+ role: userRow.role || null,
233
+ bio: userRow.bio || null
234
+ };
235
+ }
236
+ }
237
+ } catch {
238
+ }
239
+ }
240
+ if (!resEntry.metadata || Object.keys(resEntry.metadata).length === 0) return resEntry;
241
+ const fields = await getCollectionFields(event, resEntry.collectionId);
242
+ const populatedMeta = { ...resEntry.metadata };
243
+ for (const [fieldName, fieldDef] of Object.entries(fields)) {
244
+ if (fieldDef.type !== "reference") continue;
245
+ if (Array.isArray(populate) && !populate.includes(fieldName)) continue;
246
+ const value = populatedMeta[fieldName];
247
+ if (value == null) continue;
248
+ if (Array.isArray(value)) {
249
+ populatedMeta[fieldName] = await Promise.all(
250
+ value.filter(isReferenceValue).map(async (r) => ({
251
+ ...r,
252
+ entry: await resolveReference(event, r)
253
+ }))
254
+ );
255
+ } else if (isReferenceValue(value)) {
256
+ populatedMeta[fieldName] = { ...value, entry: await resolveReference(event, value) };
257
+ }
258
+ }
259
+ return { ...resEntry, metadata: populatedMeta };
260
+ }
261
+ const REFERENCE_PATH_RE = /^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+$/;
262
+ export function serializeReferencesForFrontmatter(metadata) {
263
+ const out = {};
264
+ for (const [k, v] of Object.entries(metadata)) {
265
+ if (isReferenceValue(v)) {
266
+ out[k] = `${v.collectionId}/${v.slug}`;
267
+ } else if (Array.isArray(v) && v.length > 0 && v.every((item) => isReferenceValue(item))) {
268
+ out[k] = v.map(
269
+ (item) => `${item.collectionId}/${item.slug}`
270
+ );
271
+ } else {
272
+ out[k] = v;
273
+ }
274
+ }
275
+ return out;
276
+ }
277
+ export async function resolveReferenceFieldsFromFrontmatter(event, collectionId, fm) {
278
+ const fields = await getCollectionFields(event, collectionId);
279
+ for (const [fieldName, fieldDef] of Object.entries(fields)) {
280
+ if (fieldDef.type !== "reference" || !fieldDef.reference) continue;
281
+ const refOpts = fieldDef.reference;
282
+ const multiple = refOpts.multiple === true;
283
+ const value = fm[fieldName];
284
+ if (value == null) continue;
285
+ const resolveString = async (str) => {
286
+ if (!REFERENCE_PATH_RE.test(str)) return null;
287
+ const slashIdx = str.indexOf("/");
288
+ const refCollectionId = str.slice(0, slashIdx);
289
+ const refSlug = str.slice(slashIdx + 1);
290
+ return fetchEntryRef(event, refCollectionId, refSlug);
291
+ };
292
+ if (multiple) {
293
+ const items = Array.isArray(value) ? value : [value];
294
+ const resolved = [];
295
+ for (const item of items) {
296
+ if (typeof item === "string") {
297
+ const r = await resolveString(item);
298
+ if (r) resolved.push(r);
299
+ } else if (isReferenceValue(item)) {
300
+ resolved.push(item);
301
+ }
302
+ }
303
+ if (resolved.length > 0) fm[fieldName] = resolved;
304
+ } else if (typeof value === "string" && REFERENCE_PATH_RE.test(value)) {
305
+ const r = await resolveString(value);
306
+ if (r) fm[fieldName] = r;
307
+ }
308
+ }
309
+ }
@@ -0,0 +1,7 @@
1
+ import { type RequestEvent } from "nuxt/server";
2
+ import type { CMSEntry } from "../../../../types";
3
+ export declare function pruneRevisions(event: RequestEvent, entryId: string, max: number): Promise<void>;
4
+ /** Lists all saved historical revisions for an entry. */
5
+ export declare function listCMSRevisions(event: RequestEvent, entryId: string): Promise<any>;
6
+ /** Restores an entry to a specific saved revision. */
7
+ export declare function restoreCMSRevision(event: RequestEvent, entryId: string, revisionId: string): Promise<CMSEntry>;
@@ -0,0 +1,60 @@
1
+ import { createError } from "nuxt/server";
2
+ import { getCMSDb, getCMSTenantId, cmsEntryTbl, cmsRevisionTbl } from "./common.mjs";
3
+ export async function pruneRevisions(event, entryId, max) {
4
+ const db = getCMSDb(event);
5
+ await db.prepare(
6
+ `DELETE FROM ${cmsRevisionTbl(event)} WHERE entry_id = ? AND id NOT IN (
7
+ SELECT id FROM ${cmsRevisionTbl(event)} WHERE entry_id = ? ORDER BY created_at DESC LIMIT ?
8
+ )`
9
+ ).bind(entryId, entryId, max).run();
10
+ }
11
+ export async function listCMSRevisions(event, entryId) {
12
+ const db = getCMSDb(event);
13
+ if (!db) return [];
14
+ const tenantId = getCMSTenantId(event);
15
+ const entryCheck = await db.prepare(
16
+ `SELECT id FROM ${cmsEntryTbl(event)} WHERE id = ? ${tenantId ? "AND (tenant_id = ? OR tenant_id IS NULL)" : ""} LIMIT 1`
17
+ ).bind(...tenantId ? [entryId, tenantId] : [entryId]).first();
18
+ if (!entryCheck?.id) {
19
+ throw createError({ statusCode: 404, message: `Entry '${entryId}' not found` });
20
+ }
21
+ const res = await db.prepare(
22
+ `SELECT id, entry_id as entryId, title, content_md as contentMd, metadata_json as metadataJson, author_id as authorId, created_at as createdAt FROM ${cmsRevisionTbl(event)} WHERE entry_id = ? ORDER BY created_at DESC`
23
+ ).bind(entryId).all().catch(() => ({ results: [] }));
24
+ return res?.results || [];
25
+ }
26
+ export async function restoreCMSRevision(event, entryId, revisionId) {
27
+ const db = getCMSDb(event);
28
+ if (!db) throw createError({ statusCode: 500, message: "Database not available" });
29
+ const tenantId = getCMSTenantId(event);
30
+ const entryCheck = await db.prepare(
31
+ `SELECT id FROM ${cmsEntryTbl(event)} WHERE id = ? ${tenantId ? "AND (tenant_id = ? OR tenant_id IS NULL)" : ""} LIMIT 1`
32
+ ).bind(...tenantId ? [entryId, tenantId] : [entryId]).first();
33
+ if (!entryCheck?.id) {
34
+ throw createError({
35
+ statusCode: 404,
36
+ message: `Entry '${entryId}' not found`
37
+ });
38
+ }
39
+ const rev = await db.prepare(`SELECT * FROM ${cmsRevisionTbl(event)} WHERE id = ? AND entry_id = ?`).bind(revisionId, entryId).first();
40
+ if (!rev) {
41
+ throw createError({
42
+ statusCode: 404,
43
+ message: `Revision '${revisionId}' not found for entry '${entryId}'`
44
+ });
45
+ }
46
+ let meta = {};
47
+ try {
48
+ meta = JSON.parse(rev.metadata_json || "{}");
49
+ } catch {
50
+ }
51
+ delete meta._ast;
52
+ delete meta._toc;
53
+ delete meta._nav;
54
+ const { updateCMSEntry } = await import("./mutations.mjs");
55
+ return await updateCMSEntry(event, entryId, {
56
+ title: rev.title,
57
+ contentMd: rev.content_md,
58
+ metadata: meta
59
+ });
60
+ }
@@ -1,4 +1,4 @@
1
- import { parse as parseYaml } from "yaml";
1
+ const _yaml = () => require("yaml");
2
2
  import { getKVBinding } from "../../../kv.mjs";
3
3
  import { resolveIndexName } from "../table-names.mjs";
4
4
  import {
@@ -430,7 +430,7 @@ export async function syncLocalContentFiles(event, contentDir, collectionsConfig
430
430
  if (existsSync(navYmlPath)) {
431
431
  try {
432
432
  const raw = await readFile(navYmlPath);
433
- return parseYaml(raw) || {};
433
+ return _yaml().parse(raw) || {};
434
434
  } catch {
435
435
  }
436
436
  }
@@ -778,7 +778,7 @@ export async function syncLocalContentFiles(event, contentDir, collectionsConfig
778
778
  }
779
779
  } else {
780
780
  try {
781
- parsed = parseYaml(rawContent) || {};
781
+ parsed = _yaml().parse(rawContent) || {};
782
782
  } catch {
783
783
  }
784
784
  }
@@ -1,5 +1,3 @@
1
- import { type RequestEvent } from "nuxt/server";
2
- import type { CMSComponentDefinition, CMSEntry, CMSEntryStatus, CMSCreateInput, CMSUpdateInput, CMSQueryOptions, CMSFieldDefinition } from "../../../types";
3
1
  export * from "./cms/common";
4
2
  export * from "./cms/path";
5
3
  export * from "./cms/markdown";
@@ -10,83 +8,8 @@ export * from "./cms/taxonomy";
10
8
  export * from "./cms/navigation";
11
9
  export * from "./cms/sync";
12
10
  export * from "./cms/collections";
13
- /**
14
- * Formats a CMSEntry into standard markdown with YAML frontmatter. Omits internal system fields
15
- * starting with `_`.
16
- */
17
- export declare function formatEntryAsMarkdown(entry: CMSEntry | Record<string, any>): string;
18
- export declare function projectCMSEntry(entry: CMSEntry | null, fields?: string[] | string): any;
19
- export declare function rowToEntry(row: Record<string, unknown>): CMSEntry;
20
- /**
21
- * Resolves the effective field definitions for a collection, merging config-seeded fields with any
22
- * schema stored in D1 (DB-created collections take precedence for overlapping keys).
23
- */
24
- export declare function getCollectionFields(event: RequestEvent, collectionId: string): Promise<Record<string, CMSFieldDefinition>>;
25
- /**
26
- * Returns a typed component definition from the CMS config by name, or null if not found.
27
- *
28
- * @public
29
- */
30
- export declare function defineCMSComponent(def: CMSComponentDefinition): CMSComponentDefinition;
31
- /**
32
- * Resolves a stored reference object into the full referenced {@link CMSEntry}. Used to populate
33
- * references on read.
34
- */
35
- export declare function resolveReference(event: RequestEvent, ref: {
36
- id?: string;
37
- collectionId?: string;
38
- slug?: string;
39
- }): Promise<CMSEntry | null>;
40
- /**
41
- * Resolves `collection/slug` frontmatter strings for declared reference fields into stored
42
- * reference objects. Field-aware so plain string fields are left untouched. Mutates `fm` in place.
43
- */
44
- export declare function resolveReferenceFieldsFromFrontmatter(event: RequestEvent, collectionId: string, fm: Record<string, unknown>): Promise<void>;
45
- export declare function invalidateCMSEntryCache(event: RequestEvent, collectionId: string, slug?: string | string[]): Promise<void>;
46
- /** Queries entries for a collection with filtering, pagination, and sorting. */
47
- export declare function queryCMSEntries(event: RequestEvent, collectionId: string, opts?: CMSQueryOptions): Promise<{
48
- entries: CMSEntry[];
49
- total: number;
50
- page: number;
51
- limit: number;
52
- }>;
53
- /**
54
- * Fetches a single entry by slug or ID. Supports draft preview via authenticated session or HMAC
55
- * preview token.
56
- */
57
- export declare function getCMSEntry(event: RequestEvent, collectionId: string, slugOrId: string, opts?: {
58
- preview?: boolean;
59
- previewToken?: string;
60
- previewExpires?: string;
61
- populate?: boolean | string[];
62
- fields?: string[] | string;
63
- }): Promise<CMSEntry | null>;
64
- /** Creates a new entry in D1. Renders contentHtml from contentMd on write. */
65
- export declare function createCMSEntry(event: RequestEvent, collectionId: string, data: CMSCreateInput): Promise<CMSEntry>;
66
- /**
67
- * Updates an existing entry. Re-renders contentHtml if contentMd changed. Saves a revision and
68
- * prunes beyond max.
69
- */
70
- export declare function updateCMSEntry(event: RequestEvent, entryId: string, data: CMSUpdateInput): Promise<CMSEntry>;
71
- /** Deletes an entry and all its revisions. */
72
- export declare function deleteCMSEntry(event: RequestEvent, entryId: string): Promise<void>;
73
- /**
74
- * Toggles entry status between `draft`, `pending_review`, `scheduled`, `published`, or `archived`.
75
- * Validates approval policies if approval workflow is enabled for the collection.
76
- */
77
- export declare function publishCMSEntry(event: RequestEvent, entryId: string, targetStatus: CMSEntryStatus, targetPublishedAt?: number | null): Promise<void>;
78
- /**
79
- * Scans for entries with status = 'scheduled' and published_at <= Date.now(). Transitions them to
80
- * 'published' and triggers git-sync, webhooks, and deploy hooks. Returns the list of entries
81
- * published.
82
- */
83
- export declare function publishDueScheduledEntries(event?: RequestEvent): Promise<CMSEntry[]>;
84
- /** Submits an entry for editorial review (transitions status from draft to pending_review). */
85
- export declare function submitCMSEntryForReview(event: RequestEvent, entryId: string, note?: string): Promise<CMSEntry>;
86
- /** Rejects a pending review entry and reverts its status back to draft with reviewer feedback. */
87
- export declare function rejectCMSEntryReview(event: RequestEvent, entryId: string, reason?: string): Promise<CMSEntry>;
88
- export declare function pruneRevisions(event: RequestEvent, entryId: string, max: number): Promise<void>;
89
- /** Lists all saved historical revisions for an entry. */
90
- export declare function listCMSRevisions(event: RequestEvent, entryId: string): Promise<any>;
91
- /** Restores an entry to a specific saved revision. */
92
- export declare function restoreCMSRevision(event: RequestEvent, entryId: string, revisionId: string): Promise<CMSEntry>;
11
+ export { formatEntryAsMarkdown, projectCMSEntry, rowToEntry, queryCMSEntries, getCMSEntry, } from "./cms/queries";
12
+ export { getCollectionFields, defineCMSComponent, resolveReference, resolveReferenceFieldsFromFrontmatter, } from "./cms/references";
13
+ export { invalidateCMSEntryCache } from "./cms/cache";
14
+ export { createCMSEntry, updateCMSEntry, deleteCMSEntry, publishCMSEntry, publishDueScheduledEntries, submitCMSEntryForReview, rejectCMSEntryReview, } from "./cms/mutations";
15
+ export { pruneRevisions, listCMSRevisions, restoreCMSRevision, } from "./cms/revisions";