Skip to content

Conversation

@wise-king-sullyman
Copy link
Contributor

@wise-king-sullyman wise-king-sullyman commented Dec 18, 2025

closes #169

Summary by CodeRabbit

  • New Features

    • Added endpoints to list examples and fetch raw example source; main API route now redirects to a dedicated text endpoint.
  • Behavior Changes

    • Primary API route switched from precomputed build-time paths to runtime redirects; prerender behavior adjusted.
    • GET handlers now validate params and return clear 400/404 error responses.
  • Documentation

    • OpenAPI/docs updated to include new routes, parameters and responses.
  • Tests

    • Tests updated to expect redirect flow and validate new error cases; added index integrity tests.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 18, 2025

Walkthrough

Adds runtime API endpoints for tab redirects, raw page text, example listings, and example source; extends ApiIndex with per-tab examples and extraction logic; moves some routes from prerendered to runtime and updates tests and OpenAPI docs accordingly.

Changes

Cohort / File(s) Summary
Main tab route
src/pages/api/[version]/[section]/[page]/[tab].ts
Removed build-time getStaticPaths; changed prerender to false; expanded GET signature to accept redirect and url; added 400/404 validation; now redirects to /text instead of returning content.
Text content route
src/pages/api/[version]/[section]/[page]/[tab]/text.ts
New route with prerender = true; implements getStaticPaths, validates params against ApiIndex, resolves content via content collections, and returns raw body text with detailed 404 handling.
Examples listing route
src/pages/api/[version]/[section]/[page]/[tab]/examples.ts
New runtime route (prerender = false) that validates params, calls fetchApiIndex(url), builds index key, and returns the examples array (exampleName, title) as JSON.
Example source route
src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts
New route (prerender = true) that builds static paths from index, resolves MDX imports to example file paths, reads example files from disk, and returns source as text with robust error handling.
API root / docs / OpenAPI
src/pages/api/index.ts, src/pages/api/openapi.json.ts
Added OpenAPI paths and docs for the redirecting main route plus /text, /examples, and /examples/{example} endpoints; updated parameter/response schemas and usage text.
ApiIndex generation & runtime
src/utils/apiIndex/generate.ts, src/utils/apiIndex/get.ts, src/utils/apiIndex/index.ts
Extended ApiIndex with examples keyed by version::section::page::tab; added extractExamplesWithTitles to parse LiveExample + H3 titles and populate index.examples; added runtime getExamples() and validation for parsed.examples; exported getExamples and fetchApiIndex.
Tests
src/utils/__tests__/apiIndex.test.ts, src/__tests__/pages/api/__tests__/[version]/[section]/[page]/[tab].test.ts
Added ApiIndex tests; updated route tests to mock fetchApiIndex, expect 302 redirects to /text, include 400/404 checks, and track redirect invocation.
Other change
src/pages/[section]/[page]/[tab].astro
Switched second argument to addDemosOrDeprecated from entry.id to entry.filePath when building tab metadata.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant MainRoute as /api/{version}/{section}/{page}/{tab}
    participant Index as ApiIndex (fetchApiIndex)
    participant TextRoute as /api/{version}/{section}/{page}/{tab}/text
    participant ExamplesRoute as /api/{version}/{section}/{page}/{tab}/examples
    participant ExampleRoute as /api/{version}/{section}/{page}/{tab}/examples/{example}
    participant Collections as Content Collections
    participant FileSystem as File System

    Client->>MainRoute: GET /api/v/.../{tab}
    MainRoute->>Index: fetchApiIndex(url)
    Index-->>MainRoute: index
    MainRoute-->>Client: 302 Redirect to /text

    Client->>TextRoute: GET /api/v/.../{tab}/text
    TextRoute->>Collections: load entries for version
    Collections-->>TextRoute: entries
    TextRoute->>TextRoute: validate params & find entry
    TextRoute-->>Client: 200 text/markdown

    Client->>ExamplesRoute: GET /api/v/.../{tab}/examples
    ExamplesRoute->>Index: fetchApiIndex(url)
    Index-->>ExamplesRoute: index.examples[key]
    ExamplesRoute-->>Client: 200 JSON [{exampleName,title},...]

    Client->>ExampleRoute: GET /api/v/.../{tab}/examples/{example}
    ExampleRoute->>Collections: load entries for version
    Collections-->>ExampleRoute: entries
    ExampleRoute->>ExampleRoute: resolve import -> example file path
    ExampleRoute->>FileSystem: read example file
    FileSystem-->>ExampleRoute: source
    ExampleRoute-->>Client: 200 text/plain (source)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Areas to focus review on:
    • src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts — MDX import parsing, import resolution, filesystem reads, query-raw handling.
    • src/pages/api/[version]/[section]/[page]/[tab]/text.tsgetStaticPaths generation, tab defaulting and matching logic, error messages.
    • src/utils/apiIndex/generate.tsextractExamplesWithTitles correctness (title association, uniqueness, ordering) and index.examples keying.
    • Tests and OpenAPI docs — ensure responses, status codes, and schemas match implemented behavior.

