@conloca/content-api 0.2.1 → 0.2.2
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-metadata-store.d.ts.map +1 -1
- package/dist/asset-operations.d.ts +15 -17
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +216 -78
- package/dist/asset-operations.js.map +1 -1
- package/dist/cf-access.d.ts +4 -2
- package/dist/cf-access.d.ts.map +1 -1
- package/dist/cf-access.js +29 -13
- package/dist/cf-access.js.map +1 -1
- package/dist/component-registry.d.ts +0 -3
- package/dist/component-registry.d.ts.map +1 -1
- package/dist/content-file-id.d.ts +9 -0
- package/dist/content-file-id.d.ts.map +1 -0
- package/dist/content-file-id.js +33 -0
- package/dist/content-file-id.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +0 -15
- package/dist/filesystem-content-api.d.ts.map +1 -1
- package/dist/git-operations.d.ts.map +1 -1
- package/dist/git-operations.js +162 -34
- package/dist/git-operations.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +5 -0
- package/dist/middleware.js.map +1 -1
- package/dist/schemas/index.d.ts +9 -97
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +8 -83
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/normalize.d.ts +101 -0
- package/dist/schemas/normalize.d.ts.map +1 -0
- package/dist/schemas/normalize.js +168 -0
- package/dist/schemas/normalize.js.map +1 -0
- package/dist/schemas/types.d.ts +135 -0
- package/dist/schemas/types.d.ts.map +1 -0
- package/dist/schemas/types.js +17 -0
- package/dist/schemas/types.js.map +1 -0
- package/dist/schemas/validators.d.ts +46 -0
- package/dist/schemas/validators.d.ts.map +1 -0
- package/dist/schemas/validators.js +938 -0
- package/dist/schemas/validators.js.map +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +12 -5
- package/src/asset-operations.ts +256 -83
- package/src/cf-access.ts +30 -14
- package/src/content-file-id.ts +31 -0
- package/src/git-operations.ts +188 -41
- package/src/index.ts +17 -1
- package/src/middleware.ts +5 -0
- package/src/schemas/index.ts +34 -108
- package/src/schemas/normalize.ts +194 -0
- package/src/schemas/types.ts +154 -0
- package/src/schemas/validators.ts +176 -0
- package/src/types.ts +7 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry id a complete content file declares, for callers that hold a
|
|
3
|
+
* repository path rather than a content-layout one (a file a commit removed)
|
|
4
|
+
* and must name the entry the index knew it by.
|
|
5
|
+
*
|
|
6
|
+
* Its own subpath, apart from `./indexing`: that chunk evaluates WASM-backed
|
|
7
|
+
* hashing at import time, which a runtime that forbids compiling WASM from
|
|
8
|
+
* bytes refuses. This module imports only the frontmatter parser.
|
|
9
|
+
*/
|
|
10
|
+
import matter from 'gray-matter';
|
|
11
|
+
|
|
12
|
+
const textDecoder = new TextDecoder();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The id `parseContentHeader` would index the file under, read the same way:
|
|
16
|
+
* YAML frontmatter through gray-matter (fresh options, so no memo cache) for
|
|
17
|
+
* `.mdx`, the top-level `id` of the JSON document for `.json` and `.vxjson`
|
|
18
|
+
* (canonicalizing a complete VXJSON file keeps that field as it is). Null for
|
|
19
|
+
* a non-content file, an unparseable one, or one that declares no id.
|
|
20
|
+
*/
|
|
21
|
+
export function contentFileId(path: string, bytes: Uint8Array): string | null {
|
|
22
|
+
const text = textDecoder.decode(bytes);
|
|
23
|
+
let id: unknown;
|
|
24
|
+
try {
|
|
25
|
+
if (path.endsWith('.mdx')) id = matter(text, {}).data.id;
|
|
26
|
+
else if (path.endsWith('.json') || path.endsWith('.vxjson')) id = (JSON.parse(text) as { id?: unknown } | null)?.id;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return typeof id === 'string' && id !== '' ? id : null;
|
|
31
|
+
}
|
package/src/git-operations.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import { access, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { access, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
3
4
|
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
6
|
+
|
|
7
|
+
/** Tries at lining the shared index up after a commit; ~1.5s total covers a briefly held index.lock. */
|
|
8
|
+
const INDEX_SYNC_ATTEMPTS = 6;
|
|
4
9
|
|
|
5
10
|
export interface GitStatus {
|
|
6
11
|
isRepo: boolean;
|
|
@@ -53,9 +58,15 @@ interface GitCommandResult {
|
|
|
53
58
|
stderr: string;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
function runGitCommand(
|
|
61
|
+
function runGitCommand(
|
|
62
|
+
cwd: string,
|
|
63
|
+
args: string[],
|
|
64
|
+
env?: NodeJS.ProcessEnv,
|
|
65
|
+
input?: string,
|
|
66
|
+
): Promise<GitCommandResult> {
|
|
57
67
|
return new Promise((resolveCommand, reject) => {
|
|
58
|
-
const process = spawn('git', args, { cwd, stdio:
|
|
68
|
+
const process = spawn('git', args, { cwd, env, stdio: 'pipe' });
|
|
69
|
+
process.stdin.end(input);
|
|
59
70
|
let stdout = '';
|
|
60
71
|
let stderr = '';
|
|
61
72
|
|
|
@@ -79,6 +90,66 @@ function runGitCommand(cwd: string, args: string[]): Promise<GitCommandResult> {
|
|
|
79
90
|
});
|
|
80
91
|
}
|
|
81
92
|
|
|
93
|
+
/** One path a commit changed, from `git diff-tree -r -z` raw output. */
|
|
94
|
+
interface TreeChange {
|
|
95
|
+
path: string;
|
|
96
|
+
oldBlob: string;
|
|
97
|
+
newMode: string;
|
|
98
|
+
newBlob: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function parseRawDiff(raw: string): TreeChange[] {
|
|
102
|
+
const fields = raw.split('\0');
|
|
103
|
+
const changes: TreeChange[] = [];
|
|
104
|
+
for (let i = 0; i + 1 < fields.length; i += 2) {
|
|
105
|
+
// ":oldmode newmode oldblob newblob status" then the path
|
|
106
|
+
const [, newMode, oldBlob, newBlob] = fields[i].replace(/^:/, '').split(' ');
|
|
107
|
+
if (!newBlob) continue;
|
|
108
|
+
changes.push({ path: fields[i + 1], oldBlob, newMode, newBlob });
|
|
109
|
+
}
|
|
110
|
+
return changes;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Point the shared index at the committed blobs, so committed files do not show as staged
|
|
115
|
+
* reversals — but only for entries that still hold what the parent commit had. An entry
|
|
116
|
+
* someone staged differently in the meantime is theirs and is left alone, as is everything
|
|
117
|
+
* outside the commit. Retries while another Git process briefly holds index.lock, re-reading
|
|
118
|
+
* the index each time so a `git add` made during the wait is not overwritten.
|
|
119
|
+
*/
|
|
120
|
+
async function syncSharedIndex(
|
|
121
|
+
repoRoot: string,
|
|
122
|
+
contentPath: string,
|
|
123
|
+
changes: TreeChange[],
|
|
124
|
+
): Promise<{ ok: true } | { ok: false; error: string }> {
|
|
125
|
+
if (changes.length === 0) return { ok: true };
|
|
126
|
+
let error = 'git update-index failed';
|
|
127
|
+
for (let attempt = 0; attempt < INDEX_SYNC_ATTEMPTS; attempt += 1) {
|
|
128
|
+
// oxlint-disable-next-line no-await-in-loop -- each retry waits for the lock holder to finish.
|
|
129
|
+
if (attempt > 0) await sleep(attempt * 100);
|
|
130
|
+
// A commit only ever changes paths under contentPath, so one pathspec covers them all
|
|
131
|
+
// without passing each path on the command line.
|
|
132
|
+
// oxlint-disable-next-line no-await-in-loop -- each attempt must see the index as it is now.
|
|
133
|
+
const staged = await runGitCommand(repoRoot, ['ls-files', '-s', '-z', '--', contentPath]);
|
|
134
|
+
const stagedBlob = new Map<string, string>();
|
|
135
|
+
for (const entry of staged.stdout.split('\0')) {
|
|
136
|
+
const tab = entry.indexOf('\t');
|
|
137
|
+
if (tab < 0) continue;
|
|
138
|
+
const [, blob, stage] = entry.slice(0, tab).split(' ');
|
|
139
|
+
if (stage === '0') stagedBlob.set(entry.slice(tab + 1), blob);
|
|
140
|
+
}
|
|
141
|
+
const lines = changes
|
|
142
|
+
.filter((c) => (stagedBlob.get(c.path) ?? '0'.repeat(c.oldBlob.length)) === c.oldBlob)
|
|
143
|
+
.map((c) => (/^0+$/.test(c.newBlob) ? `0 ${c.newBlob}\t${c.path}\0` : `${c.newMode} ${c.newBlob}\t${c.path}\0`));
|
|
144
|
+
if (lines.length === 0) return { ok: true };
|
|
145
|
+
// oxlint-disable-next-line no-await-in-loop -- retries are sequential by design.
|
|
146
|
+
const result = await runGitCommand(repoRoot, ['update-index', '-z', '--index-info'], undefined, lines.join(''));
|
|
147
|
+
if (result.exitCode === 0) return { ok: true };
|
|
148
|
+
error = result.stderr || error;
|
|
149
|
+
}
|
|
150
|
+
return { ok: false, error };
|
|
151
|
+
}
|
|
152
|
+
|
|
82
153
|
/**
|
|
83
154
|
* Resolve the git repository root from a content path.
|
|
84
155
|
* Uses `git rev-parse --show-toplevel` starting from the parent of the content directory.
|
|
@@ -93,6 +164,22 @@ async function resolveRepoRoot(contentPath: string): Promise<string | null> {
|
|
|
93
164
|
return result.stdout;
|
|
94
165
|
}
|
|
95
166
|
|
|
167
|
+
/**
|
|
168
|
+
* Commits and pulls both move HEAD; queueing them per repository keeps one request's commit
|
|
169
|
+
* from landing inside another's. Module-wide and keyed by repo root because the router builds
|
|
170
|
+
* a fresh GitOperations for every request.
|
|
171
|
+
*/
|
|
172
|
+
const headQueues = new Map<string, Promise<unknown>>();
|
|
173
|
+
function serializedOnRepo<T>(repoRoot: string, operation: () => Promise<T>): Promise<T> {
|
|
174
|
+
const run = (headQueues.get(repoRoot) ?? Promise.resolve()).then(operation, operation);
|
|
175
|
+
const settled = run.catch(() => undefined);
|
|
176
|
+
headQueues.set(repoRoot, settled);
|
|
177
|
+
void settled.then(() => {
|
|
178
|
+
if (headQueues.get(repoRoot) === settled) headQueues.delete(repoRoot);
|
|
179
|
+
});
|
|
180
|
+
return run;
|
|
181
|
+
}
|
|
182
|
+
|
|
96
183
|
/**
|
|
97
184
|
* Create git operations using local git CLI commands.
|
|
98
185
|
* Requires git binary available on PATH and CONTENT_PATH inside a git repository.
|
|
@@ -112,6 +199,100 @@ export function createGitOperations(config: GitConfig): GitOperations {
|
|
|
112
199
|
return cachedRepoRoot;
|
|
113
200
|
}
|
|
114
201
|
|
|
202
|
+
async function commitContent(repoRoot: string, message: string, author?: GitAuthor): Promise<GitCommitResult> {
|
|
203
|
+
// Build the commit in a private index seeded from HEAD. The shared index may hold work
|
|
204
|
+
// someone staged outside contentPath; it must neither ride along nor lose its staging.
|
|
205
|
+
// Writing the tree from the private index (not a pathspec commit, which re-reads the
|
|
206
|
+
// working tree) also pins the snapshot `git add` took against content writes meanwhile.
|
|
207
|
+
const indexDir = await mkdtemp(join(tmpdir(), 'conloca-git-index-'));
|
|
208
|
+
const privateIndex = { ...process.env, GIT_INDEX_FILE: join(indexDir, 'index') };
|
|
209
|
+
try {
|
|
210
|
+
const headResult = await runGitCommand(repoRoot, ['rev-parse', '--verify', '--quiet', 'HEAD']);
|
|
211
|
+
const parent = headResult.exitCode === 0 ? headResult.stdout : null;
|
|
212
|
+
const seed = await runGitCommand(
|
|
213
|
+
repoRoot,
|
|
214
|
+
parent ? ['read-tree', parent] : ['read-tree', '--empty'],
|
|
215
|
+
privateIndex,
|
|
216
|
+
);
|
|
217
|
+
if (seed.exitCode !== 0) {
|
|
218
|
+
return { success: false, error: seed.stderr || 'Failed to prepare commit' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const addResult = await runGitCommand(repoRoot, ['add', '-A', '--', contentPath], privateIndex);
|
|
222
|
+
if (addResult.exitCode !== 0) {
|
|
223
|
+
return { success: false, error: addResult.stderr || 'Failed to stage changes' };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const treeResult = await runGitCommand(repoRoot, ['write-tree'], privateIndex);
|
|
227
|
+
if (treeResult.exitCode !== 0) {
|
|
228
|
+
return { success: false, error: treeResult.stderr || 'Failed to write tree' };
|
|
229
|
+
}
|
|
230
|
+
const tree = treeResult.stdout;
|
|
231
|
+
if (parent && (await runGitCommand(repoRoot, ['rev-parse', `${parent}^{tree}`])).stdout === tree) {
|
|
232
|
+
return { success: false, error: 'No changes to commit' };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// author.name/author.email set only the author; the committer stays the server's identity,
|
|
236
|
+
// as `git commit --author` left it.
|
|
237
|
+
const authorConfig = author ? ['-c', `author.name=${author.name}`, '-c', `author.email=${author.email}`] : [];
|
|
238
|
+
const commitResult = await runGitCommand(repoRoot, [
|
|
239
|
+
...authorConfig,
|
|
240
|
+
'commit-tree',
|
|
241
|
+
tree,
|
|
242
|
+
...(parent ? ['-p', parent] : []),
|
|
243
|
+
'-m',
|
|
244
|
+
message,
|
|
245
|
+
]);
|
|
246
|
+
if (commitResult.exitCode !== 0) {
|
|
247
|
+
return { success: false, error: commitResult.stderr || 'Commit failed' };
|
|
248
|
+
}
|
|
249
|
+
const commitSha = commitResult.stdout;
|
|
250
|
+
|
|
251
|
+
// Move HEAD only if it still points where the tree was built from. A commit another
|
|
252
|
+
// process made meanwhile would otherwise be silently reverted by this stale tree.
|
|
253
|
+
const expected = parent ?? '0'.repeat(commitSha.length);
|
|
254
|
+
const moveHead = await runGitCommand(repoRoot, [
|
|
255
|
+
'update-ref',
|
|
256
|
+
'-m',
|
|
257
|
+
`commit: ${message}`,
|
|
258
|
+
'HEAD',
|
|
259
|
+
commitSha,
|
|
260
|
+
expected,
|
|
261
|
+
]);
|
|
262
|
+
if (moveHead.exitCode !== 0) {
|
|
263
|
+
return { success: false, error: 'The branch moved while committing; nothing was committed. Try again.' };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Count and sync from the commit just made, never from HEAD, which others may move again.
|
|
267
|
+
const changesResult = await runGitCommand(repoRoot, [
|
|
268
|
+
'diff-tree',
|
|
269
|
+
'-r',
|
|
270
|
+
'-z',
|
|
271
|
+
'--no-renames',
|
|
272
|
+
'--no-commit-id',
|
|
273
|
+
...(parent ? [parent, commitSha] : ['--root', commitSha]),
|
|
274
|
+
]);
|
|
275
|
+
const changes = parseRawDiff(changesResult.stdout);
|
|
276
|
+
|
|
277
|
+
const synced = await syncSharedIndex(repoRoot, contentPath, changes);
|
|
278
|
+
if (!synced.ok) {
|
|
279
|
+
return {
|
|
280
|
+
success: false,
|
|
281
|
+
commit: commitSha,
|
|
282
|
+
error: `Committed ${commitSha}, but the Git index could not be updated for the content folder (${synced.error}). Run \`git reset -- ${contentPath}\` before committing outside the CMS.`,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
success: true,
|
|
288
|
+
commit: commitSha,
|
|
289
|
+
summary: `${changes.length} files committed`,
|
|
290
|
+
};
|
|
291
|
+
} finally {
|
|
292
|
+
await rm(indexDir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
115
296
|
return {
|
|
116
297
|
async getStatus(): Promise<GitStatus> {
|
|
117
298
|
try {
|
|
@@ -164,43 +345,7 @@ export function createGitOperations(config: GitConfig): GitOperations {
|
|
|
164
345
|
if (!repoRoot) {
|
|
165
346
|
return { success: false, error: 'Not a git repository' };
|
|
166
347
|
}
|
|
167
|
-
|
|
168
|
-
// Stage all changes
|
|
169
|
-
const addResult = await runGitCommand(repoRoot, ['add', '-A', '--', contentPath]);
|
|
170
|
-
if (addResult.exitCode !== 0) {
|
|
171
|
-
return { success: false, error: addResult.stderr || 'Failed to stage changes' };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Check if there's anything to commit after staging
|
|
175
|
-
const diffResult = await runGitCommand(repoRoot, ['diff', '--cached', '--quiet', '--', contentPath]);
|
|
176
|
-
if (diffResult.exitCode === 0) {
|
|
177
|
-
return { success: false, error: 'No changes to commit' };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Count staged files for summary
|
|
181
|
-
const diffStatResult = await runGitCommand(repoRoot, ['diff', '--cached', '--numstat', '--', contentPath]);
|
|
182
|
-
const fileCount = diffStatResult.stdout ? diffStatResult.stdout.split('\n').filter((l) => l.trim()).length : 0;
|
|
183
|
-
|
|
184
|
-
// Build commit command
|
|
185
|
-
const commitArgs = ['commit', '-m', message];
|
|
186
|
-
if (author) {
|
|
187
|
-
commitArgs.push('--author', `${author.name} <${author.email}>`);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const commitResult = await runGitCommand(repoRoot, commitArgs);
|
|
191
|
-
if (commitResult.exitCode !== 0) {
|
|
192
|
-
return { success: false, error: commitResult.stderr || 'Commit failed' };
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Get the commit SHA
|
|
196
|
-
const shaResult = await runGitCommand(repoRoot, ['rev-parse', 'HEAD']);
|
|
197
|
-
const commitSha = shaResult.stdout || undefined;
|
|
198
|
-
|
|
199
|
-
return {
|
|
200
|
-
success: true,
|
|
201
|
-
commit: commitSha,
|
|
202
|
-
summary: `${fileCount} files committed`,
|
|
203
|
-
};
|
|
348
|
+
return await serializedOnRepo(repoRoot, () => commitContent(repoRoot, message, author));
|
|
204
349
|
} catch (error) {
|
|
205
350
|
return {
|
|
206
351
|
success: false,
|
|
@@ -279,7 +424,9 @@ export function createGitOperations(config: GitConfig): GitOperations {
|
|
|
279
424
|
targetBranch = branchResult.stdout;
|
|
280
425
|
}
|
|
281
426
|
|
|
282
|
-
const pullResult = await
|
|
427
|
+
const pullResult = await serializedOnRepo(repoRoot, () =>
|
|
428
|
+
runGitCommand(repoRoot, ['pull', '--autostash', '--rebase', 'origin', targetBranch]),
|
|
429
|
+
);
|
|
283
430
|
|
|
284
431
|
if (pullResult.exitCode !== 0) {
|
|
285
432
|
return {
|
package/src/index.ts
CHANGED
|
@@ -37,7 +37,23 @@ export type { GitCommitResult, GitPullResult, GitPushResult, GitStatus } from '.
|
|
|
37
37
|
export { pageReferenceOf, pageTimestampsOf, selectPagesByPrefix } from './page-references.js';
|
|
38
38
|
// Reader backend interface types (browser-safe; type-only re-export).
|
|
39
39
|
export type { BackendReadResult, ContentReaderBackend } from './reader-backend.js';
|
|
40
|
-
|
|
40
|
+
// Content metadata shapes. Types only: the generated validators live on the
|
|
41
|
+
// `./schemas` subpath so this browser barrel stays free of the Typia
|
|
42
|
+
// transform's output (and of anything a consumer would tree-shake to avoid it).
|
|
43
|
+
export type {
|
|
44
|
+
DataEditable,
|
|
45
|
+
DataMeta,
|
|
46
|
+
FragmentEditable,
|
|
47
|
+
FragmentMeta,
|
|
48
|
+
LooseDataMeta,
|
|
49
|
+
LooseFragmentMeta,
|
|
50
|
+
LoosePageMeta,
|
|
51
|
+
MdxPageFrontmatter,
|
|
52
|
+
PageEditable,
|
|
53
|
+
PageMeta,
|
|
54
|
+
SchemaIssue,
|
|
55
|
+
SchemaParseResult,
|
|
56
|
+
} from './schemas/types.js';
|
|
41
57
|
export { Site } from './site.js';
|
|
42
58
|
export * from './types.js';
|
|
43
59
|
// SSRF host predicate — pure string/number logic, so any host's import guards can share it.
|
package/src/middleware.ts
CHANGED
|
@@ -560,6 +560,11 @@ export function createContentAPIRouter(api: ContentAPI, options?: ContentAPIRout
|
|
|
560
560
|
|
|
561
561
|
const result = await assetOps.moveAssets(filenames, sourceFolder, targetFolder);
|
|
562
562
|
|
|
563
|
+
// A partly finished move answers with the result itself, so the editor learns which
|
|
564
|
+
// assets stayed and can keep exactly those selected for a retry.
|
|
565
|
+
if (!result.success && result.moved) {
|
|
566
|
+
return c.json(result);
|
|
567
|
+
}
|
|
563
568
|
if (!result.success) {
|
|
564
569
|
return c.json(errorResponse(ErrorCodes.INVALID_REQUEST, result.error), 400);
|
|
565
570
|
}
|
package/src/schemas/index.ts
CHANGED
|
@@ -1,113 +1,39 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
// ===== Page Schemas =====
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Base schema for page metadata - editable fields only.
|
|
7
|
-
* Used by CMS forms for dynamic form generation.
|
|
8
|
-
* Add .describe() for form field labels/hints.
|
|
9
|
-
*/
|
|
10
|
-
export const pageEditableSchema = z.object({
|
|
11
|
-
title: z.string().describe('Page title for SEO and browser tab'),
|
|
12
|
-
description: z.string().optional().describe('Meta description for search engines'),
|
|
13
|
-
pathname: z.string().describe('URL path (e.g., /about)'),
|
|
14
|
-
publishAt: z.coerce.date().nullable().optional().describe('Schedule publish date/time'),
|
|
15
|
-
unpublishAt: z.coerce.date().nullable().optional().describe('Schedule unpublish date/time'),
|
|
16
|
-
robots: z.string().optional().describe('Robots meta tag (e.g., index, follow)'),
|
|
17
|
-
canonical: z.string().url().optional().describe('Canonical URL for duplicate content'),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export type PageEditable = z.infer<typeof pageEditableSchema>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Full page schema with system fields.
|
|
24
|
-
* Used by Astro Content Collections for validation.
|
|
25
|
-
*/
|
|
26
|
-
export const pageMetaSchema = pageEditableSchema.extend({
|
|
27
|
-
id: z.string(),
|
|
28
|
-
locale: z.string(),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
export type PageMeta = z.infer<typeof pageMetaSchema>;
|
|
32
|
-
|
|
33
|
-
// ===== Fragment Schemas =====
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Base schema for fragment metadata - editable fields only.
|
|
37
|
-
* Used by CMS forms for dynamic form generation.
|
|
38
|
-
* Add .describe() for form field labels/hints.
|
|
39
|
-
*/
|
|
40
|
-
export const fragmentEditableSchema = z.object({
|
|
41
|
-
title: z.string().describe('Display name for the fragment'),
|
|
42
|
-
description: z.string().optional().describe('Brief description of this fragment'),
|
|
43
|
-
category: z.string().optional().describe('Category for organizing fragments (e.g., headers, cta, content)'),
|
|
44
|
-
tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
export type FragmentEditable = z.infer<typeof fragmentEditableSchema>;
|
|
48
|
-
|
|
49
1
|
/**
|
|
50
|
-
*
|
|
51
|
-
* Used by Astro Content Collections for validation.
|
|
52
|
-
*/
|
|
53
|
-
export const fragmentMetaSchema = fragmentEditableSchema.extend({
|
|
54
|
-
id: z.string(),
|
|
55
|
-
locale: z.string(),
|
|
56
|
-
name: z.string(),
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export type FragmentMeta = z.infer<typeof fragmentMetaSchema>;
|
|
60
|
-
|
|
61
|
-
// ===== MDX Page Schemas =====
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Base schema for mdx-type page frontmatter — renderer-neutral.
|
|
2
|
+
* Content metadata shapes and their generated validators.
|
|
65
3
|
*
|
|
66
|
-
*
|
|
67
|
-
* `
|
|
68
|
-
*
|
|
4
|
+
* Zod is gone from this package: the shapes are plain TypeScript types
|
|
5
|
+
* (`./types`), the validators are Typia-generated (`./validators`), and the
|
|
6
|
+
* coercion/strip behaviour the Zod schemas performed implicitly is explicit and
|
|
7
|
+
* separately tested (`./normalize`).
|
|
69
8
|
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
9
|
+
* The `parseLoose*` variants are what `@conloca/astro-cms`'s loader validates
|
|
10
|
+
* collection entries with before they reach Astro's data store.
|
|
72
11
|
*/
|
|
73
|
-
export const mdxPageFrontmatterSchema = z
|
|
74
|
-
.object({
|
|
75
|
-
title: z.string().describe('Page title for SEO and browser tab'),
|
|
76
|
-
description: z.string().optional().describe('Meta description for search engines'),
|
|
77
|
-
draft: z.boolean().optional().describe('Hide the page from production builds'),
|
|
78
|
-
})
|
|
79
|
-
.passthrough();
|
|
80
|
-
|
|
81
|
-
export type MdxPageFrontmatter = z.infer<typeof mdxPageFrontmatterSchema>;
|
|
82
|
-
|
|
83
|
-
// ===== Data Schemas =====
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Base schema for data entry metadata - editable fields only.
|
|
87
|
-
* Used by CMS forms for dynamic form generation.
|
|
88
|
-
* Add .describe() for form field labels/hints.
|
|
89
|
-
*/
|
|
90
|
-
export const dataEditableSchema = z.object({
|
|
91
|
-
title: z.string().describe('Display name for the entry'),
|
|
92
|
-
description: z.string().optional().describe('Brief description of this entry'),
|
|
93
|
-
category: z.string().optional().describe('Category for organizing entries'),
|
|
94
|
-
tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
export type DataEditable = z.infer<typeof dataEditableSchema>;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Zod schema for data entry metadata.
|
|
101
|
-
* Covers common organizational fields for structured data collections.
|
|
102
|
-
*/
|
|
103
|
-
export const dataMetaSchema = dataEditableSchema.extend({
|
|
104
|
-
// System fields (added by loader)
|
|
105
|
-
id: z.string(),
|
|
106
|
-
locale: z.string(),
|
|
107
|
-
name: z.string(),
|
|
108
|
-
|
|
109
|
-
// Actual data content (arbitrary JSON structure)
|
|
110
|
-
data: z.record(z.string(), z.unknown()).optional(),
|
|
111
|
-
});
|
|
112
12
|
|
|
113
|
-
export type
|
|
13
|
+
export type {
|
|
14
|
+
DataEditable,
|
|
15
|
+
DataMeta,
|
|
16
|
+
FragmentEditable,
|
|
17
|
+
FragmentMeta,
|
|
18
|
+
LooseDataMeta,
|
|
19
|
+
LooseFragmentMeta,
|
|
20
|
+
LoosePageMeta,
|
|
21
|
+
MdxPageFrontmatter,
|
|
22
|
+
PageEditable,
|
|
23
|
+
PageMeta,
|
|
24
|
+
SchemaIssue,
|
|
25
|
+
SchemaParseResult,
|
|
26
|
+
} from './types.js';
|
|
27
|
+
export {
|
|
28
|
+
PAGE_META_KEYS,
|
|
29
|
+
parseDataEditable,
|
|
30
|
+
parseDataMeta,
|
|
31
|
+
parseFragmentEditable,
|
|
32
|
+
parseFragmentMeta,
|
|
33
|
+
parseLooseDataMeta,
|
|
34
|
+
parseLooseFragmentMeta,
|
|
35
|
+
parseLoosePageMeta,
|
|
36
|
+
parseMdxPageFrontmatter,
|
|
37
|
+
parsePageEditable,
|
|
38
|
+
parsePageMeta,
|
|
39
|
+
} from './validators.js';
|