@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.
- package/dist/asset-constraints.d.ts +16 -0
- package/dist/asset-constraints.d.ts.map +1 -0
- package/dist/asset-constraints.js +29 -0
- package/dist/asset-constraints.js.map +1 -0
- package/dist/asset-metadata-store.d.ts +60 -0
- package/dist/asset-metadata-store.d.ts.map +1 -0
- package/dist/asset-metadata-store.js +335 -0
- package/dist/asset-metadata-store.js.map +1 -0
- package/dist/asset-operations.d.ts +21 -28
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +268 -305
- package/dist/asset-operations.js.map +1 -0
- package/dist/asset-types.d.ts +23 -7
- package/dist/asset-types.d.ts.map +1 -1
- package/dist/asset-types.js +8 -2
- package/dist/asset-types.js.map +1 -0
- package/dist/asset-usage.d.ts +11 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +50 -0
- package/dist/asset-usage.js.map +1 -0
- package/dist/base-content-index.d.ts +8 -2
- package/dist/base-content-index.d.ts.map +1 -1
- package/dist/base-content-index.js +24 -3
- package/dist/base-content-index.js.map +1 -0
- package/dist/cf-access.js +1 -0
- package/dist/cf-access.js.map +1 -0
- package/dist/component-registry.d.ts +1 -1
- package/dist/component-registry.d.ts.map +1 -1
- package/dist/component-registry.js +2 -1
- package/dist/component-registry.js.map +1 -0
- package/dist/component-registry.types.js +1 -0
- package/dist/component-registry.types.js.map +1 -0
- package/dist/component-vocabulary.d.ts +33 -0
- package/dist/component-vocabulary.d.ts.map +1 -0
- package/dist/component-vocabulary.js +105 -0
- package/dist/component-vocabulary.js.map +1 -0
- package/dist/content-api.interface.d.ts +5 -5
- package/dist/content-api.interface.d.ts.map +1 -1
- package/dist/content-api.interface.js +1 -0
- package/dist/content-api.interface.js.map +1 -0
- package/dist/content-filters.d.ts +2 -2
- package/dist/content-filters.d.ts.map +1 -1
- package/dist/content-filters.js +9 -8
- package/dist/content-filters.js.map +1 -0
- package/dist/content-index.d.ts +74 -16
- package/dist/content-index.d.ts.map +1 -1
- package/dist/content-index.js +183 -55
- package/dist/content-index.js.map +1 -0
- package/dist/content-operations.d.ts +71 -7
- package/dist/content-operations.d.ts.map +1 -1
- package/dist/content-operations.js +166 -82
- package/dist/content-operations.js.map +1 -0
- package/dist/content-path.d.ts +64 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +95 -0
- package/dist/content-path.js.map +1 -0
- package/dist/content-reader.d.ts +2 -2
- package/dist/content-reader.d.ts.map +1 -1
- package/dist/content-reader.js +2 -1
- package/dist/content-reader.js.map +1 -0
- package/dist/content-utils.d.ts +53 -10
- package/dist/content-utils.d.ts.map +1 -1
- package/dist/content-utils.js +103 -34
- package/dist/content-utils.js.map +1 -0
- package/dist/content-watcher.d.ts +3 -3
- package/dist/content-watcher.d.ts.map +1 -1
- package/dist/content-watcher.js +13 -10
- package/dist/content-watcher.js.map +1 -0
- package/dist/data-index.d.ts +1 -1
- package/dist/data-index.d.ts.map +1 -1
- package/dist/data-index.js +2 -1
- package/dist/data-index.js.map +1 -0
- package/dist/data.d.ts +5 -13
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +7 -6
- package/dist/data.js.map +1 -0
- package/dist/editor-data-bindings.d.ts +10 -0
- package/dist/editor-data-bindings.d.ts.map +1 -0
- package/dist/editor-data-bindings.js +67 -0
- package/dist/editor-data-bindings.js.map +1 -0
- package/dist/etag-utils.d.ts.map +1 -1
- package/dist/etag-utils.js +5 -4
- package/dist/etag-utils.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +45 -18
- package/dist/filesystem-content-api.d.ts.map +1 -1
- package/dist/filesystem-content-api.js +455 -286
- package/dist/filesystem-content-api.js.map +1 -0
- package/dist/filesystem-reader-backend.d.ts +46 -0
- package/dist/filesystem-reader-backend.d.ts.map +1 -0
- package/dist/filesystem-reader-backend.js +119 -0
- package/dist/filesystem-reader-backend.js.map +1 -0
- package/dist/fragment-index.d.ts +8 -0
- package/dist/fragment-index.d.ts.map +1 -0
- package/dist/fragment-index.js +8 -0
- package/dist/fragment-index.js.map +1 -0
- package/dist/fragments.d.ts +16 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +104 -0
- package/dist/fragments.js.map +1 -0
- package/dist/git-operations.d.ts.map +1 -1
- package/dist/git-operations.js +5 -1
- package/dist/git-operations.js.map +1 -0
- package/dist/header-parser.d.ts +4 -4
- package/dist/header-parser.d.ts.map +1 -1
- package/dist/header-parser.js +8 -61
- package/dist/header-parser.js.map +1 -0
- package/dist/in-memory-content-api.d.ts +7 -7
- package/dist/in-memory-content-api.d.ts.map +1 -1
- package/dist/in-memory-content-api.js +74 -56
- package/dist/in-memory-content-api.js.map +1 -0
- package/dist/index.d.ts +19 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -9
- package/dist/index.js.map +1 -0
- package/dist/indexing.d.ts +4 -4
- package/dist/indexing.d.ts.map +1 -1
- package/dist/indexing.js +6 -5
- package/dist/indexing.js.map +1 -0
- package/dist/json-whitespace-view.js +1 -0
- package/dist/json-whitespace-view.js.map +1 -0
- package/dist/middleware.d.ts +8 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +178 -142
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +24 -17
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -14
- package/dist/node.js.map +1 -0
- package/dist/page-references.d.ts +21 -0
- package/dist/page-references.d.ts.map +1 -0
- package/dist/page-references.js +50 -0
- package/dist/page-references.js.map +1 -0
- package/dist/read-date.d.ts +3 -0
- package/dist/read-date.d.ts.map +1 -0
- package/dist/read-date.js +9 -0
- package/dist/read-date.js.map +1 -0
- package/dist/reader-backend.d.ts +54 -0
- package/dist/reader-backend.d.ts.map +1 -0
- package/dist/reader-backend.js +2 -0
- package/dist/reader-backend.js.map +1 -0
- package/dist/reader.d.ts +8 -8
- package/dist/reader.d.ts.map +1 -1
- package/dist/reader.js +8 -7
- package/dist/reader.js.map +1 -0
- package/dist/schemas/index.d.ts +6 -6
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +9 -8
- package/dist/schemas/index.js.map +1 -0
- package/dist/site-index.d.ts +3 -3
- package/dist/site-index.d.ts.map +1 -1
- package/dist/site-index.js +9 -5
- package/dist/site-index.js.map +1 -0
- package/dist/site.d.ts +5 -3
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +14 -2
- package/dist/site.js.map +1 -0
- package/dist/types.d.ts +203 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/url-safe-base64.js +1 -0
- package/dist/url-safe-base64.js.map +1 -0
- package/dist/url-validation.d.ts +14 -0
- package/dist/url-validation.d.ts.map +1 -1
- package/dist/url-validation.js +56 -14
- package/dist/url-validation.js.map +1 -0
- package/dist/utils/atomic-write.js +1 -0
- package/dist/utils/atomic-write.js.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -0
- package/dist/vite-plugin.d.ts +2 -2
- package/dist/vite-plugin.js +5 -4
- package/dist/vite-plugin.js.map +1 -0
- package/dist/vxjson-serialize.d.ts +5 -6
- package/dist/vxjson-serialize.d.ts.map +1 -1
- package/dist/vxjson-serialize.js +13 -10
- package/dist/vxjson-serialize.js.map +1 -0
- package/dist/vxjson.d.ts +4 -3
- package/dist/vxjson.d.ts.map +1 -1
- package/dist/vxjson.js +10 -20
- package/dist/vxjson.js.map +1 -0
- package/dist/xxhash.d.ts.map +1 -1
- package/dist/xxhash.js +6 -2
- package/dist/xxhash.js.map +1 -0
- package/package.json +92 -56
- package/src/asset-constraints.ts +33 -0
- package/src/asset-metadata-store.ts +401 -0
- package/src/asset-operations.ts +814 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +48 -0
- package/src/base-content-index.ts +227 -0
- package/src/cf-access.ts +85 -0
- package/src/component-registry.ts +145 -0
- package/src/component-registry.types.ts +12 -0
- package/src/component-vocabulary.ts +87 -0
- package/src/content-api.interface.ts +94 -0
- package/src/content-filters.ts +278 -0
- package/src/content-index.ts +642 -0
- package/src/content-operations.ts +497 -0
- package/src/content-path.ts +121 -0
- package/src/content-reader.ts +18 -0
- package/src/content-utils.ts +474 -0
- package/src/content-watcher.ts +122 -0
- package/src/data-index.ts +7 -0
- package/src/data.ts +134 -0
- package/src/editor-data-bindings.ts +66 -0
- package/src/etag-utils.ts +204 -0
- package/src/filesystem-content-api.ts +2740 -0
- package/src/filesystem-reader-backend.ts +135 -0
- package/src/fragment-index.ts +7 -0
- package/src/fragments.ts +113 -0
- package/src/git-operations.ts +340 -0
- package/src/header-parser.ts +272 -0
- package/src/in-memory-content-api.ts +760 -0
- package/src/index.ts +45 -0
- package/src/indexing.ts +8 -0
- package/src/json-whitespace-view.ts +155 -0
- package/src/middleware.ts +1322 -0
- package/src/node.ts +40 -0
- package/src/page-references.ts +76 -0
- package/src/read-date.ts +7 -0
- package/src/reader-backend.ts +54 -0
- package/src/reader.ts +15 -0
- package/src/schemas/index.ts +113 -0
- package/src/site-index.ts +330 -0
- package/src/site.ts +198 -0
- package/src/types.ts +759 -0
- package/src/url-safe-base64.ts +38 -0
- package/src/url-validation.ts +121 -0
- package/src/utils/atomic-write.ts +20 -0
- package/src/utils.ts +84 -0
- package/src/vite-plugin.ts +45 -0
- package/src/vxjson-serialize.ts +66 -0
- package/src/vxjson.ts +442 -0
- package/src/xxhash.ts +43 -0
- package/dist/asset-manifest.d.ts +0 -23
- package/dist/asset-manifest.d.ts.map +0 -1
- package/dist/asset-manifest.js +0 -73
- package/dist/block-index.d.ts +0 -9
- package/dist/block-index.d.ts.map +0 -1
- package/dist/block-index.js +0 -11
- package/dist/blocks.d.ts +0 -16
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -103
- package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
- package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
- package/dist/content-index-D092_vk1.mjs +0 -629
- package/dist/content-index-D092_vk1.mjs.map +0 -1
- package/dist/content-index-D6IVMmAB.d.mts +0 -127
- package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
- package/dist/content-operations-DIbsxG5A.d.mts +0 -354
- package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
- package/dist/content-utils-fWNHTrPy.mjs +0 -280
- package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
- package/dist/data-DquNIEnQ.mjs +0 -62
- package/dist/data-DquNIEnQ.mjs.map +0 -1
- package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
- package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
- package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
- package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
- package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
- package/dist/index-DxVTFK66.d.mts +0 -105
- package/dist/index-DxVTFK66.d.mts.map +0 -1
- package/dist/index.d.mts +0 -80
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -78
- package/dist/index.mjs.map +0 -1
- package/dist/indexing.d.mts +0 -152
- package/dist/indexing.d.mts.map +0 -1
- package/dist/indexing.mjs +0 -255
- package/dist/indexing.mjs.map +0 -1
- package/dist/node.d.mts +0 -373
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -2572
- package/dist/node.mjs.map +0 -1
- package/dist/reader.d.mts +0 -15
- package/dist/reader.d.mts.map +0 -1
- package/dist/reader.mjs +0 -22
- package/dist/reader.mjs.map +0 -1
- package/dist/schemas/index.d.mts +0 -2
- package/dist/schemas/index.mjs +0 -2
- package/dist/schemas-CFRD0FIQ.mjs +0 -84
- package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
- package/dist/site-8oaj1Q60.mjs +0 -366
- package/dist/site-8oaj1Q60.mjs.map +0 -1
- package/dist/site-index-B_254bCc.d.mts +0 -136
- package/dist/site-index-B_254bCc.d.mts.map +0 -1
- package/dist/types-DOdldbfD.mjs +0 -34
- package/dist/types-DOdldbfD.mjs.map +0 -1
- package/dist/types-DjfhS272.d.mts +0 -456
- package/dist/types-DjfhS272.d.mts.map +0 -1
- package/dist/vxjson-qHrgLGip.mjs +0 -555
- package/dist/vxjson-qHrgLGip.mjs.map +0 -1
- package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
- package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
- package/dist/vxjson-serialize.d.mts +0 -17
- package/dist/vxjson-serialize.d.mts.map +0 -1
- package/dist/vxjson-serialize.mjs +0 -2
package/dist/middleware.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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"}
|
package/dist/middleware.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Hono } from 'hono';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
-
//
|
|
32
|
-
|
|
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
|
|
50
|
-
const
|
|
51
|
-
for (const collection of
|
|
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'); // '
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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:
|
|
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
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
|
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{.+} —
|
|
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
|
|
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
|
-
|
|
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 === '
|
|
600
|
-
// Use the efficient collections getter for
|
|
601
|
-
const collections = Array.from(api.
|
|
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
|
|
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
|
|
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 === '
|
|
622
|
-
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, '
|
|
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 === '
|
|
650
|
-
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, '
|
|
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 /
|
|
687
|
-
app.get('/
|
|
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: '
|
|
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
|
|
746
|
+
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list fragments'), 500);
|
|
701
747
|
}
|
|
702
748
|
});
|
|
703
|
-
// GET /
|
|
704
|
-
|
|
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
|
-
|
|
714
|
-
|
|
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
|
-
|
|
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
|
|
767
|
+
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check fragment name availability'), 500);
|
|
745
768
|
}
|
|
746
769
|
});
|
|
747
|
-
// GET /
|
|
748
|
-
app.get('/
|
|
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
|
|
754
|
-
if (!
|
|
755
|
-
return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, '
|
|
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 &&
|
|
781
|
+
if (locale && fragment.locales[locale]) {
|
|
759
782
|
return c.json({
|
|
760
|
-
...
|
|
761
|
-
locales: { [locale]:
|
|
783
|
+
...fragment,
|
|
784
|
+
locales: { [locale]: fragment.locales[locale] },
|
|
762
785
|
});
|
|
763
786
|
}
|
|
764
|
-
return c.json(
|
|
787
|
+
return c.json(fragment);
|
|
765
788
|
}
|
|
766
789
|
catch (error) {
|
|
767
|
-
return c.json(logAndCreateErrorResponse(error, ErrorCodes.FETCH_ERROR, 'Failed to fetch
|
|
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
|
|
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
|