-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.ts
More file actions
98 lines (83 loc) · 2.78 KB
/
reference.ts
File metadata and controls
98 lines (83 loc) · 2.78 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
import type { Blueprint, Route } from '@seamapi/blueprint'
import type Metalsmith from 'metalsmith'
import {
type ApiEndpointLayoutContext,
type ApiNamespaceLayoutContext,
type ApiRouteLayoutContext,
setApiRouteLayoutContext,
setEndpointLayoutContext,
setNamespaceLayoutContext,
} from './layout/index.js'
import { PathMetadataSchema } from './path-metadata.js'
type Metadata = Partial<Pick<Blueprint, 'routes' | 'resources'>>
type File = ApiEndpointLayoutContext &
ApiRouteLayoutContext &
ApiNamespaceLayoutContext & { layout: string }
const rootPath = 'api'
const indexFile = 'README.md'
export const reference = (
files: Metalsmith.Files,
metalsmith: Metalsmith,
): void => {
const metadata = metalsmith.metadata() as Metadata
// UPSTREAM: Ideally, path metadata would be unnecessary and contained inside the blueprint.
const pathMetadata =
'pathMetadata' in metadata
? PathMetadataSchema.parse(metadata.pathMetadata)
: {}
const blueprint: Blueprint = {
title: '',
routes: [],
resources: {},
events: [],
actionAttempts: [],
pagination: null,
...metadata,
}
const namespacePaths = getNamespacePaths(blueprint.routes)
for (const path of namespacePaths) {
const k = `${rootPath}${path}/${indexFile}`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-namespace.hbs'
setNamespaceLayoutContext(file, path, blueprint.resources, pathMetadata)
}
for (const route of blueprint.routes ?? []) {
if (
!route.path.startsWith('/acs') &&
!route.path.startsWith('/thermostats') &&
!route.path.startsWith('/phones') &&
!route.path.startsWith('/user_identities') &&
!route.path.startsWith('/webhooks')
) {
continue
}
if (route.isUndocumented) continue
if (pathMetadata[route.path]?.title == null) continue
const k = `${rootPath}${route.path}/${indexFile}`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-route.hbs'
setApiRouteLayoutContext(file, route, blueprint, pathMetadata)
for (const endpoint of route.endpoints) {
if (endpoint.isUndocumented) continue
if (endpoint.title.length === 0) continue
const k = `${rootPath}${endpoint.path}.md`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-endpoint.hbs'
setEndpointLayoutContext(file, endpoint, blueprint.actionAttempts)
}
}
}
const getNamespacePaths = (routes: Route[]): string[] => {
return Array.from(
new Set(
routes
.filter(({ isUndocumented }) => !isUndocumented)
.flatMap((route) =>
route.namespace != null ? [route.namespace.path] : [],
),
),
)
}