-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.ts
More file actions
140 lines (132 loc) · 4.58 KB
/
doc.ts
File metadata and controls
140 lines (132 loc) · 4.58 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { Def, DefBase } from '@01edu/types/validator'
import type { GenericRoutes } from '@01edu/types/router'
import { route } from './router.ts'
import { ARR, BOOL, LIST, OBJ, optional, STR } from './validator.ts'
/**
* Recursive type representing the structure of input/output documentation.
* It mirrors the structure of the validator definitions but simplified for documentation purposes.
*/
export type Documentation =
& (
| { type: Exclude<DefBase['type'], 'object' | 'array' | 'list' | 'union'> }
| { type: 'object'; properties: Record<string, Documentation> }
| { type: 'array'; items: Documentation }
| { type: 'list'; options: (string | number)[] }
| { type: 'union'; options: Documentation[] }
)
& { description?: string; optional?: boolean }
/**
* Represents the documentation for a single API endpoint.
*/
export type EndpointDoc = {
method: string
path: string
requiresAuth: boolean
authFunction: string
description?: string
input?: Documentation
output?: Documentation
}
/**
* Extracts documentation from a validator definition.
* Recursively processes objects and arrays to build a `Documentation` structure.
*
* @param def - The validator definition to extract documentation from.
* @returns The extracted documentation or undefined if no definition is provided.
*/
function extractDocs(def?: Def): Documentation | undefined {
if (!def) return undefined
const base = {
type: def.type,
description: def.description,
optional: def.optional,
}
switch (def.type) {
case 'object': {
const properties: Record<string, Documentation> = {}
for (const [key, value] of Object.entries(def.properties)) {
const doc = extractDocs(value)
if (doc) {
properties[key] = doc
}
}
return { ...base, properties, type: 'object' }
}
case 'array': {
const items = extractDocs(def.of) as Documentation
return { ...base, items, type: 'array' }
}
case 'list':
return { ...base, options: def.of as (string | number)[], type: 'list' }
case 'union':
return {
...base,
options: def.of.map((d: Def) => extractDocs(d) as Documentation),
type: 'union',
}
case 'boolean':
return { ...base, type: 'boolean' }
case 'number':
return { ...base, type: 'number' }
case 'string':
return { ...base, type: 'string' }
}
}
/**
* Generates API documentation for a set of routes.
* Iterates through the route definitions and extracts metadata, input, and output documentation.
*
* @param defs - The route definitions to generate documentation for.
* @returns An array of `EndpointDoc` objects describing the API.
*/
export const generateApiDocs = (defs: GenericRoutes) => {
return Object.entries<typeof defs[keyof typeof defs]>(defs).map(
([key, handler]) => {
const slashIndex = key.indexOf('/')
const method = key.slice(0, slashIndex).toUpperCase()
const path = key.slice(slashIndex)
const requiresAuth = handler.authorize ? true : false
return {
method,
path,
requiresAuth,
authFunction: handler.authorize?.name || '',
description: 'description' in handler ? handler.description : undefined,
input: 'input' in handler ? extractDocs(handler.input) : undefined,
output: 'output' in handler ? extractDocs(handler.output) : undefined,
}
},
)
}
const encoder = new TextEncoder()
const apiDocOutputDef: Def = ARR(
OBJ({
method: LIST(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], 'HTTP method'),
path: STR('API endpoint path'),
requiresAuth: BOOL('whether authentication is required'),
authFunction: STR('name of the authorization function'),
description: STR('Endpoint description'),
input: optional(OBJ({}, 'Input documentation structure')),
output: optional(OBJ({}, 'Output documentation structure')),
}, 'API documentation object structure'),
'API documentation array',
)
/**
* Creates a route handler that serves the generated API documentation.
* The documentation is served as a JSON array of `EndpointDoc` objects.
*
* @param defs - The route definitions to generate documentation for.
* @returns A route handler that serves the API documentation.
*/
export const createDocRoute = (defs: GenericRoutes) => {
const docStr = JSON.stringify(generateApiDocs(defs))
const docBuffer = encoder.encode(docStr)
return route({
fn: () =>
new Response(docBuffer, {
headers: { 'content-type': 'application/json' },
}),
output: apiDocOutputDef,
description: 'Get the API documentation',
})
}