-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathconfig.ts
More file actions
86 lines (77 loc) · 2.26 KB
/
config.ts
File metadata and controls
86 lines (77 loc) · 2.26 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
import { z } from 'zod'
import { fetchRepoFile } from './documents.server'
import { createServerFn } from '@tanstack/start'
import { setHeaders } from 'vinxi/http'
export type MenuItem = {
label: string | React.ReactNode
children: {
label: string | React.ReactNode
to: string
badge?: string
}[]
collapsible?: boolean
defaultCollapsed?: boolean
}
const configSchema = z.object({
sections: z.array(
z.object({
label: z.string(),
children: z.array(
z.object({
label: z.string(),
to: z.string(),
badge: z.string().optional(),
})
),
frameworks: z
.array(
z.object({
label: z.string(),
children: z.array(
z.object({
label: z.string(),
to: z.string(),
badge: z.string().optional(),
})
),
})
)
.optional(),
collapsible: z.boolean().optional(),
defaultCollapsed: z.boolean().optional(),
})
),
users: z.array(z.string()).optional(),
})
export type ConfigSchema = z.infer<typeof configSchema>
/**
Fetch the config file for the project and validate it.
*/
export const getTanstackDocsConfig = createServerFn({ method: 'GET' })
.validator(
z.object({ repo: z.string(), branch: z.string(), docsRoot: z.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 = configSchema.safeParse(
tanstackDocsConfigFromJson
)
if (!validationResult.success) {
// Log the issues that come up during validation
console.error(JSON.stringify(validationResult.error, null, 2))
throw new Error('Zod validation failed')
}
setHeaders({
'cache-control': 'public, max-age=0, must-revalidate',
'cdn-cache-control': 'max-age=300, stale-while-revalidate=300, durable',
})
return validationResult.data
} catch (e) {
throw new Error('Invalid docs/config.json file')
}
})