@dyrected/core 2.6.3 → 2.6.4

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 (352) hide show
  1. package/dist/__tests__/access-features.test.d.ts +2 -0
  2. package/dist/__tests__/access-features.test.d.ts.map +1 -0
  3. package/dist/__tests__/access-features.test.js +159 -0
  4. package/dist/__tests__/access-features.test.js.map +1 -0
  5. package/dist/__tests__/access-parity.test.d.ts +2 -0
  6. package/dist/__tests__/access-parity.test.d.ts.map +1 -0
  7. package/dist/__tests__/access-parity.test.js +338 -0
  8. package/dist/__tests__/access-parity.test.js.map +1 -0
  9. package/dist/__tests__/access.test.d.ts +2 -0
  10. package/dist/__tests__/access.test.d.ts.map +1 -0
  11. package/dist/__tests__/access.test.js +137 -0
  12. package/dist/__tests__/access.test.js.map +1 -0
  13. package/dist/__tests__/admin-auth.test.d.ts +2 -0
  14. package/dist/__tests__/admin-auth.test.d.ts.map +1 -0
  15. package/dist/__tests__/admin-auth.test.js +440 -0
  16. package/dist/__tests__/admin-auth.test.js.map +1 -0
  17. package/dist/__tests__/app.test.d.ts +2 -0
  18. package/dist/__tests__/app.test.d.ts.map +1 -0
  19. package/dist/__tests__/app.test.js +112 -0
  20. package/dist/__tests__/app.test.js.map +1 -0
  21. package/dist/__tests__/audit.test.d.ts +2 -0
  22. package/dist/__tests__/audit.test.d.ts.map +1 -0
  23. package/dist/__tests__/audit.test.js +125 -0
  24. package/dist/__tests__/audit.test.js.map +1 -0
  25. package/dist/__tests__/auth-hydration.test.d.ts +2 -0
  26. package/dist/__tests__/auth-hydration.test.d.ts.map +1 -0
  27. package/dist/__tests__/auth-hydration.test.js +90 -0
  28. package/dist/__tests__/auth-hydration.test.js.map +1 -0
  29. package/dist/__tests__/auth-invite.test.d.ts +2 -0
  30. package/dist/__tests__/auth-invite.test.d.ts.map +1 -0
  31. package/dist/__tests__/auth-invite.test.js +152 -0
  32. package/dist/__tests__/auth-invite.test.js.map +1 -0
  33. package/dist/__tests__/auth-lockout.test.d.ts +2 -0
  34. package/dist/__tests__/auth-lockout.test.d.ts.map +1 -0
  35. package/dist/__tests__/auth-lockout.test.js +154 -0
  36. package/dist/__tests__/auth-lockout.test.js.map +1 -0
  37. package/dist/__tests__/auth-sessions.test.d.ts +2 -0
  38. package/dist/__tests__/auth-sessions.test.d.ts.map +1 -0
  39. package/dist/__tests__/auth-sessions.test.js +118 -0
  40. package/dist/__tests__/auth-sessions.test.js.map +1 -0
  41. package/dist/__tests__/config.test.d.ts +2 -0
  42. package/dist/__tests__/config.test.d.ts.map +1 -0
  43. package/dist/__tests__/config.test.js +402 -0
  44. package/dist/__tests__/config.test.js.map +1 -0
  45. package/dist/__tests__/define-field.test.d.ts +2 -0
  46. package/dist/__tests__/define-field.test.d.ts.map +1 -0
  47. package/dist/__tests__/define-field.test.js +121 -0
  48. package/dist/__tests__/define-field.test.js.map +1 -0
  49. package/dist/__tests__/deleteMany.test.d.ts +2 -0
  50. package/dist/__tests__/deleteMany.test.d.ts.map +1 -0
  51. package/dist/__tests__/deleteMany.test.js +75 -0
  52. package/dist/__tests__/deleteMany.test.js.map +1 -0
  53. package/dist/__tests__/depth.test.d.ts +2 -0
  54. package/dist/__tests__/depth.test.d.ts.map +1 -0
  55. package/dist/__tests__/depth.test.js +115 -0
  56. package/dist/__tests__/depth.test.js.map +1 -0
  57. package/dist/__tests__/dynamic-options.test.d.ts +2 -0
  58. package/dist/__tests__/dynamic-options.test.d.ts.map +1 -0
  59. package/dist/__tests__/dynamic-options.test.js +168 -0
  60. package/dist/__tests__/dynamic-options.test.js.map +1 -0
  61. package/dist/__tests__/email.service.test.d.ts +2 -0
  62. package/dist/__tests__/email.service.test.d.ts.map +1 -0
  63. package/dist/__tests__/email.service.test.js +52 -0
  64. package/dist/__tests__/email.service.test.js.map +1 -0
  65. package/dist/__tests__/field-inference.test-types.d.ts +161 -0
  66. package/dist/__tests__/field-inference.test-types.d.ts.map +1 -0
  67. package/dist/__tests__/field-inference.test-types.js +87 -0
  68. package/dist/__tests__/field-inference.test-types.js.map +1 -0
  69. package/dist/__tests__/fixtures/editorial-workflow.d.ts +14 -0
  70. package/dist/__tests__/fixtures/editorial-workflow.d.ts.map +1 -0
  71. package/dist/__tests__/fixtures/editorial-workflow.js +12 -0
  72. package/dist/__tests__/fixtures/editorial-workflow.js.map +1 -0
  73. package/dist/__tests__/hooks.test.d.ts +2 -0
  74. package/dist/__tests__/hooks.test.d.ts.map +1 -0
  75. package/dist/__tests__/hooks.test.js +320 -0
  76. package/dist/__tests__/hooks.test.js.map +1 -0
  77. package/dist/__tests__/mocks.d.ts +75 -0
  78. package/dist/__tests__/mocks.d.ts.map +1 -0
  79. package/dist/__tests__/mocks.js +170 -0
  80. package/dist/__tests__/mocks.js.map +1 -0
  81. package/dist/__tests__/observability.test.d.ts +2 -0
  82. package/dist/__tests__/observability.test.d.ts.map +1 -0
  83. package/dist/__tests__/observability.test.js +233 -0
  84. package/dist/__tests__/observability.test.js.map +1 -0
  85. package/dist/__tests__/openapi.test.d.ts +2 -0
  86. package/dist/__tests__/openapi.test.d.ts.map +1 -0
  87. package/dist/__tests__/openapi.test.js +105 -0
  88. package/dist/__tests__/openapi.test.js.map +1 -0
  89. package/dist/__tests__/parse-sort.test.d.ts +2 -0
  90. package/dist/__tests__/parse-sort.test.d.ts.map +1 -0
  91. package/dist/__tests__/parse-sort.test.js +24 -0
  92. package/dist/__tests__/parse-sort.test.js.map +1 -0
  93. package/dist/__tests__/preferences.test.d.ts +2 -0
  94. package/dist/__tests__/preferences.test.d.ts.map +1 -0
  95. package/dist/__tests__/preferences.test.js +168 -0
  96. package/dist/__tests__/preferences.test.js.map +1 -0
  97. package/dist/__tests__/router.test.d.ts +2 -0
  98. package/dist/__tests__/router.test.d.ts.map +1 -0
  99. package/dist/__tests__/router.test.js +70 -0
  100. package/dist/__tests__/router.test.js.map +1 -0
  101. package/dist/__tests__/search.test.d.ts +2 -0
  102. package/dist/__tests__/search.test.d.ts.map +1 -0
  103. package/dist/__tests__/search.test.js +174 -0
  104. package/dist/__tests__/search.test.js.map +1 -0
  105. package/dist/__tests__/upload-validation.test.d.ts +2 -0
  106. package/dist/__tests__/upload-validation.test.d.ts.map +1 -0
  107. package/dist/__tests__/upload-validation.test.js +41 -0
  108. package/dist/__tests__/upload-validation.test.js.map +1 -0
  109. package/dist/__tests__/where.test.d.ts +2 -0
  110. package/dist/__tests__/where.test.d.ts.map +1 -0
  111. package/dist/__tests__/where.test.js +105 -0
  112. package/dist/__tests__/where.test.js.map +1 -0
  113. package/dist/__tests__/workflow.test.d.ts +2 -0
  114. package/dist/__tests__/workflow.test.d.ts.map +1 -0
  115. package/dist/__tests__/workflow.test.js +684 -0
  116. package/dist/__tests__/workflow.test.js.map +1 -0
  117. package/dist/app-config-C9VJ6rTv.d.cts +2092 -0
  118. package/dist/app-config-C9VJ6rTv.d.ts +2092 -0
  119. package/dist/app-config-CYV70eoF.d.cts +2100 -0
  120. package/dist/app-config-CYV70eoF.d.ts +2100 -0
  121. package/dist/app.d.ts +35 -0
  122. package/dist/app.d.ts.map +1 -0
  123. package/dist/app.js +164 -0
  124. package/dist/app.js.map +1 -0
  125. package/dist/auth/access.d.ts +5 -0
  126. package/dist/auth/access.d.ts.map +1 -0
  127. package/dist/auth/access.js +59 -0
  128. package/dist/auth/access.js.map +1 -0
  129. package/dist/auth/jexl.d.ts +13 -0
  130. package/dist/auth/jexl.d.ts.map +1 -0
  131. package/dist/auth/jexl.js +30 -0
  132. package/dist/auth/jexl.js.map +1 -0
  133. package/dist/auth/lockout.d.ts +11 -0
  134. package/dist/auth/lockout.d.ts.map +1 -0
  135. package/dist/auth/lockout.js +37 -0
  136. package/dist/auth/lockout.js.map +1 -0
  137. package/dist/auth/password.d.ts +10 -0
  138. package/dist/auth/password.d.ts.map +1 -0
  139. package/dist/auth/password.js +28 -0
  140. package/dist/auth/password.js.map +1 -0
  141. package/dist/auth/sessions.d.ts +39 -0
  142. package/dist/auth/sessions.d.ts.map +1 -0
  143. package/dist/auth/sessions.js +126 -0
  144. package/dist/auth/sessions.js.map +1 -0
  145. package/dist/auth/token.d.ts +23 -0
  146. package/dist/auth/token.d.ts.map +1 -0
  147. package/dist/auth/token.js +40 -0
  148. package/dist/auth/token.js.map +1 -0
  149. package/dist/chunk-23NM4LDU.js +2883 -0
  150. package/dist/chunk-2TKDCCIR.js +2622 -0
  151. package/dist/chunk-3SFITPX4.js +2628 -0
  152. package/dist/chunk-CQVTQ67D.js +2632 -0
  153. package/dist/chunk-ELHRRPKX.js +2863 -0
  154. package/dist/chunk-HRLSFTLR.js +3061 -0
  155. package/dist/chunk-QTIAQBNH.js +3097 -0
  156. package/dist/chunk-T2DGC4MD.js +3008 -0
  157. package/dist/chunk-TLU77TLE.js +3203 -0
  158. package/dist/chunk-YEBCT77K.js +3269 -0
  159. package/dist/chunk-YXEKNN5X.js +3085 -0
  160. package/dist/controllers/admin-auth.controller.d.ts +71 -0
  161. package/dist/controllers/admin-auth.controller.d.ts.map +1 -0
  162. package/dist/controllers/admin-auth.controller.js +450 -0
  163. package/dist/controllers/admin-auth.controller.js.map +1 -0
  164. package/dist/controllers/audit.controller.d.ts +42 -0
  165. package/dist/controllers/audit.controller.d.ts.map +1 -0
  166. package/dist/controllers/audit.controller.js +149 -0
  167. package/dist/controllers/audit.controller.js.map +1 -0
  168. package/dist/controllers/auth.controller.d.ts +143 -0
  169. package/dist/controllers/auth.controller.d.ts.map +1 -0
  170. package/dist/controllers/auth.controller.js +572 -0
  171. package/dist/controllers/auth.controller.js.map +1 -0
  172. package/dist/controllers/collection.controller.d.ts +170 -0
  173. package/dist/controllers/collection.controller.d.ts.map +1 -0
  174. package/dist/controllers/collection.controller.js +1033 -0
  175. package/dist/controllers/collection.controller.js.map +1 -0
  176. package/dist/controllers/global.controller.d.ts +20 -0
  177. package/dist/controllers/global.controller.d.ts.map +1 -0
  178. package/dist/controllers/global.controller.js +182 -0
  179. package/dist/controllers/global.controller.js.map +1 -0
  180. package/dist/controllers/media.controller.d.ts +39 -0
  181. package/dist/controllers/media.controller.d.ts.map +1 -0
  182. package/dist/controllers/media.controller.js +178 -0
  183. package/dist/controllers/media.controller.js.map +1 -0
  184. package/dist/controllers/preview.controller.d.ts +37 -0
  185. package/dist/controllers/preview.controller.d.ts.map +1 -0
  186. package/dist/controllers/preview.controller.js +48 -0
  187. package/dist/controllers/preview.controller.js.map +1 -0
  188. package/dist/index.cjs +707 -27
  189. package/dist/index.d.cts +77 -5
  190. package/dist/index.d.ts +77 -5
  191. package/dist/index.d.ts.map +1 -0
  192. package/dist/index.js +37 -1
  193. package/dist/index.js.map +1 -0
  194. package/dist/middleware/auth.d.ts +19 -0
  195. package/dist/middleware/auth.d.ts.map +1 -0
  196. package/dist/middleware/auth.js +157 -0
  197. package/dist/middleware/auth.js.map +1 -0
  198. package/dist/middleware/rate-limit.d.ts +9 -0
  199. package/dist/middleware/rate-limit.d.ts.map +1 -0
  200. package/dist/middleware/rate-limit.js +75 -0
  201. package/dist/middleware/rate-limit.js.map +1 -0
  202. package/dist/network.d.ts +6 -0
  203. package/dist/network.d.ts.map +1 -0
  204. package/dist/network.js +70 -0
  205. package/dist/network.js.map +1 -0
  206. package/dist/observability.d.ts +113 -0
  207. package/dist/observability.d.ts.map +1 -0
  208. package/dist/observability.js +497 -0
  209. package/dist/observability.js.map +1 -0
  210. package/dist/router.d.ts +8 -0
  211. package/dist/router.d.ts.map +1 -0
  212. package/dist/router.js +704 -0
  213. package/dist/router.js.map +1 -0
  214. package/dist/server.cjs +861 -54
  215. package/dist/server.d.cts +9 -1
  216. package/dist/server.d.ts +9 -1
  217. package/dist/server.d.ts.map +1 -0
  218. package/dist/server.js +189 -26
  219. package/dist/server.js.map +1 -0
  220. package/dist/services/audit.service.d.ts +23 -0
  221. package/dist/services/audit.service.d.ts.map +1 -0
  222. package/dist/services/audit.service.js +39 -0
  223. package/dist/services/audit.service.js.map +1 -0
  224. package/dist/services/defaults.service.d.ts +8 -0
  225. package/dist/services/defaults.service.d.ts.map +1 -0
  226. package/dist/services/defaults.service.js +55 -0
  227. package/dist/services/defaults.service.js.map +1 -0
  228. package/dist/services/email-template.d.ts +46 -0
  229. package/dist/services/email-template.d.ts.map +1 -0
  230. package/dist/services/email-template.js +88 -0
  231. package/dist/services/email-template.js.map +1 -0
  232. package/dist/services/email.service.d.ts +34 -0
  233. package/dist/services/email.service.d.ts.map +1 -0
  234. package/dist/services/email.service.js +129 -0
  235. package/dist/services/email.service.js.map +1 -0
  236. package/dist/services/media.service.d.ts +20 -0
  237. package/dist/services/media.service.d.ts.map +1 -0
  238. package/dist/services/media.service.js +49 -0
  239. package/dist/services/media.service.js.map +1 -0
  240. package/dist/services/population.service.d.ts +20 -0
  241. package/dist/services/population.service.d.ts.map +1 -0
  242. package/dist/services/population.service.js +224 -0
  243. package/dist/services/population.service.js.map +1 -0
  244. package/dist/types/access.d.ts +48 -0
  245. package/dist/types/access.d.ts.map +1 -0
  246. package/dist/types/access.js +2 -0
  247. package/dist/types/access.js.map +1 -0
  248. package/dist/types/adapters.d.ts +152 -0
  249. package/dist/types/adapters.d.ts.map +1 -0
  250. package/dist/types/adapters.js +2 -0
  251. package/dist/types/adapters.js.map +1 -0
  252. package/dist/types/admin-auth.d.ts +132 -0
  253. package/dist/types/admin-auth.d.ts.map +1 -0
  254. package/dist/types/admin-auth.js +2 -0
  255. package/dist/types/admin-auth.js.map +1 -0
  256. package/dist/types/admin.d.ts +86 -0
  257. package/dist/types/admin.d.ts.map +1 -0
  258. package/dist/types/admin.js +2 -0
  259. package/dist/types/admin.js.map +1 -0
  260. package/dist/types/app-config.d.ts +279 -0
  261. package/dist/types/app-config.d.ts.map +1 -0
  262. package/dist/types/app-config.js +2 -0
  263. package/dist/types/app-config.js.map +1 -0
  264. package/dist/types/documents.d.ts +65 -0
  265. package/dist/types/documents.d.ts.map +1 -0
  266. package/dist/types/documents.js +2 -0
  267. package/dist/types/documents.js.map +1 -0
  268. package/dist/types/hooks.d.ts +122 -0
  269. package/dist/types/hooks.d.ts.map +1 -0
  270. package/dist/types/hooks.js +2 -0
  271. package/dist/types/hooks.js.map +1 -0
  272. package/dist/types/index.d.ts +13 -0
  273. package/dist/types/index.d.ts.map +1 -0
  274. package/dist/types/index.js +13 -0
  275. package/dist/types/index.js.map +1 -0
  276. package/dist/types/request.d.ts +42 -0
  277. package/dist/types/request.d.ts.map +1 -0
  278. package/dist/types/request.js +2 -0
  279. package/dist/types/request.js.map +1 -0
  280. package/dist/types/schema-config.d.ts +443 -0
  281. package/dist/types/schema-config.d.ts.map +1 -0
  282. package/dist/types/schema-config.js +2 -0
  283. package/dist/types/schema-config.js.map +1 -0
  284. package/dist/types/schema-core.d.ts +628 -0
  285. package/dist/types/schema-core.d.ts.map +1 -0
  286. package/dist/types/schema-core.js +2 -0
  287. package/dist/types/schema-core.js.map +1 -0
  288. package/dist/types/schema-inference.d.ts +73 -0
  289. package/dist/types/schema-inference.d.ts.map +1 -0
  290. package/dist/types/schema-inference.js +2 -0
  291. package/dist/types/schema-inference.js.map +1 -0
  292. package/dist/types/workflows.d.ts +121 -0
  293. package/dist/types/workflows.d.ts.map +1 -0
  294. package/dist/types/workflows.js +7 -0
  295. package/dist/types/workflows.js.map +1 -0
  296. package/dist/utils/access-control.d.ts +35 -0
  297. package/dist/utils/access-control.d.ts.map +1 -0
  298. package/dist/utils/access-control.js +161 -0
  299. package/dist/utils/access-control.js.map +1 -0
  300. package/dist/utils/admin-auth.d.ts +5 -0
  301. package/dist/utils/admin-auth.d.ts.map +1 -0
  302. package/dist/utils/admin-auth.js +39 -0
  303. package/dist/utils/admin-auth.js.map +1 -0
  304. package/dist/utils/block-references.d.ts +10 -0
  305. package/dist/utils/block-references.d.ts.map +1 -0
  306. package/dist/utils/block-references.js +100 -0
  307. package/dist/utils/block-references.js.map +1 -0
  308. package/dist/utils/collection-search.d.ts +11 -0
  309. package/dist/utils/collection-search.d.ts.map +1 -0
  310. package/dist/utils/collection-search.js +204 -0
  311. package/dist/utils/collection-search.js.map +1 -0
  312. package/dist/utils/config.d.ts +8 -0
  313. package/dist/utils/config.d.ts.map +1 -0
  314. package/dist/utils/config.js +336 -0
  315. package/dist/utils/config.js.map +1 -0
  316. package/dist/utils/hooks.d.ts +41 -0
  317. package/dist/utils/hooks.d.ts.map +1 -0
  318. package/dist/utils/hooks.js +177 -0
  319. package/dist/utils/hooks.js.map +1 -0
  320. package/dist/utils/openapi.d.ts +6 -0
  321. package/dist/utils/openapi.d.ts.map +1 -0
  322. package/dist/utils/openapi.js +981 -0
  323. package/dist/utils/openapi.js.map +1 -0
  324. package/dist/utils/parse-sort.d.ts +43 -0
  325. package/dist/utils/parse-sort.d.ts.map +1 -0
  326. package/dist/utils/parse-sort.js +70 -0
  327. package/dist/utils/parse-sort.js.map +1 -0
  328. package/dist/utils/parse-where.d.ts +69 -0
  329. package/dist/utils/parse-where.d.ts.map +1 -0
  330. package/dist/utils/parse-where.js +202 -0
  331. package/dist/utils/parse-where.js.map +1 -0
  332. package/dist/utils/readonly-db.d.ts +9 -0
  333. package/dist/utils/readonly-db.d.ts.map +1 -0
  334. package/dist/utils/readonly-db.js +21 -0
  335. package/dist/utils/readonly-db.js.map +1 -0
  336. package/dist/utils/swagger.d.ts +11 -0
  337. package/dist/utils/swagger.d.ts.map +1 -0
  338. package/dist/utils/swagger.js +62 -0
  339. package/dist/utils/swagger.js.map +1 -0
  340. package/dist/utils/upload-validation.d.ts +23 -0
  341. package/dist/utils/upload-validation.d.ts.map +1 -0
  342. package/dist/utils/upload-validation.js +46 -0
  343. package/dist/utils/upload-validation.js.map +1 -0
  344. package/dist/utils/where-sanitizer.d.ts +10 -0
  345. package/dist/utils/where-sanitizer.d.ts.map +1 -0
  346. package/dist/utils/where-sanitizer.js +67 -0
  347. package/dist/utils/where-sanitizer.js.map +1 -0
  348. package/dist/workflows.d.ts +86 -0
  349. package/dist/workflows.d.ts.map +1 -0
  350. package/dist/workflows.js +405 -0
  351. package/dist/workflows.js.map +1 -0
  352. package/package.json +1 -1
