oas-toolkit 0.9.0 → 0.10.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/canonical-server.js +47 -0
- package/canonical-server.test.js +77 -0
- package/cli/bin.js +16 -4
- package/cli/commands/canonical-server.js +14 -0
- package/package.json +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const isEqual = require("lodash.isequal");
|
|
2
|
+
const uniqWith = require("lodash.uniqwith");
|
|
3
|
+
const url = require("url");
|
|
4
|
+
function run(oas) {
|
|
5
|
+
oas = JSON.parse(JSON.stringify(oas)); // Prevent modification of original object
|
|
6
|
+
|
|
7
|
+
// Extract the base path from servers
|
|
8
|
+
const basePaths = uniqWith(
|
|
9
|
+
oas.servers.map((server) => {
|
|
10
|
+
const path = url.parse(server.url).pathname;
|
|
11
|
+
if (path.slice(-1) === "/") {
|
|
12
|
+
return path.slice(0, -1);
|
|
13
|
+
}
|
|
14
|
+
return path;
|
|
15
|
+
}),
|
|
16
|
+
isEqual
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (basePaths.length > 1) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Base paths are different in the servers block. Found: ${basePaths.join(
|
|
22
|
+
", "
|
|
23
|
+
)}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for (let path in oas.paths) {
|
|
28
|
+
let newPath = basePaths[0] + path;
|
|
29
|
+
oas.paths[newPath] = oas.paths[path];
|
|
30
|
+
delete oas.paths[path];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Remove paths from servers
|
|
34
|
+
oas.servers = oas.servers.map((server) => {
|
|
35
|
+
const u = url.parse(server.url);
|
|
36
|
+
return {
|
|
37
|
+
...server,
|
|
38
|
+
url: `${u.protocol}//${u.hostname}/`,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return oas;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
run,
|
|
47
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const c = require("./canonical-server");
|
|
2
|
+
|
|
3
|
+
const oas = {
|
|
4
|
+
servers: [
|
|
5
|
+
{
|
|
6
|
+
url: "https://api.example.com/v1",
|
|
7
|
+
},
|
|
8
|
+
],
|
|
9
|
+
paths: {
|
|
10
|
+
"/foo/hello": {},
|
|
11
|
+
"/foo/world": {},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function getOas(urls) {
|
|
16
|
+
return {
|
|
17
|
+
servers: urls.map((url) => {
|
|
18
|
+
return {
|
|
19
|
+
url,
|
|
20
|
+
};
|
|
21
|
+
}),
|
|
22
|
+
paths: oas.paths,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("#run", () => {
|
|
27
|
+
it("prefixes all routes", () => {
|
|
28
|
+
const o = getOas(["https://api.example.com/v1"]);
|
|
29
|
+
expect(c.run(o).paths).toEqual({
|
|
30
|
+
"/v1/foo/hello": {},
|
|
31
|
+
"/v1/foo/world": {},
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("handles multiple servers", () => {
|
|
36
|
+
const o = getOas([
|
|
37
|
+
"https://api.example.com/v1",
|
|
38
|
+
"https://stagingapi.example.com/v1",
|
|
39
|
+
]);
|
|
40
|
+
expect(c.run(o).paths).toEqual({
|
|
41
|
+
"/v1/foo/hello": {},
|
|
42
|
+
"/v1/foo/world": {},
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("handles trailing slashes", () => {
|
|
47
|
+
const o = getOas([
|
|
48
|
+
"https://api.example.com/v1/",
|
|
49
|
+
"https://stagingapi.example.com/v1/",
|
|
50
|
+
]);
|
|
51
|
+
expect(c.run(o).paths).toEqual({
|
|
52
|
+
"/v1/foo/hello": {},
|
|
53
|
+
"/v1/foo/world": {},
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("throws if the paths are different in a single OAS", () => {
|
|
58
|
+
const o = getOas([
|
|
59
|
+
"https://api.example.com/v1",
|
|
60
|
+
"https://api.example.com/v2",
|
|
61
|
+
]);
|
|
62
|
+
expect(() => c.run(o).paths).toThrow(
|
|
63
|
+
"Base paths are different in the servers block. Found: /v1, /v2"
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("rewrites the servers", () => {
|
|
68
|
+
const o = getOas([
|
|
69
|
+
"https://api.example.com/v1",
|
|
70
|
+
"https://stagingapi.example.com/v1",
|
|
71
|
+
]);
|
|
72
|
+
expect(c.run(o).servers).toEqual([
|
|
73
|
+
{ url: "https://api.example.com/" },
|
|
74
|
+
{ url: "https://stagingapi.example.com/" },
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
});
|
package/cli/bin.js
CHANGED
|
@@ -5,22 +5,22 @@ const { hideBin } = require("yargs/helpers");
|
|
|
5
5
|
|
|
6
6
|
yargs(hideBin(process.argv))
|
|
7
7
|
.command(
|
|
8
|
-
"merge <openapi
|
|
8
|
+
"merge <openapi> <...more.yaml>",
|
|
9
9
|
"merge the provided OpenAPI files",
|
|
10
10
|
require("./commands/merge")
|
|
11
11
|
)
|
|
12
12
|
.command(
|
|
13
|
-
"check-conflicts <openapi
|
|
13
|
+
"check-conflicts <openapi> <...more.yaml>",
|
|
14
14
|
"check for conflicting components, paths, tags, and security schemes",
|
|
15
15
|
require("./commands/check-conflicts")
|
|
16
16
|
)
|
|
17
17
|
.command(
|
|
18
|
-
"remove-unused-components <openapi
|
|
18
|
+
"remove-unused-components <openapi>",
|
|
19
19
|
"remove unused components from the provided OpenAPI file",
|
|
20
20
|
require("./commands/remove-unused-components")
|
|
21
21
|
)
|
|
22
22
|
.command(
|
|
23
|
-
"remove-unused-tags <openapi
|
|
23
|
+
"remove-unused-tags <openapi>",
|
|
24
24
|
"remove unused tags from the provided OpenAPI file",
|
|
25
25
|
require("./commands/remove-unused-tags")
|
|
26
26
|
)
|
|
@@ -61,4 +61,16 @@ yargs(hideBin(process.argv))
|
|
|
61
61
|
},
|
|
62
62
|
require("./commands/rewrite-path")
|
|
63
63
|
)
|
|
64
|
+
.command(
|
|
65
|
+
"canonical-server <openapi>",
|
|
66
|
+
"move the path from the servers block in to /paths",
|
|
67
|
+
(yargs) => {
|
|
68
|
+
yargs.positional("openapi", {
|
|
69
|
+
require: true,
|
|
70
|
+
describe: "the OpenAPI file to rewrite",
|
|
71
|
+
type: "string",
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
require("./commands/canonical-server")
|
|
75
|
+
)
|
|
64
76
|
.parse();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const yaml = require("js-yaml");
|
|
3
|
+
|
|
4
|
+
module.exports = async function (argv) {
|
|
5
|
+
try {
|
|
6
|
+
const p = require("../../canonical-server");
|
|
7
|
+
let oas = yaml.load(fs.readFileSync(argv.openapi));
|
|
8
|
+
oas = p.run(oas);
|
|
9
|
+
console.log(yaml.dump(oas));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
console.error(`ERROR: ${e.message}`);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
};
|