1 | const jsYml = require('js-yaml');
|
2 | const fs = require('fs');
|
3 | const integrationBuilder = require('../integrations/builder')
|
4 |
|
5 | class CreateIntegrationFile {
|
6 | invoke(options, content, serverless) {
|
7 | try {
|
8 | fs.mkdirSync(options.outputDirectory, { recursive: true })
|
9 | Object.entries(content.paths).forEach(([path, pathContent]) => {
|
10 | let integrationMethods = {[path]: {}};
|
11 | Object.entries(pathContent).forEach(([method, methodContent]) => {
|
12 | if (!methodContent.hasOwnProperty('x-amazon-apigateway-integration')) {
|
13 | integrationMethods[path][method] = integrationBuilder.build(
|
14 | options.type,
|
15 | method,
|
16 | methodContent.parameters,
|
17 | methodContent.responses
|
18 | );
|
19 | }
|
20 | });
|
21 |
|
22 | if (Object.keys(integrationMethods[path]).length > 0) {
|
23 | const filename = options.outputDirectory + '/' + Date.now() + Math.floor(Math.random() * 1000) + '.yml';
|
24 | fs.writeFileSync(filename, jsYml.dump({'paths': integrationMethods}), 'utf8');
|
25 | serverless.cli.log(
|
26 | `Generated integration ${path} in file ${filename}`,
|
27 | 'OpenApi Integration Plugin',
|
28 | {color: 'green'}
|
29 | );
|
30 | } else {
|
31 | serverless.cli.log(
|
32 | `All methods in path ${path} contains x-amazon-apigateway-integration blocks. Nothing to do`,
|
33 | 'OpenApi Integration Plugin',
|
34 | {color: 'orange'}
|
35 | );
|
36 | }
|
37 |
|
38 | });
|
39 |
|
40 | } catch (e) {
|
41 | console.log(e);
|
42 | }
|
43 |
|
44 | return content;
|
45 | }
|
46 | }
|
47 |
|
48 | module.exports = CreateIntegrationFile
|