@blocklet/pages-kit-block-studio 0.4.90 → 0.4.91
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/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/generate-wrapper-code.js +72 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/generate-wrapper-code.js +71 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/generate-wrapper-code.d.ts +8 -0
- package/package.json +11 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// 导入 Zod 工具集
|
|
2
2
|
import { getComponentDependencies } from '@blocklet/pages-kit/utils/property';
|
|
3
|
+
import { z } from 'zod';
|
|
3
4
|
import { BASIC_COMPONENT_SECTION_TYPES, BASIC_COMPONENT_SECTION_JSON_SCHEMAS } from './block-props-utils';
|
|
4
5
|
import { propertiesToZodSchema, zodSchemaToTypeString, zodSchemaToJsonSchema } from './zod-utils';
|
|
5
6
|
export async function generateWrapperCode({ project, state, version }) {
|
|
@@ -461,4 +462,74 @@ export function generatePropertiesTypesWithZod(properties) {
|
|
|
461
462
|
const typeString = zodSchemaToTypeString(zodSchema, 'Props');
|
|
462
463
|
return typeString;
|
|
463
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* 生成特定页面所有 section 数据的 Zod 类型定义
|
|
467
|
+
* @param state 应用状态
|
|
468
|
+
* @param pageId 目标页面ID
|
|
469
|
+
* @returns 包含所有 section 数据类型的 Zod schema
|
|
470
|
+
*/
|
|
471
|
+
export function generatePageSectionsDataZod(state, pageId) {
|
|
472
|
+
const page = state.pages[pageId];
|
|
473
|
+
if (!page) {
|
|
474
|
+
return z.object({}).describe('Empty page data');
|
|
475
|
+
}
|
|
476
|
+
const sectionSchemas = {};
|
|
477
|
+
// 收集页面所有section的配置信息
|
|
478
|
+
page.sectionIds.forEach((sectionId) => {
|
|
479
|
+
const section = page.sections[sectionId];
|
|
480
|
+
if (!section || !section.isTemplateSection)
|
|
481
|
+
return;
|
|
482
|
+
const sectionName = section.name || section.id;
|
|
483
|
+
if (section.component === 'custom-component') {
|
|
484
|
+
const componentId = section.config?.componentId;
|
|
485
|
+
if (!componentId)
|
|
486
|
+
return;
|
|
487
|
+
const component = state.components[componentId]?.data || state.resources.components?.[componentId]?.component;
|
|
488
|
+
if (!component)
|
|
489
|
+
return;
|
|
490
|
+
// 使用propertiesToZodSchema生成Zod Schema,并包含llmConfig配置
|
|
491
|
+
const zodSchema = propertiesToZodSchema(component.properties || {}, {
|
|
492
|
+
addZodDescribe: false,
|
|
493
|
+
llmConfig: section.llmConfig?.properties || {},
|
|
494
|
+
});
|
|
495
|
+
// 添加section描述
|
|
496
|
+
sectionSchemas[sectionName] = zodSchema.describe(section.templateDescription || '');
|
|
497
|
+
}
|
|
498
|
+
else if (BASIC_COMPONENT_SECTION_TYPES[section.component]) {
|
|
499
|
+
// 处理基本组件类型
|
|
500
|
+
const componentType = section.component;
|
|
501
|
+
const schemaProperties = BASIC_COMPONENT_SECTION_JSON_SCHEMAS[componentType];
|
|
502
|
+
// 将JSON Schema转换为Zod Schema
|
|
503
|
+
const zodProperties = Object.entries(schemaProperties || {}).reduce((acc, [key, value]) => {
|
|
504
|
+
// 简单处理常见类型
|
|
505
|
+
if (value.type === 'string') {
|
|
506
|
+
acc[key] = z.string().optional();
|
|
507
|
+
}
|
|
508
|
+
else if (value.type === 'number') {
|
|
509
|
+
acc[key] = z.number().optional();
|
|
510
|
+
}
|
|
511
|
+
else if (value.type === 'boolean') {
|
|
512
|
+
acc[key] = z.boolean().optional();
|
|
513
|
+
}
|
|
514
|
+
else if (value.type === 'array') {
|
|
515
|
+
acc[key] = z.array(z.any()).optional();
|
|
516
|
+
}
|
|
517
|
+
else if (value.type === 'object') {
|
|
518
|
+
acc[key] = z.record(z.string(), z.any()).optional();
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
acc[key] = z.any().optional();
|
|
522
|
+
}
|
|
523
|
+
return acc;
|
|
524
|
+
}, {});
|
|
525
|
+
sectionSchemas[sectionName] = z.object(zodProperties).describe(section.templateDescription || '');
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
// 其他类型的section,使用默认对象
|
|
529
|
+
sectionSchemas[sectionName] = z.object({}).describe(section.templateDescription || '');
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
// 创建包含所有section的schema
|
|
533
|
+
return z.object(sectionSchemas).describe(`Page ${page.title || pageId} sections data`);
|
|
534
|
+
}
|
|
464
535
|
export default generateWrapperCode;
|