1 | const jsYml = require('js-yaml');
|
2 | const integrationBuilder = require('../integrations/builder');
|
3 | const merge = require('lodash.merge');
|
4 |
|
5 | class CreateProxy {
|
6 | invoke(options, content, serverless) {
|
7 | try {
|
8 | if (!options.proxy) {
|
9 | return content;
|
10 | }
|
11 |
|
12 | Object.entries(content.paths).forEach(([path, pathContent]) => {
|
13 | let integrationMethods = {[path]: {}};
|
14 | Object.entries(pathContent).forEach(([method, methodContent]) => {
|
15 | if (!methodContent.hasOwnProperty('x-amazon-apigateway-integration')) {
|
16 |
|
17 | let resolvedPath = path;
|
18 | if (options.proxy.pattern) {
|
19 | resolvedPath = resolvedPath.match(
|
20 | options.proxy.pattern
|
21 | )
|
22 | }
|
23 |
|
24 | integrationMethods[path][method] = integrationBuilder.build(
|
25 | options.proxy.type,
|
26 | method,
|
27 | methodContent.parameters,
|
28 | methodContent.responses,
|
29 | `${options.proxy.baseUrl}${resolvedPath}`
|
30 | );
|
31 | }
|
32 | });
|
33 |
|
34 | if (Object.keys(integrationMethods[path]).length > 0) {
|
35 | content = merge(content, {'paths': integrationMethods})
|
36 | serverless.cli.log(
|
37 | `[BETA] Generating Proxy for ${path}`,
|
38 | 'OpenApi Integration Plugin',
|
39 | {color: 'green'}
|
40 | );
|
41 | }
|
42 | });
|
43 |
|
44 | } catch (e) {
|
45 | console.log(e);
|
46 | }
|
47 |
|
48 | return content;
|
49 | }
|
50 | }
|
51 |
|
52 | module.exports = CreateProxy
|