-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathapi-doc-scanner.js
More file actions
340 lines (291 loc) · 10.1 KB
/
api-doc-scanner.js
File metadata and controls
340 lines (291 loc) · 10.1 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
/**
* API documentation scanner for docs-v2 repository
*
* Parses OpenAPI spec files in api-docs/influxdb3/{core,enterprise}/v3/
* (influxdb3-core-openapi.yaml and influxdb3-enterprise-openapi.yaml)
* to find documented API endpoints and their parameters.
*
* @module api-doc-scanner
*/
import { promises as fs } from 'fs';
import { join } from 'path';
import yaml from 'js-yaml';
/**
* Scans API documentation in docs-v2 repository
*/
export class APIDocScanner {
constructor(docsRepoPath, product) {
this.docsRepoPath = docsRepoPath;
this.product = product; // 'core' or 'enterprise'
this.documentedEndpoints = new Map(); // endpoint -> { file: '', title: '', description: '' }
}
/**
* Scan for documented API endpoints from OpenAPI specs
*/
async scanDocumentation() {
console.log(`🔍 Scanning ${this.product} API documentation...`);
const specPaths = this.getOpenAPISpecPaths();
for (const specPath of specPaths) {
await this.parseOpenAPISpec(specPath);
}
console.log(` 📄 Found ${this.documentedEndpoints.size} documented endpoints`);
return this.documentedEndpoints;
}
/**
* Get OpenAPI spec file paths based on product
*/
getOpenAPISpecPaths() {
const paths = [];
if (this.product === 'core' || this.product === 'both') {
paths.push(join(this.docsRepoPath, 'api-docs/influxdb3/core/v3/influxdb3-core-openapi.yaml'));
}
if (this.product === 'enterprise' || this.product === 'both') {
paths.push(join(this.docsRepoPath, 'api-docs/influxdb3/enterprise/v3/influxdb3-enterprise-openapi.yaml'));
}
return paths;
}
/**
* Parse OpenAPI spec file to extract endpoints
*/
async parseOpenAPISpec(specPath) {
try {
const content = await fs.readFile(specPath, 'utf-8');
const spec = yaml.load(content);
if (!spec.paths) {
console.warn(` ⚠️ No paths found in OpenAPI spec: ${specPath}`);
return;
}
// Extract endpoints from paths
for (const [path, pathItem] of Object.entries(spec.paths)) {
// Get all HTTP methods for this path
const methods = [];
const operations = [];
for (const method of ['get', 'post', 'put', 'delete', 'patch']) {
if (pathItem[method]) {
methods.push(method.toUpperCase());
operations.push(pathItem[method]);
}
}
if (methods.length > 0) {
// Use first operation for description
const operation = operations[0];
// Extract documented parameters from requestBody schema
const documentedParams = this.extractDocumentedParams(operation, spec);
this.documentedEndpoints.set(path, {
file: specPath.replace(this.docsRepoPath + '/', ''),
title: operation.summary || 'API Endpoint',
description: operation.description || '',
methods,
operationId: operation.operationId || '',
tags: operation.tags || [],
parameters: documentedParams,
});
}
}
} catch (error) {
if (error.code === 'ENOENT') {
console.warn(` ⚠️ OpenAPI spec not found: ${specPath}`);
} else {
console.warn(` ⚠️ Error parsing OpenAPI spec ${specPath}:`, error.message);
}
}
}
/**
* Extract documented parameters from an operation's requestBody
*/
extractDocumentedParams(operation, spec) {
const params = {
query: [], // URL query parameters
body: [], // Request body properties
path: [], // Path parameters
};
// Extract query and path parameters
if (operation.parameters) {
for (const param of operation.parameters) {
const paramInfo = {
name: param.name,
type: param.schema?.type || 'unknown',
required: param.required || false,
description: param.description || '',
in: param.in,
};
if (param.in === 'query') {
params.query.push(paramInfo);
} else if (param.in === 'path') {
params.path.push(paramInfo);
}
}
}
// Extract request body properties
if (operation.requestBody?.content) {
const content = operation.requestBody.content;
const jsonContent = content['application/json'] || content['application/x-www-form-urlencoded'];
if (jsonContent?.schema) {
const schema = this.resolveSchemaRef(jsonContent.schema, spec);
if (schema?.properties) {
const requiredFields = schema.required || [];
for (const [propName, propSchema] of Object.entries(schema.properties)) {
const resolvedProp = this.resolveSchemaRef(propSchema, spec);
params.body.push({
name: propName,
type: this.getSchemaType(resolvedProp),
required: requiredFields.includes(propName),
description: resolvedProp.description || '',
properties: resolvedProp.properties ? Object.keys(resolvedProp.properties) : null,
});
}
}
}
}
return params;
}
/**
* Resolve a $ref schema reference
*/
resolveSchemaRef(schema, spec) {
if (!schema) return null;
if (schema.$ref) {
// Format: #/components/schemas/SchemaName
const refPath = schema.$ref.replace('#/', '').split('/');
let resolved = spec;
for (const part of refPath) {
resolved = resolved?.[part];
}
return resolved || schema;
}
return schema;
}
/**
* Get a human-readable type from schema
*/
getSchemaType(schema) {
if (!schema) return 'unknown';
if (schema.type === 'array') {
const itemType = schema.items?.type || 'unknown';
return `array<${itemType}>`;
}
if (schema.type === 'object' && schema.additionalProperties) {
const valueType = schema.additionalProperties.type || 'unknown';
return `map<string, ${valueType}>`;
}
if (schema.enum) {
return `enum(${schema.enum.join('|')})`;
}
return schema.type || 'object';
}
}
/**
* Compare discovered endpoints with documented endpoints
*/
export function compareEndpoints(discoveredEndpoints, documentedEndpoints) {
const missing = [];
const documented = [];
const coverage = {
total: discoveredEndpoints.size,
documented: 0,
missing: 0,
percentage: 0,
};
for (const [path, endpoint] of discoveredEndpoints.entries()) {
const isDocumented = documentedEndpoints.has(path) ||
Array.from(documentedEndpoints.keys()).some(docPath =>
path.startsWith(docPath) && endpoint.isPrefix
);
if (isDocumented) {
documented.push({
...endpoint,
documentation: documentedEndpoints.get(path),
});
coverage.documented++;
} else {
missing.push(endpoint);
coverage.missing++;
}
}
coverage.percentage = Math.round((coverage.documented / coverage.total) * 100);
return {
missing,
documented,
coverage,
};
}
/**
* Compare discovered request parameters with documented parameters
*
* @param {Array<{endpoint: string, method: string, request: Object|null, response: Object|null}>} discoveredParams - Parameters extracted from Rust structs
* @param {Map<string, {file: string, title: string, description: string, methods: string[], parameters: Object}>} documentedEndpoints - Endpoints with documented parameters from OpenAPI
* @returns {{endpoints: Array<Object>, summary: {totalEndpoints: number, endpointsWithMissingParams: number, totalMissingParams: number, totalDocumentedParams: number, totalDiscoveredParams: number}}} Parameter comparison results
*/
export function compareParameters(discoveredParams, documentedEndpoints) {
const results = {
endpoints: [],
summary: {
totalEndpoints: 0,
endpointsWithMissingParams: 0,
totalMissingParams: 0,
totalDocumentedParams: 0,
totalDiscoveredParams: 0,
},
};
for (const discovered of discoveredParams) {
const endpointKey = `${discovered.method} ${discovered.endpoint}`;
const documented = documentedEndpoints.get(discovered.endpoint);
results.summary.totalEndpoints++;
const endpointResult = {
endpoint: discovered.endpoint,
method: discovered.method,
missingParams: [],
extraDocumentedParams: [],
matchedParams: [],
discoveredParams: [],
documentedParams: [],
};
// Get discovered request parameters
if (discovered.request?.fields) {
for (const field of discovered.request.fields) {
endpointResult.discoveredParams.push({
name: field.serializedName || field.name,
type: field.type,
required: field.required,
description: field.description,
});
results.summary.totalDiscoveredParams++;
}
}
// Get documented parameters
if (documented?.parameters?.body) {
for (const param of documented.parameters.body) {
endpointResult.documentedParams.push({
name: param.name,
type: param.type,
required: param.required,
description: param.description,
});
results.summary.totalDocumentedParams++;
}
}
// Compare discovered vs documented
const documentedNames = new Set(endpointResult.documentedParams.map(p => p.name));
const discoveredNames = new Set(endpointResult.discoveredParams.map(p => p.name));
// Find missing parameters (in source but not in docs)
for (const param of endpointResult.discoveredParams) {
if (!documentedNames.has(param.name)) {
endpointResult.missingParams.push(param);
results.summary.totalMissingParams++;
} else {
endpointResult.matchedParams.push(param);
}
}
// Find extra documented parameters (in docs but not in source)
for (const param of endpointResult.documentedParams) {
if (!discoveredNames.has(param.name)) {
endpointResult.extraDocumentedParams.push(param);
}
}
if (endpointResult.missingParams.length > 0) {
results.summary.endpointsWithMissingParams++;
}
results.endpoints.push(endpointResult);
}
return results;
}