oas-toolkit 0.4.0 → 0.5.0

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/bin.js CHANGED
@@ -14,4 +14,9 @@ yargs(hideBin(process.argv))
14
14
  "check for conflicting components, paths, tags, and security schemes",
15
15
  require("./commands/check-conflicts")
16
16
  )
17
+ .command(
18
+ "remove-unused-components <openapi.yaml>",
19
+ "remove unused components from the provided OpenAPI file",
20
+ require("./commands/remove-unused-components")
21
+ )
17
22
  .parse();
@@ -0,0 +1,25 @@
1
+ const fs = require("fs");
2
+ const yaml = require("js-yaml");
3
+
4
+ module.exports = async function ({ argv }) {
5
+ try {
6
+ const oasFiles = argv._.slice(1);
7
+ if (oasFiles.length !== 1) {
8
+ return;
9
+ }
10
+
11
+ const components = require("../../components");
12
+ let oas = yaml.load(fs.readFileSync(oasFiles[0]));
13
+
14
+ const used = components.getReferencedComponents(oas);
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);
21
+ } catch (e) {
22
+ console.error(`ERROR: ${e.message}`);
23
+ process.exit(1);
24
+ }
25
+ };
package/components.js ADDED
@@ -0,0 +1,51 @@
1
+ const traverse = require("traverse");
2
+ const get = require("lodash.get");
3
+ const difference = require("lodash.difference");
4
+
5
+ function removeComponents(oas, unused) {
6
+ oas = traverse(oas).clone();
7
+ return traverse(oas).forEach(function (x) {
8
+ const path = this.path.join(".");
9
+ if (unused.includes(path)) {
10
+ this.remove();
11
+ if (Object.keys(this.parent.node).length === 0) {
12
+ this.parent.remove();
13
+ }
14
+ }
15
+ });
16
+ }
17
+
18
+ function getReferencedComponents(oas) {
19
+ return traverse(oas).reduce(function (acc, x) {
20
+ if (this.isLeaf && this.key == "$ref") {
21
+ acc.push(x.replace("#/", "").replace(/\//g, "."));
22
+ }
23
+ return acc;
24
+ }, []);
25
+ }
26
+
27
+ function getDefinedComponents(oas) {
28
+ return traverse(oas).reduce(function (acc, x) {
29
+ if (this.path[0] !== "components") {
30
+ return acc;
31
+ }
32
+
33
+ // We're at a schema definition
34
+ if (this.path.length == 3) {
35
+ acc.push(this.path.join("."));
36
+ }
37
+
38
+ return acc;
39
+ }, []);
40
+ }
41
+
42
+ function getUnusedComponents(all, referenced) {
43
+ return difference(all, referenced);
44
+ }
45
+
46
+ module.exports = {
47
+ getReferencedComponents,
48
+ getDefinedComponents,
49
+ getUnusedComponents,
50
+ removeComponents,
51
+ };
@@ -0,0 +1,148 @@
1
+ const c = require("./components");
2
+
3
+ const components = {
4
+ schemas: {
5
+ Foo: {
6
+ type: "object",
7
+ properties: {
8
+ bar: { type: "string" },
9
+ created_at: { type: "string" },
10
+ },
11
+ },
12
+ Baz: {
13
+ type: "object",
14
+ properties: {
15
+ bee: { type: "string" },
16
+ },
17
+ },
18
+ },
19
+ requestBodies: {
20
+ CreateFoo: {
21
+ content: {
22
+ "application/json": {
23
+ schema: {
24
+ type: "object",
25
+ properties: {
26
+ bar: { type: "string" },
27
+ },
28
+ },
29
+ },
30
+ },
31
+ },
32
+ },
33
+ };
34
+
35
+ const oas = {
36
+ info: { title: "One" },
37
+ paths: {
38
+ "/foo": {
39
+ post: {
40
+ requestBody: {
41
+ $ref: "#/components/requestBodies/CreateFoo",
42
+ },
43
+ responses: {
44
+ 201: {
45
+ content: {
46
+ "application/json": {
47
+ schema: {
48
+ $ref: "#/components/schemas/Foo",
49
+ },
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ },
56
+ },
57
+ components,
58
+ };
59
+
60
+ describe("#components", () => {
61
+ it("extracts referenced components", () => {
62
+ expect(c.getReferencedComponents(oas)).toEqual([
63
+ "components.requestBodies.CreateFoo",
64
+ "components.schemas.Foo",
65
+ ]);
66
+ });
67
+
68
+ it("returns unused components (without unused)", () => {
69
+ expect(
70
+ c.getUnusedComponents(
71
+ ["components.schemas.Foo", "components.schemas.Baz"],
72
+ ["components.schemas.Baz", "components.schemas.Foo"]
73
+ )
74
+ ).toEqual([]);
75
+ });
76
+
77
+ it("returns unused components (with unused)", () => {
78
+ expect(
79
+ c.getUnusedComponents(
80
+ ["components.schemas.Foo", "components.schemas.Baz"],
81
+ ["components.schemas.Baz"]
82
+ )
83
+ ).toEqual(["components.schemas.Foo"]);
84
+ });
85
+
86
+ it("returns all defined components)", () => {
87
+ expect(c.getDefinedComponents(oas)).toEqual([
88
+ "components.schemas.Foo",
89
+ "components.schemas.Baz",
90
+ "components.requestBodies.CreateFoo",
91
+ ]);
92
+ });
93
+
94
+ it("removed unused components including parent", () => {
95
+ expect(
96
+ c.removeComponents({ components }, ["components.requestBodies.CreateFoo"])
97
+ ).toEqual({
98
+ components: {
99
+ schemas: {
100
+ Foo: {
101
+ properties: {
102
+ bar: { type: "string" },
103
+ created_at: { type: "string" },
104
+ },
105
+ type: "object",
106
+ },
107
+ Baz: {
108
+ type: "object",
109
+ properties: {
110
+ bee: { type: "string" },
111
+ },
112
+ },
113
+ },
114
+ },
115
+ });
116
+ });
117
+
118
+ it("removed unused components but leaves the parent", () => {
119
+ expect(
120
+ c.removeComponents({ components }, ["components.schemas.Foo"])
121
+ ).toEqual({
122
+ components: {
123
+ schemas: {
124
+ Baz: {
125
+ type: "object",
126
+ properties: {
127
+ bee: { type: "string" },
128
+ },
129
+ },
130
+ },
131
+ requestBodies: {
132
+ CreateFoo: {
133
+ content: {
134
+ "application/json": {
135
+ schema: {
136
+ type: "object",
137
+ properties: {
138
+ bar: { type: "string" },
139
+ },
140
+ },
141
+ },
142
+ },
143
+ },
144
+ },
145
+ },
146
+ });
147
+ });
148
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oas-toolkit",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -19,9 +19,12 @@
19
19
  "debug": "^4.3.4",
20
20
  "js-yaml": "^4.1.0",
21
21
  "jsonpath-plus": "^7.2.0",
22
+ "lodash.difference": "^4.5.0",
23
+ "lodash.get": "^4.4.2",
22
24
  "lodash.isequal": "^4.5.0",
23
25
  "lodash.uniqwith": "^4.5.0",
24
26
  "mergician": "^1.1.0",
27
+ "traverse": "^0.6.7",
25
28
  "yargs": "^17.7.1"
26
29
  }
27
30
  }