Repository navigation
Expand file tree
/
Copy pathendpoint-docs.ts
More file actions
158 lines (144 loc) · 5.7 KB
/
Copy pathendpoint-docs.ts
File metadata and controls
158 lines (144 loc) · 5.7 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* The docs.github.com page behind every REST route and GraphQL operation the sections declare, for the coverage
* page's Endpoints cells (gen-docs.ts). REST pages are the OpenAPI descriptor's own externalDocs links;
* endpoint-docs.yml beside this file holds only what the descriptor lacks: the routes GitHub does not document and
* every GraphQL operation. resolveAnchors() fails the docs build by name on a declared call with no page, a hand
* entry the descriptor already covers, and a hand entry no section declares.
*/
import { join } from "node:path";
import { z } from "zod";
import { readDocsYaml } from "../../src/sections/contract/docs.js";
import { allEndpoints, allGraphqlOps } from "../../src/sections/registry.js";
import { loadSpec } from "../../test/e2e/openapi/validate.js";
const DOCS_URL = z.string().url().startsWith("https://docs.github.com/en/");
const ROUTE = /^(?:GET|POST|PUT|PATCH|DELETE) \/\S+$/;
const OPERATION = /^[A-Z][A-Za-z0-9]*$/;
export const EndpointDocs = z
.strictObject({
rest: z
.record(z.string().regex(ROUTE, 'a route is "METHOD /path/{param}"'), DOCS_URL)
.readonly(),
graphql: z
.record(z.string().regex(OPERATION, "an operation name is UpperCamelCase"), DOCS_URL)
.readonly(),
})
.readonly();
export type EndpointDocs = z.infer<typeof EndpointDocs>;
export const ENDPOINT_DOCS_PATH = ".github/scripts/endpoint-docs.yml";
export const ENDPOINT_DOCS = readDocsYaml(
join(import.meta.dir, "endpoint-docs.yml"),
EndpointDocs,
).match(
(docs) => docs,
(problem) => {
throw new Error(problem);
},
);
/** The page of every declared route and operation, resolved; the renderer reads these and nothing else. */
export interface EndpointAnchors {
readonly rest: Readonly<Record<string, string>>;
readonly graphql: Readonly<Record<string, string>>;
}
/** The descriptor as far as the anchors read it: `paths[path][method].externalDocs.url`. */
export type SpecOperations = Readonly<
Record<string, Readonly<Record<string, { readonly externalDocs?: { readonly url: string } }>>>
>;
/** One kind's pages by call, and every problem met resolving them. */
interface Resolved {
readonly pages: Record<string, string>;
readonly problems: string[];
}
/** The descriptor's link in the spelling docs.github.com serves: the locale added, the Pages placeholder rendered. */
export function renderedDocsUrl(url: string): string {
return url
.replace("https://docs.github.com/rest/", "https://docs.github.com/en/rest/")
.replace("apiname", "github");
}
/** Each declared route's page: the descriptor's, else the hand entry; both or neither is a problem, as is a hand entry no section declares. */
function resolveRest(
spec: SpecOperations,
hand: EndpointDocs,
routes: readonly string[],
): Resolved {
const pages: Record<string, string> = {};
const problems: string[] = [];
for (const route of routes) {
const space = route.indexOf(" ");
const documented =
spec[route.slice(space + 1)]?.[route.slice(0, space).toLowerCase()]?.externalDocs?.url;
const byHand = hand.rest[route];
if (documented !== undefined && byHand !== undefined) {
problems.push(
`"${route}" has a page in both the descriptor and ${ENDPOINT_DOCS_PATH}; delete the hand entry`,
);
continue;
}
if (documented !== undefined) {
pages[route] = renderedDocsUrl(documented);
continue;
}
if (byHand !== undefined) {
pages[route] = byHand;
continue;
}
problems.push(
`"${route}" has no page: the descriptor does not document it, so add one under rest: in ${ENDPOINT_DOCS_PATH}`,
);
}
for (const route of Object.keys(hand.rest)) {
if (!routes.includes(route)) {
problems.push(
`${ENDPOINT_DOCS_PATH} names "${route}" under rest:, which no section declares; delete the entry`,
);
}
}
return { pages, problems };
}
/** Each declared operation's page, which only the hand file holds; a missing one and a hand entry no section declares are problems. */
function resolveGraphql(hand: EndpointDocs, operations: readonly string[]): Resolved {
const pages: Record<string, string> = {};
const problems: string[] = [];
for (const name of operations) {
const byHand = hand.graphql[name];
if (byHand === undefined) {
problems.push(
`the GraphQL operation "${name}" has no page; add one under graphql: in ${ENDPOINT_DOCS_PATH}`,
);
continue;
}
pages[name] = byHand;
}
for (const name of Object.keys(hand.graphql)) {
if (!operations.includes(name)) {
problems.push(
`${ENDPOINT_DOCS_PATH} names "${name}" under graphql:, which no section declares; delete the entry`,
);
}
}
return { pages, problems };
}
export function resolveAnchors(
spec: SpecOperations,
hand: EndpointDocs,
routes: readonly string[],
operations: readonly string[],
): EndpointAnchors {
const rest = resolveRest(spec, hand, routes);
const graphql = resolveGraphql(hand, operations);
const problems = [...rest.problems, ...graphql.problems];
if (problems.length > 0) {
throw new Error(`endpoint-docs:\n ${problems.join("\n ")}`);
}
return { rest: rest.pages, graphql: graphql.pages };
}
/** The distinct routes and operation names the sections declare, in registry order. */
export function declaredCalls(): { routes: string[]; operations: string[] } {
return {
routes: [...new Set(Object.values(allEndpoints()).map((endpoint) => endpoint.route))],
operations: [...new Set(Object.values(allGraphqlOps()).map((op) => op.name))],
};
}
export const ENDPOINT_ANCHORS: EndpointAnchors = (() => {
const { routes, operations } = declaredCalls();
return resolveAnchors(loadSpec().paths, ENDPOINT_DOCS, routes, operations);
})();