-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b0d6828
feat(API): Added CSS API endpoint for react tokens.
dlabaj 5c701cd
Added endpoint tests.
dlabaj 69413e0
Fixed styles and linting errors.
dlabaj e094651
Removed non standard code.
dlabaj 48b7f53
Update src/utils/extractReactTokens.ts
dlabaj c4e8c97
undid code rabbit modification.
dlabaj 4f6281c
refactor(extractReactTokens): replace Function constructor with JSON …
dlabaj 584ed7d
Updated with review comments.
dlabaj a18ac3c
chore: Fixed lint error.
dlabaj 667eb46
fixed parsing error.
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
238 changes: 238 additions & 0 deletions
238
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,238 @@ | ||
| 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', | ||
| var: '--pf-v6-c-alert--BackgroundColor', | ||
| description: 'Alert background color', | ||
| }, | ||
| { | ||
| name: '--pf-v6-c-alert--Color', | ||
| value: '#151515', | ||
| var: '--pf-v6-c-alert--Color', | ||
| description: 'Alert text color', | ||
| }, | ||
| ], | ||
| 'v6::components::button': [ | ||
| { | ||
| name: '--pf-v6-c-button--BackgroundColor', | ||
| value: '#0066cc', | ||
| var: '--pf-v6-c-button--BackgroundColor', | ||
| 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('var') | ||
| 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 empty array when no CSS tokens are found for page', 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(200) | ||
| expect(Array.isArray(body)).toBe(true) | ||
| expect(body).toHaveLength(0) | ||
| }) | ||
|
|
||
| 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(200) | ||
| expect(Array.isArray(body)).toBe(true) | ||
| expect(body).toHaveLength(0) | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,34 @@ | ||
| 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([]) | ||
| } | ||
|
|
||
| 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.
I'm not seeing a description entry in the responses, should this be the var field?
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.
Done