Skip to content

Commit c1ce1b8

Browse files
chore(API): clean up [page] API logic
1 parent 76919d9 commit c1ce1b8

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import type { APIRoute } from 'astro'
22
import type { CollectionEntry, CollectionKey } from 'astro:content'
33
import { getCollection } from 'astro:content'
44
import { content } from '../../../../content'
5-
import { kebabCase, getDefaultTab, addDemosOrDeprecated } from '../../../../utils'
5+
import {
6+
kebabCase,
7+
getDefaultTab,
8+
addDemosOrDeprecated,
9+
} from '../../../../utils'
610

711
export const prerender = false
812

@@ -39,30 +43,29 @@ export const GET: APIRoute = async ({ params }) => {
3943
)
4044

4145
// Build tabs dictionary similar to the main page logic
42-
const tabsDictionary: Record<string, string[]> = {}
43-
44-
collections.flat().forEach((entry: ContentEntry) => {
45-
const { tab, source } = entry.data
46-
const hasTab = !!tab || !!source
47-
let finalTab = tab
48-
49-
if (!hasTab) {
50-
finalTab = getDefaultTab(entry.filePath)
51-
}
52-
53-
if (hasTab && entry.id) {
54-
finalTab = addDemosOrDeprecated(entry.data.tab, entry.id)
55-
}
46+
const tabsDictionary: Record<string, string[]> = collections
47+
.flat()
48+
.reduce((acc: Record<string, string[]>, entry: ContentEntry) => {
49+
const { tab: specifiedTab, source } = entry.data
50+
const hasTab = !!specifiedTab || !!source
51+
let tab = specifiedTab
52+
53+
if (!hasTab) {
54+
tab = getDefaultTab(entry.filePath)
55+
} else {
56+
tab = addDemosOrDeprecated(specifiedTab, entry.id)
57+
}
5658

57-
if (finalTab) {
58-
const tabEntry = tabsDictionary[entry.data.id]
59+
const tabEntry = acc[entry.data.id]
60+
5961
if (tabEntry === undefined) {
60-
tabsDictionary[entry.data.id] = [finalTab]
61-
} else if (!tabEntry.includes(finalTab)) {
62-
tabsDictionary[entry.data.id] = [...tabEntry, finalTab]
62+
acc[entry.data.id] = [tab]
63+
} else if (!tabEntry.includes(tab)) {
64+
acc[entry.data.id] = [...tabEntry, tab]
6365
}
64-
}
65-
})
66+
67+
return acc
68+
}, {})
6669

6770
// Sort tabs
6871
const defaultOrder = 50
@@ -96,8 +99,7 @@ export const GET: APIRoute = async ({ params }) => {
9699
const flatEntries = collections.flat()
97100
const matchingEntry = flatEntries.find(
98101
(entry: ContentEntry) =>
99-
entry.data.section === section &&
100-
kebabCase(entry.data.id) === page,
102+
entry.data.section === section && kebabCase(entry.data.id) === page,
101103
)
102104

103105
if (!matchingEntry) {
@@ -111,13 +113,10 @@ export const GET: APIRoute = async ({ params }) => {
111113

112114
const tabs = tabsDictionary[matchingEntry.data.id] || []
113115

114-
return new Response(
115-
JSON.stringify(tabs),
116-
{
117-
status: 200,
118-
headers: {
119-
'Content-Type': 'application/json',
120-
},
116+
return new Response(JSON.stringify(tabs), {
117+
status: 200,
118+
headers: {
119+
'Content-Type': 'application/json',
121120
},
122-
)
121+
})
123122
}

0 commit comments

Comments
 (0)