-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinfo.ts
181 lines (164 loc) · 5.44 KB
/
info.ts
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
import { Command, Flags, Args } from '@oclif/core';
import * as SwaggerParser from '@apidevtools/swagger-parser';
import { parseDefinition, resolveDefinition, printInfo, getOperations } from '../common/definition';
import * as commonFlags from '../common/flags';
import { Document } from '@apidevtools/swagger-parser';
export class Info extends Command {
public static description = 'Display API information';
public static examples = [
'$ openapi info https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml',
`$ openapi info ./openapi.yml`,
];
public static flags = {
...commonFlags.help(),
...commonFlags.parseOpts(),
security: Flags.boolean({ description: 'list security schemes in document', default: false }),
operations: Flags.boolean({ description: 'list operations in document', default: false }),
schemas: Flags.boolean({ description: 'list schemas in document', default: false }),
};
public static args = {
definition: Args.string({
description: 'input definition file'
})
}
public async run() {
const { args, flags } = await this.parse(Info);
const { dereference, bundle, validate, header } = flags;
const definition = resolveDefinition(args.definition);
if (!definition) {
this.error('Please load a definition file', { exit: 1 });
}
let document: Document;
try {
document = await parseDefinition({
definition,
dereference,
bundle,
validate,
strip: flags.strip,
servers: flags.server,
inject: flags.inject,
excludeExt: flags?.['exclude-ext'],
header,
});
} catch (err) {
this.error(err, { exit: 1 });
}
this.log(`Loaded: ${definition}`);
this.log();
printInfo(document, this);
this.printServers(document);
if (flags.operations) {
this.log();
this.printOperations(document);
} else {
this.log();
this.log(`operations: ${getOperations(document).length}`);
this.log(`tags: ${document.tags ? document.tags.length : 0}`);
}
if (flags.schemas) {
this.log();
this.printSchemas(document);
} else {
this.log(`schemas: ${document.components?.schemas ? Object.entries(document.components.schemas).length : 0}`);
}
if (flags.security) {
this.log();
this.printSecuritySchemes(document);
} else {
this.log(
`securitySchemes: ${
document.components?.securitySchemes ? Object.entries(document.components.securitySchemes).length : 0
}`,
);
}
}
private printOperations(document: SwaggerParser.Document) {
const operations: { [tag: string]: { routes: string[]; description?: string } } = {};
if (document.tags) {
for (const tag of document.tags) {
const { name, description } = tag;
operations[name] = {
description,
routes: [],
};
}
}
for (const path in document.paths) {
if (document.paths[path]) {
for (const method in document.paths[path]) {
if (document.paths[path][method]) {
const { operationId, summary, description, tags } = document.paths[path][method];
let route = `${method.toUpperCase()} ${path}`;
if (summary) {
route = `${route} - ${summary}`;
} else if (description) {
route = `${route} - ${description}`;
}
if (operationId) {
route = `${route} (${operationId})`;
}
for (const tag of tags || ['default']) {
if (!operations[tag]) {
operations[tag] = { routes: [] };
}
operations[tag].routes.push(route);
}
}
}
}
}
this.log(`operations (${getOperations(document).length}):`);
for (const tag in operations) {
if (operations[tag]) {
const routes = operations[tag].routes;
for (const route of routes) {
this.log(`- ${route}`);
}
}
}
}
private printSchemas(document: SwaggerParser.Document) {
const schemas = (document.components && document.components.schemas) || {};
const count = Object.entries(schemas).length;
if (count > 0) {
this.log(`schemas (${count}):`);
for (const schema in schemas) {
if (schemas[schema]) {
this.log(`- ${schema}`);
}
}
}
}
private printServers(document: SwaggerParser.Document) {
const servers = document.servers ?? [];
if (servers.length > 0) {
this.log(`servers:`);
for (const server of servers) {
this.log(`- ${server.url}${server.description ? ` (${server.description})` : ''}`);
}
} else {
this.log('servers: 0');
}
}
private printSecuritySchemes(document: SwaggerParser.Document) {
const securitySchemes = document.components?.securitySchemes || {};
const count = Object.entries(securitySchemes).length;
if (count > 0) {
this.log(`securitySchemes (${count}):`);
for (const scheme in securitySchemes) {
if (securitySchemes[scheme]) {
this.log(
`- ${scheme}: (${[
securitySchemes[scheme]['type'],
securitySchemes[scheme]['scheme'],
securitySchemes[scheme]['name'],
]
.filter(Boolean)
.join(', ')}) ${securitySchemes[scheme]['description']}`,
);
}
}
}
}
}