vls-openapi-generator 1.7.0 → 1.8.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.
@@ -41,6 +41,7 @@ require("module-alias/register.js");
41
41
  const path = __importStar(require("path"));
42
42
  const util_1 = require("util");
43
43
  const zod_1 = require("zod");
44
+ const client_cloudformation_1 = require("@aws-sdk/client-cloudformation");
44
45
  const OPENAPI_FILE = path.join(process.cwd(), 'openapi.json');
45
46
  const HANDLERS_DIR = path.join(process.cwd(), 'dist', 'src', 'handlers');
46
47
  const SCHEMAS_DIR = path.join(process.cwd(), 'dist', 'src', 'schemas');
@@ -49,9 +50,20 @@ const generateOpenAPI = async () => {
49
50
  console.info('Generating Open API documentation...');
50
51
  const args = process.argv;
51
52
  const params = {};
52
- for (let i = 2; i < args.length; i += 2) {
53
+ for (let i = 2; i < args.length;) {
53
54
  const key = args[i].replace('--', '');
54
55
  params[key] = args[i + 1];
56
+ if (!params[key]) {
57
+ i++;
58
+ continue;
59
+ }
60
+ if (params[key].startsWith('--')) {
61
+ i++;
62
+ delete params[key];
63
+ }
64
+ else {
65
+ i += 2;
66
+ }
55
67
  }
56
68
  await (0, util_1.promisify)(child_process_1.exec)('rm -rf ./dist');
57
69
  await (0, util_1.promisify)(child_process_1.exec)('npx tsc');
@@ -121,7 +133,43 @@ const generateOpenAPI = async () => {
121
133
  }
122
134
  };
123
135
  }
136
+ const partnerName = params['partner-name'];
137
+ if (partnerName) {
138
+ const developmentURLIndex = existingOpenAPIFile.servers.findIndex((x) => x.url.includes('development'));
139
+ const fetchedDevelopmentURL = await getStackURL(partnerName, 'development');
140
+ if (fetchedDevelopmentURL) {
141
+ existingOpenAPIFile.servers[developmentURLIndex].url = fetchedDevelopmentURL;
142
+ }
143
+ const productionURLIndex = existingOpenAPIFile.servers.findIndex((x) => x.url.includes('production'));
144
+ const fetchedProductionURL = await getStackURL(partnerName, 'production');
145
+ if (fetchedProductionURL) {
146
+ existingOpenAPIFile.servers[productionURLIndex].url = fetchedProductionURL;
147
+ }
148
+ }
124
149
  await fs_1.promises.writeFile(OUTPUT_FILE, JSON.stringify(existingOpenAPIFile, undefined, 4));
125
150
  console.info('Open API documentation generated successfully');
126
151
  };
127
152
  exports.generateOpenAPI = generateOpenAPI;
153
+ async function getStackURL(partnerName, environment) {
154
+ const cloudFormationClient = new client_cloudformation_1.CloudFormationClient();
155
+ const describeStacksCommand = new client_cloudformation_1.DescribeStacksCommand({
156
+ StackName: `${partnerName}-development`
157
+ });
158
+ const result = await cloudFormationClient.send(describeStacksCommand);
159
+ if (!result.Stacks) {
160
+ console.warn(`Base URL was not found with the given environment. Environment will not be updated. Given environment: ${environment}`);
161
+ return;
162
+ }
163
+ for (const cloudFormationStack of result.Stacks) {
164
+ if (!cloudFormationStack.Outputs) {
165
+ console.warn(`Output was not found within the given stack. Environment will not be updated. Given environment: ${environment}`);
166
+ return;
167
+ }
168
+ for (const output of cloudFormationStack.Outputs) {
169
+ if (output.OutputKey !== 'WebEndpoint' || !output.OutputValue) {
170
+ continue;
171
+ }
172
+ return output.OutputValue;
173
+ }
174
+ }
175
+ }
@@ -40,7 +40,6 @@ const openapi_to_postmanv2_1 = require("openapi-to-postmanv2");
40
40
  const path = __importStar(require("path"));
41
41
  const util_1 = require("util");
42
42
  const postman_service_1 = require("./postman-service");
43
- const client_cloudformation_1 = require("@aws-sdk/client-cloudformation");
44
43
  const OPENAPI_FILE = path.join(process.cwd(), 'openapi.json');
45
44
  const uploadOpenAPI = async () => {
46
45
  console.info('Uploading Open API documentation to Postman...');
@@ -111,36 +110,20 @@ const uploadOpenAPI = async () => {
111
110
  }
112
111
  }
