Skip to content

Commit 215c9f2

Browse files
Address feedback
1 parent c1ce1b8 commit 215c9f2

File tree

15 files changed

+277
-98
lines changed

15 files changed

+277
-98
lines changed

src/pages/text/[version].ts renamed to src/pages/api/[version].ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 { createJsonResponse } from '../../utils/apiHelpers'
56

67
export const prerender = false
78

@@ -13,9 +14,9 @@ export const GET: APIRoute = async ({ params }) => {
1314
const { version } = params
1415

1516
if (!version) {
16-
return new Response(
17-
JSON.stringify({ error: 'Version parameter is required' }),
18-
{ status: 400, headers: { 'Content-Type': 'application/json' } },
17+
return createJsonResponse(
18+
{ error: 'Version parameter is required' },
19+
400,
1920
)
2021
}
2122

@@ -33,10 +34,7 @@ export const GET: APIRoute = async ({ params }) => {
3334
.map((entry) => entry.name as CollectionKey)
3435

3536
if (collectionsToFetch.length === 0) {
36-
return new Response(
37-
JSON.stringify({ error: `Version '${version}' not found` }),
38-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
39-
)
37+
return createJsonResponse({ error: `Version '${version}' not found` }, 404)
4038
}
4139

4240
const collections = await Promise.all(
@@ -50,13 +48,5 @@ export const GET: APIRoute = async ({ params }) => {
5048
}
5149
})
5250

53-
return new Response(
54-
JSON.stringify(Array.from(sections).sort()),
55-
{
56-
status: 200,
57-
headers: {
58-
'Content-Type': 'application/json',
59-
},
60-
},
61-
)
51+
return createJsonResponse(Array.from(sections).sort())
6252
}

src/pages/text/[version]/[section].ts renamed to src/pages/api/[version]/[section].ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CollectionEntry, CollectionKey } from 'astro:content'
33
import { getCollection } from 'astro:content'
44
import { content } from '../../../content'
55
import { kebabCase } from '../../../utils'
6+
import { createJsonResponse } from '../../../utils/apiHelpers'
67

78
export const prerender = false
89

