@prunellejs/cms 0.0.0-stage → 0.5.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/src/shared.ts ADDED
@@ -0,0 +1,119 @@
1
+ // Shared helpers for @prunellejs/cms HTTP routes (full-page CMS and dev in-page editing API).
2
+
3
+ import type { ContentCollectionDescriptor, ContentCollectionsService } from 'prunelle/config'
4
+ import type { RequestContext } from 'prunelle/server'
5
+ import type { CmsField } from './schema'
6
+ import { formatIssuePath } from 'prunelle/server'
7
+ import { normalizeFieldErrorPath } from './form-data'
8
+ import { extractJsonSchema, parseJsonSchemaFields } from './schema'
9
+
10
+ export type CmsGuard = (ctx: RequestContext) => boolean | Response | Promise<boolean | Response>
11
+
12
+ export interface CmsHandlerConfig {
13
+ readonly basePath: string
14
+ readonly guard?: CmsGuard | undefined
15
+ }
16
+
17
+ export function htmlResponse(html: string, status = 200): Response {
18
+ return new Response(html, {
19
+ status,
20
+ headers: { 'content-type': 'text/html; charset=utf-8' },
21
+ })
22
+ }
23
+
24
+ export function textResponse(text: string, status: number): Response {
25
+ return new Response(text, {
26
+ status,
27
+ headers: { 'content-type': 'text/plain; charset=utf-8' },
28
+ })
29
+ }
30
+
31
+ export async function runGuard(guard: CmsGuard | undefined, ctx: RequestContext): Promise<Response | undefined> {
32
+ if (guard === undefined)
33
+ return undefined
34
+ const decision = await guard(ctx)
35
+ if (decision instanceof Response)
36
+ return decision
37
+ if (decision === false)
38
+ return textResponse('Forbidden', 403)
39
+ return undefined
40
+ }
41
+
42
+ export function wrapGuarded(
43
+ guard: CmsGuard | undefined,
44
+ handler: (ctx: RequestContext) => Response | Promise<Response>,
45
+ ): (ctx: RequestContext) => Promise<Response> {
46
+ return async (ctx) => {
47
+ const blocked = await runGuard(guard, ctx)
48
+ if (blocked !== undefined)
49
+ return blocked
50
+ return await handler(ctx)
51
+ }
52
+ }
53
+
54
+ export async function loadCollections(
55
+ service: ContentCollectionsService | undefined,
56
+ root: string,
57
+ ): Promise<readonly ContentCollectionDescriptor[]> {
58
+ if (service === undefined)
59
+ return []
60
+ return service.discover !== undefined
61
+ ? await service.discover(root)
62
+ : await service.list(root)
63
+ }
64
+
65
+ export function resolveSchemaFields(col: ContentCollectionDescriptor): {
66
+ fields?: readonly CmsField[] | undefined
67
+ warning?: string | undefined
68
+ } {
69
+ if (col.schema === undefined)
70
+ return {}
71
+ const jsonSchema = extractJsonSchema(col.schema)
72
+ if (jsonSchema === undefined) {
73
+ return {
74
+ warning: `Collection "${col.name}" schema does not expose JSON Schema output; editing raw YAML frontmatter (schema validation still runs on save).`,
75
+ }
76
+ }
77
+ const parsed = parseJsonSchemaFields(jsonSchema)
78
+ if (parsed.issues.length > 0) {
79
+ const details = parsed.issues.map(i => `${i.path}: ${i.message}`).join('; ')
80
+ return {
81
+ fields: parsed.fields,
82
+ warning: `Some schema fields are unsupported by the CMS: ${details}`,
83
+ }
84
+ }
85
+ return { fields: parsed.fields }
86
+ }
87
+
88
+ export function parseRawFrontmatterYaml(
89
+ rawYaml: string,
90
+ ): { ok: true, data: Record<string, unknown> } | { ok: false, message: string } {
91
+ if (rawYaml.trim().length === 0)
92
+ return { ok: true, data: {} }
93
+ let parsed: unknown
94
+ try {
95
+ parsed = Bun.YAML.parse(rawYaml)
96
+ }
97
+ catch (err) {
98
+ return { ok: false, message: err instanceof Error ? err.message : 'Invalid YAML frontmatter.' }
99
+ }
100
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed))
101
+ return { ok: false, message: 'Frontmatter YAML must be a mapping (key: value object).' }
102
+ return { ok: true, data: parsed as Record<string, unknown> }
103
+ }
104
+
105
+ export function collectValidationErrors(
106
+ issues: ReadonlyArray<{
107
+ readonly message: string
108
+ readonly path?: ReadonlyArray<PropertyKey | { readonly key: PropertyKey }> | undefined
109
+ }>,
110
+ ): Record<string, string> {
111
+ const errors: Record<string, string> = {}
112
+ for (const issue of issues) {
113
+ const rawPath = formatIssuePath(issue)
114
+ const key = normalizeFieldErrorPath(rawPath)
115
+ if (!Object.hasOwn(errors, key))
116
+ errors[key] = issue.message
117
+ }
118
+ return errors
119
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.