UNPKG

1.44 kBJavaScriptView Raw
1const jsYml = require('js-yaml');
2const fs = require('fs');
3const path = require('path');
4const merge = require('lodash.merge')
5
6// if a method has request params this step copies the specification to the options method
7class 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
43module.exports = AddCorsParams