Possibly related PRs

Suggested reviewers

  • dlabaj

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning One out-of-scope change detected: modifying entry.id to entry.filePath in src/pages/[section]/[page]/[tab].astro affects how tabs are computed, which is unrelated to the example API endpoints objective. Review the change to src/pages/[section]/[page]/[tab].astro and confirm whether it's necessary for example API functionality. If not, remove it or isolate it in a separate PR.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat(api): add example API endpoints' accurately and concisely describes the main changes—adding new API endpoints for accessing code examples/demos as outlined in the PR objectives.
Linked Issues check ✅ Passed The PR successfully implements API support for accessing code-based examples/demos from external files, including new endpoints for listing and retrieving examples, matching issue #169's scope.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-example-api

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9228833 and 07fc9ad.

📒 Files selected for processing (4)
  • src/pages/[section]/[page]/[tab].astro (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/text.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples.ts
🧰 Additional context used
🧬 Code graph analysis (2)
src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (4)
src/utils/apiIndex/generate.ts (1)
  • generateAndWriteApiIndex (214-218)
src/utils/apiHelpers.ts (3)
  • createIndexKey (48-50)
  • createJsonResponse (18-27)
  • createTextResponse (29-37)
src/utils/packageUtils.ts (2)
  • getDefaultTabForApi (69-69)
  • addDemosOrDeprecated (44-58)
src/utils/case.ts (1)
  • kebabCase (9-10)
src/pages/api/[version]/[section]/[page]/[tab]/text.ts (7)
src/utils/apiIndex/generate.ts (1)
  • generateAndWriteApiIndex (214-218)
src/utils/apiHelpers.ts (2)
  • createJsonResponse (18-27)
  • createTextResponse (29-37)
src/utils/apiIndex/get.ts (1)
  • getApiIndex (13-48)
src/content.config.ts (1)
  • collections (50-59)
src/__mocks__/astro-content.ts (1)
  • getCollection (4-4)
src/utils/packageUtils.ts (1)
  • getDefaultTabForApi (69-69)
src/utils/case.ts (1)
  • kebabCase (9-10)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build (22.x)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (6)
src/pages/[section]/[page]/[tab].astro (1)

53-53: LGTM! Correct use of entry.filePath for tab detection.

The change from entry.id to entry.filePath correctly provides the full file path needed by addDemosOrDeprecated to detect 'demos' and 'deprecated' directories. This aligns with the same fix applied in other API routes.

src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (2)

1-57: Well-structured static path generation.

The getStaticPaths implementation correctly generates paths from the API index at build time, ensuring all example routes are prerendered. The use of generateAndWriteApiIndex provides a centralized index for other API routes to reference.


174-253: Comprehensive error handling and validation.

The GET handler demonstrates robust error handling:

  • Parameter validation (lines 176-181)
  • Content resolution with descriptive errors (lines 187-192, 208-211, 215-220)
  • Separate error handling for content entry and example file reads (lines 195-204, 228-243)
  • ENOENT detection for proper 404 responses on missing example files (line 233)
  • Query parameter stripping before file read (line 223) to handle imports like import Foo from './example?raw'

The preference for .mdx files over .md files (lines 153-157) is well-documented and aligns with the goal of extracting examples from LiveExample components.

src/pages/api/[version]/[section]/[page]/[tab]/text.ts (3)

25-56: Defensive path generation with helpful diagnostics.

The getStaticPaths implementation includes defensive checks:

  • Warning for missing tabs (lines 48-51) aids debugging during build
  • Filtering tabless paths (line 55) prevents build failures
  • Comment explains the unlikely but handled edge case

These safeguards demonstrate good defensive programming practices.


68-104: Efficient index-based validation for fast 404 responses.

The hierarchical validation against the prebuilt index (version → section → page → tab) provides fast-path 404 responses without fetching content collections. This optimization is particularly valuable for invalid requests and reduces unnecessary I/O.


124-147: Correct use of entry.filePath and helpful mismatch warning.

The content matching logic correctly uses entry.filePath with addDemosOrDeprecated (line 126), enabling proper detection of demos and deprecated content directories. The mismatch warning (lines 137-140) provides valuable diagnostics when the index and actual content fall out of sync, aiding troubleshooting.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai bot requested a review from dlabaj December 18, 2025 22:03
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Dec 18, 2025

Deploying patternfly-doc-core with  Cloudflare Pages  Cloudflare Pages

Latest commit: 07fc9ad
Status: ✅  Deploy successful!
Preview URL: https://2a7e59c2.patternfly-doc-core.pages.dev
Branch Preview URL: https://add-example-api.patternfly-doc-core.pages.dev

View logs

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (7)
src/utils/apiIndex/generate.ts (1)

57-85: Consider making the regex more robust.

The regex /<LiveExample.*src={(\w*)}.*\/?>/g has a few potential edge cases:

  1. \w* allows empty captures - consider \w+ to require at least one character
  2. The greedy .* could over-match if multiple LiveExample tags appear on the same line

For the current documentation content this likely works fine, but a more precise pattern would be safer.

🔎 Optional improvement
-  const exampleRegex = /<LiveExample.*src={(\w*)}.*\/?>/g
+  const exampleRegex = /<LiveExample[^>]*src={(\w+)}[^>]*\/?>/g

Using [^>]* instead of .* prevents matching across tag boundaries.

src/utils/__tests__/apiIndex.test.ts (1)

126-148: Consider adding tests for getExamples.

The test file covers getVersions, getSections, getPages, and getTabs, but the new getExamples function (added in get.ts) doesn't have dedicated tests. Once exported from the index module, consider adding a describe('getExamples') block.

src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (2)

57-74: Regex patterns may miss valid imports and could cause silent failures.

The import regex on line 58 requires both single quotes around the path, but imports can use double quotes. The ? character requirement in the path regex (line 69) seems intentional for raw imports but isn't documented.

Consider making the patterns more robust:

🔎 Proposed improvements
 function getImports(fileContent: string) {
-  const importRegex = /import.*from.*['"]\..*\/[\w\.\?]*['"]/gm
+  const importRegex = /import.*from\s*['"]\..*\/[\w.?]*['"]/gm
   const matches = fileContent.match(importRegex)
   return matches
 }

 function getExampleFilePath(imports: string[], exampleName: string) {
   const exampleImport = imports.find((imp) => imp.includes(exampleName))
   if (!exampleImport) {
     console.error('No import path found for example', exampleName)
     return null
   }
-  const match = exampleImport.match(/['"]\..*\/[\w\.]*\?/)
+  // Match relative path ending with `?raw` query string
+  const match = exampleImport.match(/['"]\..*\/[\w.]+\?/)
   if (!match) {
     return null
   }
   return match[0].replace(/['"?]/g, '')
 }

76-91: Code duplication: getCollections is duplicated across files.

This function appears nearly identical to code in text.ts (lines 107-122). Consider extracting to a shared utility.

src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (1)

51-53: Remove trailing blank lines.

src/pages/api/[version]/[section]/[page]/[tab].ts (1)

26-33: Unused variable sectionKey created prematurely.

sectionKey is created on line 27 but the section check on line 28 uses index.sections[version]. The sectionKey is only needed later for the page check. Consider moving it closer to its usage.

🔎 Proposed fix
   // Check if section exists for this version
-  const sectionKey = createIndexKey(version, section)
   if (!index.sections[version]?.includes(section)) {
     return createJsonResponse(
       { error: `Section '${section}' not found for version '${version}'` },
       404,
     )
   }

   // Check if page exists for this section
+  const sectionKey = createIndexKey(version, section)
   const pageKey = createIndexKey(version, section, page)
src/pages/api/[version]/[section]/[page]/[tab]/text.ts (1)

68-104: Validation logic duplicated with [tab].ts.

The index validation (lines 68-104) is nearly identical to the validation in [tab].ts (lines 18-54). Consider extracting to a shared validation helper.

🔎 Suggested approach

Create a shared validation function in apiHelpers.ts:

export async function validateTabPath(
  index: ApiIndex,
  version: string,
  section: string,
  page: string,
  tab: string
): { valid: true } | { valid: false; response: Response } {
  if (!index.versions.includes(version)) {
    return { valid: false, response: createJsonResponse({ error: `Version '${version}' not found` }, 404) }
  }
  // ... rest of validation
  return { valid: true }
}
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 11156f9 and 9d6dfa2.

📒 Files selected for processing (10)
  • src/pages/api/[version]/[section]/[page]/[tab].ts (3 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/text.ts (1 hunks)
  • src/pages/api/index.ts (4 hunks)
  • src/pages/api/openapi.json.ts (3 hunks)
  • src/utils/__tests__/apiIndex.test.ts (1 hunks)
  • src/utils/apiIndex/generate.ts (5 hunks)
  • src/utils/apiIndex/get.ts (2 hunks)
  • src/utils/apiIndex/index.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
src/utils/__tests__/apiIndex.test.ts (2)
src/utils/apiIndex/get.ts (5)
  • getApiIndex (13-48)
  • getVersions (55-58)
  • getSections (66-69)
  • getPages (78-83)
  • getTabs (93-98)
src/utils/apiHelpers.ts (1)
  • createIndexKey (48-50)
src/pages/api/[version]/[section]/[page]/[tab]/text.ts (5)
src/utils/apiIndex/generate.ts (1)
  • generateAndWriteApiIndex (212-216)
src/utils/apiHelpers.ts (3)
  • createIndexKey (48-50)
  • createJsonResponse (18-27)
  • createTextResponse (29-37)
src/utils/apiIndex/get.ts (1)
  • getApiIndex (13-48)
src/utils/packageUtils.ts (2)
  • getDefaultTabForApi (69-69)
  • addDemosOrDeprecated (44-58)
src/utils/case.ts (1)
  • kebabCase (9-10)
src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (3)
src/pages/api/[version]/[section]/[page]/[tab].ts (1)
  • GET (8-58)
src/utils/apiIndex/get.ts (1)
  • getApiIndex (13-48)
src/utils/apiHelpers.ts (2)
  • createIndexKey (48-50)
  • createJsonResponse (18-27)
src/utils/apiIndex/get.ts (1)
src/utils/apiHelpers.ts (1)
  • createIndexKey (48-50)
src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (6)
src/utils/apiIndex/generate.ts (1)
  • generateAndWriteApiIndex (212-216)
src/utils/apiIndex/index.ts (1)
  • generateAndWriteApiIndex (21-21)
src/utils/apiHelpers.ts (2)
  • createIndexKey (48-50)
  • createTextResponse (29-37)
src/__mocks__/astro-content.ts (1)
  • getCollection (4-4)
src/utils/packageUtils.ts (1)
  • getDefaultTab (60-65)
src/utils/case.ts (1)
  • kebabCase (9-10)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build (22.x)
🔇 Additional comments (17)
src/utils/apiIndex/get.ts (2)

26-28: LGTM!

The validation for the examples object is consistent with the existing versions validation pattern and provides a clear error message.


109-119: LGTM!

The getExamples function follows the established pattern of other getter functions (getTabs, getPages, getSections) with consistent use of dynamic imports for createIndexKey and safe fallback to an empty array when the key is not found.

src/utils/apiIndex/generate.ts (2)

46-48: LGTM!

The examples field definition with its descriptive comment clearly documents the key format and value structure.


159-165: LGTM!

The examples collection logic correctly extracts examples per tab and only stores entries when examples are found.

src/utils/__tests__/apiIndex.test.ts (2)

1-67: LGTM!

Comprehensive test coverage for the API index structure, including the new examples field validation with proper checks for key format and value structure.


150-185: LGTM!

The architecture test validates the full hierarchical navigation flow including the new examples layer, with appropriate handling for tabs that may not have examples.

src/pages/api/index.ts (3)

135-173: LGTM!

The updated tab endpoint documentation correctly describes the redirect behavior to /text and provides complete parameter documentation.


214-304: LGTM!

The new endpoints for /examples (list) and /examples/{example} (code retrieval) are well-documented with appropriate parameters, return types, and examples that align with the PR objectives.


306-323: LGTM!

The updated usage flow and architecture documentation clearly explains the API navigation pattern including the new examples endpoints and the runtime optimization strategy.

src/pages/api/openapi.json.ts (3)

309-379: LGTM!

The OpenAPI spec for the tab validation/redirect endpoint is well-documented with appropriate 302 and 404 response definitions.


460-553: Verify the 400 response is reachable.

The spec documents a 400 response for "Missing required parameters", but since all parameters are path parameters (not query params), they should always be present when the route matches. Consider if 404 would be more appropriate here for invalid combinations, or verify that the handler implementation can actually return 400.


554-643: LGTM!

The OpenAPI spec for the example code endpoint is complete with proper parameter definitions and response schemas including the 404 case for missing examples.

src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (1)

13-55: Static path generation looks correct.

The nested iteration over index structure properly builds all example paths. The use of generateAndWriteApiIndex() ensures the index is available at build time.

src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (1)

34-50: GET handler implementation is clean.

The handler correctly validates params, fetches the index, and returns examples. The fallback to empty array (|| []) handles missing keys gracefully.

src/pages/api/[version]/[section]/[page]/[tab].ts (1)

8-57: Validation and redirect logic is well-structured.

The cascading validation provides clear error messages at each level, and the redirect to /text cleanly separates the routing concern from content delivery.

src/pages/api/[version]/[section]/[page]/[tab]/text.ts (2)

47-55: Defensive coding for tabless paths is good.

The warning and filter provide safety against build crashes while alerting developers to potential issues.


149-152: Empty string fallback for missing body content.

Returning an empty string when matchingEntry.body is undefined may not be the desired behavior. Consider returning a 404 or 204 (No Content) if the body is genuinely empty.

Is returning empty text intentional for entries without body content, or should this be treated as an error condition?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/pages/api/[version]/[section]/[page]/[tab].ts (1)

17-53: Add error handling for fetchApiIndex failures.

Similar to the examples endpoint, the fetchApiIndex(url) call can reject if the fetch fails or the response cannot be parsed. Without error handling, this will result in an unclear 500 error.

🔎 Recommended fix: Add try-catch wrapper
-  // Validate using index first (fast path for 404s)
-  const index = await fetchApiIndex(url)
-
-  // Check if version exists
-  if (!index.versions.includes(version)) {
+  // Validate using index first (fast path for 404s)
+  let index
+  try {
+    index = await fetchApiIndex(url)
+  } catch (error) {
+    return createJsonResponse(
+      { error: `Failed to fetch API index: ${error instanceof Error ? error.message : 'Unknown error'}` },
+      500,
+    )
+  }
+
+  // Check if version exists
+  if (!index.versions.includes(version)) {

Otherwise, the validation logic is well-structured.

The hierarchical validation checks (version → section → page → tab) are logical and provide clear, specific error messages for each failure case.

🧹 Nitpick comments (1)
src/utils/apiIndex/generate.ts (1)

50-87: LGTM! Example extraction logic is sound.

The function correctly extracts LiveExample components and associates them with preceding H3 headings. The regex pattern and logic are appropriate for typical JSX/TSX usage.

Optional: Document regex assumptions

Consider adding a comment noting that the regex expects src={ExampleName} format (no quotes, standard JSX identifier syntax) to help future maintainers understand the supported patterns.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d6dfa2 and 9228833.

📒 Files selected for processing (8)
  • src/__tests__/pages/api/__tests__/[version]/[section]/[page]/[tab].test.ts (5 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab].ts (3 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (1 hunks)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts (1 hunks)
  • src/utils/__tests__/apiIndex.test.ts (1 hunks)
  • src/utils/apiIndex/generate.ts (5 hunks)
  • src/utils/apiIndex/get.ts (2 hunks)
  • src/utils/apiIndex/index.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/utils/tests/apiIndex.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/pages/api/[version]/[section]/[page]/[tab]/examples/[example].ts
  • src/utils/apiIndex/index.ts
🧰 Additional context used
🧬 Code graph analysis (4)
src/__tests__/pages/api/__tests__/[version]/[section]/[page]/[tab].test.ts (3)
src/pages/api/[version]/[section]/[page]/[tab].ts (1)
  • GET (7-57)
src/pages/api/[version]/[section].ts (1)
  • GET (7-37)
src/pages/api/[version]/[section]/[page].ts (1)
  • GET (7-38)
src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (2)
src/utils/apiHelpers.ts (2)
  • createJsonResponse (18-27)
  • createIndexKey (48-50)
src/utils/apiIndex/index.ts (1)
  • fetchApiIndex (27-27)
src/utils/apiIndex/generate.ts (1)
src/utils/packageUtils.ts (1)
  • addDemosOrDeprecated (44-58)
src/utils/apiIndex/get.ts (2)
src/utils/apiIndex/index.ts (2)
  • getExamples (25-25)
  • getApiIndex (25-25)
src/utils/apiHelpers.ts (1)
  • createIndexKey (48-50)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build (22.x)
🔇 Additional comments (14)
src/pages/api/[version]/[section]/[page]/[tab]/examples.ts (2)

1-5: LGTM! Configuration is correct for SSR.

The prerender flag and imports are appropriate for an on-demand server-rendered route. The past review concern about getStaticPaths has been addressed (it's not present in the current code).


7-15: LGTM! Parameter validation is thorough.

The validation correctly checks all required parameters and returns a clear 400 error when any are missing.

src/utils/apiIndex/get.ts (2)

26-28: LGTM! Validation aligns with existing patterns.

The runtime validation for index.examples is consistent with the existing validation for index.versions and provides a clear error message.


100-119: LGTM! Implementation follows established patterns.

The getExamples function is consistent with the existing getter functions (getVersions, getSections, getPages, getTabs) in terms of structure, error handling, and the use of dynamic imports for createIndexKey.

src/utils/apiIndex/generate.ts (4)

46-48: LGTM! Interface extension is well-documented.

The examples field addition to the ApiIndex interface is clear, well-typed, and includes a helpful JSDoc example.


162-167: LGTM! Example collection logic is correct.

The code correctly builds the composite key, extracts examples from the entry body, and stores them in the map only when examples are found.


181-183: LGTM! Index population follows established patterns.

The examples are populated into the index using the same approach as pages and tabs, maintaining consistency across the codebase.


156-156: No action needed. The use of entry.filePath in addDemosOrDeprecated(entryTab, entry.filePath) is correct. The function checks whether the file path contains 'demos' or 'deprecated' directory segments to append appropriate suffixes to the tab name. Since entry.filePath represents the actual file path, it is the appropriate data source for detecting these directory-based classifications. Comprehensive test coverage confirms this behavior works as intended.

src/__tests__/pages/api/__tests__/[version]/[section]/[page]/[tab].test.ts (3)

134-150: LGTM! Mock setup is appropriate.

The fetchApiIndex mock returns data consistent with the existing getApiIndex mock, providing the necessary structure for the redirect-based tests.


156-221: LGTM! Redirect test coverage is thorough.

The tests correctly verify the redirect behavior, including proper status codes (302), target paths, and coverage across different tabs. The use of fresh mockRedirect instances per test prevents cross-test pollution.


223-314: LGTM! Error handling test coverage is comprehensive.

The tests validate all error scenarios (404 for nonexistent entities, 400 for missing parameters) with appropriate status codes and descriptive error messages.

src/pages/api/[version]/[section]/[page]/[tab].ts (3)

1-5: LGTM! Configuration is correct for SSR.

The imports and prerender = false setting are appropriate for the new redirect-based, server-rendered approach.


7-15: LGTM! Parameter validation is consistent and thorough.

The validation correctly checks all required parameters and provides a clear error message for missing values.


55-56: LGTM! Redirect implementation is clean.

The redirect to the /text endpoint is straightforward and maintains good separation of concerns between validation and content retrieval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API support for accessing code based examples/demos

2 participants