oas-toolkit 0.5.0 → 0.5.1
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 +11 -2
- package/components.test.js +55 -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(".");
|
|
@@ -47,5 +55,6 @@ module.exports = {
|
|
|
47
55
|
getReferencedComponents,
|
|
48
56
|
getDefinedComponents,
|
|
49
57
|
getUnusedComponents,
|
|
50
|
-
|
|
58
|
+
removeSpecifiedComponents,
|
|
59
|
+
removeUnusedComponents,
|
|
51
60
|
};
|
package/components.test.js
CHANGED
|
@@ -93,7 +93,9 @@ describe("#components", () => {
|
|
|
93
93
|
|
|
94
94
|
it("removed unused components including parent", () => {
|
|
95
95
|
expect(
|
|
96
|
-
c.
|
|
96
|
+
c.removeSpecifiedComponents({ components }, [
|
|
97
|
+
"components.requestBodies.CreateFoo",
|
|
98
|
+
])
|
|
97
99
|
).toEqual({
|
|
98
100
|
components: {
|
|
99
101
|
schemas: {
|
|
@@ -117,7 +119,7 @@ describe("#components", () => {
|
|
|
117
119
|
|
|
118
120
|
it("removed unused components but leaves the parent", () => {
|
|
119
121
|
expect(
|
|
120
|
-
c.
|
|
122
|
+
c.removeSpecifiedComponents({ components }, ["components.schemas.Foo"])
|
|
121
123
|
).toEqual({
|
|
122
124
|
components: {
|
|
123
125
|
schemas: {
|
|
@@ -145,4 +147,55 @@ describe("#components", () => {
|
|
|
145
147
|
},
|
|
146
148
|
});
|
|
147
149
|
});
|
|
150
|
+
|
|
151
|
+
it("autodiscovers and removes unused components", () => {
|
|
152
|
+
expect(c.removeUnusedComponents(oas)).toEqual({
|
|
153
|
+
info: { title: "One" },
|
|
154
|
+
paths: {
|
|
155
|
+
"/foo": {
|
|
156
|
+
post: {
|
|
157
|
+
requestBody: {
|
|
158
|
+
$ref: "#/components/requestBodies/CreateFoo",
|
|
159
|
+
},
|
|
160
|
+
responses: {
|
|
161
|
+
201: {
|
|
162
|
+
content: {
|
|
163
|
+
"application/json": {
|
|
164
|
+
schema: {
|
|
165
|
+
$ref: "#/components/schemas/Foo",
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
components: {
|
|
175
|
+
schemas: {
|
|
176
|
+
Foo: {
|
|
177
|
+
type: "object",
|
|
178
|
+
properties: {
|
|
179
|
+
bar: { type: "string" },
|
|
180
|
+
created_at: { type: "string" },
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
requestBodies: {
|
|
185
|
+
CreateFoo: {
|
|
186
|
+
content: {
|
|
187
|
+
"application/json": {
|
|
188
|
+
schema: {
|
|
189
|
+
type: "object",
|
|
190
|
+
properties: {
|
|
191
|
+
bar: { type: "string" },
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
});
|
|
148
201
|
});
|
package/index.js
ADDED