@conloca/content-api 0.1.16 → 0.2.1

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 (299) hide show
  1. package/dist/asset-constraints.d.ts +16 -0
  2. package/dist/asset-constraints.d.ts.map +1 -0
  3. package/dist/asset-constraints.js +29 -0
  4. package/dist/asset-constraints.js.map +1 -0
  5. package/dist/asset-metadata-store.d.ts +60 -0
  6. package/dist/asset-metadata-store.d.ts.map +1 -0
  7. package/dist/asset-metadata-store.js +335 -0
  8. package/dist/asset-metadata-store.js.map +1 -0
  9. package/dist/asset-operations.d.ts +21 -28
  10. package/dist/asset-operations.d.ts.map +1 -1
  11. package/dist/asset-operations.js +268 -305
  12. package/dist/asset-operations.js.map +1 -0
  13. package/dist/asset-types.d.ts +23 -7
  14. package/dist/asset-types.d.ts.map +1 -1
  15. package/dist/asset-types.js +8 -2
  16. package/dist/asset-types.js.map +1 -0
  17. package/dist/asset-usage.d.ts +11 -0
  18. package/dist/asset-usage.d.ts.map +1 -0
  19. package/dist/asset-usage.js +50 -0
  20. package/dist/asset-usage.js.map +1 -0
  21. package/dist/base-content-index.d.ts +8 -2
  22. package/dist/base-content-index.d.ts.map +1 -1
  23. package/dist/base-content-index.js +24 -3
  24. package/dist/base-content-index.js.map +1 -0
  25. package/dist/cf-access.js +1 -0
  26. package/dist/cf-access.js.map +1 -0
  27. package/dist/component-registry.d.ts +1 -1
  28. package/dist/component-registry.d.ts.map +1 -1
  29. package/dist/component-registry.js +2 -1
  30. package/dist/component-registry.js.map +1 -0
  31. package/dist/component-registry.types.js +1 -0
  32. package/dist/component-registry.types.js.map +1 -0
  33. package/dist/component-vocabulary.d.ts +33 -0
  34. package/dist/component-vocabulary.d.ts.map +1 -0
  35. package/dist/component-vocabulary.js +105 -0
  36. package/dist/component-vocabulary.js.map +1 -0
  37. package/dist/content-api.interface.d.ts +5 -5
  38. package/dist/content-api.interface.d.ts.map +1 -1
  39. package/dist/content-api.interface.js +1 -0
  40. package/dist/content-api.interface.js.map +1 -0
  41. package/dist/content-filters.d.ts +2 -2
  42. package/dist/content-filters.d.ts.map +1 -1
  43. package/dist/content-filters.js +9 -8
  44. package/dist/content-filters.js.map +1 -0
  45. package/dist/content-index.d.ts +74 -16
  46. package/dist/content-index.d.ts.map +1 -1
  47. package/dist/content-index.js +183 -55
  48. package/dist/content-index.js.map +1 -0
  49. package/dist/content-operations.d.ts +71 -7
  50. package/dist/content-operations.d.ts.map +1 -1
  51. package/dist/content-operations.js +166 -82
  52. package/dist/content-operations.js.map +1 -0
  53. package/dist/content-path.d.ts +64 -0
  54. package/dist/content-path.d.ts.map +1 -0
  55. package/dist/content-path.js +95 -0
  56. package/dist/content-path.js.map +1 -0
  57. package/dist/content-reader.d.ts +2 -2
  58. package/dist/content-reader.d.ts.map +1 -1
  59. package/dist/content-reader.js +2 -1
  60. package/dist/content-reader.js.map +1 -0
  61. package/dist/content-utils.d.ts +53 -10
  62. package/dist/content-utils.d.ts.map +1 -1
  63. package/dist/content-utils.js +103 -34
  64. package/dist/content-utils.js.map +1 -0
  65. package/dist/content-watcher.d.ts +3 -3
  66. package/dist/content-watcher.d.ts.map +1 -1
  67. package/dist/content-watcher.js +13 -10
  68. package/dist/content-watcher.js.map +1 -0
  69. package/dist/data-index.d.ts +1 -1
  70. package/dist/data-index.d.ts.map +1 -1
  71. package/dist/data-index.js +2 -1
  72. package/dist/data-index.js.map +1 -0
  73. package/dist/data.d.ts +5 -13
  74. package/dist/data.d.ts.map +1 -1
  75. package/dist/data.js +7 -6
  76. package/dist/data.js.map +1 -0
  77. package/dist/editor-data-bindings.d.ts +10 -0
  78. package/dist/editor-data-bindings.d.ts.map +1 -0
  79. package/dist/editor-data-bindings.js +67 -0
  80. package/dist/editor-data-bindings.js.map +1 -0
  81. package/dist/etag-utils.d.ts.map +1 -1
  82. package/dist/etag-utils.js +5 -4
  83. package/dist/etag-utils.js.map +1 -0
  84. package/dist/filesystem-content-api.d.ts +45 -18
  85. package/dist/filesystem-content-api.d.ts.map +1 -1
  86. package/dist/filesystem-content-api.js +455 -286
  87. package/dist/filesystem-content-api.js.map +1 -0
  88. package/dist/filesystem-reader-backend.d.ts +46 -0
  89. package/dist/filesystem-reader-backend.d.ts.map +1 -0
  90. package/dist/filesystem-reader-backend.js +119 -0
  91. package/dist/filesystem-reader-backend.js.map +1 -0
  92. package/dist/fragment-index.d.ts +8 -0
  93. package/dist/fragment-index.d.ts.map +1 -0
  94. package/dist/fragment-index.js +8 -0
  95. package/dist/fragment-index.js.map +1 -0
  96. package/dist/fragments.d.ts +16 -0
  97. package/dist/fragments.d.ts.map +1 -0
  98. package/dist/fragments.js +104 -0
  99. package/dist/fragments.js.map +1 -0
  100. package/dist/git-operations.d.ts.map +1 -1
  101. package/dist/git-operations.js +5 -1
  102. package/dist/git-operations.js.map +1 -0
  103. package/dist/header-parser.d.ts +4 -4
  104. package/dist/header-parser.d.ts.map +1 -1
  105. package/dist/header-parser.js +8 -61
  106. package/dist/header-parser.js.map +1 -0
  107. package/dist/in-memory-content-api.d.ts +7 -7
  108. package/dist/in-memory-content-api.d.ts.map +1 -1
  109. package/dist/in-memory-content-api.js +74 -56
  110. package/dist/in-memory-content-api.js.map +1 -0
  111. package/dist/index.d.ts +19 -12
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +21 -9
  114. package/dist/index.js.map +1 -0
  115. package/dist/indexing.d.ts +4 -4
  116. package/dist/indexing.d.ts.map +1 -1
  117. package/dist/indexing.js +6 -5
  118. package/dist/indexing.js.map +1 -0
  119. package/dist/json-whitespace-view.js +1 -0
  120. package/dist/json-whitespace-view.js.map +1 -0
  121. package/dist/middleware.d.ts +8 -6
  122. package/dist/middleware.d.ts.map +1 -1
  123. package/dist/middleware.js +178 -142
  124. package/dist/middleware.js.map +1 -0
  125. package/dist/node.d.ts +24 -17
  126. package/dist/node.d.ts.map +1 -1
  127. package/dist/node.js +17 -14
  128. package/dist/node.js.map +1 -0
  129. package/dist/page-references.d.ts +21 -0
  130. package/dist/page-references.d.ts.map +1 -0
  131. package/dist/page-references.js +50 -0
  132. package/dist/page-references.js.map +1 -0
  133. package/dist/read-date.d.ts +3 -0
  134. package/dist/read-date.d.ts.map +1 -0
  135. package/dist/read-date.js +9 -0
  136. package/dist/read-date.js.map +1 -0
  137. package/dist/reader-backend.d.ts +54 -0
  138. package/dist/reader-backend.d.ts.map +1 -0
  139. package/dist/reader-backend.js +2 -0
  140. package/dist/reader-backend.js.map +1 -0
  141. package/dist/reader.d.ts +8 -8
  142. package/dist/reader.d.ts.map +1 -1
  143. package/dist/reader.js +8 -7
  144. package/dist/reader.js.map +1 -0
  145. package/dist/schemas/index.d.ts +6 -6
  146. package/dist/schemas/index.d.ts.map +1 -1
  147. package/dist/schemas/index.js +9 -8
  148. package/dist/schemas/index.js.map +1 -0
  149. package/dist/site-index.d.ts +3 -3
  150. package/dist/site-index.d.ts.map +1 -1
  151. package/dist/site-index.js +9 -5
  152. package/dist/site-index.js.map +1 -0
  153. package/dist/site.d.ts +5 -3
  154. package/dist/site.d.ts.map +1 -1
  155. package/dist/site.js +14 -2
  156. package/dist/site.js.map +1 -0
  157. package/dist/types.d.ts +203 -15
  158. package/dist/types.d.ts.map +1 -1
  159. package/dist/types.js +9 -0
  160. package/dist/types.js.map +1 -0
  161. package/dist/url-safe-base64.js +1 -0
  162. package/dist/url-safe-base64.js.map +1 -0
  163. package/dist/url-validation.d.ts +14 -0
  164. package/dist/url-validation.d.ts.map +1 -1
  165. package/dist/url-validation.js +56 -14
  166. package/dist/url-validation.js.map +1 -0
  167. package/dist/utils/atomic-write.js +1 -0
  168. package/dist/utils/atomic-write.js.map +1 -0
  169. package/dist/utils.d.ts +1 -1
  170. package/dist/utils.js +2 -1
  171. package/dist/utils.js.map +1 -0
  172. package/dist/vite-plugin.d.ts +2 -2
  173. package/dist/vite-plugin.js +5 -4
  174. package/dist/vite-plugin.js.map +1 -0
  175. package/dist/vxjson-serialize.d.ts +5 -6
  176. package/dist/vxjson-serialize.d.ts.map +1 -1
  177. package/dist/vxjson-serialize.js +13 -10
  178. package/dist/vxjson-serialize.js.map +1 -0
  179. package/dist/vxjson.d.ts +4 -3
  180. package/dist/vxjson.d.ts.map +1 -1
  181. package/dist/vxjson.js +10 -20
  182. package/dist/vxjson.js.map +1 -0
  183. package/dist/xxhash.d.ts.map +1 -1
  184. package/dist/xxhash.js +6 -2
  185. package/dist/xxhash.js.map +1 -0
  186. package/package.json +92 -56
  187. package/src/asset-constraints.ts +33 -0
  188. package/src/asset-metadata-store.ts +401 -0
  189. package/src/asset-operations.ts +814 -0
  190. package/src/asset-types.ts +74 -0
  191. package/src/asset-usage.ts +48 -0
  192. package/src/base-content-index.ts +227 -0
  193. package/src/cf-access.ts +85 -0
  194. package/src/component-registry.ts +145 -0
  195. package/src/component-registry.types.ts +12 -0
  196. package/src/component-vocabulary.ts +87 -0
  197. package/src/content-api.interface.ts +94 -0
  198. package/src/content-filters.ts +278 -0
  199. package/src/content-index.ts +642 -0
  200. package/src/content-operations.ts +497 -0
  201. package/src/content-path.ts +121 -0
  202. package/src/content-reader.ts +18 -0
  203. package/src/content-utils.ts +474 -0
  204. package/src/content-watcher.ts +122 -0
  205. package/src/data-index.ts +7 -0
  206. package/src/data.ts +134 -0
  207. package/src/editor-data-bindings.ts +66 -0
  208. package/src/etag-utils.ts +204 -0
  209. package/src/filesystem-content-api.ts +2740 -0
  210. package/src/filesystem-reader-backend.ts +135 -0
  211. package/src/fragment-index.ts +7 -0
  212. package/src/fragments.ts +113 -0
  213. package/src/git-operations.ts +340 -0
  214. package/src/header-parser.ts +272 -0
  215. package/src/in-memory-content-api.ts +760 -0
  216. package/src/index.ts +45 -0
  217. package/src/indexing.ts +8 -0
  218. package/src/json-whitespace-view.ts +155 -0
  219. package/src/middleware.ts +1322 -0
  220. package/src/node.ts +40 -0
  221. package/src/page-references.ts +76 -0
  222. package/src/read-date.ts +7 -0
  223. package/src/reader-backend.ts +54 -0
  224. package/src/reader.ts +15 -0
  225. package/src/schemas/index.ts +113 -0
  226. package/src/site-index.ts +330 -0
  227. package/src/site.ts +198 -0
  228. package/src/types.ts +759 -0
  229. package/src/url-safe-base64.ts +38 -0
  230. package/src/url-validation.ts +121 -0
  231. package/src/utils/atomic-write.ts +20 -0
  232. package/src/utils.ts +84 -0
  233. package/src/vite-plugin.ts +45 -0
  234. package/src/vxjson-serialize.ts +66 -0
  235. package/src/vxjson.ts +442 -0
  236. package/src/xxhash.ts +43 -0
  237. package/dist/asset-manifest.d.ts +0 -23
  238. package/dist/asset-manifest.d.ts.map +0 -1
  239. package/dist/asset-manifest.js +0 -73
  240. package/dist/block-index.d.ts +0 -9
  241. package/dist/block-index.d.ts.map +0 -1
  242. package/dist/block-index.js +0 -11
  243. package/dist/blocks.d.ts +0 -16
  244. package/dist/blocks.d.ts.map +0 -1
  245. package/dist/blocks.js +0 -103
  246. package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
  247. package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
  248. package/dist/content-index-D092_vk1.mjs +0 -629
  249. package/dist/content-index-D092_vk1.mjs.map +0 -1
  250. package/dist/content-index-D6IVMmAB.d.mts +0 -127
  251. package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
  252. package/dist/content-operations-DIbsxG5A.d.mts +0 -354
  253. package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
  254. package/dist/content-utils-fWNHTrPy.mjs +0 -280
  255. package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
  256. package/dist/data-DquNIEnQ.mjs +0 -62
  257. package/dist/data-DquNIEnQ.mjs.map +0 -1
  258. package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
  259. package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
  260. package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
  261. package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
  262. package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
  263. package/dist/index-DxVTFK66.d.mts +0 -105
  264. package/dist/index-DxVTFK66.d.mts.map +0 -1
  265. package/dist/index.d.mts +0 -80
  266. package/dist/index.d.mts.map +0 -1
  267. package/dist/index.mjs +0 -78
  268. package/dist/index.mjs.map +0 -1
  269. package/dist/indexing.d.mts +0 -152
  270. package/dist/indexing.d.mts.map +0 -1
  271. package/dist/indexing.mjs +0 -255
  272. package/dist/indexing.mjs.map +0 -1
  273. package/dist/node.d.mts +0 -373
  274. package/dist/node.d.mts.map +0 -1
  275. package/dist/node.mjs +0 -2572
  276. package/dist/node.mjs.map +0 -1
  277. package/dist/reader.d.mts +0 -15
  278. package/dist/reader.d.mts.map +0 -1
  279. package/dist/reader.mjs +0 -22
  280. package/dist/reader.mjs.map +0 -1
  281. package/dist/schemas/index.d.mts +0 -2
  282. package/dist/schemas/index.mjs +0 -2
  283. package/dist/schemas-CFRD0FIQ.mjs +0 -84
  284. package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
  285. package/dist/site-8oaj1Q60.mjs +0 -366
  286. package/dist/site-8oaj1Q60.mjs.map +0 -1
  287. package/dist/site-index-B_254bCc.d.mts +0 -136
  288. package/dist/site-index-B_254bCc.d.mts.map +0 -1
  289. package/dist/types-DOdldbfD.mjs +0 -34
  290. package/dist/types-DOdldbfD.mjs.map +0 -1
  291. package/dist/types-DjfhS272.d.mts +0 -456
  292. package/dist/types-DjfhS272.d.mts.map +0 -1
  293. package/dist/vxjson-qHrgLGip.mjs +0 -555
  294. package/dist/vxjson-qHrgLGip.mjs.map +0 -1
  295. package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
  296. package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
  297. package/dist/vxjson-serialize.d.mts +0 -17
  298. package/dist/vxjson-serialize.d.mts.map +0 -1
  299. package/dist/vxjson-serialize.mjs +0 -2
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,oBAAoB,CAAC;AACvE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AA+B1D;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAC9C,sDAAsD;IACtD,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,uBAAuB,8EAiqCxF;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,uBAAuB,SAI5E,GAAG,OAAO,GAAG,QAAQ,MAAM,IAAI,mBAkCnD"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,uBAAuB,CAAC;AAE1E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AA6F7D;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gHAAgH;IAChH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,YAAY,GAAG,aAAa,CAAC,CAAC;IAC9D,8GAA8G;IAC9G,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,uBAAuB,8EA6nCxF;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,uBAAuB,SAI5E,GAAG,OAAO,GAAG,QAAQ,MAAM,IAAI,mBAkCnD"}
@@ -1,9 +1,9 @@
1
1
  import { Hono } from 'hono';
