Skip to content

Commit 76919d9

Browse files
chore(API): only return available tabs as an array for the [page] route
1 parent ed901de commit 76919d9

File tree

4 files changed

+16
-63
lines changed

4 files changed

+16
-63
lines changed

src/pages/text/[version]/[section]/[page].ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { CollectionEntry, CollectionKey } from 'astro:content'
33
import { getCollection } from 'astro:content'
44
import { content } from '../../../../content'
55
import { kebabCase, getDefaultTab, addDemosOrDeprecated } from '../../../../utils'
6-
import { tabNames } from '../../../../globals'
76

87
export const prerender = false
98

@@ -43,8 +42,8 @@ export const GET: APIRoute = async ({ params }) => {
4342
const tabsDictionary: Record<string, string[]> = {}
4443

4544
collections.flat().forEach((entry: ContentEntry) => {
46-
const { tab, source, tabName } = entry.data
47-
const hasTab = !!tab || !!source || !!tabName
45+
const { tab, source } = entry.data
46+
const hasTab = !!tab || !!source
4847
let finalTab = tab
4948

5049
if (!hasTab) {
@@ -63,10 +62,6 @@ export const GET: APIRoute = async ({ params }) => {
6362
tabsDictionary[entry.data.id] = [...tabEntry, finalTab]
6463
}
6564
}
66-
67-
if (tabName) {
68-
tabNames[tab] = tabName
69-
}
7065
})
7166

7267
// Sort tabs
@@ -117,12 +112,7 @@ export const GET: APIRoute = async ({ params }) => {
117112
const tabs = tabsDictionary[matchingEntry.data.id] || []
118113

119114
return new Response(
120-
JSON.stringify(
121-
tabs.map((tab) => ({
122-
slug: tab,
123-
name: tabNames[tab] || tab,
124-
})),
125-
),
115+
JSON.stringify(tabs),
126116
{
127117
status: 200,
128118
headers: {

src/pages/text/__tests__/[version]/[section]/[page].test.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ jest.mock('../../../../../utils', () => {
3434
return mockUtils
3535
})
3636

37-
/**
38-
* Mock tab name dictionary for display names
39-
*/
40-
jest.mock('../../../../../globals', () => {
41-
const { mockTabNames } = jest.requireActual('../../testHelpers')
42-
return { tabNames: mockTabNames }
43-
})
44-
4537
beforeEach(() => {
4638
jest.clearAllMocks()
4739
})
@@ -58,17 +50,14 @@ it('returns all tabs available for a page', async () => {
5850
expect(body.length).toBeGreaterThan(0)
5951
})
6052

61-
it('includes slug and display name for each tab', async () => {
53+
it('returns tab slugs as strings', async () => {
6254
const response = await GET({
6355
params: { version: 'v6', section: 'components', page: 'alert' },
6456
} as any)
6557
const body = await response.json()
6658

6759
body.forEach((tab: any) => {
68-
expect(tab).toHaveProperty('slug')
69-
expect(tab).toHaveProperty('name')
70-
expect(typeof tab.slug).toBe('string')
71-
expect(typeof tab.name).toBe('string')
60+
expect(typeof tab).toBe('string')
7261
})
7362
})
7463

@@ -79,9 +68,8 @@ it('sorts tabs by priority (React before HTML, design guidelines last)', async (
7968
const body = await response.json()
8069

8170
// Check that tabs are in expected order (react before html, design guidelines near end)
82-
const slugs = body.map((t: any) => t.slug)
83-
const reactIndex = slugs.indexOf('react')
84-
const htmlIndex = slugs.indexOf('html')
71+
const reactIndex = body.indexOf('react')
72+
const htmlIndex = body.indexOf('html')
8573

8674
if (reactIndex !== -1 && htmlIndex !== -1) {
8775
expect(reactIndex).toBeLessThan(htmlIndex)
@@ -138,6 +126,5 @@ it('aggregates tabs from multiple collections for the same page', async () => {
138126
const body = await response.json()
139127

140128
// Should have tabs from both react-component-docs and core-docs
141-
const slugs = body.map((t: any) => t.slug)
142-
expect(slugs.length).toBeGreaterThan(1)
129+
expect(body.length).toBeGreaterThan(1)
143130
})

src/pages/text/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,9 @@ export const GET: APIRoute = async () => new Response(
112112
],
113113
returns: {
114114
type: 'array',
115-
items: {
116-
type: 'object',
117-
properties: {
118-
slug: 'string',
119-
name: 'string',
120-
},
121-
},
122-
example: [
123-
{ slug: 'react', name: 'React' },
124-
{ slug: 'react-demos', name: 'React demos' },
125-
{ slug: 'html', name: 'HTML' },
126-
],
115+
items: 'string',
116+
description: 'Array of tab slugs',
117+
example: ['react', 'react-demos', 'html'],
127118
},
128119
},
129120
{
@@ -173,7 +164,7 @@ export const GET: APIRoute = async () => new Response(
173164
'GET /text/versions → ["v6"]',
174165
'GET /text/v6 → ["components", "layouts", ...]',
175166
'GET /text/v6/components → ["alert", "button", ...]',
176-
'GET /text/v6/components/alert → [{slug: "react", name: "React"}, ...]',
167+
'GET /text/v6/components/alert → ["react", "html", ...]',
177168
'GET /text/v6/components/alert/react → (markdown content)',
178169
],
179170
},

src/pages/text/openapi.json.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export const GET: APIRoute = async () => {
222222
get: {
223223
summary: 'List tabs for a page',
224224
description:
225-
'Returns an array of available tabs for the specified page with slug and display name',
225+
'Returns an array of available tab slugs for the specified page',
226226
operationId: 'getTabs',
227227
parameters: [
228228
{
@@ -259,31 +259,16 @@ export const GET: APIRoute = async () => {
259259
],
260260
responses: {
261261
'200': {
262-
description: 'List of available tabs',
262+
description: 'List of available tab slugs',
263263
content: {
264264
'application/json': {
265265
schema: {
266266
type: 'array',
267267
items: {
268-
type: 'object',
269-
properties: {
270-
slug: {
271-
type: 'string',
272-
description: 'Tab identifier',
273-
},
274-
name: {
275-
type: 'string',
276-
description: 'Tab display name',
277-
},
278-
},
279-
required: ['slug', 'name'],
268+
type: 'string',
280269
},
281270
},
282-
example: [
283-
{ slug: 'react', name: 'React' },
284-
{ slug: 'react-demos', name: 'React demos' },
285-
{ slug: 'html', name: 'HTML' },
286-
],
271+
example: ['react', 'react-demos', 'html'],
287272
},
288273
},
289274
},

0 commit comments

Comments
 (0)