-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.ts
More file actions
474 lines (400 loc) · 11.7 KB
/
Copy pathreport.ts
File metadata and controls
474 lines (400 loc) · 11.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import type {
ActionAttempt,
Blueprint,
Endpoint,
EventResource,
Namespace,
Parameter,
Property,
Resource,
Route,
} from '@seamapi/blueprint'
import { openapi } from '@seamapi/types/connect'
import type Metalsmith from 'metalsmith'
import { apiReferenceRoot } from './config.js'
const defaultDeprecatedMessage = 'No deprecated message provided'
const defaultDraftMessage = 'No draft message provided'
const defaultUndocumentedMessage = 'No undocumented message provided'
interface Report {
undocumented: ReportSection
noDescription: ReportSection
draft: ReportSection
deprecated: ReportSection
unusedResources: UnusedResourcesReport[]
extraResponseKeys: MissingResponseKeyReport[]
missingResources: MissingResourcesReport[]
endpointsWithoutCodeSamples: string[]
resourcesWithoutResourceSamples: string[]
noTitle: Pick<ReportSection, 'namespaces' | 'routes' | 'endpoints'>
}
interface ReportSection {
routes: ReportItem[]
resources: ReportItem[]
resourceProperties: ReportItem[]
namespaces: ReportItem[]
endpoints: ReportItem[]
parameters: ParameterReportItem[]
}
interface MissingResourcesReport {
path: string
responseKey: string
}
interface UnusedResourcesReport {
name: string
}
interface MissingResponseKeyReport {
path: string
keys: string[]
}
interface ReportItem {
name: string
reason?: string
}
interface ParameterReportItem {
path: string
params: ReportItem[]
}
interface Metadata {
blueprint: Blueprint
pathMetadata: unknown
}
export const report = (
files: Metalsmith.Files,
metalsmith: Metalsmith,
): void => {
const metadata = metalsmith.metadata() as Metadata
const reportData = generateReport(metadata)
files[`${apiReferenceRoot}/_report.md`] = {
contents: Buffer.from('\n'),
layout: 'report.hbs',
...reportData,
}
files[`${apiReferenceRoot}/_blueprint.json`] = {
contents: Buffer.from(JSON.stringify(metadata.blueprint, null, 2)),
layout: 'default.hbs',
}
}
function generateReport(metadata: Metadata): Report {
const { blueprint } = metadata
const report: Report = {
undocumented: createEmptyReportSection(),
noDescription: { ...createEmptyReportSection(), resources: [] },
draft: { ...createEmptyReportSection(), resourceProperties: [] },
deprecated: createEmptyReportSection(),
missingResources: [],
extraResponseKeys: [],
unusedResources: [],
endpointsWithoutCodeSamples: [],
resourcesWithoutResourceSamples: [],
noTitle: {
namespaces: [],
routes: [],
endpoints: [],
},
}
const routes = blueprint.routes ?? []
for (const route of routes) {
processRoute(route, report)
}
for (const namespace of blueprint.namespaces ?? []) {
processNamespace(namespace, report)
}
const resources = blueprint.resources ?? []
for (const resource of resources) {
processResource(resource, routes, report)
}
const events = blueprint.events ?? []
for (const event of events) {
processEvent(event, report)
}
const actionAttempts = blueprint.actionAttempts ?? []
for (const actionAttempt of actionAttempts) {
processActionAttempt(actionAttempt, report)
}
return report
}
function createEmptyReportSection(): ReportSection {
return {
resources: [],
resourceProperties: [],
endpoints: [],
parameters: [],
namespaces: [],
routes: [],
}
}
function processResource(
resource: Resource,
routes: Route[],
report: Report,
): void {
const { resourceType: name } = resource
if (resource.description == null || resource.description.trim() === '') {
report.noDescription.resources.push({ name })
}
if (resource.isDeprecated) {
report.deprecated.resources.push({
name,
reason: resource.deprecationMessage ?? defaultDeprecatedMessage,
})
}
if (resource.isUndocumented) {
report.undocumented.resources.push({
name,
reason: resource.undocumentedMessage ?? defaultUndocumentedMessage,
})
if (resource.isDraft) {
report.draft.resources.push({
name,
reason: resource.draftMessage ?? defaultDraftMessage,
})
}
}
if (resource.resourceSamples.length === 0 && !resource.isUndocumented) {
report.resourcesWithoutResourceSamples.push(name)
}
for (const property of resource.properties) {
processProperty(name, property, report)
}
let isResourceUsed = false
for (const route of routes) {
for (const endpoint of route.endpoints) {
if (endpoint.response.responseType === 'void') continue
if (endpoint.response.resourceType === name) {
isResourceUsed = true
}
}
}
if (!isResourceUsed) {
report.unusedResources.push({
name,
})
}
}
function processActionAttempt(
actionAttempt: ActionAttempt,
report: Report,
): void {
if (
actionAttempt.resourceSamples.length === 0 &&
!actionAttempt.isUndocumented
) {
report.resourcesWithoutResourceSamples.push(
`action_attempt: ${actionAttempt.actionAttemptType}`,
)
}
}
function processEvent(event: EventResource, report: Report): void {
if (event.resourceSamples.length === 0 && !event.isUndocumented) {
report.resourcesWithoutResourceSamples.push(`event: ${event.eventType}`)
}
}
function processProperty(
resourceName: string,
property: Property,
report: Report,
): void {
const propertyName = `${resourceName}.${property.name}`
if (property.isUndocumented) {
report.undocumented.resourceProperties.push({
name: propertyName,
reason: property.undocumentedMessage ?? defaultUndocumentedMessage,
})
}
if (property.description == null || property.description.trim() === '') {
report.noDescription.resourceProperties.push({ name: propertyName })
}
if (property.isDeprecated) {
report.deprecated.resourceProperties.push({
name: propertyName,
reason: property.deprecationMessage ?? defaultDeprecatedMessage,
})
}
if (property.isDraft) {
report.draft.resourceProperties.push({
name: propertyName,
reason: property.draftMessage ?? defaultDraftMessage,
})
}
}
function processRoute(route: Route, report: Report): void {
if (route.isUndocumented) {
report.undocumented.routes.push({
name: route.path,
reason: defaultUndocumentedMessage, // TODO: undocumentedMessage
})
}
if (route.isDeprecated) {
report.deprecated.routes.push({
name: route.path,
reason: defaultDeprecatedMessage, // TODO: deprecationMessage
})
}
if (route.isDraft) {
report.draft.routes.push({
name: route.path,
reason: defaultDraftMessage, // TODO: draftMessage
})
}
for (const endpoint of route.endpoints) {
processEndpoint(endpoint, report)
}
}
function processNamespace(namespace: Namespace, report: Report): void {
const addNamespace = (section: ReportItem[], reason: string): void => {
if (section.some((item) => item.name === namespace.path)) return
section.push({ name: namespace.path, reason })
}
if (namespace.isDeprecated) {
addNamespace(report.deprecated.namespaces, defaultDeprecatedMessage)
}
if (namespace.isDraft) {
addNamespace(report.draft.namespaces, defaultDraftMessage)
}
if (namespace.isUndocumented) {
addNamespace(report.undocumented.namespaces, defaultUndocumentedMessage)
}
}
function processEndpoint(endpoint: Endpoint, report: Report): void {
if (endpoint.isUndocumented) {
report.undocumented.endpoints.push({
name: endpoint.path,
reason: endpoint.undocumentedMessage ?? defaultUndocumentedMessage,
})
}
if (endpoint.description == null || endpoint.description.trim() === '') {
report.noDescription.endpoints.push({ name: endpoint.path })
}
if (endpoint.isDeprecated) {
report.deprecated.endpoints.push({
name: endpoint.path,
reason: endpoint.deprecationMessage ?? defaultDeprecatedMessage,
})
}
if (endpoint.isDraft) {
report.draft.endpoints.push({
name: endpoint.path,
reason: endpoint.draftMessage ?? defaultDraftMessage,
})
}
if (endpoint.codeSamples.length === 0 && !endpoint.isUndocumented) {
report.endpointsWithoutCodeSamples.push(endpoint.path)
}
if (endpoint.title.length === 0 && !endpoint.isUndocumented) {
report.noTitle.endpoints.push({ name: endpoint.path })
}
processResponseKeys(endpoint, report)
processResponseType(endpoint, report)
processParameters(endpoint.path, endpoint.request.parameters, report)
}
function processResponseType(endpoint: Endpoint, report: Report): void {
if (endpoint.response.responseType === 'void') return
if (endpoint.response.resourceType === 'unknown') {
report.missingResources.push({
path: endpoint.path,
responseKey: endpoint.response.responseKey,
})
}
}
function processResponseKeys(endpoint: Endpoint, report: Report): void {
if (!('responseKey' in endpoint.response)) return
const openapiResponseSchemaProps = getOpenapiResponseProperties(endpoint.path)
if (openapiResponseSchemaProps == null) return
const openapiResponsePropKeys = Object.keys(
openapiResponseSchemaProps,
).filter((key) => !['ok', 'pagination'].includes(key))
if (openapiResponsePropKeys.length <= 1) return
const endpointResponseKey = endpoint.response.responseKey
const extraResponseKeys = openapiResponsePropKeys.filter(
(key) => key !== endpointResponseKey,
)
report.extraResponseKeys.push({
path: endpoint.path,
keys: extraResponseKeys,
})
}
function getOpenapiResponseProperties(
path: string,
): Record<string, unknown> | null {
const openapiEndpointDef = openapi.paths[path as keyof typeof openapi.paths]
if (openapiEndpointDef == null) {
// eslint-disable-next-line no-console
console.warn(`OpenAPI definition not found for endpoint: ${path}`)
return null
}
if (openapiEndpointDef.post?.responses == null) return null
const responseObj = openapiEndpointDef.post.responses['200']
if ('content' in responseObj) {
const jsonContent = responseObj.content['application/json']
if (
jsonContent?.schema != null &&
'properties' in jsonContent.schema &&
jsonContent.schema.properties != null
) {
return jsonContent.schema.properties
}
}
return null
}
function processParameters(
path: string,
parameters: Parameter[],
report: Report,
): void {
const categorizedParams = parameters.reduce(
(acc, param) => {
if (param.isUndocumented) {
acc.undocumented.push({
name: param.name,
reason: param.undocumentedMessage ?? defaultUndocumentedMessage,
})
}
if (param.description == null || param.description.trim() === '') {
acc.noDescription.push({ name: param.name })
}
if (param.isDeprecated) {
acc.deprecated.push({
name: param.name,
reason: param.deprecationMessage ?? defaultDeprecatedMessage,
})
}
if (param.isDraft) {
acc.draft.push({
name: param.name,
reason: param.draftMessage ?? defaultDraftMessage,
})
}
return acc
},
{
undocumented: [] as ReportItem[],
noDescription: [] as ReportItem[],
deprecated: [] as ReportItem[],
draft: [] as ReportItem[],
},
)
if (categorizedParams.undocumented.length > 0) {
report.undocumented.parameters.push({
path,
params: categorizedParams.undocumented,
})
}
if (categorizedParams.noDescription.length > 0) {
report.noDescription.parameters.push({
path,
params: categorizedParams.noDescription,
})
}
if (categorizedParams.deprecated.length > 0) {
report.deprecated.parameters.push({
path,
params: categorizedParams.deprecated,
})
}
if (categorizedParams.draft.length > 0) {
report.draft.parameters.push({
path,
params: categorizedParams.draft,
})
}
}