oas-toolkit 0.6.3 → 0.7.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 +10 -0
- package/cli/commands/remove-unused-tags.js +19 -0
- package/cli/commands/rewrite-path.js +24 -0
- package/package.json +1 -1
- package/rewrite-path.js +16 -0
- package/rewrite-path.test.js +61 -0
- package/tags.js +41 -0
- package/tags.test.js +92 -0
package/cli/bin.js
CHANGED
|
@@ -19,4 +19,14 @@ yargs(hideBin(process.argv))
|
|
|
19
19
|
"remove unused components from the provided OpenAPI file",
|
|
20
20
|
require("./commands/remove-unused-components")
|
|
21
21
|
)
|
|
22
|
+
.command(
|
|
23
|
+
"remove-unused-tags <openapi.yaml>",
|
|
24
|
+
"remove unused tags from the provided OpenAPI file",
|
|
25
|
+
require("./commands/remove-unused-tags")
|
|
26
|
+
)
|
|
27
|
+
.command(
|
|
28
|
+
"rewrite-path <openapi.yaml> --oldPrefix '^/v1' --newPrefix '/v2'",
|
|
29
|
+
"rewrite paths in the provided OpenAPI file",
|
|
30
|
+
require("./commands/rewrite-path")
|
|
31
|
+
)
|
|
22
32
|
.parse();
|
|
@@ -0,0 +1,19 @@
|
|
|
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 tags = require("../../tags");
|
|
12
|
+
let oas = yaml.load(fs.readFileSync(oasFiles[0]));
|
|
13
|
+
oas = tags.removeUnusedTags(oas);
|
|
14
|
+
console.log(yaml.dump(oas));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
console.error(`ERROR: ${e.message}`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
if (!argv.oldPrefix || !argv.newPrefix) {
|
|
12
|
+
console.error(`ERROR: --oldPrefix and --newPrefix are required`);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const p = require("../../rewrite-path");
|
|
17
|
+
let oas = yaml.load(fs.readFileSync(oasFiles[0]));
|
|
18
|
+
oas = p.regex(oas, argv.oldPrefix, argv.newPrefix);
|
|
19
|
+
console.log(yaml.dump(oas));
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error(`ERROR: ${e.message}`);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
};
|
package/package.json
CHANGED
package/rewrite-path.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function regex(oas, oldPrefix, newPrefix) {
|
|
2
|
+
oldPrefix = new RegExp(oldPrefix);
|
|
3
|
+
oas = JSON.parse(JSON.stringify(oas)); // Prevent modification of original object
|
|
4
|
+
for (let path in oas.paths) {
|
|
5
|
+
if (path.match(oldPrefix)) {
|
|
6
|
+
const newPath = path.replace(oldPrefix, newPrefix);
|
|
7
|
+
oas.paths[newPath] = oas.paths[path];
|
|
8
|
+
delete oas.paths[path];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return oas;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
regex,
|
|
16
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const p = require("./rewrite-path");
|
|
2
|
+
|
|
3
|
+
const hello = {
|
|
4
|
+
post: {
|
|
5
|
+
operationId: "create-hello",
|
|
6
|
+
requestBody: {
|
|
7
|
+
$ref: "#/components/requestBodies/CreateHello",
|
|
8
|
+
},
|
|
9
|
+
responses: {},
|
|
10
|
+
},
|
|
11
|
+
get: {
|
|
12
|
+
operationId: "get-hello",
|
|
13
|
+
responses: {},
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const world = {
|
|
18
|
+
get: {
|
|
19
|
+
operationId: "create-world",
|
|
20
|
+
requestBody: {
|
|
21
|
+
$ref: "#/components/requestBodies/CreateWorld",
|
|
22
|
+
},
|
|
23
|
+
responses: {},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const oas = {
|
|
28
|
+
paths: {
|
|
29
|
+
"/v1/foo/hello": hello,
|
|
30
|
+
"/v1/foo/world": world,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
describe("#rewrite-path", () => {
|
|
35
|
+
it("rewrites a prefix", () => {
|
|
36
|
+
expect(p.regex(oas, "^/v1", "/v2")).toEqual({
|
|
37
|
+
paths: {
|
|
38
|
+
"/v2/foo/hello": hello,
|
|
39
|
+
"/v2/foo/world": world,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("rewrites anywhere in the string", () => {
|
|
45
|
+
expect(p.regex(oas, "/foo", "/bar")).toEqual({
|
|
46
|
+
paths: {
|
|
47
|
+
"/v1/bar/hello": hello,
|
|
48
|
+
"/v1/bar/world": world,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("does not rewrite non-matching regex", () => {
|
|
54
|
+
expect(p.regex(oas, "^/foo", "/bar")).toEqual({
|
|
55
|
+
paths: {
|
|
56
|
+
"/v1/foo/hello": hello,
|
|
57
|
+
"/v1/foo/world": world,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
package/tags.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const traverse = require("traverse");
|
|
2
|
+
const difference = require("lodash.difference");
|
|
3
|
+
|
|
4
|
+
function removeUnusedTags(oas) {
|
|
5
|
+
const used = getReferencedTags(oas);
|
|
6
|
+
const defined = getDefinedTags(oas);
|
|
7
|
+
const unused = getUnusedTags(defined, used);
|
|
8
|
+
|
|
9
|
+
return removeSpecifiedTags(oas, unused);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function removeSpecifiedTags(oas, unused) {
|
|
13
|
+
oas = JSON.parse(JSON.stringify(oas)); // Prevent modification of original object
|
|
14
|
+
oas.tags = oas.tags.filter((t) => !unused.includes(t.name));
|
|
15
|
+
return oas;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getReferencedTags(oas) {
|
|
19
|
+
return traverse(oas).reduce(function (acc, x) {
|
|
20
|
+
if (this.node && this.node["operationId"] && this.node["tags"]) {
|
|
21
|
+
acc = acc.concat(this.node["tags"]);
|
|
22
|
+
}
|
|
23
|
+
return acc;
|
|
24
|
+
}, []);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getDefinedTags(oas) {
|
|
28
|
+
return oas.tags.map((t) => t.name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getUnusedTags(all, referenced) {
|
|
32
|
+
return difference(all, referenced);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
getReferencedTags,
|
|
37
|
+
getDefinedTags,
|
|
38
|
+
getUnusedTags,
|
|
39
|
+
removeSpecifiedTags,
|
|
40
|
+
removeUnusedTags,
|
|
41
|
+
};
|
package/tags.test.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const t = require("./tags");
|
|
2
|
+
|
|
3
|
+
const tags = [
|
|
4
|
+
{
|
|
5
|
+
name: "One",
|
|
6
|
+
description: "Tag Number One",
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
name: "Two",
|
|
10
|
+
description: "Tag Number One",
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: "Three",
|
|
14
|
+
description: "Tag Number One",
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const oas = {
|
|
19
|
+
info: { title: "One" },
|
|
20
|
+
security: [{ personalAccessToken: {} }],
|
|
21
|
+
paths: {
|
|
22
|
+
"/foo": {
|
|
23
|
+
post: {
|
|
24
|
+
operationId: "create-foo",
|
|
25
|
+
tags: ["One"],
|
|
26
|
+
requestBody: {
|
|
27
|
+
$ref: "#/components/requestBodies/CreateFoo",
|
|
28
|
+
},
|
|
29
|
+
responses: {
|
|
30
|
+
201: {
|
|
31
|
+
content: {
|
|
32
|
+
"application/json": {
|
|
33
|
+
schema: {
|
|
34
|
+
$ref: "#/components/schemas/Foo",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
tags,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
describe("#tags", () => {
|
|
47
|
+
it("extracts referenced tags", () => {
|
|
48
|
+
expect(t.getReferencedTags(oas)).toEqual(["One"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns unused tags (without unused)", () => {
|
|
52
|
+
expect(t.getUnusedTags(["One", "Two"], ["Two", "One"])).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("returns unused tags (with unused)", () => {
|
|
56
|
+
expect(t.getUnusedTags(["One", "Two"], ["Two"])).toEqual(["One"]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns all defined tags", () => {
|
|
60
|
+
expect(t.getDefinedTags(oas)).toEqual(["One", "Two", "Three"]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("removes unused tags", () => {
|
|
64
|
+
expect(t.removeUnusedTags(oas)).toEqual({
|
|
65
|
+
info: { title: "One" },
|
|
66
|
+
security: [{ personalAccessToken: {} }],
|
|
67
|
+
paths: {
|
|
68
|
+
"/foo": {
|
|
69
|
+
post: {
|
|
70
|
+
operationId: "create-foo",
|
|
71
|
+
tags: ["One"],
|
|
72
|
+
requestBody: {
|
|
73
|
+
$ref: "#/components/requestBodies/CreateFoo",
|
|
74
|
+
},
|
|
75
|
+
responses: {
|
|
76
|
+
201: {
|
|
77
|
+
content: {
|
|
78
|
+
"application/json": {
|
|
79
|
+
schema: {
|
|
80
|
+
$ref: "#/components/schemas/Foo",
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
tags: [{ name: "One", description: "Tag Number One" }],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|