-
-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathconfig.ts
More file actions
91 lines (82 loc) · 2.45 KB
/
config.ts
File metadata and controls
91 lines (82 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as v from 'valibot'
import { fetchRepoFile } from './documents.server'
import { createServerFn } from '@tanstack/react-start'
import { setResponseHeaders } from '@tanstack/react-start/server'
export type MenuItem = {
label: string | React.ReactNode
children: {
label: string | React.ReactNode
to: string
badge?: string
}[]
collapsible?: boolean
defaultCollapsed?: boolean
}
const configSchema = v.object({
sections: v.array(
v.object({
label: v.string(),
children: v.array(
v.object({
label: v.string(),
to: v.string(),
badge: v.optional(v.string()),
}),
),
frameworks: v.optional(
v.array(
v.object({
label: v.string(),
children: v.array(
v.object({
label: v.string(),
to: v.string(),
badge: v.optional(v.string()),
}),
),
}),
),
),
collapsible: v.optional(v.boolean()),
defaultCollapsed: v.optional(v.boolean()),
}),
),
users: v.optional(v.array(v.string())),
})
export type ConfigSchema = v.InferOutput<typeof configSchema>
/**
Fetch the config file for the project and validate it.
*/
export const getTanstackDocsConfig = createServerFn({ method: 'POST' })
.inputValidator(
v.object({ repo: v.string(), branch: v.string(), docsRoot: v.string() }),
)
.handler(async ({ data: { repo, branch, docsRoot } }) => {
const config = await fetchRepoFile(repo, branch, `${docsRoot}/config.json`)
if (!config) {
throw new Error(`Repo's ${docsRoot}/config.json was not found!`)
}
try {
const tanstackDocsConfigFromJson = JSON.parse(config)
const validationResult = v.safeParse(
configSchema,
tanstackDocsConfigFromJson,
)
if (!validationResult.success) {
// Log the issues that come up during validation
console.error(JSON.stringify(validationResult.issues, null, 2))
throw new Error('Valibot validation failed')
}
setResponseHeaders(
new Headers({
'Cache-Control': 'public, max-age=0, must-revalidate',
'Netlify-CDN-Cache-Control':
'public, max-age=300, durable, stale-while-revalidate=300',
}),
)
return validationResult.output
} catch (e) {
console.error(e)
throw new Error('Invalid docs/config.json file', { cause: e })
}
})