package/dist/router.js ADDED
@@ -0,0 +1,704 @@
1
+ import { CollectionController } from "./controllers/collection.controller.js";
2
+ import { GlobalController } from "./controllers/global.controller.js";
3
+ import { MediaController } from "./controllers/media.controller.js";
4
+ import { AuthController } from "./controllers/auth.controller.js";
5
+ import { AdminAuthController } from "./controllers/admin-auth.controller.js";
6
+ import { PreviewController } from "./controllers/preview.controller.js";
7
+ import { AuditController } from "./controllers/audit.controller.js";
8
+ import { requireAuth, optionalAuth } from "./middleware/auth.js";
9
+ import { generateOpenApi } from "./utils/openapi.js";
10
+ import { getSwaggerHtml } from "./utils/swagger.js";
11
+ import { getPublicAdminAuthConfig } from "./utils/admin-auth.js";
12
+ import { mergeDynamicConfig } from "./utils/block-references.js";
13
+ import { resolveBooleanAccess, toHookRequestContext, } from "./utils/access-control.js";
14
+ import { getConfigLogger, getRequestLogger } from "./observability.js";
15
+ /**
16
+ * Access gate middleware for granular permissions using Jexl.
17
+ */
18
+ function accessGate(config, target, action) {
19
+ return async (c, next) => {
20
+ const user = c.get("user");
21
+ const accessExpr = target.access?.[action];
22
+ // If no access expression, default to public (true) for now to maintain parity with old behavior.
23
+ // However, if we want to be secure by default, we could change this to false.
24
+ if (accessExpr === undefined || accessExpr === null) {
25
+ return await next();
26
+ }
27
+ const allowed = await resolveBooleanAccess(config, accessExpr, {
28
+ user,
29
+ req: toHookRequestContext(c.req),
30
+ });
31
+ if (!allowed) {
32
+ return c.json({ error: true, message: `Access denied: ${action} on ${target.slug}` }, 403);
33
+ }
34
+ await next();
35
+ };
36
+ }
37
+ function serializeFieldForApi(f) {
38
+ if (!f)
39
+ return f;
40
+ const serialized = { ...f };
41
+ if (serialized.admin?.hooks) {
42
+ const hooks = { ...serialized.admin.hooks };
43
+ if (typeof hooks.onChange === "function") {
44
+ hooks.onChange = hooks.onChange.toString();
45
+ }
46
+ if (typeof hooks.options === "function") {
47
+ hooks.options = hooks.options.toString();
48
+ }
49
+ serialized.admin = { ...serialized.admin, hooks };
50
+ }
51
+ if (typeof serialized.options === "function" ||
52
+ (serialized.options &&
53
+ typeof serialized.options === "object" &&
54
+ "resolve" in serialized.options)) {
55
+ serialized.options = { _dynamic: true };
56
+ }
57
+ if (serialized.fields) {
58
+ serialized.fields = serialized.fields.map(serializeFieldForApi);
59
+ }
60
+ if (serialized.blocks && !serialized.blockReferences?.length) {
61
+ serialized.blocks = serialized.blocks.map((b) => ({
62
+ ...b,
63
+ fields: b.fields?.map(serializeFieldForApi),
64
+ }));
65
+ }
66
+ else if (serialized.blockReferences?.length) {
67
+ delete serialized.blocks;
68
+ }
69
+ return serialized;
70
+ }
71
+ function serializeBlockForApi(block) {
72
+ return {
73
+ ...block,
74
+ fields: block.fields?.map(serializeFieldForApi),
75
+ };
76
+ }
77
+ /**
78
+ * Register dynamic routes based on the provided configuration.
79
+ */
80
+ export function registerRoutes(app, config) {
81
+ // Per-app cache for dynamic option resolvers that opt in via `cacheTTL`.
82
+ // Scoped to this config so tests and multi-tenant setups stay isolated.
83
+ const optionsCache = new Map();
84
+ // 1. Schema Endpoints
85
+ // Used by the SDK and Admin to understand the content structure
86
+ app.get("/api/schemas", optionalAuth(config), async (c) => {
87
+ const siteId = c.req.header("X-Site-Id");
88
+ const requestConfig = siteId && config.onSchemaFetch
89
+ ? mergeDynamicConfig(config, await config.onSchemaFetch(siteId))
90
+ : config;
91
+ const collections = [...requestConfig.collections];
92
+ const globals = [...requestConfig.globals];
93
+ const user = c.get("user");
94
+ const accessArgs = { user, req: toHookRequestContext(c.req) };
95
+ const serializeAccess = async (access) => {
96
+ if (typeof access === "string")
97
+ return access;
98
+ if (typeof access === "boolean")
99
+ return access;
100
+ // Named policies: inline a string/boolean policy so the admin evaluates it
101
+ // live against the form; resolve function policies to a static boolean.
102
+ if (access &&
103
+ typeof access === "object" &&
104
+ typeof access.policy === "string") {
105
+ const policy = requestConfig.accessPolicies?.[access.policy];
106
+ if (typeof policy === "string" || typeof policy === "boolean")
107
+ return policy;
108
+ }
109
+ return resolveBooleanAccess(requestConfig, access, accessArgs);
110
+ };
111
+ const filteredCollections = await Promise.all(collections
112
+ .filter((col) => !siteId || col.shared || !col.siteId || col.siteId === siteId)
113
+ .map(async (col) => ({
114
+ slug: col.slug,
115
+ labels: col.labels,
116
+ access: {
117
+ read: await serializeAccess(col.access?.read),
118
+ create: await serializeAccess(col.access?.create),
119
+ update: await serializeAccess(col.access?.update),
120
+ delete: await serializeAccess(col.access?.delete),
121
+ },
122
+ fields: await Promise.all(col.fields.map(serializeFieldForApi).map(async (f) => ({
123
+ name: f.name,
124
+ type: f.type,
125
+ label: f.label,
126
+ required: f.required,
127
+ defaultValue: f.defaultValue,
128
+ options: f.options,
129
+ relationTo: f.relationTo,
130
+ hasMany: f.hasMany,
131
+ fields: f.fields,
132
+ blocks: f.blocks,
133
+ blockReferences: f.blockReferences,
134
+ collection: f.collection,
135
+ on: f.on,
136
+ limit: f.limit,
137
+ admin: f.admin,
138
+ access: {
139
+ read: await serializeAccess(f.access?.read),
140
+ create: await serializeAccess(f.access?.create),
141
+ update: await serializeAccess(f.access?.update),
142
+ },
143
+ }))),
144
+ upload: !!col.upload,
145
+ auth: !!col.auth,
146
+ audit: !!col.audit,
147
+ drafts: !!col.drafts,
148
+ admin: col.admin,
149
+ workflow: col.workflow
150
+ ? {
151
+ initialState: col.workflow.initialState,
152
+ draftState: col.workflow.draftState,
153
+ states: col.workflow.states,
154
+ transitions: col.workflow.transitions.map((t) => ({
155
+ name: t.name,
156
+ label: t.label,
157
+ from: t.from,
158
+ to: t.to,
159
+ requiredCapabilities: t.requiredCapabilities,
160
+ requireComment: t.requireComment,
161
+ unpublish: t.unpublish,
162
+ })),
163
+ roles: col.workflow.roles,
164
+ }
165
+ : undefined,
166
+ })));
167
+ const filteredGlobals = await Promise.all(globals
168
+ .filter((glb) => !siteId || glb.shared || !glb.siteId || glb.siteId === siteId)
169
+ .map(async (glb) => ({
170
+ slug: glb.slug,
171
+ label: glb.label,
172
+ access: {
173
+ read: await serializeAccess(glb.access?.read),
174
+ update: await serializeAccess(glb.access?.update),
175
+ },
176
+ fields: await Promise.all(glb.fields.map(serializeFieldForApi).map(async (f) => ({
177
+ name: f.name,
178
+ type: f.type,
179
+ label: f.label,
180
+ required: f.required,
181
+ defaultValue: f.defaultValue,
182
+ options: f.options,
183
+ relationTo: f.relationTo,
184
+ hasMany: f.hasMany,
185
+ fields: f.fields,
186
+ blocks: f.blocks,
187
+ blockReferences: f.blockReferences,
188
+ collection: f.collection,
189
+ on: f.on,
190
+ limit: f.limit,
191
+ admin: f.admin,
192
+ access: {
193
+ read: await serializeAccess(f.access?.read),
194
+ update: await serializeAccess(f.access?.update),
195
+ },
196
+ }))),
197
+ admin: glb.admin,
198
+ })));
199
+ return c.json({
200
+ blocks: requestConfig.blocks?.map(serializeBlockForApi),
201
+ collections: filteredCollections,
202
+ globals: filteredGlobals,
203
+ admin: requestConfig.admin || {},
204
+ adminAuth: getPublicAdminAuthConfig(requestConfig.adminAuth, collections),
205
+ hasStorage: !!requestConfig.storage,
206
+ });
207
+ });
208
+ app.get("/api/dyrected/options/:collection/:field", optionalAuth(config), async (c) => {
209
+ const { collection: colSlug, field: fieldName } = c.req.param();
210
+ const siteId = c.req.header("X-Site-Id");
211
+ // Resolve collections
212
+ const requestConfig = siteId && config.onSchemaFetch
213
+ ? mergeDynamicConfig(config, await config.onSchemaFetch(siteId))
214
+ : config;
215
+ let collections = [...requestConfig.collections];
216
+ const user = c.get("user");
217
+ let collection = collections.find((col) => col.slug === colSlug);
218
+ let field;
219
+ if (collection) {
220
+ // Check read access on collection
221
+ const accessExpr = collection.access?.read;
222
+ if (accessExpr !== undefined && accessExpr !== null) {
223
+ const allowed = await resolveBooleanAccess(config, accessExpr, {
224
+ user,
225
+ req: toHookRequestContext(c.req),
226
+ });
227
+ if (!allowed) {
228
+ return c.json({ error: true, message: `Access denied: read on ${colSlug}` }, 403);
229
+ }
230
+ }
231
+ field = collection.fields.find((f) => f.name === fieldName);
232
+ }
233
+ else {
234
+ const globals = [...requestConfig.globals];
235
+ const glb = globals.find((g) => g.slug === colSlug);
236
+ if (!glb) {
237
+ return c.json({
238
+ error: true,
239
+ message: `${colSlug} not found as collection or global`,
240
+ }, 404);
241
+ }
242
+ // Check read access on global
243
+ const accessExpr = glb.access?.read;
244
+ if (accessExpr !== undefined && accessExpr !== null) {
245
+ const allowed = await resolveBooleanAccess(config, accessExpr, {
246
+ user,
247
+ req: toHookRequestContext(c.req),
248
+ });
249
+ if (!allowed) {
250
+ return c.json({
251
+ error: true,
252
+ message: `Access denied: read on global ${colSlug}`,
253
+ }, 403);
254
+ }
255
+ }
256
+ field = glb.fields.find((f) => f.name === fieldName);
257
+ }
258
+ if (!field) {
259
+ return c.json({
260
+ error: true,
261
+ message: `Field ${fieldName} not found in ${colSlug}`,
262
+ }, 404);
263
+ }
264
+ // Get the resolver
265
+ let resolver;
266
+ let cacheTTL;
267
+ if (typeof field.options === "function") {
268
+ resolver = field.options;
269
+ }
270
+ else if (field.options &&
271
+ typeof field.options === "object" &&
272
+ "resolve" in field.options) {
273
+ resolver = field.options.resolve;
274
+ cacheTTL = field.options.cacheTTL;
275
+ }
276
+ if (!resolver) {
277
+ return c.json({
278
+ error: true,
279
+ message: `Field ${fieldName} in ${colSlug} is not dynamic`,
280
+ }, 400);
281
+ }
282
+ try {
283
+ const db = c.get("db") || config.db;
284
+ // Construct a request query helper
285
+ const queryParams = c.req.query();
286
+ const reqContext = {
287
+ query: queryParams,
288
+ headers: c.req.header(),
289
+ raw: c.req.raw,
290
+ };
291
+ // When the resolver opts into caching, key the result by the inputs that
292
+ // change it: the field, the query params, and the requesting user (so a
293
+ // user-scoped resolver never serves another user's options).
294
+ const shouldCache = typeof cacheTTL === "number" && cacheTTL > 0;
295
+ let cacheKey = "";
296
+ if (shouldCache) {
297
+ cacheKey = JSON.stringify([
298
+ siteId ?? "",
299
+ colSlug,
300
+ fieldName,
301
+ user?.id ?? null,
302
+ queryParams,
303
+ ]);
304
+ const hit = optionsCache.get(cacheKey);
305
+ if (hit && hit.expires > Date.now()) {
306
+ return c.json(hit.value);
307
+ }
308
+ }
309
+ const result = await resolver({
310
+ db,
311
+ user,
312
+ req: reqContext,
313
+ });
314
+ if (shouldCache) {
315
+ optionsCache.set(cacheKey, {
316
+ expires: Date.now() + cacheTTL * 1000,
317
+ value: result,
318
+ });
319
+ }
320
+ return c.json(result);
321
+ }
322
+ catch (err) {
323
+ getRequestLogger(c, "router").error({
324
+ err,
325
+ msg: "Failed to resolve dynamic field options",
326
+ fieldName,
327
+ });
328
+ return c.json({
329
+ error: true,
330
+ message: err.message || "Failed to resolve dynamic options",
331
+ }, 500);
332
+ }
333
+ });
334
+ app.get("/api/openapi.json", (c) => {
335
+ return c.json(generateOpenApi(config));
336
+ });
337
+ app.get("/api/docs", (c) => {
338
+ return c.html(getSwaggerHtml());
339
+ });
340
+ app.get("/api/preferences/:key", requireAuth(config), async (c) => {
341
+ const db = config.db;
342
+ const user = c.get("user");
343
+ const key = c.req.param("key");
344
+ const scope = c.req.query("scope");
345
+ if (!db)
346
+ return c.json({ message: "Database not configured" }, 500);
347
+ if (!user?.collection || !user.sub)
348
+ return c.json({ error: true, message: "Authentication required." }, 401);
349
+ if (!key)
350
+ return c.json({ error: true, message: "Preference key is required." }, 400);
351
+ const getGlobalPreference = async () => {
352
+ const globalDoc = await db.findOne({
353
+ collection: "__global_preferences",
354
+ id: key,
355
+ });
356
+ return globalDoc ? globalDoc.value : null;
357
+ };
358
+ if (scope === "global") {
359
+ const globalValue = await getGlobalPreference();
360
+ return c.json({ key, value: globalValue });
361
+ }
362
+ const doc = await db.findOne({ collection: user.collection, id: user.sub });
363
+ if (!doc)
364
+ return c.json({ error: true, message: "User not found." }, 404);
365
+ const preferences = typeof doc.__preferences === "object" && doc.__preferences !== null
366
+ ? doc.__preferences
367
+ : {};
368
+ if (key in preferences) {
369
+ return c.json({ key, value: preferences[key] ?? null });
370
+ }
371
+ const globalValue = await getGlobalPreference();
372
+ return c.json({ key, value: globalValue });
373
+ });
374
+ app.put("/api/preferences/:key", requireAuth(config), async (c) => {
375
+ const db = config.db;
376
+ const user = c.get("user");
377
+ const key = c.req.param("key");
378
+ const scope = c.req.query("scope");
379
+ if (!db)
380
+ return c.json({ message: "Database not configured" }, 500);
381
+ if (!user?.collection || !user.sub)
382
+ return c.json({ error: true, message: "Authentication required." }, 401);
383
+ if (!key)
384
+ return c.json({ error: true, message: "Preference key is required." }, 400);
385
+ const body = await c.req.json().catch(() => ({}));
386
+ if (scope === "global") {
387
+ const isAdminUser = Array.isArray(user?.roles) && user.roles.includes("admin");
388
+ if (!isAdminUser) {
389
+ return c.json({
390
+ error: true,
391
+ message: "Only administrators can save global preferences.",
392
+ }, 403);
393
+ }
394
+ const existing = await db.findOne({
395
+ collection: "__global_preferences",
396
+ id: key,
397
+ });
398
+ if (existing) {
399
+ await db.update({
400
+ collection: "__global_preferences",
401
+ id: key,
402
+ data: { value: body.value },
403
+ });
404
+ }
405
+ else {
406
+ await db.create({
407
+ collection: "__global_preferences",
408
+ data: { id: key, value: body.value },
409
+ });
410
+ }
411
+ return c.json({ key, value: body.value });
412
+ }
413
+ const doc = await db.findOne({ collection: user.collection, id: user.sub });
414
+ if (!doc)
415
+ return c.json({ error: true, message: "User not found." }, 404);
416
+ const preferences = typeof doc.__preferences === "object" && doc.__preferences !== null
417
+ ? doc.__preferences
418
+ : {};
419
+ const nextPreferences = { ...preferences, [key]: body.value };
420
+ await db.update({
421
+ collection: user.collection,
422
+ id: user.sub,
423
+ data: { __preferences: nextPreferences },
424
+ });
425
+ return c.json({ key, value: body.value });
426
+ });
427
+ app.delete("/api/preferences/:key", requireAuth(config), async (c) => {
428
+ const db = config.db;
429
+ const user = c.get("user");
430
+ const key = c.req.param("key");
431
+ const scope = c.req.query("scope");
432
+ if (!db)
433
+ return c.json({ message: "Database not configured" }, 500);
434
+ if (!user?.collection || !user.sub)
435
+ return c.json({ error: true, message: "Authentication required." }, 401);
436
+ if (!key)
437
+ return c.json({ error: true, message: "Preference key is required." }, 400);
438
+ if (scope === "global") {
439
+ const isAdminUser = Array.isArray(user?.roles) && user.roles.includes("admin");
440
+ if (!isAdminUser) {
441
+ return c.json({
442
+ error: true,
443
+ message: "Only administrators can delete global preferences.",
444
+ }, 403);
445
+ }
446
+ await db.delete({ collection: "__global_preferences", id: key });
447
+ return c.json({ success: true });
448
+ }
449
+ const doc = await db.findOne({ collection: user.collection, id: user.sub });
450
+ if (!doc)
451
+ return c.json({ error: true, message: "User not found." }, 404);
452
+ const preferences = typeof doc.__preferences === "object" && doc.__preferences !== null
453
+ ? { ...doc.__preferences }
454
+ : {};
455
+ delete preferences[key];
456
+ await db.update({
457
+ collection: user.collection,
458
+ id: user.sub,
459
+ data: { __preferences: preferences },
460
+ });
461
+ return c.json({ success: true });
462
+ });
463
+ // Global Media Fallback (Proxies to the 'media' collection)
464
+ app.get("/api/media/:filename{.+$}", async (c) => {
465
+ const mediaController = new MediaController("media");
466
+ return mediaController.serve(c);
467
+ });
468
+ app.get("/media/:filename{.+$}", async (c) => {
469
+ const mediaController = new MediaController("media");
470
+ return mediaController.serve(c);
471
+ });
472
+ // 2. Media Routes (Conditional & Dynamic)
473
+ if (config.storage) {
474
+ const uploadCollections = config.collections.filter((c) => c.upload);
475
+ // Register routes for each upload-enabled collection
476
+ for (const col of uploadCollections) {
477
+ const mediaController = new MediaController(col.slug);
478
+ const prefix = `/api/collections/${col.slug}`;
479
+ app.get(`${prefix}/media`, accessGate(config, col, "read"), (c) => mediaController.find(c));
480
+ app.get(`${prefix}/media/:filename{.+$}`, (c) => mediaController.serve(c));
481
+ app.post(`${prefix}/media`, accessGate(config, col, "create"), (c) => mediaController.upload(c));
482
+ app.delete(`${prefix}/media/:id`, accessGate(config, col, "delete"), (c) => mediaController.delete(c));
483
+ }
484
+ }
485
+ // 2b. Admin Auth Routes
486
+ const adminAuthController = new AdminAuthController(config);
487
+ app.get("/api/admin/auth/providers", (c) => adminAuthController.providers(c));
488
+ app.get("/api/admin/auth/:provider/start", (c) => adminAuthController.start(c));
489
+ app.get("/api/admin/auth/:provider/callback", (c) => adminAuthController.callback(c));
490
+ app.post("/api/admin/auth/:provider/exchange", (c) => adminAuthController.exchange(c));
491
+ app.post("/api/admin/logout", (c) => adminAuthController.logout(c));
492
+ // 3. Auth Routes — for collections with auth: true
493
+ for (const collection of config.collections) {
494
+ if (!collection.auth)
495
+ continue;
496
+ const path = `/api/collections/${collection.slug}`;
497
+ const authController = new AuthController(collection);
498
+ app.post(`${path}/login`, (c) => authController.login(c));
499
+ app.post(`${path}/logout`, (c) => authController.logout(c));
500
+ app.get(`${path}/init`, (c) => authController.init(c));
501
+ app.post(`${path}/first-user`, (c) => authController.registerFirstUser(c));
502
+ // /me and /refresh-token require a valid token
503
+ app.get(`${path}/me`, requireAuth(config), (c) => authController.me(c));
504
+ app.post(`${path}/refresh-token`, requireAuth(config), (c) => authController.refreshToken(c));
505
+ app.post(`${path}/forgot-password`, (c) => authController.forgotPassword(c));
506
+ app.post(`${path}/reset-password`, (c) => authController.resetPassword(c));
507
+ app.post(`${path}/invite`, requireAuth(config), (c) => authController.invite(c));
508
+ app.post(`${path}/accept-invite`, (c) => authController.acceptInvite(c));
509
+ }
510
+ // 4. Collection Routes (Static)
511
+ const auditController = new AuditController();
512
+ for (const collection of config.collections) {
513
+ const path = `/api/collections/${collection.slug}`;
514
+ const controller = new CollectionController(collection);
515
+ app.get(path, (c) => controller.find(c));
516
+ app.post(path, (c) => controller.create(c));
517
+ app.post(`${path}/media`, (c) => controller.create(c));
518
+ // delete-many must be registered before /:id to avoid the wildcard swallowing it
519
+ app.delete(`${path}/delete-many`, (c) => controller.deleteMany(c));
520
+ if (collection.audit) {
521
+ app.get(`${path}/__audit`, (c) => auditController.findForCollection(c, collection));
522
+ }
523
+ app.get(`${path}/:id`, (c) => controller.findOne(c));
524
+ app.patch(`${path}/:id`, (c) => controller.update(c));
525
+ app.delete(`${path}/:id`, (c) => controller.delete(c));
526
+ // Dedicated password-change endpoint (auth collections only)
527
+ if (collection.auth) {
528
+ app.post(`${path}/:id/change-password`, requireAuth(config), (c) => controller.changePassword(c));
529
+ }
530
+ // Workflow routes — only registered when the collection has a workflow configured
531
+ if (collection.workflow) {
532
+ app.post(`${path}/:id/transitions/:transition`, requireAuth(config), (c) => controller.transition(c));
533
+ app.get(`${path}/:id/workflow-history`, requireAuth(config), (c) => controller.workflowHistory(c));
534
+ }
535
+ }
536
+ // 5. Global Routes (Static)
537
+ for (const global of config.globals) {
538
+ const path = `/api/globals/${global.slug}`;
539
+ const controller = new GlobalController(global);
540
+ app.get(path, (c) => controller.get(c));
541
+ app.patch(path, (c) => controller.update(c));
542
+ }
543
+ // 6. Preview Routes
544
+ if (!process.env.DYRECTED_JWT_SECRET) {
545
+ getConfigLogger(config, "router").warn({
546
+ msg: "DYRECTED_JWT_SECRET is not set; token-mode live preview is signing with an insecure default",
547
+ });
548
+ }
549
+ const previewController = new PreviewController();
550
+ app.post("/api/preview-token", requireAuth(config), (c) => previewController.createToken(c));
551
+ app.get("/api/preview-data", (c) => previewController.getData(c));
552
+ app.get("/api/audit", (c) => auditController.findAll(c));
553
+ // 7. Dynamic Routes (Tenant-specific)
554
+ // This handles collections/globals defined via sync:schema and fetched via onSchemaFetch
555
+ // 7a. Audit and workflow sub-routes for dynamic tenant collections.
556
+ // Must be registered BEFORE the /:id? catch-all so Hono doesn't swallow deeper paths.
557
+ // Pattern: GET /api/collections/:slug/__audit
558
+ // POST /api/collections/:slug/:id/transitions/:transition
559
+ // GET /api/collections/:slug/:id/workflow-history
560
+ app.get("/api/collections/:slug/__audit", async (c) => {
561
+ const slug = c.req.param("slug");
562
+ const siteId = c.req.header("X-Site-Id") || c.get("siteId");
563
+ const config = c.get("config");
564
+ if (config.collections.some((col) => col.slug === slug)) {
565
+ return c.json({ message: "Not Found" }, 404);
566
+ }
567
+ if (!config.onSchemaFetch || !siteId) {
568
+ return c.json({ message: `Collection "${slug}" not found` }, 404);
569
+ }
570
+ const requestConfig = mergeDynamicConfig(config, await config.onSchemaFetch(siteId));
571
+ const collection = requestConfig.collections.find((col) => col.slug === slug);
572
+ if (!collection?.audit) {
573
+ return c.json({ message: `Collection "${slug}" not found or has no audit log` }, 404);
574
+ }
575
+ return auditController.findForCollection(c, collection);
576
+ });
577
+ app.post("/api/collections/:slug/:id/transitions/:transition", requireAuth(config), async (c) => {
578
+ const slug = c.req.param("slug");
579
+ const siteId = c.req.header("X-Site-Id") || c.get("siteId");
580
+ const config = c.get("config");
581
+ // Skip if static — static workflow routes are registered directly above;
582
+ // if this wildcard fires for a static slug it means the sub-path is unknown.
583
+ if (config.collections.some((col) => col.slug === slug)) {
584
+ return c.json({ message: "Not Found" }, 404);
585
+ }
586
+ if (!config.onSchemaFetch || !siteId) {
587
+ return c.json({ message: `Collection "${slug}" not found` }, 404);
588
+ }
589
+ const requestConfig = mergeDynamicConfig(config, await config.onSchemaFetch(siteId));
590
+ const collection = requestConfig.collections.find((col) => col.slug === slug);
591
+ if (!collection?.workflow) {
592
+ return c.json({ message: `Collection "${slug}" not found or has no workflow` }, 404);
593
+ }
594
+ const controller = new CollectionController(collection);
595
+ return controller.transition(c);
596
+ });
597
+ app.get("/api/collections/:slug/:id/workflow-history", requireAuth(config), async (c) => {
598
+ const slug = c.req.param("slug");
599
+ const siteId = c.req.header("X-Site-Id") || c.get("siteId");
600
+ const config = c.get("config");
601
+ if (config.collections.some((col) => col.slug === slug)) {
602
+ return c.json({ message: "Not Found" }, 404);
603
+ }
604
+ if (!config.onSchemaFetch || !siteId) {
605
+ return c.json({ message: `Collection "${slug}" not found` }, 404);
606
+ }
607
+ const requestConfig = mergeDynamicConfig(config, await config.onSchemaFetch(siteId));
608
+ const collection = requestConfig.collections.find((col) => col.slug === slug);
609
+ if (!collection?.workflow) {
610
+ return c.json({ message: `Collection "${slug}" not found or has no workflow` }, 404);
611
+ }
612
+ const controller = new CollectionController(collection);
613
+ return controller.workflowHistory(c);
614
+ });
615
+ // 7b. Core dynamic catch-all for tenant collections (list, create, findOne, update, delete).
616
+ app.all("/api/collections/:slug/:id?", async (c) => {
617
+ const slug = c.req.param("slug");
618
+ const id = c.req.param("id");
619
+ const siteId = c.req.header("X-Site-Id") || c.get("siteId");
620
+ const config = c.get("config");
621
+ // Skip if static (already handled by routes above)
622
+ if (config.collections.some((col) => col.slug === slug)) {
623
+ return c.json({ message: "Method Not Allowed" }, 405);
624
+ }
625
+ if (config.onSchemaFetch && siteId) {
626
+ const requestConfig = mergeDynamicConfig(config, await config.onSchemaFetch(siteId));
627
+ let collection = requestConfig.collections.find((col) => col.slug === slug);
628
+ if (!collection && slug === "media") {
629
+ collection = {
630
+ slug: "media",
631
+ labels: { singular: "Media", plural: "Media" },
632
+ upload: true,
633
+ fields: [],
634
+ };
635
+ }
636
+ if (collection) {
637
+ // Handle auth sub-routes for dynamic auth collections
638
+ if (collection.auth && id) {
639
+ const authController = new AuthController(collection);
640
+ const method = c.req.method;
641
+ if (method === "POST" && id === "login")
642
+ return authController.login(c);
643
+ if (method === "POST" && id === "logout")
644
+ return authController.logout(c);
645
+ if (method === "GET" && id === "me")
646
+ return authController.me(c);
647
+ if (method === "POST" && id === "refresh-token")
648
+ return authController.refreshToken(c);
649
+ if (method === "POST" && id === "forgot-password")
650
+ return authController.forgotPassword(c);
651
+ if (method === "POST" && id === "reset-password")
652
+ return authController.resetPassword(c);
653
+ }
654
+ const controller = new CollectionController(collection);
655
+ const method = c.req.method;
656
+ if (id) {
657
+ if (method === "GET")
658
+ return controller.findOne(c);
659
+ if (method === "PATCH")
660
+ return controller.update(c);
661
+ if (method === "DELETE" && id === "delete-many")
662
+ return controller.deleteMany(c);
663
+ if (method === "DELETE")
664
+ return controller.delete(c);
665
+ if (method === "POST" && id === "media")
666
+ return controller.create(c);
667
+ }
668
+ else {
669
+ if (method === "GET")
670
+ return controller.find(c);
671
+ if (method === "POST")
672
+ return controller.create(c);
673
+ }
674
+ }
675
+ }
676
+ return c.json({ message: `Collection "${slug}" not found` }, 404);
677
+ });
678
+ app.all("/api/globals/:slug/:id?", async (c) => {
679
+ const slug = c.req.param("slug");
680
+ const id = c.req.param("id");
681
+ const siteId = c.req.header("X-Site-Id") || c.get("siteId");
682
+ const config = c.get("config");
683
+ // Skip if static
684
+ if (config.globals.some((glb) => glb.slug === slug)) {
685
+ return c.json({ message: "Method Not Allowed" }, 405);
686
+ }
687
+ if (config.onSchemaFetch && siteId) {
688
+ const requestConfig = mergeDynamicConfig(config, await config.onSchemaFetch(siteId));
689
+ const global = requestConfig.globals.find((glb) => glb.slug === slug);
690
+ if (global) {
691
+ const controller = new GlobalController(global);
692
+ const method = c.req.method;
693
+ if (!id) {
694
+ if (method === "GET")
695
+ return controller.get(c);
696
+ if (method === "PATCH")
697
+ return controller.update(c);
698
+ }
699
+ }
700
+ }
701
+ return c.json({ message: `Global "${slug}" not found` }, 404);
702
+ });
703
+ }
704
+ //# sourceMappingURL=router.js.map