feat(API): [PF-3167] Add component-index.json API endpoint for MCP#221
Conversation
WalkthroughAdds a new Astro API route that builds a versioned component index JSON by reading the API index and optional props.json, iterating latest-version sections/pages/tabs, and aggregating example counts, CSS presence, and props availability per component. Changes
Sequence DiagramsequenceDiagram
participant Client
participant APIRoute as Component Index API
participant FileSystem as File System (props.json)
participant ApiIndex as getApiIndex
participant Aggregator as Data Aggregator
Client->>APIRoute: GET /api/component-index.json
APIRoute->>FileSystem: Read props.json (optional)
alt props.json exists
FileSystem-->>APIRoute: Props data
APIRoute->>APIRoute: Extract component names
else props.json missing
FileSystem-->>APIRoute: ENOENT
APIRoute->>APIRoute: Continue without props
end
APIRoute->>ApiIndex: Fetch API index
ApiIndex-->>APIRoute: Index with versions & sections
APIRoute->>APIRoute: Select latest version, validate
APIRoute->>Aggregator: Iterate sections/pages/tabs
loop per page/tab
Aggregator->>Aggregator: Sum example counts
Aggregator->>Aggregator: Check CSS entries
Aggregator->>Aggregator: Check props availability (PascalCase)
end
Aggregator-->>APIRoute: Component metadata map
APIRoute-->>Client: ComponentIndex JSON
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Signed-off-by: Jeff Puzzo <jpuzzo@redhat.com>
4697420 to
abdafc5
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/pages/api/component-index.json.ts (1)
47-51: Add a warning whenprops.jsonis missing.Line 48 handles
ENOENTsilently. This makeshasPropsdegrade to all-false without visibility, which is hard to detect in build logs.Proposed improvement
} catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { throw error } + console.warn('[component-index] props.json not found; hasProps will be false for all components') }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/pages/api/component-index.json.ts` around lines 47 - 51, The catch swallowing ENOENT hides missing props.json files; update the catch in the component index generation (the block that checks (error as NodeJS.ErrnoException).code !== 'ENOENT') to log a warning when ENOENT occurs (include the component path/name and that props.json is missing) instead of silently ignoring it so hasProps consumers can be diagnosed; use the existing logging mechanism (e.g., processLogger.warn or console.warn) and include the error message/stack for context while still not throwing for ENOENT.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/pages/api/component-index.json.ts`:
- Around line 43-46: The regex propsSuffixPattern currently matches "Props"
anywhere, causing names like "hasProps" to be treated as prop interfaces; change
the pattern used in the componentNamesWithProps computation to only match names
that end with "Props" (e.g., make propsSuffixPattern a suffix-anchored,
case-insensitive regex) so the Object.keys(propsData).filter((name) =>
!propsSuffixPattern.test(name)) logic only strips true "*Props" interfaces;
update the symbol propsSuffixPattern in the file where componentNamesWithProps
and propsData are used.
---
Nitpick comments:
In `@src/pages/api/component-index.json.ts`:
- Around line 47-51: The catch swallowing ENOENT hides missing props.json files;
update the catch in the component index generation (the block that checks (error
as NodeJS.ErrnoException).code !== 'ENOENT') to log a warning when ENOENT occurs
(include the component path/name and that props.json is missing) instead of
silently ignoring it so hasProps consumers can be diagnosed; use the existing
logging mechanism (e.g., processLogger.warn or console.warn) and include the
error message/stack for context while still not throwing for ENOENT.
| const propsSuffixPattern = /Props/i | ||
| componentNamesWithProps = new Set( | ||
| Object.keys(propsData).filter((name) => !propsSuffixPattern.test(name)), | ||
| ) |
There was a problem hiding this comment.
Anchor the Props filter to suffix-only matching.
Line 43 uses /Props/i, which filters any key containing “Props” anywhere, not just interface names ending with Props. That can misclassify hasProps.
Proposed fix
- const propsSuffixPattern = /Props/i
+ const propsSuffixPattern = /Props$/
componentNamesWithProps = new Set(
Object.keys(propsData).filter((name) => !propsSuffixPattern.test(name)),
)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/pages/api/component-index.json.ts` around lines 43 - 46, The regex
propsSuffixPattern currently matches "Props" anywhere, causing names like
"hasProps" to be treated as prop interfaces; change the pattern used in the
componentNamesWithProps computation to only match names that end with "Props"
(e.g., make propsSuffixPattern a suffix-anchored, case-insensitive regex) so the
Object.keys(propsData).filter((name) => !propsSuffixPattern.test(name)) logic
only strips true "*Props" interfaces; update the symbol propsSuffixPattern in
the file where componentNamesWithProps and propsData are used.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/pages/api/component-index.json.ts (1)
24-30: Edge case: consecutive hyphens produce empty segments.If a page name contains consecutive hyphens (e.g.,
"some--page"),split('-')yields empty strings that become empty segments in the result. This is unlikely given your naming conventions, but worth noting.Optional defensive fix
function pageToPascalCase(page: string): string { return removeSubsection(page) .split('-') + .filter(Boolean) .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join('') }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/pages/api/component-index.json.ts` around lines 24 - 30, The pageToPascalCase function can produce empty segments when page contains consecutive hyphens; update pageToPascalCase (which calls removeSubsection) to filter out empty strings after split('-') before mapping/capitalizing so only non-empty words are transformed and joined, ensuring inputs like "some--page" become "SomePage" instead of introducing empty segments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/pages/api/component-index.json.ts`:
- Around line 24-30: The pageToPascalCase function can produce empty segments
when page contains consecutive hyphens; update pageToPascalCase (which calls
removeSubsection) to filter out empty strings after split('-') before
mapping/capitalizing so only non-empty words are transformed and joined,
ensuring inputs like "some--page" become "SomePage" instead of introducing empty
segments.
|
🎉 This PR is included in version 1.21.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
@patternfly/patternfly-mcpto power component search, documentation lookup, and schema tools without hardcoded dataSummary by CodeRabbit