@conloca/cms-spa 0.2.0 → 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/spa/main.css +17 -3
- package/dist/ts/App.d.ts +5 -1
- package/dist/ts/App.d.ts.map +1 -1
- package/dist/ts/App.js +7 -2
- package/dist/ts/App.js.map +1 -1
- package/dist/ts/components/CMSDashboard.d.ts.map +1 -1
- package/dist/ts/components/CMSDashboard.js +6 -2
- package/dist/ts/components/CMSDashboard.js.map +1 -1
- package/dist/ts/components/RouteErrorPage.d.ts +11 -0
- package/dist/ts/components/RouteErrorPage.d.ts.map +1 -0
- package/dist/ts/components/RouteErrorPage.js +44 -0
- package/dist/ts/components/RouteErrorPage.js.map +1 -0
- package/dist/ts/components/cards/DataEntryCard.d.ts +1 -1
- package/dist/ts/components/cards/DataEntryCard.d.ts.map +1 -1
- package/dist/ts/components/cards/SectionCard.d.ts +1 -1
- package/dist/ts/components/cards/SectionCard.d.ts.map +1 -1
- package/dist/ts/components/dashboard/RecentActivity.d.ts.map +1 -1
- package/dist/ts/components/dashboard/RecentActivity.js +2 -8
- package/dist/ts/components/dashboard/RecentActivity.js.map +1 -1
- package/dist/ts/components/dialogs/CreateDataDialog.js +2 -2
- package/dist/ts/components/dialogs/CreateDataDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js +2 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts +13 -0
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts.map +1 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js +100 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js.map +1 -0
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/DataPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts +2 -2
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/MoveFolderDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/MoveFolderDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts +2 -12
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.js +3 -21
- package/dist/ts/components/dialogs/PageMetadataDialog.js.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.d.ts +2 -0
- package/dist/ts/components/editor/DrawerItemOverride.d.ts.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.js +4 -3
- package/dist/ts/components/editor/DrawerItemOverride.js.map +1 -1
- package/dist/ts/components/editor/GenericBlock.js +2 -2
- package/dist/ts/components/editor/GenericBlock.js.map +1 -1
- package/dist/ts/components/editor/LocaleSelector.d.ts +1 -1
- package/dist/ts/components/editor/LocaleSelector.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditor.d.ts +7 -8
- package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditor.js +78 -17
- package/dist/ts/components/editor/PageEditor.js.map +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.d.ts +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.js +17 -7
- package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
- package/dist/ts/components/editor/draft-media-config.d.ts +4 -0
- package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -0
- package/dist/ts/components/editor/draft-media-config.js +95 -0
- package/dist/ts/components/editor/draft-media-config.js.map +1 -0
- package/dist/ts/components/editor/page-setting-targets.d.ts +1 -1
- package/dist/ts/components/editor/page-setting-targets.d.ts.map +1 -1
- package/dist/ts/components/editors/DataEditor.d.ts +3 -3
- package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
- package/dist/ts/components/forms/HintForm.js +8 -5
- package/dist/ts/components/forms/HintForm.js.map +1 -1
- package/dist/ts/components/forms/SchemaForm.d.ts +17 -5
- package/dist/ts/components/forms/SchemaForm.d.ts.map +1 -1
- package/dist/ts/components/forms/SchemaForm.js +16 -9
- package/dist/ts/components/forms/SchemaForm.js.map +1 -1
- package/dist/ts/components/media/AssetCard.d.ts +1 -1
- package/dist/ts/components/media/AssetCard.d.ts.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.d.ts +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.js +4 -1
- package/dist/ts/components/media/FolderTreeSidebar.js.map +1 -1
- package/dist/ts/components/media/MediaLibrary.d.ts +1 -1
- package/dist/ts/components/media/MediaLibrary.d.ts.map +1 -1
- package/dist/ts/components/media/MediaToolbar.d.ts +1 -1
- package/dist/ts/components/media/MediaToolbar.d.ts.map +1 -1
- package/dist/ts/components/pages/DataList.d.ts.map +1 -1
- package/dist/ts/components/pages/DataList.js +6 -4
- package/dist/ts/components/pages/DataList.js.map +1 -1
- package/dist/ts/components/pages/PageList.d.ts.map +1 -1
- package/dist/ts/components/pages/PageList.js +4 -96
- package/dist/ts/components/pages/PageList.js.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.d.ts +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.d.ts.map +1 -1
- package/dist/ts/components/ui/IconButton.d.ts +1 -1
- package/dist/ts/components/ui/IconButton.d.ts.map +1 -1
- package/dist/ts/content-form-fields.d.ts +41 -0
- package/dist/ts/content-form-fields.d.ts.map +1 -0
- package/dist/ts/content-form-fields.js +70 -0
- package/dist/ts/content-form-fields.js.map +1 -0
- package/dist/ts/data-schemas.d.ts +4 -3
- package/dist/ts/data-schemas.d.ts.map +1 -1
- package/dist/ts/data-schemas.js.map +1 -1
- package/dist/ts/host-discovery.d.ts.map +1 -1
- package/dist/ts/host-schema.d.ts +73 -0
- package/dist/ts/host-schema.d.ts.map +1 -0
- package/dist/ts/host-schema.js +66 -0
- package/dist/ts/host-schema.js.map +1 -0
- package/dist/ts/host-style-probe.d.ts.map +1 -1
- package/dist/ts/host-style-probe.js +16 -0
- package/dist/ts/host-style-probe.js.map +1 -1
- package/dist/ts/main-spa.d.ts.map +1 -1
- package/dist/ts/main-spa.js +2 -3
- package/dist/ts/main-spa.js.map +1 -1
- package/dist/ts/page-schema-define.d.ts +14 -12
- package/dist/ts/page-schema-define.d.ts.map +1 -1
- package/dist/ts/page-schema-define.js +8 -5
- package/dist/ts/page-schema-define.js.map +1 -1
- package/dist/ts/page-schemas.d.ts.map +1 -1
- package/dist/ts/puck-config.d.ts.map +1 -1
- package/dist/ts/selected-block.d.ts.map +1 -1
- package/dist/ts/site-styles.d.ts +3 -2
- package/dist/ts/site-styles.d.ts.map +1 -1
- package/dist/ts/site-styles.js +6 -4
- package/dist/ts/site-styles.js.map +1 -1
- package/dist/ts/ui-config.d.ts +13 -4
- package/dist/ts/ui-config.d.ts.map +1 -1
- package/dist/ts/ui-config.js +9 -0
- package/dist/ts/ui-config.js.map +1 -1
- package/dist/ts/utils/asset-url.d.ts +2 -0
- package/dist/ts/utils/asset-url.d.ts.map +1 -1
- package/dist/ts/utils/asset-url.js +7 -0
- package/dist/ts/utils/asset-url.js.map +1 -1
- package/dist/ts/utils/pageMetadata.d.ts.map +1 -1
- package/dist/ts/utils/pageMetadata.js +27 -10
- package/dist/ts/utils/pageMetadata.js.map +1 -1
- package/dist/ts/utils/schema-fields.d.ts +23 -0
- package/dist/ts/utils/schema-fields.d.ts.map +1 -0
- package/dist/ts/utils/schema-fields.js +82 -0
- package/dist/ts/utils/schema-fields.js.map +1 -0
- package/package.json +41 -13
- package/src/App.tsx +13 -4
- package/src/components/CMSDashboard.tsx +12 -4
- package/src/components/RouteErrorPage.tsx +75 -0
- package/src/components/dashboard/RecentActivity.tsx +3 -7
- package/src/components/dialogs/CreateDataDialog.tsx +2 -2
- package/src/components/dialogs/CreateFolderDialog.tsx +2 -1
- package/src/components/dialogs/CreatePageFlow.tsx +117 -0
- package/src/components/dialogs/DataPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/FragmentPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/PageMetadataDialog.tsx +5 -28
- package/src/components/editor/DrawerItemOverride.tsx +14 -8
- package/src/components/editor/GenericBlock.tsx +2 -2
- package/src/components/editor/PageEditor.tsx +90 -19
- package/src/components/editor/PageEditorHeaderActions.tsx +1 -1
- package/src/components/editor/PageEditorWrapper.tsx +17 -7
- package/src/components/editor/draft-media-config.ts +95 -0
- package/src/components/editors/DataEditor.tsx +2 -2
- package/src/components/forms/HintForm.tsx +7 -5
- package/src/components/forms/SchemaForm.tsx +41 -12
- package/src/components/media/FolderTreeSidebar.tsx +3 -1
- package/src/components/pages/DataList.tsx +6 -4
- package/src/components/pages/PageList.tsx +6 -116
- package/src/content-form-fields.ts +79 -0
- package/src/data-schemas.ts +4 -3
- package/src/host-schema.ts +122 -0
- package/src/host-style-probe.ts +16 -0
- package/src/main-spa.tsx +2 -3
- package/src/main.css +10 -7
- package/src/page-schema-define.ts +29 -17
- package/src/site-styles.ts +6 -4
- package/src/ui-config.ts +16 -4
- package/src/utils/asset-url.ts +8 -0
- package/src/utils/pageMetadata.ts +27 -10
- package/src/utils/schema-fields.ts +106 -0
- package/dist/ts/utils/zodIntrospect.d.ts +0 -19
- package/dist/ts/utils/zodIntrospect.d.ts.map +0 -1
- package/dist/ts/utils/zodIntrospect.js +0 -95
- package/dist/ts/utils/zodIntrospect.js.map +0 -1
- package/src/utils/zodIntrospect.ts +0 -136
package/src/utils/asset-url.ts
CHANGED
|
@@ -11,6 +11,14 @@ export function buildAssetPath(folder: string | undefined, filename: string): st
|
|
|
11
11
|
return assetPath(folder, filename);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/** Join asset paths without moving an existing query or fragment into the resource path. */
|
|
15
|
+
export function appendAssetUrlPath(base: string, path: string): string {
|
|
16
|
+
const suffixIndex = base.search(/[?#]/);
|
|
17
|
+
const pathname = suffixIndex === -1 ? base : base.slice(0, suffixIndex);
|
|
18
|
+
const suffix = suffixIndex === -1 ? '' : base.slice(suffixIndex);
|
|
19
|
+
return `${pathname.replace(/\/$/, '')}/${path}${suffix}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
/** Build a full asset serve URL, correctly handling subfolder paths. */
|
|
15
23
|
export function buildAssetServeUrl(basePath: string, folder: string | undefined, filename: string): string {
|
|
16
24
|
return `${basePath}/${assetPath(folder, filename)}`;
|
|
@@ -102,7 +102,7 @@ export function extractPageMetadata(
|
|
|
102
102
|
const customMeta: Record<string, unknown> = {};
|
|
103
103
|
for (const [key, value] of Object.entries(localized.meta)) {
|
|
104
104
|
if (!coreKeys.has(key)) {
|
|
105
|
-
customMeta
|
|
105
|
+
setDataKey(customMeta, key, value);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
|
|
@@ -144,7 +144,7 @@ export function buildMetadataUpdate(metadata: PageMetadata): MetadataUpdatePaylo
|
|
|
144
144
|
const cleared = (value: unknown) => (value === undefined || value === '' ? null : value);
|
|
145
145
|
const custom: Record<string, unknown> = {};
|
|
146
146
|
for (const [key, value] of Object.entries(metadata.customMeta || {})) {
|
|
147
|
-
custom
|
|
147
|
+
setDataKey(custom, key, cleared(value));
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
return {
|
|
@@ -200,8 +200,10 @@ export function flattenForHints(
|
|
|
200
200
|
const segments = dotted.split('.');
|
|
201
201
|
let cursor: unknown = value;
|
|
202
202
|
for (const seg of segments) {
|
|
203
|
-
|
|
204
|
-
|
|
203
|
+
// Own properties only: an inherited one is not the author's data.
|
|
204
|
+
const own = cursor && typeof cursor === 'object' ? Object.getOwnPropertyDescriptor(cursor, seg) : undefined;
|
|
205
|
+
if (own) {
|
|
206
|
+
cursor = own.value;
|
|
205
207
|
} else {
|
|
206
208
|
cursor = undefined;
|
|
207
209
|
break;
|
|
@@ -217,7 +219,7 @@ export function flattenForHints(
|
|
|
217
219
|
if (remainder === undefined) {
|
|
218
220
|
delete out[segments[0]];
|
|
219
221
|
} else {
|
|
220
|
-
out
|
|
222
|
+
setDataKey(out, segments[0], remainder);
|
|
221
223
|
}
|
|
222
224
|
}
|
|
223
225
|
}
|
|
@@ -242,13 +244,25 @@ function pruneLeaf(branch: unknown, path: string[]): unknown {
|
|
|
242
244
|
if (pruned === undefined) {
|
|
243
245
|
delete copy[head];
|
|
244
246
|
} else {
|
|
245
|
-
copy
|
|
247
|
+
setDataKey(copy, head, pruned);
|
|
246
248
|
}
|
|
247
249
|
}
|
|
248
250
|
|
|
249
251
|
return Object.keys(copy).length > 0 ? copy : undefined;
|
|
250
252
|
}
|
|
251
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Store a frontmatter key as data. Plain assignment of `__proto__` would run the legacy prototype setter,
|
|
256
|
+
* swapping the target's prototype and dropping the author's value (same rule as content-api's applyNullClearPatch).
|
|
257
|
+
*/
|
|
258
|
+
function setDataKey(target: Record<string, unknown>, key: string, value: unknown): void {
|
|
259
|
+
if (key === '__proto__') {
|
|
260
|
+
Object.defineProperty(target, key, { value, enumerable: true, configurable: true, writable: true });
|
|
261
|
+
} else {
|
|
262
|
+
target[key] = value;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
252
266
|
/**
|
|
253
267
|
* Inverse of `flattenForHints` — materialize dotted keys back into nested
|
|
254
268
|
* objects so YAML frontmatter renders as `sidebar: { label, order }`
|
|
@@ -264,22 +278,25 @@ export function unflattenFromHints(value: Record<string, unknown>): Record<strin
|
|
|
264
278
|
// remainder) has to be in place before dotted leaves merge into it,
|
|
265
279
|
// otherwise assigning it later would overwrite them.
|
|
266
280
|
for (const [key, val] of entries) {
|
|
267
|
-
if (!key.includes('.')) out
|
|
281
|
+
if (!key.includes('.')) setDataKey(out, key, val);
|
|
268
282
|
}
|
|
269
283
|
|
|
270
284
|
for (const [key, val] of entries) {
|
|
271
285
|
if (!key.includes('.')) continue;
|
|
272
286
|
const segments = key.split('.');
|
|
287
|
+
// Walk own properties only: an inherited one (`__proto__` resolves to
|
|
288
|
+
// Object.prototype) would make the leaf write below pollute every object.
|
|
289
|
+
// Names like `prototype` stay ordinary nested keys, so they still round-trip.
|
|
273
290
|
let cursor = out;
|
|
274
291
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
275
292
|
const seg = segments[i];
|
|
276
|
-
const existing = cursor
|
|
293
|
+
const existing = Object.getOwnPropertyDescriptor(cursor, seg)?.value;
|
|
277
294
|
if (!existing || typeof existing !== 'object') {
|
|
278
|
-
cursor
|
|
295
|
+
setDataKey(cursor, seg, {});
|
|
279
296
|
}
|
|
280
297
|
cursor = cursor[seg] as Record<string, unknown>;
|
|
281
298
|
}
|
|
282
|
-
cursor
|
|
299
|
+
setDataKey(cursor, segments[segments.length - 1], val);
|
|
283
300
|
}
|
|
284
301
|
return out;
|
|
285
302
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type HostSchema, type HostSchemaMetadata, resolveHostJsonSchema, rootObjectOf } from '../host-schema.js';
|
|
2
|
+
|
|
3
|
+
export interface FieldInfo {
|
|
4
|
+
type: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'enum';
|
|
5
|
+
format?: 'url' | 'email' | 'date-time';
|
|
6
|
+
description?: string;
|
|
7
|
+
isOptional: boolean;
|
|
8
|
+
isNullable: boolean;
|
|
9
|
+
/** For enum types, the list of allowed values */
|
|
10
|
+
enumValues?: string[];
|
|
11
|
+
/** For string types, the max length constraint if defined */
|
|
12
|
+
maxLength?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A resolved form definition: one {@link FieldInfo} per field, in render order. */
|
|
16
|
+
export type FormFields = Record<string, FieldInfo>;
|
|
17
|
+
|
|
18
|
+
type JsonSchema = HostSchemaMetadata['schema'];
|
|
19
|
+
|
|
20
|
+
/** The control-relevant part of a field schema, after `$ref`s and `| null` are unwrapped. */
|
|
21
|
+
function describeField(
|
|
22
|
+
metadata: HostSchemaMetadata,
|
|
23
|
+
node: JsonSchema,
|
|
24
|
+
): Omit<FieldInfo, 'isOptional' | 'isNullable'> & {
|
|
25
|
+
isNullable: boolean;
|
|
26
|
+
} {
|
|
27
|
+
const resolved = resolveHostJsonSchema(metadata, node);
|
|
28
|
+
const description = node.description ?? resolved?.description;
|
|
29
|
+
const base = description === undefined ? {} : { description };
|
|
30
|
+
if (!resolved) return { type: 'string', ...base, isNullable: false };
|
|
31
|
+
|
|
32
|
+
if ('oneOf' in resolved) {
|
|
33
|
+
const members = resolved.oneOf.map((member) => resolveHostJsonSchema(metadata, member));
|
|
34
|
+
const isNullable = members.some((member) => member !== null && 'type' in member && member.type === 'null');
|
|
35
|
+
const rest = members.filter((member) => !(member !== null && 'type' in member && member.type === 'null'));
|
|
36
|
+
const constants = rest.flatMap((member) =>
|
|
37
|
+
member !== null && 'const' in member && typeof member.const === 'string' ? [member.const] : [],
|
|
38
|
+
);
|
|
39
|
+
if (rest.length > 0 && constants.length === rest.length) {
|
|
40
|
+
return { type: 'enum', enumValues: constants, ...base, isNullable };
|
|
41
|
+
}
|
|
42
|
+
const [only] = rest;
|
|
43
|
+
if (rest.length === 1 && only) return { ...describeField(metadata, only), ...base, isNullable };
|
|
44
|
+
// A union of unrelated shapes (`string | boolean`) has no single control;
|
|
45
|
+
// the text input is the same fallback an unknown field always got.
|
|
46
|
+
return { type: 'string', ...base, isNullable };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if ('const' in resolved) {
|
|
50
|
+
return typeof resolved.const === 'string'
|
|
51
|
+
? { type: 'enum', enumValues: [resolved.const], ...base, isNullable: false }
|
|
52
|
+
: { type: 'string', ...base, isNullable: false };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!('type' in resolved)) return { type: 'string', ...base, isNullable: false };
|
|
56
|
+
switch (resolved.type) {
|
|
57
|
+
case 'string': {
|
|
58
|
+
if (resolved.format === 'date-time') return { type: 'date', format: 'date-time', ...base, isNullable: false };
|
|
59
|
+
// A format and a length limit are independent constraints; the form keeps both.
|
|
60
|
+
const limit = resolved.maxLength === undefined ? {} : { maxLength: resolved.maxLength };
|
|
61
|
+
if (resolved.format === 'url' || resolved.format === 'uri') {
|
|
62
|
+
return { type: 'string', format: 'url', ...base, ...limit, isNullable: false };
|
|
63
|
+
}
|
|
64
|
+
if (resolved.format === 'email') return { type: 'string', format: 'email', ...base, ...limit, isNullable: false };
|
|
65
|
+
return { type: 'string', ...base, ...limit, isNullable: false };
|
|
66
|
+
}
|
|
67
|
+
case 'integer':
|
|
68
|
+
case 'number':
|
|
69
|
+
return { type: 'number', ...base, isNullable: false };
|
|
70
|
+
case 'boolean':
|
|
71
|
+
return { type: 'boolean', ...base, isNullable: false };
|
|
72
|
+
case 'array':
|
|
73
|
+
return { type: 'array', ...base, isNullable: false };
|
|
74
|
+
case 'null':
|
|
75
|
+
return { type: 'string', ...base, isNullable: true };
|
|
76
|
+
default:
|
|
77
|
+
return { type: 'string', ...base, isNullable: false };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Metadata is generated once and never mutated, so the derived form is too:
|
|
82
|
+
// resolving it once keeps every render handing out the same record.
|
|
83
|
+
const fieldsByMetadata = new WeakMap<HostSchemaMetadata, FormFields>();
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The form generated JSON Schema metadata (`typia.json.schema<T>()`) renders
|
|
87
|
+
* as: one {@link FieldInfo} per top-level property, in declaration order,
|
|
88
|
+
* optional when the property is not `required`.
|
|
89
|
+
*/
|
|
90
|
+
export function fieldsOfMetadata(metadata: HostSchemaMetadata): FormFields {
|
|
91
|
+
const cached = fieldsByMetadata.get(metadata);
|
|
92
|
+
if (cached) return cached;
|
|
93
|
+
const root = rootObjectOf(metadata);
|
|
94
|
+
const required = new Set(root?.required ?? []);
|
|
95
|
+
const fields: FormFields = {};
|
|
96
|
+
for (const [name, node] of Object.entries(root?.properties ?? {})) {
|
|
97
|
+
fields[name] = { ...describeField(metadata, node), isOptional: !required.has(name) };
|
|
98
|
+
}
|
|
99
|
+
fieldsByMetadata.set(metadata, fields);
|
|
100
|
+
return fields;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The form a {@link HostSchema} renders as — its metadata's {@link fieldsOfMetadata}. */
|
|
104
|
+
export function fieldsOfHostSchema(schema: HostSchema): FormFields {
|
|
105
|
+
return fieldsOfMetadata(schema.metadata);
|
|
106
|
+
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { z } from 'zod';
|
|
2
|
-
export interface FieldInfo {
|
|
3
|
-
type: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'enum';
|
|
4
|
-
format?: 'url' | 'email' | 'date-time';
|
|
5
|
-
/** Help text the schema gives the field, from .describe() or .meta({ description }). */
|
|
6
|
-
description?: string;
|
|
7
|
-
isOptional: boolean;
|
|
8
|
-
isNullable: boolean;
|
|
9
|
-
enumValues?: string[];
|
|
10
|
-
maxLength?: number;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Introspects the subset of Zod 3/4 internals required to render form hints.
|
|
14
|
-
* The single assertion is the compatibility boundary; every field read after
|
|
15
|
-
* it is represented explicitly by ZodCompat/ZodDef.
|
|
16
|
-
*/
|
|
17
|
-
export declare function getZodFieldInfo(field: z.ZodRawShape[string]): FieldInfo;
|
|
18
|
-
export declare function getZodShape(schema: z.ZodObject<z.ZodRawShape>): Record<string, FieldInfo>;
|
|
19
|
-
//# sourceMappingURL=zodIntrospect.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zodIntrospect.d.ts","sourceRoot":"","sources":["../../../src/utils/zodIntrospect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAClE,MAAM,CAAC,EAAE,KAAK,GAAG,OAAO,GAAG,WAAW,CAAC;IACvC,wFAAwF;IACxF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA6CD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAoEvE;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAIzF"}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
function definition(schema) {
|
|
2
|
-
return schema._def ?? schema._zod?.def;
|
|
3
|
-
}
|
|
4
|
-
function getZodTypeName(def) {
|
|
5
|
-
if (typeof def.typeName === 'string')
|
|
6
|
-
return def.typeName;
|
|
7
|
-
return typeof def.type === 'string' ? def.type : '';
|
|
8
|
-
}
|
|
9
|
-
function readStringCheck(def) {
|
|
10
|
-
const format = def.check === 'string_format' ? def.format : def.kind;
|
|
11
|
-
if (format === 'url' || format === 'email')
|
|
12
|
-
return { format };
|
|
13
|
-
if (def.check === 'max_length' && typeof def.maximum === 'number')
|
|
14
|
-
return { maxLength: def.maximum };
|
|
15
|
-
if (def.kind === 'max' && typeof def.value === 'number')
|
|
16
|
-
return { maxLength: def.value };
|
|
17
|
-
return {};
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Introspects the subset of Zod 3/4 internals required to render form hints.
|
|
21
|
-
* The single assertion is the compatibility boundary; every field read after
|
|
22
|
-
* it is represented explicitly by ZodCompat/ZodDef.
|
|
23
|
-
*/
|
|
24
|
-
export function getZodFieldInfo(field) {
|
|
25
|
-
// SAFETY: both supported Zod generations expose either _def or _zod.def;
|
|
26
|
-
// ZodCompat contains only optional fields read by this compatibility adapter.
|
|
27
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- documented third-party compatibility boundary
|
|
28
|
-
let current = field;
|
|
29
|
-
let isOptional = false;
|
|
30
|
-
let isNullable = false;
|
|
31
|
-
let helpText;
|
|
32
|
-
while (true) {
|
|
33
|
-
helpText ??= current.description;
|
|
34
|
-
const def = definition(current);
|
|
35
|
-
if (!def)
|
|
36
|
-
break;
|
|
37
|
-
const typeName = getZodTypeName(def);
|
|
38
|
-
if (typeName === 'ZodOptional' || typeName === 'optional') {
|
|
39
|
-
isOptional = true;
|
|
40
|
-
}
|
|
41
|
-
else if (typeName === 'ZodNullable' || typeName === 'nullable') {
|
|
42
|
-
isNullable = true;
|
|
43
|
-
}
|
|
44
|
-
else if (typeName !== 'ZodDefault' && typeName !== 'default') {
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
if (!def.innerType)
|
|
48
|
-
break;
|
|
49
|
-
current = def.innerType;
|
|
50
|
-
}
|
|
51
|
-
const def = definition(current);
|
|
52
|
-
const typeName = def ? getZodTypeName(def) : '';
|
|
53
|
-
if (typeName === 'ZodEnum' || typeName === 'enum') {
|
|
54
|
-
const enumValues = Array.isArray(def?.values)
|
|
55
|
-
? def.values.filter((value) => typeof value === 'string')
|
|
56
|
-
: (current.options ?? []);
|
|
57
|
-
return { type: 'enum', enumValues, description: helpText, isOptional, isNullable };
|
|
58
|
-
}
|
|
59
|
-
if (typeName === 'ZodNativeEnum' || typeName === 'nativeEnum') {
|
|
60
|
-
const values = def?.values;
|
|
61
|
-
const enumValues = values && !Array.isArray(values)
|
|
62
|
-
? Object.values(values).filter((value) => typeof value === 'string')
|
|
63
|
-
: [];
|
|
64
|
-
return { type: 'enum', enumValues, description: helpText, isOptional, isNullable };
|
|
65
|
-
}
|
|
66
|
-
if (typeName === 'ZodDate' || typeName === 'date') {
|
|
67
|
-
return { type: 'date', format: 'date-time', description: helpText, isOptional, isNullable };
|
|
68
|
-
}
|
|
69
|
-
if (typeName === 'ZodBoolean' || typeName === 'boolean') {
|
|
70
|
-
return { type: 'boolean', description: helpText, isOptional, isNullable };
|
|
71
|
-
}
|
|
72
|
-
if (typeName === 'ZodNumber' || typeName === 'number') {
|
|
73
|
-
return { type: 'number', description: helpText, isOptional, isNullable };
|
|
74
|
-
}
|
|
75
|
-
if (typeName === 'ZodArray' || typeName === 'array') {
|
|
76
|
-
return { type: 'array', description: helpText, isOptional, isNullable };
|
|
77
|
-
}
|
|
78
|
-
if (typeName === 'ZodString' || typeName === 'string') {
|
|
79
|
-
const checks = def?.checks ?? [];
|
|
80
|
-
const constraints = [def, ...checks.map((check) => check._zod?.def ?? check)]
|
|
81
|
-
.filter((check) => check !== undefined)
|
|
82
|
-
.map(readStringCheck);
|
|
83
|
-
const format = constraints.find((constraint) => constraint.format)?.format;
|
|
84
|
-
const maxLength = constraints.find((constraint) => constraint.maxLength !== undefined)?.maxLength;
|
|
85
|
-
return { type: 'string', format, description: helpText, isOptional, isNullable, maxLength };
|
|
86
|
-
}
|
|
87
|
-
return { type: 'string', description: helpText, isOptional, isNullable };
|
|
88
|
-
}
|
|
89
|
-
export function getZodShape(schema) {
|
|
90
|
-
const result = {};
|
|
91
|
-
for (const [key, field] of Object.entries(schema.shape))
|
|
92
|
-
result[key] = getZodFieldInfo(field);
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
//# sourceMappingURL=zodIntrospect.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zodIntrospect.js","sourceRoot":"","sources":["../../../src/utils/zodIntrospect.ts"],"names":[],"mappings":"AAuCA,SAAS,UAAU,CAAC,MAAiB;IACnC,OAAO,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC;AACzC,CAAC;AAED,SAAS,cAAc,CAAC,GAAW;IACjC,IAAI,OAAO,GAAG,CAAC,QAAQ,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC,QAAQ,CAAC;IAC1D,OAAO,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;AACtD,CAAC;AAED,SAAS,eAAe,CAAC,GAAW;IAClC,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,KAAK,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;IACrE,IAAI,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,OAAO;QAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IAC9D,IAAI,GAAG,CAAC,KAAK,KAAK,YAAY,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ;QAAE,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;IACrG,IAAI,GAAG,CAAC,IAAI,KAAK,KAAK,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;QAAE,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC;IACzF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C;IAC7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;QAAE,MAAM,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9F,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import type { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
export interface FieldInfo {
|
|
4
|
-
type: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'enum';
|
|
5
|
-
format?: 'url' | 'email' | 'date-time';
|
|
6
|
-
/** Help text the schema gives the field, from .describe() or .meta({ description }). */
|
|
7
|
-
description?: string;
|
|
8
|
-
isOptional: boolean;
|
|
9
|
-
isNullable: boolean;
|
|
10
|
-
enumValues?: string[];
|
|
11
|
-
maxLength?: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/* oxlint-disable no-underscore-dangle -- Zod 3/4 expose these compatibility internals with underscore-prefixed names. */
|
|
15
|
-
|
|
16
|
-
interface ZodCompat {
|
|
17
|
-
_def?: ZodDef;
|
|
18
|
-
_zod?: { def?: ZodDef };
|
|
19
|
-
description?: string;
|
|
20
|
-
options?: string[];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface ZodDef {
|
|
24
|
-
typeName?: string;
|
|
25
|
-
type?: string;
|
|
26
|
-
check?: string;
|
|
27
|
-
format?: string;
|
|
28
|
-
kind?: string;
|
|
29
|
-
maximum?: number;
|
|
30
|
-
value?: number;
|
|
31
|
-
innerType?: ZodCompat;
|
|
32
|
-
values?: string[] | Record<string, string | number>;
|
|
33
|
-
checks?: ZodCheck[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface ZodCheck extends ZodDef {
|
|
37
|
-
_zod?: { def?: ZodDef };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function definition(schema: ZodCompat): ZodDef | undefined {
|
|
41
|
-
return schema._def ?? schema._zod?.def;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function getZodTypeName(def: ZodDef): string {
|
|
45
|
-
if (typeof def.typeName === 'string') return def.typeName;
|
|
46
|
-
return typeof def.type === 'string' ? def.type : '';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function readStringCheck(def: ZodDef): Pick<FieldInfo, 'format' | 'maxLength'> {
|
|
50
|
-
const format = def.check === 'string_format' ? def.format : def.kind;
|
|
51
|
-
if (format === 'url' || format === 'email') return { format };
|
|
52
|
-
if (def.check === 'max_length' && typeof def.maximum === 'number') return { maxLength: def.maximum };
|
|
53
|
-
if (def.kind === 'max' && typeof def.value === 'number') return { maxLength: def.value };
|
|
54
|
-
return {};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Introspects the subset of Zod 3/4 internals required to render form hints.
|
|
59
|
-
* The single assertion is the compatibility boundary; every field read after
|
|
60
|
-
* it is represented explicitly by ZodCompat/ZodDef.
|
|
61
|
-
*/
|
|
62
|
-
export function getZodFieldInfo(field: z.ZodRawShape[string]): FieldInfo {
|
|
63
|
-
// SAFETY: both supported Zod generations expose either _def or _zod.def;
|
|
64
|
-
// ZodCompat contains only optional fields read by this compatibility adapter.
|
|
65
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- documented third-party compatibility boundary
|
|
66
|
-
let current = field as ZodCompat;
|
|
67
|
-
let isOptional = false;
|
|
68
|
-
let isNullable = false;
|
|
69
|
-
let helpText: string | undefined;
|
|
70
|
-
|
|
71
|
-
while (true) {
|
|
72
|
-
helpText ??= current.description;
|
|
73
|
-
const def = definition(current);
|
|
74
|
-
if (!def) break;
|
|
75
|
-
const typeName = getZodTypeName(def);
|
|
76
|
-
|
|
77
|
-
if (typeName === 'ZodOptional' || typeName === 'optional') {
|
|
78
|
-
isOptional = true;
|
|
79
|
-
} else if (typeName === 'ZodNullable' || typeName === 'nullable') {
|
|
80
|
-
isNullable = true;
|
|
81
|
-
} else if (typeName !== 'ZodDefault' && typeName !== 'default') {
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!def.innerType) break;
|
|
86
|
-
current = def.innerType;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const def = definition(current);
|
|
90
|
-
const typeName = def ? getZodTypeName(def) : '';
|
|
91
|
-
|
|
92
|
-
if (typeName === 'ZodEnum' || typeName === 'enum') {
|
|
93
|
-
const enumValues = Array.isArray(def?.values)
|
|
94
|
-
? def.values.filter((value): value is string => typeof value === 'string')
|
|
95
|
-
: (current.options ?? []);
|
|
96
|
-
return { type: 'enum', enumValues, description: helpText, isOptional, isNullable };
|
|
97
|
-
}
|
|
98
|
-
if (typeName === 'ZodNativeEnum' || typeName === 'nativeEnum') {
|
|
99
|
-
const values = def?.values;
|
|
100
|
-
const enumValues =
|
|
101
|
-
values && !Array.isArray(values)
|
|
102
|
-
? Object.values(values).filter((value): value is string => typeof value === 'string')
|
|
103
|
-
: [];
|
|
104
|
-
return { type: 'enum', enumValues, description: helpText, isOptional, isNullable };
|
|
105
|
-
}
|
|
106
|
-
if (typeName === 'ZodDate' || typeName === 'date') {
|
|
107
|
-
return { type: 'date', format: 'date-time', description: helpText, isOptional, isNullable };
|
|
108
|
-
}
|
|
109
|
-
if (typeName === 'ZodBoolean' || typeName === 'boolean') {
|
|
110
|
-
return { type: 'boolean', description: helpText, isOptional, isNullable };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (typeName === 'ZodNumber' || typeName === 'number') {
|
|
114
|
-
return { type: 'number', description: helpText, isOptional, isNullable };
|
|
115
|
-
}
|
|
116
|
-
if (typeName === 'ZodArray' || typeName === 'array') {
|
|
117
|
-
return { type: 'array', description: helpText, isOptional, isNullable };
|
|
118
|
-
}
|
|
119
|
-
if (typeName === 'ZodString' || typeName === 'string') {
|
|
120
|
-
const checks = def?.checks ?? [];
|
|
121
|
-
const constraints = [def, ...checks.map((check) => check._zod?.def ?? check)]
|
|
122
|
-
.filter((check): check is ZodDef => check !== undefined)
|
|
123
|
-
.map(readStringCheck);
|
|
124
|
-
const format = constraints.find((constraint) => constraint.format)?.format;
|
|
125
|
-
const maxLength = constraints.find((constraint) => constraint.maxLength !== undefined)?.maxLength;
|
|
126
|
-
return { type: 'string', format, description: helpText, isOptional, isNullable, maxLength };
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return { type: 'string', description: helpText, isOptional, isNullable };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function getZodShape(schema: z.ZodObject<z.ZodRawShape>): Record<string, FieldInfo> {
|
|
133
|
-
const result: Record<string, FieldInfo> = {};
|
|
134
|
-
for (const [key, field] of Object.entries(schema.shape)) result[key] = getZodFieldInfo(field);
|
|
135
|
-
return result;
|
|
136
|
-
}
|