2
- import { cors } from 'hono/cors';
3
- import { AssetOperations } from './asset-operations';
4
- import { localesOf, normalizeAndValidatePathname } from './content-utils';
5
- import { createGitOperations } from './git-operations';
6
- import { ErrorCodes } from './types';
2
+ import { AssetOperations } from './asset-operations.js';
3
+ import { findAssetUsage } from './asset-usage.js';
4
+ import { normalizeAndValidatePathname } from './content-utils.js';
5
+ import { createGitOperations } from './git-operations.js';
6
+ import { ErrorCodes } from './types.js';
7
7
  /**
8
8
  * Helper to create standardized error responses
9
9
  */
@@ -26,10 +26,63 @@ function logAndCreateErrorResponse(error, code, message, details) {
26
26
  function errorMessage(error, fallback) {
27
27
  return error instanceof Error ? error.message : fallback;
28
28
  }
29
+ /**
30
+ * How a refused create or update answers, by reason: the error code, the HTTP status and the
31
+ * message when the backend gave none. 409 when another entry has the path or name, or a page
32
+ * redirects from the path; 400 when the caller can fix the input. A caller that can fix it by
33
+ * changing one field is told which one (`details.field`), so none of these falls through to a 500
34
+ * whose only clue is a raw error string.
35
+ */
36
+ const refusals = {
37
+ already_exists: { code: ErrorCodes.ALREADY_EXISTS, status: 409, message: 'Content already exists' },
38
+ name_taken: { code: ErrorCodes.NAME_TAKEN, status: 409, message: 'A fragment with that name already exists' },
39
+ pathname_taken: { code: ErrorCodes.PATHNAME_TAKEN, status: 409, message: 'Pathname already taken' },
40
+ pathname_in_redirects: {
41
+ code: ErrorCodes.PATHNAME_TAKEN,
42
+ status: 409,
43
+ message: 'Pathname is already in use as a redirect',
44
+ },
45
+ invalid_name: { code: ErrorCodes.INVALID_REQUEST, status: 400, message: 'Invalid name' },
46
+ invalid_collection: { code: ErrorCodes.INVALID_REQUEST, status: 400, message: 'Invalid collection' },
47
+ invalid_locale: { code: ErrorCodes.INVALID_LOCALE, status: 400, message: 'Invalid locale' },
48
+ invalid_pathname: { code: ErrorCodes.INVALID_PATHNAME, status: 400, message: 'Invalid pathname' },
49
+ metadata_too_large: { code: ErrorCodes.METADATA_TOO_LARGE, status: 400, message: 'Metadata too large' },
50
+ missing_field: { code: ErrorCodes.MISSING_REQUIRED_FIELD, status: 400, message: 'Missing required field' },
51
+ // Site resolution failing, not a bad field: with no sites configured at all, no value the caller
52
+ // could send would satisfy it.
53
+ no_sites_configured: { code: ErrorCodes.INVALID_REQUEST, status: 400, message: 'Site not found' },
54
+ unknown_site: { code: ErrorCodes.INVALID_REQUEST, status: 400, message: 'Site not found' },
55
+ };
56
+ function refusalOf(reason) {
57
+ return reason !== undefined && Object.hasOwn(refusals, reason) ? refusals[reason] : undefined;
58
+ }
59
+ /**
60
+ * Whether a request is a write another website made the browser send. The routes write into the
61
+ * project, and any page open in the browser can make it send a form post without asking the
62
+ * server first. The browser says where a request comes from: `Sec-Fetch-Site`, or in older
63
+ * browsers the `Origin` of the page, which for the editor is the address it sent the request to
64
+ * (the `Host`). A request with neither did not come from a web page.
65
+ */
66
+ function isCrossSiteWrite(c) {
67
+ if (c.req.method === 'GET' || c.req.method === 'HEAD' || c.req.method === 'OPTIONS')
68
+ return false;
69
+ const fetchSite = c.req.header('Sec-Fetch-Site');
70
+ if (fetchSite !== undefined)
71
+ return fetchSite !== 'same-origin';
72
+ const origin = c.req.header('Origin');
73
+ if (origin === undefined)
74
+ return false;
75
+ return !URL.canParse(origin) || new URL(origin).host !== c.req.header('Host');
76
+ }
29
77
  export function createContentAPIRouter(api, options) {
30
78
  const app = new Hono();
31
- // Enable CORS for CMS access
32
- app.use('/*', cors());
79
+ // The editor calls these routes from the page they are served with, so no other origin is
80
+ // granted access (no CORS headers), and a write another website sends is refused.
81
+ app.use('/*', async (c, next) => {
82
+ if (!isCrossSiteWrite(c))
83
+ return next();
84
+ return c.json(errorResponse(ErrorCodes.FORBIDDEN, 'Refused a write another website sent through the browser'), 403);
85
+ });
33
86
  // GET /auth/user - Get current authenticated user
34
87
  app.get('/auth/user', async (c) => {
35
88
  const email = c.req.header('X-CF-User-Email');
@@ -46,9 +99,9 @@ export function createContentAPIRouter(api, options) {
46
99
  app.get('/content/collections', async (c) => {
47
100
  try {
48
101
  const allCollections = new Set();
49
- // Get collections from blocks
50
- const blockCollections = api.blocks.collections;
51
- for (const collection of blockCollections) {
102
+ // Get collections from fragments
103
+ const fragmentCollections = api.fragments.collections;
104
+ for (const collection of fragmentCollections) {
52
105
  allCollections.add(collection);
53
106
  }
54
107
  // Get collections from all sites
@@ -71,7 +124,7 @@ export function createContentAPIRouter(api, options) {
71
124
  });
72
125
  // GET /content - List all content with optional filters
73
126
  app.get('/content', async (c) => {
74
- const kind = c.req.query('kind'); // 'block' or 'page'
127
+ const kind = c.req.query('kind'); // 'fragment' or 'page'
75
128
  const site = c.req.query('site');
76
129
  const collection = c.req.query('collection');
77
130
  const locales = c.req.query('locales'); // comma-separated
@@ -148,26 +201,10 @@ export function createContentAPIRouter(api, options) {
148
201
  // back to a generic "API error: 500" string and inline error copy
149
202
  // like "already exists" never reaches the user.
150
203
  const errMessage = (result.error instanceof Error ? result.error.message : undefined) ?? '';
151
- if (result.reason === 'already_exists') {
152
- return c.json({ ...result, ...errorResponse(ErrorCodes.ALREADY_EXISTS, errMessage || 'Content already exists') }, 409);
153
- }
154
- if (result.reason === 'name_taken') {
155
- return c.json({
156
- ...result,
157
- ...errorResponse(ErrorCodes.NAME_TAKEN, errMessage || 'A block with that name already exists'),
158
- }, 409);
159
- }
160
- if (result.reason === 'pathname_taken') {
161
- return c.json({ ...result, ...errorResponse(ErrorCodes.PATHNAME_TAKEN, errMessage || 'Pathname already taken') }, 409);
162
- }
163
- if (result.reason === 'invalid_name') {
164
- return c.json({ ...result, ...errorResponse(ErrorCodes.INVALID_REQUEST, errMessage || 'Invalid name') }, 400);
165
- }
166
- if (result.reason === 'invalid_pathname') {
167
- return c.json({ ...result, ...errorResponse(ErrorCodes.INVALID_PATHNAME, errMessage || 'Invalid pathname') }, 400);
168
- }
169
- if (result.reason === 'metadata_too_large') {
170
- return c.json({ ...result, ...errorResponse(ErrorCodes.METADATA_TOO_LARGE, errMessage || 'Metadata too large') }, 400);
204
+ const fieldDetails = result.field ? { field: result.field } : undefined;
205
+ const refusal = refusalOf(result.reason);
206
+ if (refusal) {
207
+ return c.json({ ...result, ...errorResponse(refusal.code, errMessage || refusal.message, fieldDetails) }, refusal.status);
171
208
  }
172
209
  return c.json({ ...result, ...errorResponse(ErrorCodes.WRITE_ERROR, errMessage || 'Failed to create content') }, 500);
173
210
  }
@@ -182,9 +219,9 @@ export function createContentAPIRouter(api, options) {
182
219
  }, 500);
183
220
  }
184
221
  });
185
- // PUT /content/:id - Update content (full update)
186
- app.put('/content/:id', async (c) => {
187
- const id = c.req.param('id');
222
+ // PUT /content/:id - Update content (full update); PATCH /content/:id - Partial update
223
+ const updateContent = async (c) => {
224
+ const id = c.req.param('id') ?? '';
188
225
  const locale = c.req.query('locale');
189
226
  const etag = c.req.query('etag') || '';
190
227
  if (!locale) {
@@ -202,56 +239,32 @@ export function createContentAPIRouter(api, options) {
202
239
  if (result.reason === 'not_found') {
203
240
  return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Content not found', { id }), 404);
204
241
  }
205
- return c.json(result, 500);
206
- }
207
- // Set ETag header
208
- c.header('ETag', result.etag);
209
- return c.json(result);
210
- }
211
- catch (error) {
212
- console.error('[Content API] Failed to update content:', error);
213
- return c.json({
214
- success: false,
215
- reason: 'write_error',
216
- error: error instanceof Error ? error.message : 'Unknown error',
217
- }, 500);
218
- }
219
- });
220
- // PATCH /content/:id - Partial update
221
- app.patch('/content/:id', async (c) => {
222
- const id = c.req.param('id');
223
- const locale = c.req.query('locale');
224
- const etag = c.req.query('etag') || '';
225
- if (!locale) {
226
- return c.json(errorResponse(ErrorCodes.MISSING_REQUIRED_FIELD, 'locale query parameter is required', { field: 'locale' }), 400);
227
- }
228
- try {
229
- const data = await c.req.json();
230
- const result = await api.updateLocalized({ id, locale, data, etag });
231
- if (!result.success) {
232
- if (result.reason === 'stale_write') {
233
- return c.json(errorResponse(ErrorCodes.STALE_WRITE, 'Content has been modified since you last loaded it', {
234
- currentEtag: result.currentEtag,
235
- }), 412);
236
- }
237
- if (result.reason === 'not_found') {
238
- return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Content not found', { id }), 404);
242
+ const refusal = refusalOf(result.reason);
243
+ if (refusal) {
244
+ return c.json({ ...result, ...errorResponse(refusal.code, errorMessage(result.error, refusal.message)) }, refusal.status);
239
245
  }
240
- return c.json(result, 500);
246
+ // The backend's own refusals, such as an emptied title or metadata over 4 KB, carry their
247
+ // message in an Error, which serializes to `{}`: send it as a refused create does.
248
+ return c.json({
249
+ ...result,
250
+ ...errorResponse(ErrorCodes.WRITE_ERROR, errorMessage(result.error, 'Failed to update content')),
251
+ }, 500);
241
252
  }
242
253
  // Set ETag header
243
254
  c.header('ETag', result.etag);
244
255
  return c.json(result);
245
256
  }
246
257
  catch (error) {
247
- console.error('[Content API] Failed to patch content:', error);
258
+ console.error('[Content API] Failed to update content:', error);
248
259
  return c.json({
249
260
  success: false,
250
261
  reason: 'write_error',
251
- error: error instanceof Error ? error.message : 'Unknown error',
262
+ ...errorResponse(ErrorCodes.WRITE_ERROR, errorMessage(error, 'Failed to update content')),
252
263
  }, 500);
253
264
  }
254
- });
265
+ };
266
+ app.put('/content/:id', updateContent);
267
+ app.patch('/content/:id', updateContent);
255
268
  // DELETE /content/:id - Delete content (entire entry or specific locale)
256
269
  app.delete('/content/:id', async (c) => {
257
270
  const id = c.req.param('id');
@@ -275,11 +288,15 @@ export function createContentAPIRouter(api, options) {
275
288
  }, 404);
276
289
  }
277
290
  if (result.reason === 'stale_write') {
291
+ // The error envelope is what a client reads to tell an outdated etag from any other failure.
278
292
  return c.json({
279
293
  success: false,
280
294
  reason: result.reason,
281
295
  currentEtag: result.currentEtag,
282
296
  message: 'Content has been modified',
297
+ ...errorResponse(ErrorCodes.STALE_WRITE, 'Content has been modified since you last loaded it', {
298
+ currentEtag: result.currentEtag,
299
+ }),
283
300
  }, 412);
284
301
  }
285
302
  return c.json(result, 500);
@@ -326,10 +343,9 @@ export function createContentAPIRouter(api, options) {
326
343
  if (available) {
327
344
  return c.json({ available: true });
328
345
  }
329
- const existing = api.data.getByName(collection, name);
330
346
  return c.json({
331
347
  available: false,
332
- existingId: existing?.id,
348
+ existingId: api.data.getNameConflict(collection, name, excludeId),
333
349
  });
334
350
  }
335
351
  catch (error) {
@@ -384,6 +400,11 @@ export function createContentAPIRouter(api, options) {
384
400
  // ===== Asset Routes =====
385
401
  // Must be registered BEFORE /:site routes to avoid route conflicts (/:site would match "assets")
386
402
  if (options?.assetsPath) {
403
+ // Asset metadata records live under the content root. There is no falling
404
+ // back to the media folder: a site serves it, so a record there is published.
405
+ if (!options.contentRoot) {
406
+ throw new Error('The assetsPath option needs contentRoot as well: asset metadata is kept under the content root');
407
+ }
387
408
  const assetOps = new AssetOperations({
388
409
  assetsPath: options.assetsPath,
389
410
  contentRoot: options.contentRoot,
@@ -399,6 +420,11 @@ export function createContentAPIRouter(api, options) {
399
420
  }
400
421
  return { folder: '/', filename: relativePath };
401
422
  }
423
+ // The router decodes `%2F`, so a path in the address can climb out of the assets folder. The
424
+ // containment check refuses it, and that is the caller's mistake, not a server fault.
425
+ function isPathTraversal(error) {
426
+ return error instanceof Error && error.message === 'Path traversal detected';
427
+ }
402
428
  // POST /assets/move — move assets between folders
403
429
  app.post('/assets/move', async (c) => {
404
430
  try {
@@ -419,7 +445,7 @@ export function createContentAPIRouter(api, options) {
419
445
  return c.json(result);
420
446
  }
421
447
  catch (error) {
422
- if (error instanceof Error && error.message === 'Path traversal detected') {
448
+ if (isPathTraversal(error)) {
423
449
  return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid folder path'), 400);
424
450
  }
425
451
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to move assets'), 500);
@@ -434,11 +460,11 @@ export function createContentAPIRouter(api, options) {
434
460
  return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'No file provided in "file" field'), 400);
435
461
  }
436
462
  const alt = typeof body.alt === 'string' ? body.alt : undefined;
437
- const uploadedBy = typeof body.uploadedBy === 'string' ? body.uploadedBy : undefined;
438
- const width = typeof body.width === 'string' ? Number.parseInt(body.width, 10) : undefined;
439
- const height = typeof body.height === 'string' ? Number.parseInt(body.height, 10) : undefined;
440
463
  const folder = typeof body.folder === 'string' ? body.folder : undefined;
441
- const result = await assetOps.upload(file, { alt, uploadedBy, width, height, folder });
464
+ // The uploader is deliberately not read from the request: anyone who can
465
+ // reach this route could otherwise put any name on any picture, and an
466
+ // audit trail that anyone can write is worse than no audit trail.
467
+ const result = await assetOps.upload(file, { alt, folder });
442
468
  if (!result.success) {
443
469
  return c.json(errorResponse(ErrorCodes.ASSET_UPLOAD_FAILED, result.error), 400);
444
470
  }
@@ -451,11 +477,12 @@ export function createContentAPIRouter(api, options) {
451
477
  // POST /assets/import-url — import image from URL
452
478
  app.post('/assets/import-url', async (c) => {
453
479
  try {
454
- const { url, alt, uploadedBy, folder } = await c.req.json();
480
+ const { url, alt, folder } = await c.req.json();
455
481
  if (!url) {
456
482
  return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'url is required'), 400);
457
483
  }
458
- const result = await assetOps.importFromUrl(url, { alt, uploadedBy, folder });
484
+ // As with /assets/upload: the uploader never comes off the wire
485
+ const result = await assetOps.importFromUrl(url, { alt, folder });
459
486
  if (!result.success) {
460
487
  return c.json(errorResponse(ErrorCodes.ASSET_UPLOAD_FAILED, result.error), 400);
461
488
  }
@@ -483,7 +510,7 @@ export function createContentAPIRouter(api, options) {
483
510
  return c.json(result);
484
511
  }
485
512
  catch (error) {
486
- if (error instanceof Error && error.message === 'Path traversal detected') {
513
+ if (isPathTraversal(error)) {
487
514
  return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid folder path'), 400);
488
515
  }
489
516
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list folder'), 500);
@@ -516,11 +543,10 @@ export function createContentAPIRouter(api, options) {
516
543
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to get folder tree'), 500);
517
544
  }
518
545
  });
519
- // GET /assets/usage/:path{.+} — get asset usage information (supports subfolders)
546
+ // GET /assets/usage/:path{.+} — content that links to the asset at this path from the assets root
520
547
  app.get('/assets/usage/:path{.+}', async (c) => {
521
548
  try {
522
- const { filename } = splitAssetPath(c.req.param('path'));
523
- const usage = await assetOps.getUsage(filename);
549
+ const usage = await findAssetUsage(api, c.req.param('path'), options.assetRefBase);
524
550
  return c.json({ usage });
525
551
  }
526
552
  catch (error) {
@@ -531,6 +557,7 @@ export function createContentAPIRouter(api, options) {
531
557
  app.get('/assets/serve/:path{.+}', async (c) => {
532
558
  try {
533
559
  const { folder, filename } = splitAssetPath(c.req.param('path'));
560
+ assetOps.getAssetPath(filename, folder);
534
561
  const result = await assetOps.readAssetFile(filename, folder);
535
562
  if (!result.success) {
536
563
  return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404);
@@ -543,6 +570,9 @@ export function createContentAPIRouter(api, options) {
543
570
  });
544
571
  }
545
572
  catch (error) {
573
+ if (isPathTraversal(error)) {
574
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
575
+ }
546
576
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to serve asset'), 500);
547
577
  }
548
578
  });
@@ -550,6 +580,7 @@ export function createContentAPIRouter(api, options) {
550
580
  app.get('/assets/meta/:path{.+}', async (c) => {
551
581
  try {
552
582
  const { folder, filename } = splitAssetPath(c.req.param('path'));
583
+ assetOps.getAssetPath(filename, folder);
553
584
  const asset = await assetOps.getAsset(filename, folder);
554
585
  if (!asset) {
555
586
  return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, 'Asset not found'), 404);
@@ -557,6 +588,9 @@ export function createContentAPIRouter(api, options) {
557
588
  return c.json(asset);
558
589
  }
559
590
  catch (error) {
591
+ if (isPathTraversal(error)) {
592
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
593
+ }
560
594
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to get asset'), 500);
561
595
  }
562
596
  });
@@ -564,14 +598,22 @@ export function createContentAPIRouter(api, options) {
564
598
  app.patch('/assets/meta/:path{.+}', async (c) => {
565
599
  try {
566
600
  const { folder, filename } = splitAssetPath(c.req.param('path'));
601
+ assetOps.getAssetPath(filename, folder);
567
602
  const { alt, tags } = await c.req.json();
568
603
  const result = await assetOps.updateMetadata(filename, { alt, tags }, folder);
569
604
  if (!result.success) {
570
- return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404);
605
+ // A disk problem is not a missing picture: saying 404 sent the editor
606
+ // back to refetch a value it had just failed to store.
607
+ return result.reason === 'not-found'
608
+ ? c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404)
609
+ : c.json(errorResponse(ErrorCodes.INTERNAL_ERROR, result.error), 500);
571
610
  }
572
611
  return c.json(result.asset);
573
612
  }
574
613
  catch (error) {
614
+ if (isPathTraversal(error)) {
615
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
616
+ }
575
617
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to update asset'), 500);
576
618
  }
577
619
  });
@@ -579,6 +621,7 @@ export function createContentAPIRouter(api, options) {
579
621
  app.delete('/assets/meta/:path{.+}', async (c) => {
580
622
  try {
581
623
  const { folder, filename } = splitAssetPath(c.req.param('path'));
624
+ assetOps.getAssetPath(filename, folder);
582
625
  const result = await assetOps.delete(filename, folder);
583
626
  if (!result.success) {
584
627
  return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404);
@@ -586,6 +629,9 @@ export function createContentAPIRouter(api, options) {
586
629
  return c.json({ success: true });
587
630
  }
588
631
  catch (error) {
632
+ if (isPathTraversal(error)) {
633
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
634
+ }
589
635
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to delete asset'), 500);
590
636
  }
591
637
  });
@@ -596,9 +642,9 @@ export function createContentAPIRouter(api, options) {
596
642
  app.get('/:site/collections', async (c) => {
597
643
  const site = c.req.param('site');
598
644
  try {
599
- if (site === 'blocks') {
600
- // Use the efficient collections getter for blocks
601
- const collections = Array.from(api.blocks.collections).sort();
645
+ if (site === 'fragments') {
646
+ // Use the efficient collections getter for fragments
647
+ const collections = Array.from(api.fragments.collections).sort();
602
648
  return c.json({ collections });
603
649
  }
604
650
  // Use the efficient collections getter for sites
@@ -609,17 +655,17 @@ export function createContentAPIRouter(api, options) {
609
655
  const collections = Array.from(siteObj.collections).sort();
610
656
  return c.json({ collections });
611
657
  }
612
- catch (error) {
658
+ catch {
613
659
  return c.json(errorResponse(ErrorCodes.SITE_NOT_FOUND, `Site '${site}' not found`, { site }), 404);
614
660
  }
615
661
  });
616
- // GET /:site/pages - Get pages for a site (not valid for blocks)
662
+ // GET /:site/pages - Get pages for a site (not valid for fragments)
617
663
  app.get('/:site/pages', async (c) => {
618
664
  const site = c.req.param('site');
619
665
  const locale = c.req.query('locale');
620
666
  try {
621
- if (site === 'blocks') {
622
- return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Blocks do not have pages. Use /content?kind=block to list blocks.'), 400);
667
+ if (site === 'fragments') {
668
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Fragments do not have pages. Use /content?kind=fragment to list fragments.'), 400);
623
669
  }
624
670
  const siteObj = api.getSite(site);
625
671
  if (!siteObj) {
@@ -646,8 +692,8 @@ export function createContentAPIRouter(api, options) {
646
692
  return c.json(errorResponse(ErrorCodes.MISSING_REQUIRED_FIELD, 'pathname query parameter is required', { field: 'pathname' }), 400);
647
693
  }
648
694
  try {
649
- if (site === 'blocks') {
650
- return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Blocks do not have pathnames. This endpoint is only for page content.'), 400);
695
+ if (site === 'fragments') {
696
+ return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Fragments do not have pathnames. This endpoint is only for page content.'), 400);
651
697
  }
652
698
  // Defensive: if the pathname shape is malformed, short-circuit with
653
699
  // a structured reason instead of a meaningless index lookup. The
@@ -683,12 +729,12 @@ export function createContentAPIRouter(api, options) {
683
729
  return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check pathname availability'), 500);
684
730
  }
685
731
  });
686
- // GET /blocks - Shorthand for all blocks
687
- app.get('/blocks', async (c) => {
732
+ // GET /fragments - Shorthand for all fragments
733
+ app.get('/fragments', async (c) => {
688
734
  const collection = c.req.query('collection');
689
735
  const locale = c.req.query('locale');
690
736
  try {
691
- const filters = { kind: 'block' };
737
+ const filters = { kind: 'fragment' };
692
738
  if (collection)
693
739
  filters.collection = collection;
694
740
  if (locale)
@@ -697,74 +743,51 @@ export function createContentAPIRouter(api, options) {
697
743
  return c.json({ items, total: items.length });
698
744
  }
699
745
  catch (error) {
700
- return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list blocks'), 500);
746
+ return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list fragments'), 500);
701
747
  }
702
748
  });
703
- // GET /blocks/name-available - Check if block name is available
704
- app.get('/blocks/name-available', async (c) => {
749
+ // GET /fragments/name-available - Check if fragment name is available, the way a create checks it: in
750
+ // any letter case and in any language. `locale` is still accepted but narrows nothing, since a fragment
751
+ // has one name for all its languages.
752
+ app.get('/fragments/name-available', async (c) => {
705
753
  const name = c.req.query('name');
706
754
  const collection = c.req.query('collection') || 'components';
707
- const locale = c.req.query('locale') || 'en';
708
755
  const excludeId = c.req.query('excludeId');
709
756
  if (!name) {
710
757
  return c.json(errorResponse(ErrorCodes.MISSING_REQUIRED_FIELD, 'name query parameter is required', { field: 'name' }), 400);
711
758
  }
712
759
  try {
713
- // Check block name availability through listing
714
- const existing = Array.from(api.listAllContent({
715
- kind: 'block',
716
- collection: collection,
717
- locales: [locale],
718
- }));
719
- let taken;
720
- for (const item of existing) {
721
- for (const localeVersion of localesOf(item)) {
722
- if (localeVersion.name === name) {
723
- taken = item;
724
- break;
725
- }
726
- }
727
- }
728
- // If no existing block, name is available
729
- if (!taken) {
730
- return c.json({ available: true });
731
- }
732
- // If existing block has same ID as excludeId, name is available
733
- // (useful when editing existing block)
734
- if (excludeId && taken.id === excludeId) {
760
+ const existingId = api.fragments.getNameConflict(collection, name, excludeId);
761
+ if (existingId === null) {
735
762
  return c.json({ available: true });
736
763
  }
737
- // Otherwise, name is taken
738
- return c.json({
739
- available: false,
740
- existingId: taken.id,
741
- });
764
+ return c.json({ available: false, existingId });
742
765
  }
743
766
  catch (error) {
744
- return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check block name availability'), 500);
767
+ return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check fragment name availability'), 500);
745
768
  }
746
769
  });
747
- // GET /blocks/:name - Get block by name
748
- app.get('/blocks/:name', async (c) => {
770
+ // GET /fragments/:name - Get fragment by name
771
+ app.get('/fragments/:name', async (c) => {
749
772
  const name = c.req.param('name');
750
773
  const collection = c.req.query('collection') || 'components';
751
774
  const locale = c.req.query('locale');
752
775
  try {
753
- const block = api.blocks.getByName(collection, name, locale);
754
- if (!block) {
755
- return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Block not found', { name, collection }), 404);
776
+ const fragment = api.fragments.getByName(collection, name, locale);
777
+ if (!fragment) {
778
+ return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Fragment not found', { name, collection }), 404);
756
779
  }
757
780
  // If locale specified and provided, filter the response
758
- if (locale && block.locales[locale]) {
781
+ if (locale && fragment.locales[locale]) {
759
782
  return c.json({
760
- ...block,
761
- locales: { [locale]: block.locales[locale] },
783
+ ...fragment,
784
+ locales: { [locale]: fragment.locales[locale] },
762
785
  });
763
786
  }
764
- return c.json(block);
787
+ return c.json(fragment);
765
788
  }
766
789
  catch (error) {
767
- return c.json(logAndCreateErrorResponse(error, ErrorCodes.FETCH_ERROR, 'Failed to fetch block'), 500);
790
+ return c.json(logAndCreateErrorResponse(error, ErrorCodes.FETCH_ERROR, 'Failed to fetch fragment'), 500);
768
791
  }
769
792
  });
770
793
  // POST /:site/move - Move page to new pathname
@@ -806,16 +829,28 @@ export function createContentAPIRouter(api, options) {
806
829
  });
807
830
  if (!updateResult.success) {
808
831
  if (updateResult.reason === 'stale_write') {
832
+ // The error envelope is what a client reads to tell an outdated etag from any other failure.
809
833
  return c.json({
810
834
  moved: false,
811
835
  reason: 'stale_write',
812
836
  currentEtag: updateResult.currentEtag,
813
837
  message: 'Content has been modified',
838
+ ...errorResponse(ErrorCodes.STALE_WRITE, 'Content has been modified since you last loaded it', {
839
+ currentEtag: updateResult.currentEtag,
840
+ }),
814
841
  }, 412);
815
842
  }
816
843
  if (updateResult.reason === 'not_found') {
817
844
  return c.json({ moved: false, ...errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Content not found', { id }) }, 404);
818
845
  }
846
+ // The update checks the new path as a create does: a malformed one is invalid_pathname, and
847
+ // another page's path written loosely ('/team/') passes the check above but not the update.
848
+ // A move calls a taken path already_exists, as the check above does.
849
+ const refusal = refusalOf(updateResult.reason);
850
+ if (refusal) {
851
+ const reason = updateResult.reason === 'invalid_pathname' ? 'invalid_pathname' : 'already_exists';
852
+ return c.json({ moved: false, reason, ...errorResponse(refusal.code, errorMessage(updateResult.error, 'Move refused')) }, refusal.status);
853
+ }
819
854
  return c.json({ moved: false, ...errorResponse(ErrorCodes.WRITE_ERROR, 'Update failed') }, 500);
820
855
  }
821
856
  return c.json({
@@ -914,12 +949,12 @@ export function createContentAPIRouter(api, options) {
914
949
  // POST /git/commit - Commit all changes
915
950
  app.post('/git/commit', async (c) => {
916
951
  try {
917
- const body = await c.req.json().catch(() => ({}));
952
+ const body = await c.req.json().catch(() => undefined);
918
953
  // Auto-generate message if not provided
919
954
  const date = new Date();
920
955
  const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
921
956
  const defaultMessage = `CMS changes - ${monthNames[date.getMonth()]} ${date.getDate()}`;
922
- const message = body.message || defaultMessage;
957
+ const message = body?.message || defaultMessage;
923
958
  // Extract user email from CF Access header for commit attribution
924
959
  const userEmail = c.req.header('X-CF-User-Email');
925
960
  const author = userEmail ? { name: userEmail.split('@')[0], email: userEmail } : undefined;
@@ -1029,3 +1064,4 @@ function readBody(req) {
1029
1064
  req.on('error', reject);
1030
1065
  });
1031
1066
  }
1067
+ //# sourceMappingURL=middleware.js.map