113
112
  await postmanService.updateCollection(collectionID, collection);
114
- const environments = (await postmanService.getAllEnvironments()).environments;
115
- const stackEnvironment = environments.find((x) => x.name === stackName);
116
- const cloudFormationClient = new client_cloudformation_1.CloudFormationClient();
117
- const describeStacksCommand = new client_cloudformation_1.DescribeStacksCommand({
118
- StackName: stackName
119
- });
120
- const result = await cloudFormationClient.send(describeStacksCommand);
121
- if (!result.Stacks) {
113
+ const baseUrl = existingOpenAPIFile.servers.find((x) => x.url.includes(environment))?.url;
114
+ if (!baseUrl) {
122
115
  console.warn(`Base URL was not found with the given environment. Environment will not be updated. Given environment: ${environment}`);
123
116
  return;
124
117
  }
125
- for (const cloudFormationStack of result.Stacks) {
126
- if (!cloudFormationStack.Outputs) {
127
- console.warn(`Output was not found within the given stack. Environment will not be updated. Given environment: ${environment}`);
128
- return;
129
- }
130
- for (const output of cloudFormationStack.Outputs) {
131
- if (output.OutputKey !== 'WebEndpoint' || !output.OutputValue) {
132
- continue;
133
- }
134
- if (!stackEnvironment) {
135
- await postmanService.createEnvironment(stackName, [{ key: 'baseUrl', value: output.OutputValue }]);
136
- }
137
- else {
138
- await postmanService.updateEnvironment(stackEnvironment.id, stackEnvironment.name, [
139
- { key: 'baseUrl', value: output.OutputValue }
140
- ]);
141
- }
142
- break;
143
- }
118
+ const environments = (await postmanService.getAllEnvironments()).environments;
119
+ const stackEnvironment = environments.find((x) => x.name === stackName);
120
+ if (!stackEnvironment) {
121
+ await postmanService.createEnvironment(stackName, [{ key: 'baseUrl', value: baseUrl }]);
122
+ }
123
+ else {
124
+ await postmanService.updateEnvironment(stackEnvironment.id, stackEnvironment.name, [
125
+ { key: 'baseUrl', value: baseUrl }
126
+ ]);
144
127
  }
145
128
  console.info('Open API documentation uploaded successfully');
146
129
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vls-openapi-generator",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -7,6 +7,7 @@ import { promisify } from 'util';
7
7
  import { z } from 'zod';
8
8
  import { LambdaConfig, OpenAPIConfig } from './lambda-type';
9
9
  import { OpenAPI } from './openapi-type';
10
+ import { CloudFormationClient, DescribeStacksCommand } from '@aws-sdk/client-cloudformation';
10
11
 
11
12
  const OPENAPI_FILE = path.join(process.cwd(), 'openapi.json');
12
13
  const HANDLERS_DIR = path.join(process.cwd(), 'dist', 'src', 'handlers');
@@ -19,9 +20,21 @@ export const generateOpenAPI = async (): Promise<void> => {
19
20
  const args = process.argv;
20
21
  const params: Record<string, string> = {};
21
22
 
22
- for (let i = 2; i < args.length; i += 2) {
23
+ for (let i = 2; i < args.length; ) {
23
24
  const key = args[i].replace('--', '');
24
25
  params[key] = args[i + 1];
26
+
27
+ if (!params[key]) {
28
+ i++;
29
+ continue;
30
+ }
31
+
32
+ if (params[key].startsWith('--')) {
33
+ i++;
34
+ delete params[key];
35
+ } else {
36
+ i += 2;
37
+ }
25
38
  }
26
39
 
27
40
  await promisify(exec)('rm -rf ./dist');
@@ -123,6 +136,62 @@ export const generateOpenAPI = async (): Promise<void> => {
123
136
  };
124
137
  }
125
138
 
139
+ const partnerName = params['partner-name'];
140
+
141
+ if (partnerName) {
142
+ const developmentURLIndex = existingOpenAPIFile.servers.findIndex((x) => x.url.includes('development'));
143
+
144
+ const fetchedDevelopmentURL = await getStackURL(partnerName, 'development');
145
+
146
+ if (fetchedDevelopmentURL) {
147
+ existingOpenAPIFile.servers[developmentURLIndex].url = fetchedDevelopmentURL;
148
+ }
149
+
150
+ const productionURLIndex = existingOpenAPIFile.servers.findIndex((x) => x.url.includes('production'));
151
+
152
+ const fetchedProductionURL = await getStackURL(partnerName, 'production');
153
+
154
+ if (fetchedProductionURL) {
155
+ existingOpenAPIFile.servers[productionURLIndex].url = fetchedProductionURL;
156
+ }
157
+ }
158
+
126
159
  await fs.writeFile(OUTPUT_FILE, JSON.stringify(existingOpenAPIFile, undefined, 4));
127
160
  console.info('Open API documentation generated successfully');
128
161
  };
162
+
163
+ async function getStackURL(partnerName: string, environment: 'development' | 'production') {
164
+ const cloudFormationClient = new CloudFormationClient();
165
+
166
+ const describeStacksCommand = new DescribeStacksCommand({
167
+ StackName: `${partnerName}-development`
168
+ });
169
+
170
+ const result = await cloudFormationClient.send(describeStacksCommand);
171
+
172
+ if (!result.Stacks) {
173
+ console.warn(
174
+ `Base URL was not found with the given environment. Environment will not be updated. Given environment: ${environment}`
175
+ );
176
+
177
+ return;
178
+ }
179
+
180
+ for (const cloudFormationStack of result.Stacks) {
181
+ if (!cloudFormationStack.Outputs) {
182
+ console.warn(
183
+ `Output was not found within the given stack. Environment will not be updated. Given environment: ${environment}`
184
+ );
185
+
186
+ return;
187
+ }
188
+
189
+ for (const output of cloudFormationStack.Outputs) {
190
+ if (output.OutputKey !== 'WebEndpoint' || !output.OutputValue) {
191
+ continue;
192
+ }
193
+
194
+ return output.OutputValue;
195
+ }
196
+ }
197
+ }
@@ -5,7 +5,6 @@ import * as path from 'path';
5
5
  import { promisify } from 'util';
6
6
  import { OpenAPI } from './openapi-type';
7
7
  import { PostmanService } from './postman-service';
8
- import { CloudFormationClient, DescribeStacksCommand } from '@aws-sdk/client-cloudformation';
9
8
 
10
9
  const OPENAPI_FILE = path.join(process.cwd(), 'openapi.json');
11
10
 
@@ -108,19 +107,9 @@ export const uploadOpenAPI = async (): Promise<void> => {
108
107
 
109
108
  await postmanService.updateCollection(collectionID, collection);
110
109
 
111
- const environments = (await postmanService.getAllEnvironments()).environments;
112
-
113
- const stackEnvironment = environments.find((x) => x.name === stackName);
114
-
115
- const cloudFormationClient = new CloudFormationClient();
110
+ const baseUrl = existingOpenAPIFile.servers.find((x) => x.url.includes(environment))?.url;
116
111
 
117
- const describeStacksCommand = new DescribeStacksCommand({
118
- StackName: stackName
119
- });
120
-
121
- const result = await cloudFormationClient.send(describeStacksCommand);
122
-
123
- if (!result.Stacks) {
112
+ if (!baseUrl) {
124
113
  console.warn(
125
114
  `Base URL was not found with the given environment. Environment will not be updated. Given environment: ${environment}`
126
115
  );
@@ -128,30 +117,16 @@ export const uploadOpenAPI = async (): Promise<void> => {
128
117
  return;
129
118
  }
130
119
 
131
- for (const cloudFormationStack of result.Stacks) {
132
- if (!cloudFormationStack.Outputs) {
133
- console.warn(
134
- `Output was not found within the given stack. Environment will not be updated. Given environment: ${environment}`
135
- );
136
-
137
- return;
138
- }
139
-
140
- for (const output of cloudFormationStack.Outputs) {
141
- if (output.OutputKey !== 'WebEndpoint' || !output.OutputValue) {
142
- continue;
143
- }
120
+ const environments = (await postmanService.getAllEnvironments()).environments;
144
121
 
145
- if (!stackEnvironment) {
146
- await postmanService.createEnvironment(stackName, [{ key: 'baseUrl', value: output.OutputValue }]);
147
- } else {
148
- await postmanService.updateEnvironment(stackEnvironment.id, stackEnvironment.name, [
149
- { key: 'baseUrl', value: output.OutputValue }
150
- ]);
151
- }
122
+ const stackEnvironment = environments.find((x) => x.name === stackName);
152
123
 
153
- break;
154
- }
124
+ if (!stackEnvironment) {
125
+ await postmanService.createEnvironment(stackName, [{ key: 'baseUrl', value: baseUrl }]);
126
+ } else {
127
+ await postmanService.updateEnvironment(stackEnvironment.id, stackEnvironment.name, [
128
+ { key: 'baseUrl', value: baseUrl }
129
+ ]);
155
130
  }
156
131
 
157
132
  console.info('Open API documentation uploaded successfully');