oas-toolkit 0.10.2 → 0.10.4
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 +11 -1
- package/canonical-server.test.js +53 -1
- package/package.json +1 -1
package/canonical-server.js
CHANGED
|
@@ -11,6 +11,9 @@ function run(oas) {
|
|
|
11
11
|
// Extract the base path from servers
|
|
12
12
|
const basePaths = uniqWith(
|
|
13
13
|
oas.servers.map((server) => {
|
|
14
|
+
if (server.variables) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
14
17
|
const path = url.parse(server.url).pathname;
|
|
15
18
|
if (path.slice(-1) === "/") {
|
|
16
19
|
return path.slice(0, -1);
|
|
@@ -18,7 +21,11 @@ function run(oas) {
|
|
|
18
21
|
return path;
|
|
19
22
|
}),
|
|
20
23
|
isEqual
|
|
21
|
-
);
|
|
24
|
+
).filter((n) => n);
|
|
25
|
+
|
|
26
|
+
if (basePaths.length === 0) {
|
|
27
|
+
return oas;
|
|
28
|
+
}
|
|
22
29
|
|
|
23
30
|
if (basePaths.length > 1) {
|
|
24
31
|
throw new Error(
|
|
@@ -36,6 +43,9 @@ function run(oas) {
|
|
|
36
43
|
|
|
37
44
|
// Remove paths from servers
|
|
38
45
|
oas.servers = oas.servers.map((server) => {
|
|
46
|
+
if (server.variables) {
|
|
47
|
+
return server;
|
|
48
|
+
}
|
|
39
49
|
const u = url.parse(server.url);
|
|
40
50
|
return {
|
|
41
51
|
...server,
|
package/canonical-server.test.js
CHANGED
|
@@ -12,13 +12,20 @@ const oas = {
|
|
|
12
12
|
},
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
function getOas(urls) {
|
|
15
|
+
function getOas(urls, variables) {
|
|
16
16
|
const o = {
|
|
17
17
|
paths: oas.paths,
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
if (urls.length > 0) {
|
|
21
21
|
o.servers = urls.map((url) => {
|
|
22
|
+
if (variables) {
|
|
23
|
+
return {
|
|
24
|
+
url,
|
|
25
|
+
variables,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
return {
|
|
23
30
|
url,
|
|
24
31
|
};
|
|
@@ -36,6 +43,22 @@ describe("#run", () => {
|
|
|
36
43
|
});
|
|
37
44
|
});
|
|
38
45
|
|
|
46
|
+
it("is idempotent", () => {
|
|
47
|
+
const o = getOas(["https://api.example.com/v1"]);
|
|
48
|
+
|
|
49
|
+
const updatedOas = c.run(o);
|
|
50
|
+
expect(updatedOas.paths).toEqual({
|
|
51
|
+
"/v1/foo/hello": {},
|
|
52
|
+
"/v1/foo/world": {},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Run a second time
|
|
56
|
+
expect(c.run(updatedOas).paths).toEqual({
|
|
57
|
+
"/v1/foo/hello": {},
|
|
58
|
+
"/v1/foo/world": {},
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
39
62
|
it("handles multiple servers", () => {
|
|
40
63
|
const o = getOas([
|
|
41
64
|
"https://api.example.com/v1",
|
|
@@ -88,4 +111,33 @@ describe("#run", () => {
|
|
|
88
111
|
},
|
|
89
112
|
});
|
|
90
113
|
});
|
|
114
|
+
|
|
115
|
+
it("ignores variable based urls", () => {
|
|
116
|
+
const vars = {
|
|
117
|
+
hostname: {
|
|
118
|
+
default: "localhost",
|
|
119
|
+
description: "Hostname for Kong's Admin API",
|
|
120
|
+
},
|
|
121
|
+
path: {
|
|
122
|
+
default: "/",
|
|
123
|
+
description: "Base path for Kong's Admin API",
|
|
124
|
+
},
|
|
125
|
+
port: {
|
|
126
|
+
default: "8001",
|
|
127
|
+
description: "Port for Kong's Admin API",
|
|
128
|
+
},
|
|
129
|
+
protocol: {
|
|
130
|
+
default: "http",
|
|
131
|
+
description: "Protocol for requests to Kong's Admin API",
|
|
132
|
+
enum: ["http", "https"],
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
const o = getOas(["{protocol}://{hostname}:{port}{path}"], vars);
|
|
136
|
+
expect(c.run(o).servers).toEqual([
|
|
137
|
+
{
|
|
138
|
+
url: "{protocol}://{hostname}:{port}{path}",
|
|
139
|
+
variables: vars,
|
|
140
|
+
},
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
91
143
|
});
|