oas-toolkit 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/commands/remove-unused-components.js +2 -8
- package/components.js +23 -3
- package/components.test.js +115 -2
- package/index.js +4 -0
- package/package.json +1 -1
|
@@ -10,14 +10,8 @@ module.exports = async function ({ argv }) {
|
|
|
10
10
|
|
|
11
11
|
const components = require("../../components");
|
|
12
12
|
let oas = yaml.load(fs.readFileSync(oasFiles[0]));
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const defined = components.getDefinedComponents(oas);
|
|
16
|
-
const unused = components.getUnusedComponents(defined, used);
|
|
17
|
-
|
|
18
|
-
oas = components.removeComponents(oas, unused);
|
|
19
|
-
|
|
20
|
-
fs.writeFileSync(oasFiles[0], oas);
|
|
13
|
+
oas = components.removeUnusedComponents(oas);
|
|
14
|
+
console.log(yaml.dump(oas));
|
|
21
15
|
} catch (e) {
|
|
22
16
|
console.error(`ERROR: ${e.message}`);
|
|
23
17
|
process.exit(1);
|
package/components.js
CHANGED
|
@@ -2,7 +2,15 @@ const traverse = require("traverse");
|
|
|
2
2
|
const get = require("lodash.get");
|
|
3
3
|
const difference = require("lodash.difference");
|
|
4
4
|
|
|
5
|
-
function
|
|
5
|
+
function removeUnusedComponents(oas) {
|
|
6
|
+
const used = getReferencedComponents(oas);
|
|
7
|
+
const defined = getDefinedComponents(oas);
|
|
8
|
+
const unused = getUnusedComponents(defined, used);
|
|
9
|
+
|
|
10
|
+
return removeSpecifiedComponents(oas, unused);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function removeSpecifiedComponents(oas, unused) {
|
|
6
14
|
oas = traverse(oas).clone();
|
|
7
15
|
return traverse(oas).forEach(function (x) {
|
|
8
16
|
const path = this.path.join(".");
|
|
@@ -16,12 +24,23 @@ function removeComponents(oas, unused) {
|
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
function getReferencedComponents(oas) {
|
|
19
|
-
|
|
27
|
+
const components = traverse(oas).reduce(function (acc, x) {
|
|
20
28
|
if (this.isLeaf && this.key == "$ref") {
|
|
21
29
|
acc.push(x.replace("#/", "").replace(/\//g, "."));
|
|
22
30
|
}
|
|
23
31
|
return acc;
|
|
24
32
|
}, []);
|
|
33
|
+
|
|
34
|
+
// Add security schemes
|
|
35
|
+
if (oas.security){
|
|
36
|
+
for (let item of oas.security){
|
|
37
|
+
for (let key of Object.keys(item)){
|
|
38
|
+
components.push(`components.securitySchemes.${key}`)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return components;
|
|
25
44
|
}
|
|
26
45
|
|
|
27
46
|
function getDefinedComponents(oas) {
|
|
@@ -47,5 +66,6 @@ module.exports = {
|
|
|
47
66
|
getReferencedComponents,
|
|
48
67
|
getDefinedComponents,
|
|
49
68
|
getUnusedComponents,
|
|
50
|
-
|
|
69
|
+
removeSpecifiedComponents,
|
|
70
|
+
removeUnusedComponents,
|
|
51
71
|
};
|
package/components.test.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const c = require("./components");
|
|
2
2
|
|
|
3
3
|
const components = {
|
|
4
|
+
securitySchemes: {
|
|
5
|
+
personalAccessToken: {
|
|
6
|
+
type: "http",
|
|
7
|
+
scheme: "bearer",
|
|
8
|
+
},
|
|
9
|
+
},
|
|
4
10
|
schemas: {
|
|
5
11
|
Foo: {
|
|
6
12
|
type: "object",
|
|
@@ -34,6 +40,7 @@ const components = {
|
|
|
34
40
|
|
|
35
41
|
const oas = {
|
|
36
42
|
info: { title: "One" },
|
|
43
|
+
security: [{ personalAccessToken: {} }],
|
|
37
44
|
paths: {
|
|
38
45
|
"/foo": {
|
|
39
46
|
post: {
|
|
@@ -62,6 +69,7 @@ describe("#components", () => {
|
|
|
62
69
|
expect(c.getReferencedComponents(oas)).toEqual([
|
|
63
70
|
"components.requestBodies.CreateFoo",
|
|
64
71
|
"components.schemas.Foo",
|
|
72
|
+
"components.securitySchemes.personalAccessToken",
|
|
65
73
|
]);
|
|
66
74
|
});
|
|
67
75
|
|
|
@@ -85,6 +93,7 @@ describe("#components", () => {
|
|
|
85
93
|
|
|
86
94
|
it("returns all defined components)", () => {
|
|
87
95
|
expect(c.getDefinedComponents(oas)).toEqual([
|
|
96
|
+
"components.securitySchemes.personalAccessToken",
|
|
88
97
|
"components.schemas.Foo",
|
|
89
98
|
"components.schemas.Baz",
|
|
90
99
|
"components.requestBodies.CreateFoo",
|
|
@@ -93,9 +102,17 @@ describe("#components", () => {
|
|
|
93
102
|
|
|
94
103
|
it("removed unused components including parent", () => {
|
|
95
104
|
expect(
|
|
96
|
-
c.
|
|
105
|
+
c.removeSpecifiedComponents({ components }, [
|
|
106
|
+
"components.requestBodies.CreateFoo",
|
|
107
|
+
])
|
|
97
108
|
).toEqual({
|
|
98
109
|
components: {
|
|
110
|
+
securitySchemes: {
|
|
111
|
+
personalAccessToken: {
|
|
112
|
+
type: "http",
|
|
113
|
+
scheme: "bearer",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
99
116
|
schemas: {
|
|
100
117
|
Foo: {
|
|
101
118
|
properties: {
|
|
@@ -117,9 +134,15 @@ describe("#components", () => {
|
|
|
117
134
|
|
|
118
135
|
it("removed unused components but leaves the parent", () => {
|
|
119
136
|
expect(
|
|
120
|
-
c.
|
|
137
|
+
c.removeSpecifiedComponents({ components }, ["components.schemas.Foo"])
|
|
121
138
|
).toEqual({
|
|
122
139
|
components: {
|
|
140
|
+
securitySchemes: {
|
|
141
|
+
personalAccessToken: {
|
|
142
|
+
type: "http",
|
|
143
|
+
scheme: "bearer",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
123
146
|
schemas: {
|
|
124
147
|
Baz: {
|
|
125
148
|
type: "object",
|
|
@@ -145,4 +168,94 @@ describe("#components", () => {
|
|
|
145
168
|
},
|
|
146
169
|
});
|
|
147
170
|
});
|
|
171
|
+
|
|
172
|
+
it("autodiscovers and removes unused components", () => {
|
|
173
|
+
expect(c.removeUnusedComponents(oas)).toEqual({
|
|
174
|
+
info: { title: "One" },
|
|
175
|
+
security: [{ personalAccessToken: {} }],
|
|
176
|
+
paths: {
|
|
177
|
+
"/foo": {
|
|
178
|
+
post: {
|
|
179
|
+
requestBody: {
|
|
180
|
+
$ref: "#/components/requestBodies/CreateFoo",
|
|
181
|
+
},
|
|
182
|
+
responses: {
|
|
183
|
+
201: {
|
|
184
|
+
content: {
|
|
185
|
+
"application/json": {
|
|
186
|
+
schema: {
|
|
187
|
+
$ref: "#/components/schemas/Foo",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
components: {
|
|
197
|
+
schemas: {
|
|
198
|
+
Foo: {
|
|
199
|
+
type: "object",
|
|
200
|
+
properties: {
|
|
201
|
+
bar: { type: "string" },
|
|
202
|
+
created_at: { type: "string" },
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
securitySchemes: {
|
|
207
|
+
personalAccessToken: {
|
|
208
|
+
type: "http",
|
|
209
|
+
scheme: "bearer",
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
requestBodies: {
|
|
213
|
+
CreateFoo: {
|
|
214
|
+
content: {
|
|
215
|
+
"application/json": {
|
|
216
|
+
schema: {
|
|
217
|
+
type: "object",
|
|
218
|
+
properties: {
|
|
219
|
+
bar: { type: "string" },
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("removes unused security schemes", () => {
|
|
231
|
+
const securityOas = {
|
|
232
|
+
info: { title: "One" },
|
|
233
|
+
security: [{ personalAccessToken: {} }],
|
|
234
|
+
components: {
|
|
235
|
+
securitySchemes: {
|
|
236
|
+
personalAccessToken: {
|
|
237
|
+
type: "http",
|
|
238
|
+
scheme: "bearer",
|
|
239
|
+
},
|
|
240
|
+
systemAccessToken: {
|
|
241
|
+
type: "http",
|
|
242
|
+
scheme: "bearer",
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
expect(c.removeUnusedComponents(securityOas)).toEqual({
|
|
249
|
+
info: { title: "One" },
|
|
250
|
+
security: [{ personalAccessToken: {} }],
|
|
251
|
+
components: {
|
|
252
|
+
securitySchemes: {
|
|
253
|
+
personalAccessToken: {
|
|
254
|
+
type: "http",
|
|
255
|
+
scheme: "bearer",
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
});
|
|
148
261
|
});
|
package/index.js
ADDED