-
Notifications
You must be signed in to change notification settings - Fork 11
feat(API): Add CSS API table support #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dlabaj
wants to merge
6
commits into
main
Choose a base branch
from
css-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
adc7eae
feat(API): Added CSS API endpoint for react tokens.
dlabaj 51c7eb3
Added endpoint tests.
dlabaj 4197aef
Fixed styles and linting errors.
dlabaj b0277f1
Removed non standard code.
dlabaj 87db468
Update src/utils/extractReactTokens.ts
dlabaj fc56188
undid code rabbit modification.
dlabaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
src/__tests__/pages/api/__tests__/[version]/[section]/[page]/css.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import { GET } from '../../../../../../../pages/api/[version]/[section]/[page]/css' | ||
|
|
||
| /** | ||
| * Mock fetchApiIndex to return API index with CSS tokens | ||
| */ | ||
| jest.mock('../../../../../../../utils/apiIndex/fetch', () => ({ | ||
| fetchApiIndex: jest.fn().mockResolvedValue({ | ||
| versions: ['v6'], | ||
| sections: { | ||
| v6: ['components'], | ||
| }, | ||
| pages: { | ||
| 'v6::components': ['alert', 'button'], | ||
| }, | ||
| tabs: { | ||
| 'v6::components::alert': ['react', 'html'], | ||
| 'v6::components::button': ['react'], | ||
| }, | ||
| css: { | ||
| 'v6::components::alert': [ | ||
| { | ||
| name: '--pf-v6-c-alert--BackgroundColor', | ||
| value: '#ffffff', | ||
| description: 'Alert background color', | ||
| }, | ||
| { | ||
| name: '--pf-v6-c-alert--Color', | ||
| value: '#151515', | ||
| description: 'Alert text color', | ||
| }, | ||
| ], | ||
| 'v6::components::button': [ | ||
| { | ||
| name: '--pf-v6-c-button--BackgroundColor', | ||
| value: '#0066cc', | ||
| description: 'Button background color', | ||
| }, | ||
| ], | ||
| }, | ||
| }), | ||
| })) | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('returns CSS tokens for a valid page', async () => { | ||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'alert', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/alert/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8') | ||
| expect(Array.isArray(body)).toBe(true) | ||
| expect(body).toHaveLength(2) | ||
| expect(body[0]).toHaveProperty('name') | ||
| expect(body[0]).toHaveProperty('value') | ||
| expect(body[0]).toHaveProperty('description') | ||
| expect(body[0].name).toBe('--pf-v6-c-alert--BackgroundColor') | ||
| expect(body[0].value).toBe('#ffffff') | ||
| }) | ||
|
|
||
| it('returns CSS tokens for different pages', async () => { | ||
| const buttonResponse = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'button', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/button/css'), | ||
| } as any) | ||
| const buttonBody = await buttonResponse.json() | ||
|
|
||
| expect(buttonResponse.status).toBe(200) | ||
| expect(Array.isArray(buttonBody)).toBe(true) | ||
| expect(buttonBody).toHaveLength(1) | ||
| expect(buttonBody[0].name).toBe('--pf-v6-c-button--BackgroundColor') | ||
| }) | ||
|
|
||
| it('returns 404 error when no CSS tokens are found', async () => { | ||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'nonexistent', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/nonexistent/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(404) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('No CSS tokens found') | ||
| expect(body.error).toContain('nonexistent') | ||
| expect(body.error).toContain('components') | ||
| expect(body.error).toContain('v6') | ||
| expect(body.error).toContain('cssPrefix') | ||
| }) | ||
|
|
||
| it('returns 400 error when version parameter is missing', async () => { | ||
| const response = await GET({ | ||
| params: { | ||
| section: 'components', | ||
| page: 'alert', | ||
| }, | ||
| url: new URL('http://localhost/api/components/alert/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('Version, section, and page parameters are required') | ||
| }) | ||
|
|
||
| it('returns 400 error when section parameter is missing', async () => { | ||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| page: 'alert', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/alert/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('Version, section, and page parameters are required') | ||
| }) | ||
|
|
||
| it('returns 400 error when page parameter is missing', async () => { | ||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('Version, section, and page parameters are required') | ||
| }) | ||
|
|
||
| it('returns 400 error when all parameters are missing', async () => { | ||
| const response = await GET({ | ||
| params: {}, | ||
| url: new URL('http://localhost/api/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('Version, section, and page parameters are required') | ||
| }) | ||
|
|
||
| it('returns 500 error when fetchApiIndex fails', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const { fetchApiIndex } = require('../../../../../../../utils/apiIndex/fetch') | ||
| fetchApiIndex.mockRejectedValueOnce(new Error('Network error')) | ||
|
|
||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'alert', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/alert/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(500) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body).toHaveProperty('details') | ||
| expect(body.error).toBe('Failed to load API index') | ||
| expect(body.details).toBe('Network error') | ||
| }) | ||
|
|
||
| it('returns 500 error when fetchApiIndex throws a non-Error object', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const { fetchApiIndex } = require('../../../../../../../utils/apiIndex/fetch') | ||
| fetchApiIndex.mockRejectedValueOnce('String error') | ||
|
|
||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'alert', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/alert/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(500) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body).toHaveProperty('details') | ||
| expect(body.error).toBe('Failed to load API index') | ||
| expect(body.details).toBe('String error') | ||
| }) | ||
|
|
||
| it('returns empty array when CSS tokens array exists but is empty', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const { fetchApiIndex } = require('../../../../../../../utils/apiIndex/fetch') | ||
| fetchApiIndex.mockResolvedValueOnce({ | ||
| versions: ['v6'], | ||
| sections: { | ||
| v6: ['components'], | ||
| }, | ||
| pages: { | ||
| 'v6::components': ['empty'], | ||
| }, | ||
| tabs: { | ||
| 'v6::components::empty': ['react'], | ||
| }, | ||
| css: { | ||
| 'v6::components::empty': [], | ||
| }, | ||
| }) | ||
|
|
||
| const response = await GET({ | ||
| params: { | ||
| version: 'v6', | ||
| section: 'components', | ||
| page: 'empty', | ||
| }, | ||
| url: new URL('http://localhost/api/v6/components/empty/css'), | ||
| } as any) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(404) | ||
| expect(body).toHaveProperty('error') | ||
| expect(body.error).toContain('No CSS tokens found') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { APIRoute } from 'astro' | ||
| import { createJsonResponse, createIndexKey } from '../../../../../utils/apiHelpers' | ||
| import { fetchApiIndex } from '../../../../../utils/apiIndex/fetch' | ||
|
|
||
| export const prerender = false | ||
|
|
||
| export const GET: APIRoute = async ({ params, url }) => { | ||
| const { version, section, page } = params | ||
|
|
||
| if (!version || !section || !page) { | ||
| return createJsonResponse( | ||
| { error: 'Version, section, and page parameters are required' }, | ||
| 400, | ||
| ) | ||
| } | ||
|
|
||
| try { | ||
| const index = await fetchApiIndex(url) | ||
| const pageKey = createIndexKey(version, section, page) | ||
| const cssTokens = index.css[pageKey] || [] | ||
|
|
||
| if (cssTokens.length === 0) { | ||
| return createJsonResponse( | ||
| { | ||
| error: `No CSS tokens found for page '${page}' in section '${section}' for version '${version}'. CSS tokens are only available for content with a cssPrefix in the front matter.`, | ||
| }, | ||
| 404, | ||
| ) | ||
| } | ||
|
|
||
| return createJsonResponse(cssTokens) | ||
| } catch (error) { | ||
| const details = error instanceof Error ? error.message : String(error) | ||
| return createJsonResponse( | ||
| { error: 'Failed to load API index', details }, | ||
| 500, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs mismatch: CSS token fields don’t match actual payload.
The API returns
{ name, value, var }, but the schema usestokenNameandvariableName. Please align the docs with the real response shape.📝 Proposed doc fix
🤖 Prompt for AI Agents