1 | const jsYml = require('js-yaml');
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 | const merge = require('lodash.merge')
|
5 |
|
6 |
|
7 | class AddCorsParams {
|
8 | invoke(options, content, serverless) {
|
9 | try {
|
10 | if (!options.cors) {
|
11 | return content;
|
12 | }
|
13 |
|
14 | Object.entries(content.paths).forEach(([path, pathContent]) => {
|
15 | let parameters = {};
|
16 | Object.entries(pathContent).forEach(([method, methodContent]) => {
|
17 | if (method !== 'options') {
|
18 | if (methodContent.hasOwnProperty('parameters')) {
|
19 | parameters = methodContent.parameters;
|
20 | }
|
21 | }
|
22 | })
|
23 |
|
24 | if (Object.keys(parameters).length > 0) {
|
25 | Object.entries(pathContent).forEach(([method, methodContent]) => {
|
26 | if (method === 'options') {
|
27 | let parameterValues = {parameters: parameters};
|
28 | Object.assign(methodContent, merge(methodContent, parameterValues));
|
29 | }
|
30 | });
|
31 | }
|
32 | });
|
33 |
|
34 |
|
35 | return content;
|
36 | } catch (e) {
|
37 | console.log(e);
|
38 | }
|
39 | return content
|
40 | }
|
41 | }
|
42 |
|
43 | module.exports = AddCorsParams
|