@conloca/content-api 0.1.16 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +18 -13
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +302 -254
- 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 +10 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +49 -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 +59 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +88 -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 +407 -281
- 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 +6 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +170 -119
- 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 +914 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +47 -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 +113 -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 +2685 -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 +1341 -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;AA8F7D;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,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,8EAipCxF;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 { 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 { localesOf, 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');
|
|
@@ -326,10 +339,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
326
339
|
if (available) {
|
|
327
340
|
return c.json({ available: true });
|
|
328
341
|
}
|
|
329
|
-
const existing = api.data.getByName(collection, name);
|
|
330
342
|
return c.json({
|
|
331
343
|
available: false,
|
|
332
|
-
existingId:
|
|
344
|
+
existingId: api.data.getNameConflict(collection, name, excludeId),
|
|
333
345
|
});
|
|
334
346
|
}
|
|
335
347
|
catch (error) {
|
|
@@ -384,6 +396,11 @@ export function createContentAPIRouter(api, options) {
|
|
|
384
396
|
// ===== Asset Routes =====
|
|
385
397
|
// Must be registered BEFORE /:site routes to avoid route conflicts (/:site would match "assets")
|
|
386
398
|
if (options?.assetsPath) {
|
|
399
|
+
// Asset metadata records live under the content root. There is no falling
|
|
400
|
+
// back to the media folder: a site serves it, so a record there is published.
|
|
401
|
+
if (!options.contentRoot) {
|
|
402
|
+
throw new Error('The assetsPath option needs contentRoot as well: asset metadata is kept under the content root');
|
|
403
|
+
}
|
|
387
404
|
const assetOps = new AssetOperations({
|
|
388
405
|
assetsPath: options.assetsPath,
|
|
389
406
|
contentRoot: options.contentRoot,
|
|
@@ -399,6 +416,11 @@ export function createContentAPIRouter(api, options) {
|
|
|
399
416
|
}
|
|
400
417
|
return { folder: '/', filename: relativePath };
|
|
401
418
|
}
|
|
419
|
+
// The router decodes `%2F`, so a path in the address can climb out of the assets folder. The
|
|
420
|
+
// containment check refuses it, and that is the caller's mistake, not a server fault.
|
|
421
|
+
function isPathTraversal(error) {
|
|
422
|
+
return error instanceof Error && error.message === 'Path traversal detected';
|
|
423
|
+
}
|
|
402
424
|
// POST /assets/move — move assets between folders
|
|
403
425
|
app.post('/assets/move', async (c) => {
|
|
404
426
|
try {
|
|
@@ -419,7 +441,7 @@ export function createContentAPIRouter(api, options) {
|
|
|
419
441
|
return c.json(result);
|
|
420
442
|
}
|
|
421
443
|
catch (error) {
|
|
422
|
-
if (error
|
|
444
|
+
if (isPathTraversal(error)) {
|
|
423
445
|
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid folder path'), 400);
|
|
424
446
|
}
|
|
425
447
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to move assets'), 500);
|
|
@@ -434,11 +456,11 @@ export function createContentAPIRouter(api, options) {
|
|
|
434
456
|
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'No file provided in "file" field'), 400);
|
|
435
457
|
}
|
|
436
458
|
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
459
|
const folder = typeof body.folder === 'string' ? body.folder : undefined;
|
|
441
|
-
|
|
460
|
+
// The uploader is deliberately not read from the request: anyone who can
|
|
461
|
+
// reach this route could otherwise put any name on any picture, and an
|
|
462
|
+
// audit trail that anyone can write is worse than no audit trail.
|
|
463
|
+
const result = await assetOps.upload(file, { alt, folder });
|
|
442
464
|
if (!result.success) {
|
|
443
465
|
return c.json(errorResponse(ErrorCodes.ASSET_UPLOAD_FAILED, result.error), 400);
|
|
444
466
|
}
|
|
@@ -451,11 +473,12 @@ export function createContentAPIRouter(api, options) {
|
|
|
451
473
|
// POST /assets/import-url — import image from URL
|
|
452
474
|
app.post('/assets/import-url', async (c) => {
|
|
453
475
|
try {
|
|
454
|
-
const { url, alt,
|
|
476
|
+
const { url, alt, folder } = await c.req.json();
|
|
455
477
|
if (!url) {
|
|
456
478
|
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'url is required'), 400);
|
|
457
479
|
}
|
|
458
|
-
|
|
480
|
+
// As with /assets/upload: the uploader never comes off the wire
|
|
481
|
+
const result = await assetOps.importFromUrl(url, { alt, folder });
|
|
459
482
|
if (!result.success) {
|
|
460
483
|
return c.json(errorResponse(ErrorCodes.ASSET_UPLOAD_FAILED, result.error), 400);
|
|
461
484
|
}
|
|
@@ -483,7 +506,7 @@ export function createContentAPIRouter(api, options) {
|
|
|
483
506
|
return c.json(result);
|
|
484
507
|
}
|
|
485
508
|
catch (error) {
|
|
486
|
-
if (error
|
|
509
|
+
if (isPathTraversal(error)) {
|
|
487
510
|
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid folder path'), 400);
|
|
488
511
|
}
|
|
489
512
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list folder'), 500);
|
|
@@ -516,11 +539,10 @@ export function createContentAPIRouter(api, options) {
|
|
|
516
539
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to get folder tree'), 500);
|
|
517
540
|
}
|
|
518
541
|
});
|
|
519
|
-
// GET /assets/usage/:path{.+} —
|
|
542
|
+
// GET /assets/usage/:path{.+} — content that links to the asset at this path from the assets root
|
|
520
543
|
app.get('/assets/usage/:path{.+}', async (c) => {
|
|
521
544
|
try {
|
|
522
|
-
const
|
|
523
|
-
const usage = await assetOps.getUsage(filename);
|
|
545
|
+
const usage = await findAssetUsage(api, c.req.param('path'));
|
|
524
546
|
return c.json({ usage });
|
|
525
547
|
}
|
|
526
548
|
catch (error) {
|
|
@@ -531,6 +553,7 @@ export function createContentAPIRouter(api, options) {
|
|
|
531
553
|
app.get('/assets/serve/:path{.+}', async (c) => {
|
|
532
554
|
try {
|
|
533
555
|
const { folder, filename } = splitAssetPath(c.req.param('path'));
|
|
556
|
+
assetOps.getAssetPath(filename, folder);
|
|
534
557
|
const result = await assetOps.readAssetFile(filename, folder);
|
|
535
558
|
if (!result.success) {
|
|
536
559
|
return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404);
|
|
@@ -543,6 +566,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
543
566
|
});
|
|
544
567
|
}
|
|
545
568
|
catch (error) {
|
|
569
|
+
if (isPathTraversal(error)) {
|
|
570
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
|
|
571
|
+
}
|
|
546
572
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to serve asset'), 500);
|
|
547
573
|
}
|
|
548
574
|
});
|
|
@@ -550,6 +576,7 @@ export function createContentAPIRouter(api, options) {
|
|
|
550
576
|
app.get('/assets/meta/:path{.+}', async (c) => {
|
|
551
577
|
try {
|
|
552
578
|
const { folder, filename } = splitAssetPath(c.req.param('path'));
|
|
579
|
+
assetOps.getAssetPath(filename, folder);
|
|
553
580
|
const asset = await assetOps.getAsset(filename, folder);
|
|
554
581
|
if (!asset) {
|
|
555
582
|
return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, 'Asset not found'), 404);
|
|
@@ -557,6 +584,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
557
584
|
return c.json(asset);
|
|
558
585
|
}
|
|
559
586
|
catch (error) {
|
|
587
|
+
if (isPathTraversal(error)) {
|
|
588
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
|
|
589
|
+
}
|
|
560
590
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to get asset'), 500);
|
|
561
591
|
}
|
|
562
592
|
});
|
|
@@ -564,14 +594,22 @@ export function createContentAPIRouter(api, options) {
|
|
|
564
594
|
app.patch('/assets/meta/:path{.+}', async (c) => {
|
|
565
595
|
try {
|
|
566
596
|
const { folder, filename } = splitAssetPath(c.req.param('path'));
|
|
597
|
+
assetOps.getAssetPath(filename, folder);
|
|
567
598
|
const { alt, tags } = await c.req.json();
|
|
568
599
|
const result = await assetOps.updateMetadata(filename, { alt, tags }, folder);
|
|
569
600
|
if (!result.success) {
|
|
570
|
-
|
|
601
|
+
// A disk problem is not a missing picture: saying 404 sent the editor
|
|
602
|
+
// back to refetch a value it had just failed to store.
|
|
603
|
+
return result.reason === 'not-found'
|
|
604
|
+
? c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404)
|
|
605
|
+
: c.json(errorResponse(ErrorCodes.INTERNAL_ERROR, result.error), 500);
|
|
571
606
|
}
|
|
572
607
|
return c.json(result.asset);
|
|
573
608
|
}
|
|
574
609
|
catch (error) {
|
|
610
|
+
if (isPathTraversal(error)) {
|
|
611
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
|
|
612
|
+
}
|
|
575
613
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to update asset'), 500);
|
|
576
614
|
}
|
|
577
615
|
});
|
|
@@ -579,6 +617,7 @@ export function createContentAPIRouter(api, options) {
|
|
|
579
617
|
app.delete('/assets/meta/:path{.+}', async (c) => {
|
|
580
618
|
try {
|
|
581
619
|
const { folder, filename } = splitAssetPath(c.req.param('path'));
|
|
620
|
+
assetOps.getAssetPath(filename, folder);
|
|
582
621
|
const result = await assetOps.delete(filename, folder);
|
|
583
622
|
if (!result.success) {
|
|
584
623
|
return c.json(errorResponse(ErrorCodes.ASSET_NOT_FOUND, result.error), 404);
|
|
@@ -586,6 +625,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
586
625
|
return c.json({ success: true });
|
|
587
626
|
}
|
|
588
627
|
catch (error) {
|
|
628
|
+
if (isPathTraversal(error)) {
|
|
629
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Invalid asset path'), 400);
|
|
630
|
+
}
|
|
589
631
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to delete asset'), 500);
|
|
590
632
|
}
|
|
591
633
|
});
|
|
@@ -596,9 +638,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
596
638
|
app.get('/:site/collections', async (c) => {
|
|
597
639
|
const site = c.req.param('site');
|
|
598
640
|
try {
|
|
599
|
-
if (site === '
|
|
600
|
-
// Use the efficient collections getter for
|
|
601
|
-
const collections = Array.from(api.
|
|
641
|
+
if (site === 'fragments') {
|
|
642
|
+
// Use the efficient collections getter for fragments
|
|
643
|
+
const collections = Array.from(api.fragments.collections).sort();
|
|
602
644
|
return c.json({ collections });
|
|
603
645
|
}
|
|
604
646
|
// Use the efficient collections getter for sites
|
|
@@ -609,17 +651,17 @@ export function createContentAPIRouter(api, options) {
|
|
|
609
651
|
const collections = Array.from(siteObj.collections).sort();
|
|
610
652
|
return c.json({ collections });
|
|
611
653
|
}
|
|
612
|
-
catch
|
|
654
|
+
catch {
|
|
613
655
|
return c.json(errorResponse(ErrorCodes.SITE_NOT_FOUND, `Site '${site}' not found`, { site }), 404);
|
|
614
656
|
}
|
|
615
657
|
});
|
|
616
|
-
// GET /:site/pages - Get pages for a site (not valid for
|
|
658
|
+
// GET /:site/pages - Get pages for a site (not valid for fragments)
|
|
617
659
|
app.get('/:site/pages', async (c) => {
|
|
618
660
|
const site = c.req.param('site');
|
|
619
661
|
const locale = c.req.query('locale');
|
|
620
662
|
try {
|
|
621
|
-
if (site === '
|
|
622
|
-
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, '
|
|
663
|
+
if (site === 'fragments') {
|
|
664
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Fragments do not have pages. Use /content?kind=fragment to list fragments.'), 400);
|
|
623
665
|
}
|
|
624
666
|
const siteObj = api.getSite(site);
|
|
625
667
|
if (!siteObj) {
|
|
@@ -646,8 +688,8 @@ export function createContentAPIRouter(api, options) {
|
|
|
646
688
|
return c.json(errorResponse(ErrorCodes.MISSING_REQUIRED_FIELD, 'pathname query parameter is required', { field: 'pathname' }), 400);
|
|
647
689
|
}
|
|
648
690
|
try {
|
|
649
|
-
if (site === '
|
|
650
|
-
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, '
|
|
691
|
+
if (site === 'fragments') {
|
|
692
|
+
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, 'Fragments do not have pathnames. This endpoint is only for page content.'), 400);
|
|
651
693
|
}
|
|
652
694
|
// Defensive: if the pathname shape is malformed, short-circuit with
|
|
653
695
|
// a structured reason instead of a meaningless index lookup. The
|
|
@@ -683,12 +725,12 @@ export function createContentAPIRouter(api, options) {
|
|
|
683
725
|
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check pathname availability'), 500);
|
|
684
726
|
}
|
|
685
727
|
});
|
|
686
|
-
// GET /
|
|
687
|
-
app.get('/
|
|
728
|
+
// GET /fragments - Shorthand for all fragments
|
|
729
|
+
app.get('/fragments', async (c) => {
|
|
688
730
|
const collection = c.req.query('collection');
|
|
689
731
|
const locale = c.req.query('locale');
|
|
690
732
|
try {
|
|
691
|
-
const filters = { kind: '
|
|
733
|
+
const filters = { kind: 'fragment' };
|
|
692
734
|
if (collection)
|
|
693
735
|
filters.collection = collection;
|
|
694
736
|
if (locale)
|
|
@@ -697,11 +739,11 @@ export function createContentAPIRouter(api, options) {
|
|
|
697
739
|
return c.json({ items, total: items.length });
|
|
698
740
|
}
|
|
699
741
|
catch (error) {
|
|
700
|
-
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list
|
|
742
|
+
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to list fragments'), 500);
|
|
701
743
|
}
|
|
702
744
|
});
|
|
703
|
-
// GET /
|
|
704
|
-
app.get('/
|
|
745
|
+
// GET /fragments/name-available - Check if fragment name is available
|
|
746
|
+
app.get('/fragments/name-available', async (c) => {
|
|
705
747
|
const name = c.req.query('name');
|
|
706
748
|
const collection = c.req.query('collection') || 'components';
|
|
707
749
|
const locale = c.req.query('locale') || 'en';
|
|
@@ -710,9 +752,9 @@ export function createContentAPIRouter(api, options) {
|
|
|
710
752
|
return c.json(errorResponse(ErrorCodes.MISSING_REQUIRED_FIELD, 'name query parameter is required', { field: 'name' }), 400);
|
|
711
753
|
}
|
|
712
754
|
try {
|
|
713
|
-
// Check
|
|
755
|
+
// Check fragment name availability through listing
|
|
714
756
|
const existing = Array.from(api.listAllContent({
|
|
715
|
-
kind: '
|
|
757
|
+
kind: 'fragment',
|
|
716
758
|
collection: collection,
|
|
717
759
|
locales: [locale],
|
|
718
760
|
}));
|
|
@@ -725,12 +767,12 @@ export function createContentAPIRouter(api, options) {
|
|
|
725
767
|
}
|
|
726
768
|
}
|
|
727
769
|
}
|
|
728
|
-
// If no existing
|
|
770
|
+
// If no existing fragment, name is available
|
|
729
771
|
if (!taken) {
|
|
730
772
|
return c.json({ available: true });
|
|
731
773
|
}
|
|
732
|
-
// If existing
|
|
733
|
-
// (useful when editing existing
|
|
774
|
+
// If existing fragment has same ID as excludeId, name is available
|
|
775
|
+
// (useful when editing existing fragment)
|
|
734
776
|
if (excludeId && taken.id === excludeId) {
|
|
735
777
|
return c.json({ available: true });
|
|
736
778
|
}
|
|
@@ -741,30 +783,30 @@ export function createContentAPIRouter(api, options) {
|
|
|
741
783
|
});
|
|
742
784
|
}
|
|
743
785
|
catch (error) {
|
|
744
|
-
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check
|
|
786
|
+
return c.json(logAndCreateErrorResponse(error, ErrorCodes.INTERNAL_ERROR, 'Failed to check fragment name availability'), 500);
|
|
745
787
|
}
|
|
746
788
|
});
|
|
747
|
-
// GET /
|
|
748
|
-
app.get('/
|
|
789
|
+
// GET /fragments/:name - Get fragment by name
|
|
790
|
+
app.get('/fragments/:name', async (c) => {
|
|
749
791
|
const name = c.req.param('name');
|
|
750
792
|
const collection = c.req.query('collection') || 'components';
|
|
751
793
|
const locale = c.req.query('locale');
|
|
752
794
|
try {
|
|
753
|
-
const
|
|
754
|
-
if (!
|
|
755
|
-
return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, '
|
|
795
|
+
const fragment = api.fragments.getByName(collection, name, locale);
|
|
796
|
+
if (!fragment) {
|
|
797
|
+
return c.json(errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Fragment not found', { name, collection }), 404);
|
|
756
798
|
}
|
|
757
799
|
// If locale specified and provided, filter the response
|
|
758
|
-
if (locale &&
|
|
800
|
+
if (locale && fragment.locales[locale]) {
|
|
759
801
|
return c.json({
|
|
760
|
-
...
|
|
761
|
-
locales: { [locale]:
|
|
802
|
+
...fragment,
|
|
803
|
+
locales: { [locale]: fragment.locales[locale] },
|
|
762
804
|
});
|
|
763
805
|
}
|
|
764
|
-
return c.json(
|
|
806
|
+
return c.json(fragment);
|
|
765
807
|
}
|
|
766
808
|
catch (error) {
|
|
767
|
-
return c.json(logAndCreateErrorResponse(error, ErrorCodes.FETCH_ERROR, 'Failed to fetch
|
|
809
|
+
return c.json(logAndCreateErrorResponse(error, ErrorCodes.FETCH_ERROR, 'Failed to fetch fragment'), 500);
|
|
768
810
|
}
|
|
769
811
|
});
|
|
770
812
|
// POST /:site/move - Move page to new pathname
|
|
@@ -816,6 +858,14 @@ export function createContentAPIRouter(api, options) {
|
|
|
816
858
|
if (updateResult.reason === 'not_found') {
|
|
817
859
|
return c.json({ moved: false, ...errorResponse(ErrorCodes.CONTENT_NOT_FOUND, 'Content not found', { id }) }, 404);
|
|
818
860
|
}
|
|
861
|
+
// The update checks the new path as a create does: a malformed one is invalid_pathname, and
|
|
862
|
+
// another page's path written loosely ('/team/') passes the check above but not the update.
|
|
863
|
+
// A move calls a taken path already_exists, as the check above does.
|
|
864
|
+
const refusal = refusalOf(updateResult.reason);
|
|
865
|
+
if (refusal) {
|
|
866
|
+
const reason = updateResult.reason === 'invalid_pathname' ? 'invalid_pathname' : 'already_exists';
|
|
867
|
+
return c.json({ moved: false, reason, ...errorResponse(refusal.code, errorMessage(updateResult.error, 'Move refused')) }, refusal.status);
|
|
868
|
+
}
|
|
819
869
|
return c.json({ moved: false, ...errorResponse(ErrorCodes.WRITE_ERROR, 'Update failed') }, 500);
|
|
820
870
|
}
|
|
821
871
|
return c.json({
|
|
@@ -914,12 +964,12 @@ export function createContentAPIRouter(api, options) {
|
|
|
914
964
|
// POST /git/commit - Commit all changes
|
|
915
965
|
app.post('/git/commit', async (c) => {
|
|
916
966
|
try {
|
|
917
|
-
const body = await c.req.json().catch(() =>
|
|
967
|
+
const body = await c.req.json().catch(() => undefined);
|
|
918
968
|
// Auto-generate message if not provided
|
|
919
969
|
const date = new Date();
|
|
920
970
|
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
921
971
|
const defaultMessage = `CMS changes - ${monthNames[date.getMonth()]} ${date.getDate()}`;
|
|
922
|
-
const message = body
|
|
972
|
+
const message = body?.message || defaultMessage;
|
|
923
973
|
// Extract user email from CF Access header for commit attribution
|
|
924
974
|
const userEmail = c.req.header('X-CF-User-Email');
|
|
925
975
|
const author = userEmail ? { name: userEmail.split('@')[0], email: userEmail } : undefined;
|
|
@@ -1029,3 +1079,4 @@ function readBody(req) {
|
|
|
1029
1079
|
req.on('error', reject);
|
|
1030
1080
|
});
|
|
1031
1081
|
}
|
|
1082
|
+
//# sourceMappingURL=middleware.js.map
|