Skip to content

Commit b6d298e

Browse files
authored
feat: json optimized schemas (#9)
* feat: json optimized schemas * docs: review update * docs: review update, architecture breakdown * docs: review update
1 parent 3c67540 commit b6d298e

File tree

6 files changed

+27027
-4
lines changed

6 files changed

+27027
-4
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ This package is specifically designed for AI-assisted development tools and Mode
4343
- **Assist with component selection** based on requirements
4444

4545
### MCP Server Integration
46+
#### Individual Component Imports (Tree-Shakeable)
4647
```javascript
4748
// MCP servers can load and query component schemas
48-
import { componentNames, getComponentSchema } from 'patternfly-component-schemas';
49+
import { componentNames, getComponentSchema } from '@patternfly/patternfly-component-schemas';
4950

5051
// Discover available components
5152
const components = componentNames; // 462 PatternFly components
@@ -55,12 +56,63 @@ const buttonSchema = await getComponentSchema('Button');
5556
// Returns: { schema, componentName, propsCount, requiredProps }
5657
```
5758

59+
#### JSON-Optimized Integration
60+
```javascript
61+
// JSON-optimized interface with lazy loading:
62+
// - Single import of lightweight metadata for fast discovery of all components
63+
// - Bulk schema access lazy loaded on first query
64+
// - Fast subsequent queries after initial load
65+
66+
import { componentNames, getComponentSchema } from '@patternfly/patternfly-component-schemas/json';
67+
68+
// Discover all available components (no full schemas loaded yet)
69+
const components = componentNames; // 462 PatternFly components
70+
71+
// Get detailed component information (lazy loads full schemas on first call)
72+
const buttonSchema = await getComponentSchema('Button');
73+
// Returns JSON Schema with properties, required props, etc.
74+
```
75+
5876
### AI Assistant Examples
5977
- **"What props does the Button component accept?"** → AI reads Button schema
6078
- **"Generate a PatternFly Alert component"** → AI uses Alert schema for validation
6179
- **"Show me all navigation components"** → AI filters components by name/description
6280
- **"Create a form with proper PatternFly components"** → AI selects appropriate form components
6381

82+
## 📦 Package Architecture
83+
84+
### Two Interfaces for Different Needs
85+
86+
This package provides two interfaces optimized for different use cases:
87+
88+
#### 🌳 Individual Component Imports (Tree-Shakeable)
89+
**Import**: `@patternfly/patternfly-component-schemas`
90+
91+
**Characteristics**:
92+
- Optimized for selective access
93+
- Each component loads individually
94+
- Tree-shakeable (only import what you need)
95+
96+
#### 🚀 JSON-Optimized Interface
97+
**Import**: `@patternfly/patternfly-component-schemas/json`
98+
99+
**Characteristics**:
100+
- Optimized for bulk access patterns
101+
- Lightweight metadata for fast discovery
102+
- Lazy-loaded (full schemas on demand)
103+
104+
### Quick Decision Guide
105+
106+
**Use Tree-Shakeable if you**:
107+
- Need minimal application bundle size
108+
- Know which components you'll use at build time
109+
- Want per-component imports
110+
111+
**Use JSON-Optimized if you**:
112+
- Need all component metadata quickly
113+
- Are building tools that need runtime discovery
114+
- Want fast discovery and bulk operations
115+
64116
## 🔧 Development
65117

66118
### Building from Source

index.json.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// PatternFly Component Schemas - JSON Optimized
2+
// Generated on: 2025-10-14T12:27:01.682Z
3+
4+
// Load metadata
5+
const { default: index } = await import('./schemas/index.json', { with: { type: 'json' } });
6+
7+
// Cache for loaded schemas
8+
let schemas = null;
9+
10+
// Export lightweight data
11+
export { index };
12+
export const componentNames = Object.keys(index.components);
13+
export const componentCount = index.totalComponents;
14+
export const schemaVersion = index.version;
15+
16+
// Get all schemas on-demand
17+
export async function getAllSchemas() {
18+
if (!schemas) {
19+
const { default: loadedSchemas } = await import('./schemas/schemas.json', { with: { type: 'json' } });
20+
schemas = loadedSchemas;
21+
}
22+
return schemas;
23+
}
24+
25+
// Lazy-loaded schema access
26+
export async function getComponentSchema(name) {
27+
if (!index.components[name]) {
28+
throw new Error(`Component '${name}' not found`);
29+
}
30+
31+
const schemas = await getAllSchemas();
32+
return schemas[name];
33+
}
34+
35+
// Fast operations using in-memory metadata
36+
export function getComponentNames(filter = 'all') {
37+
const allComponents = Object.keys(index.components);
38+
39+
switch (filter) {
40+
case 'complex':
41+
return allComponents.filter(name => index.components[name].isComplex);
42+
case 'withRequiredProps':
43+
return allComponents.filter(name => index.components[name].hasRequiredProps);
44+
default:
45+
return allComponents;
46+
}
47+
}
48+
49+
export function searchComponents(query) {
50+
const lowerQuery = query.toLowerCase();
51+
return Object.values(index.components).filter(c =>
52+
c.name.toLowerCase().includes(lowerQuery) ||
53+
c.description.toLowerCase().includes(lowerQuery)
54+
);
55+
}
56+
57+
export function getComponentsWithRequiredProps() {
58+
return Object.values(index.components).filter(c => c.hasRequiredProps);
59+
}
60+
61+
export function getComplexComponents() {
62+
return Object.values(index.components).filter(c => c.isComplex);
63+
}
64+
65+
export function getComponentStats() {
66+
return {
67+
totalComponents: index.totalComponents,
68+
totalProps: index.totalProps,
69+
averagePropsPerComponent: index.averagePropsPerComponent,
70+
componentsWithRequiredProps: index.componentsWithRequiredProps,
71+
complexComponents: index.complexComponents
72+
};
73+
}
74+
75+
// Default export
76+
export default index;

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"build": "node scripts/generate-schemas.js component-metadata.json",
8+
"build": "npm run build:components && npm run build:json",
9+
"build:components": "node scripts/generate-schemas.js component-metadata.json",
10+
"build:json": "node scripts/generate-json-schemas.js component-metadata.json",
911
"dev": "node scripts/generate-schemas.js --watch",
10-
"clean": "rm -rf components/*/",
12+
"clean": "rm -rf components/*/ schemas/ index.json.js",
1113
"rebuild": "npm run clean && npm run build",
1214
"semantic-release": "semantic-release"
1315
},
@@ -26,12 +28,16 @@
2628
},
2729
"files": [
2830
"components/",
31+
"schemas/",
2932
"index.js",
33+
"index.json.js",
3034
"README.md"
3135
],
3236
"exports": {
3337
".": "./index.js",
34-
"./components/*": "./components/*/index.js"
38+
"./components/*": "./components/*/index.js",
39+
"./json": "./index.json.js",
40+
"./schemas": "./schemas/index.json"
3541
},
3642
"repository": {
3743
"type": "git",

0 commit comments

Comments
 (0)