oas-toolkit 0.10.1 → 0.10.3

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.
@@ -4,9 +4,16 @@ const url = require("url");
4
4
  function run(oas) {
5
5
  oas = JSON.parse(JSON.stringify(oas)); // Prevent modification of original object
6
6
 
7
+ if (!oas.servers) {
8
+ return oas;
9
+ }
10
+
7
11
  // Extract the base path from servers
8
12
  const basePaths = uniqWith(
9
13
  oas.servers.map((server) => {
14
+ if (server.variables) {
15
+ return null;
16
+ }
10
17
  const path = url.parse(server.url).pathname;
11
18
  if (path.slice(-1) === "/") {
12
19
  return path.slice(0, -1);
@@ -14,7 +21,7 @@ function run(oas) {
14
21
  return path;
15
22
  }),
16
23
  isEqual
17
- );
24
+ ).filter((n) => n);
18
25
 
19
26
  if (basePaths.length > 1) {
20
27
  throw new Error(
@@ -32,6 +39,9 @@ function run(oas) {
32
39
 
33
40
  // Remove paths from servers
34
41
  oas.servers = oas.servers.map((server) => {
42
+ if (server.variables) {
43
+ return server;
44
+ }
35
45
  const u = url.parse(server.url);
36
46
  return {
37
47
  ...server,
@@ -12,15 +12,26 @@ const oas = {
12
12
  },
13
13
  };
14
14
 
15
- function getOas(urls) {
16
- return {
17
- servers: urls.map((url) => {
15
+ function getOas(urls, variables) {
16
+ const o = {
17
+ paths: oas.paths,
18
+ };
19
+
20
+ if (urls.length > 0) {
21
+ o.servers = urls.map((url) => {
22
+ if (variables) {
23
+ return {
24
+ url,
25
+ variables,
26
+ };
27
+ }
28
+
18
29
  return {
19
30
  url,
20
31
  };
21
- }),
22
- paths: oas.paths,
23
- };
32
+ });
33
+ }
34
+ return o;
24
35
  }
25
36
 
26
37
  describe("#run", () => {
@@ -74,4 +85,43 @@ describe("#run", () => {
74
85
  { url: "https://stagingapi.example.com/" },
75
86
  ]);
76
87
  });
88
+
89
+ it("ignores empty server blocks", () => {
90
+ const o = getOas([]);
91
+ expect(c.run(o)).toEqual({
92
+ paths: {
93
+ "/foo/hello": {},
94
+ "/foo/world": {},
95
+ },
96
+ });
97
+ });
98
+
99
+ it("ignores variable based urls", () => {
100
+ const vars = {
101
+ hostname: {
102
+ default: "localhost",
103
+ description: "Hostname for Kong's Admin API",
104
+ },
105
+ path: {
106
+ default: "/",
107
+ description: "Base path for Kong's Admin API",
108
+ },
109
+ port: {
110
+ default: "8001",
111
+ description: "Port for Kong's Admin API",
112
+ },
113
+ protocol: {
114
+ default: "http",
115
+ description: "Protocol for requests to Kong's Admin API",
116
+ enum: ["http", "https"],
117
+ },
118
+ };
119
+ const o = getOas(["{protocol}://{hostname}:{port}{path}"], vars);
120
+ expect(c.run(o).servers).toEqual([
121
+ {
122
+ url: "{protocol}://{hostname}:{port}{path}",
123
+ variables: vars,
124
+ },
125
+ ]);
126
+ });
77
127
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oas-toolkit",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {