-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathexpose.js
167 lines (156 loc) · 5.72 KB
/
expose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const beauty = require('js-beautify').js;
const fs = require('fs');
const { inspect } = require('util');
/**
* Expose definitions objects, create files with objects
* @param {object} definitions - object that contain definitions objects
* @param {array} methods - array of the available methods
* @param {string} path - where to generate the files, resulting path will be path/<directory>
* @param {string} directory - name of the directory
*/
function expose(definitions, methods, path, directory) {
try {
// get list of the definitions
const list = Object.keys(definitions);
// do not proceed if there are no definitions
if (list.length === 0) {
return console.log('> swagger-js-codegen @ No objects to expose!');
}
// make sure that ~/definitions directory exists
const container = `${path}/${directory}`;
if (!fs.existsSync(container)) {
fs.mkdirSync(container);
}
// process definitions
list.forEach(async (definition) => {
// bind the parameters
let parameters = '';
const props = Object.keys(definitions[definition].properties);
if (props.length && props.length > 0) {
props.forEach((prop) => {
const { type } = definitions[definition].properties[prop];
if (type) {
if (type === 'array') {
const { items } = definitions[definition].properties[prop];
if (items) {
if (items['$ref']) {
const refName = items['$ref'].split('/').slice(-1)[0];
parameters = `${parameters}
this.data['${prop}'] = [];
if (params['${prop}'].length && params['${prop}'].length > 0) {
params['${prop}'].forEach((object) => {
const ${refName} = new global.classes['${refName}'](req, res, object);
this.data.${prop}.push(${refName});
});
}`;
} else {
parameters = `${parameters}
this.data['${prop}'] = req.body['${prop}'];`;
}
} else {
parameters = `${parameters}
this.data['${prop}'] = req.body['${prop}'];`;
}
} else {
parameters = `${parameters}
this.data['${prop}'] = req.body['${prop}'];`;
}
} else {
if (definitions[definition].properties[prop]['$ref']) {
const refName = definitions[definition].properties[prop]['$ref'].split('/').slice(-1)[0];
parameters = `${parameters}
this.data['${prop}'] = new global.classes['${refName}'](req, res, req.body['${prop}']);`;
}
}
});
}
// check x-AuthFieldType field
const secure = [];
if (!(definitions[definition].properties instanceof Array)) {
const properties = Object.keys(definitions[definition].properties);
properties.forEach((property) => {
if (definitions[definition].properties[property]['x-AuthFieldType']) {
methods.forEach((method) => {
method.parameters.forEach((parameter) => {
if (parameter.name === definition) {
secure.push({
type: parameter['in'],
definition,
property,
value: definitions[definition].properties[property]['x-AuthFieldType'],
});
}
});
});
}
});
} else {
definitions[definition].properties.forEach((property, i) => {
if (property['x-AuthFieldType']) {
methods.forEach((method) => {
method.parameters.forEach((parameter) => {
if (parameter.name === definition) {
secure.push({
type: parameter['in'],
definition,
property,
value: property['x-AuthFieldType'],
});
}
})
});
}
});
}
// add validation
let validation = '';
if (secure.length > 0) {
validation = '';
secure.forEach((property) => {
let origin = property.type;
if (origin === 'path') origin = 'params';
validation = `${validation}
global.FieldValidator.validate('${property.value}', req.${origin}['${property.property}'], req, res)
.then((result) => {
if (!result) {
return;
}
})
.catch((err) => {
return;
});
`;
});
}
// compile the file
const content = `/* auto-generated: ${definition}.js */
module.exports = class {
constructor(req = {}, res = {}, params = {}) {
this.req = req;
this.res = res;
this.params = params;
this.data = {};
${parameters}
${validation}
this.schema = ${inspect(definitions[definition], { showHidden: false, depth: null })};
}
};`;
// make sure that destination definition directory exists
const destination = `${container}/${definition}`;
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination);
}
// create file in the destination folder
fs.writeFileSync(`${destination}/${definition}.js`,
beauty(content, { indent_size: 2 }),
(err) => {
if (err) {
throw new Error(err.message || err);
}
});
});
} catch (err) {
throw new Error(err.message || err);
}
}
module.exports = expose;