@@ -14,9 +15,9 @@ export const GET: APIRoute = async ({ params }) => {
1415
const { version, section } = params
1516

1617
if (!version || !section) {
17-
return new Response(
18-
JSON.stringify({ error: 'Version and section parameters are required' }),
19-
{ status: 400, headers: { 'Content-Type': 'application/json' } },
18+
return createJsonResponse(
19+
{ error: 'Version and section parameters are required' },
20+
400,
2021
)
2122
}
2223

@@ -26,10 +27,7 @@ export const GET: APIRoute = async ({ params }) => {
2627
.map((entry) => entry.name as CollectionKey)
2728

2829
if (collectionsToFetch.length === 0) {
29-
return new Response(
30-
JSON.stringify({ error: `Version '${version}' not found` }),
31-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
32-
)
30+
return createJsonResponse({ error: `Version '${version}' not found` }, 404)
3331
}
3432

3533
const collections = await Promise.all(
@@ -44,18 +42,11 @@ export const GET: APIRoute = async ({ params }) => {
4442
})
4543

4644
if (pages.size === 0) {
47-
return new Response(
48-
JSON.stringify({
49-
error: `Section '${section}' not found for version '${version}'`,
50-
}),
51-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
45+
return createJsonResponse(
46+
{ error: `Section '${section}' not found for version '${version}'` },
47+
404,
5248
)
5349
}
5450

55-
return new Response(JSON.stringify(Array.from(pages).sort()), {
56-
status: 200,
57-
headers: {
58-
'Content-Type': 'application/json',
59-
},
60-
})
51+
return createJsonResponse(Array.from(pages).sort())
6152
}

src/pages/text/[version]/[section]/[page].ts renamed to src/pages/api/[version]/[section]/[page].ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getDefaultTab,
88
addDemosOrDeprecated,
99
} from '../../../../utils'
10+
import { createJsonResponse } from '../../../../utils/apiHelpers'
1011

1112
export const prerender = false
1213

@@ -18,11 +19,9 @@ export const GET: APIRoute = async ({ params }) => {
1819
const { version, section, page } = params
1920

2021
if (!version || !section || !page) {
21-
return new Response(
22-
JSON.stringify({
23-
error: 'Version, section, and page parameters are required',
24-
}),
25-
{ status: 400, headers: { 'Content-Type': 'application/json' } },
22+
return createJsonResponse(
23+
{ error: 'Version, section, and page parameters are required' },
24+
400,
2625
)
2726
}
2827

@@ -32,10 +31,7 @@ export const GET: APIRoute = async ({ params }) => {
3231
.map((entry) => entry.name as CollectionKey)
3332

3433
if (collectionsToFetch.length === 0) {
35-
return new Response(
36-
JSON.stringify({ error: `Version '${version}' not found` }),
37-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
38-
)
34+
return createJsonResponse({ error: `Version '${version}' not found` }, 404)
3935
}
4036

4137
const collections = await Promise.all(
@@ -103,20 +99,15 @@ export const GET: APIRoute = async ({ params }) => {
10399
)
104100

105101
if (!matchingEntry) {
106-
return new Response(
107-
JSON.stringify({
102+
return createJsonResponse(
103+
{
108104
error: `Page '${page}' not found in section '${section}' for version '${version}'`,
109-
}),
110-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
105+
},
106+
404,
111107
)
112108
}
113109

114110
const tabs = tabsDictionary[matchingEntry.data.id] || []
115111

116-
return new Response(JSON.stringify(tabs), {
117-
status: 200,
118-
headers: {
119-
'Content-Type': 'application/json',
120-
},
121-
})
112+
return createJsonResponse(tabs)
122113
}

src/pages/text/[version]/[section]/[page]/[tab].ts renamed to src/pages/api/[version]/[section]/[page]/[tab].ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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 { createJsonResponse, createTextResponse } from '../../../../../utils/apiHelpers'
67

78
export const prerender = false
89

@@ -14,11 +15,9 @@ export const GET: APIRoute = async ({ params }) => {
1415
const { version, section, page, tab } = params
1516

1617
if (!version || !section || !page || !tab) {
17-
return new Response(
18-
JSON.stringify({
19-
error: 'Version, section, page, and tab parameters are required',
20-
}),
21-
{ status: 400, headers: { 'Content-Type': 'application/json' } },
18+
return createJsonResponse(
19+
{ error: 'Version, section, page, and tab parameters are required' },
20+
400,
2221
)
2322
}
2423

@@ -28,10 +27,7 @@ export const GET: APIRoute = async ({ params }) => {
2827
.map((entry) => entry.name as CollectionKey)
2928

3029
if (collectionsToFetch.length === 0) {
31-
return new Response(
32-
JSON.stringify({ error: `Version '${version}' not found` }),
33-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
34-
)
30+
return createJsonResponse({ error: `Version '${version}' not found` }, 404)
3531
}
3632

3733
const collections = await Promise.all(
@@ -60,21 +56,16 @@ export const GET: APIRoute = async ({ params }) => {
6056
})
6157

6258
if (!matchingEntry) {
63-
return new Response(
64-
JSON.stringify({
59+
return createJsonResponse(
60+
{
6561
error: `Tab '${tab}' not found for page '${page}' in section '${section}' for version '${version}'`,
66-
}),
67-
{ status: 404, headers: { 'Content-Type': 'application/json' } },
62+
},
63+
404,
6864
)
6965
}
7066

7167
// Get the raw body content (markdown/mdx text)
7268
const textContent = matchingEntry.body || ''
7369

74-
return new Response(textContent, {
75-
status: 200,
76-
headers: {
77-
'Content-Type': 'text/plain; charset=utf-8',
78-
},
79-
})
70+
return createTextResponse(textContent)
8071
}

src/pages/text/__tests__/[version].test.ts renamed to src/pages/api/__tests__/[version].test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ it('returns all sections for a valid version', async () => {
4444
const body = await response.json()
4545

4646
expect(response.status).toBe(200)
47-
expect(response.headers.get('Content-Type')).toBe('application/json')
47+
expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8')
4848
expect(Array.isArray(body)).toBe(true)
4949
expect(body).toContain('components')
5050
expect(body).toContain('layouts')

src/pages/text/__tests__/[version]/[section].test.ts renamed to src/pages/api/__tests__/[version]/[section].test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ it('returns all pages within a section', async () => {
5252
const body = await response.json()
5353

5454
expect(response.status).toBe(200)
55-
expect(response.headers.get('Content-Type')).toBe('application/json')
55+
expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8')
5656
expect(Array.isArray(body)).toBe(true)
5757
expect(body).toContain('alert')
5858
expect(body).toContain('button')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ it('returns all tabs available for a page', async () => {
4545
const body = await response.json()
4646

4747
expect(response.status).toBe(200)
48-
expect(response.headers.get('Content-Type')).toBe('application/json')
48+
expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8')
4949
expect(Array.isArray(body)).toBe(true)
5050
expect(body.length).toBeGreaterThan(0)
5151
})
File renamed without changes.

src/pages/text/__tests__/versions.test.ts renamed to src/pages/api/__tests__/versions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ it('returns unique versions as sorted array', async () => {
1717
const body = await response.json()
1818

1919
expect(response.status).toBe(200)
20-
expect(response.headers.get('Content-Type')).toBe('application/json')
20+
expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8')
2121
expect(body).toEqual(['v5', 'v6'])
2222
})
2323

0 commit comments

Comments
 (0)