forked from guifi/fiberfy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.js
114 lines (101 loc) · 3.18 KB
/
doc.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
// Script to extract project Models to json (swagger)
process.chdir(__dirname);
const fs = require('fs');
var models = require('require-all')({
dirname: __dirname + '/api/models',
excludeDirs: /^\.(git|svn)$/,
recursive: true,
});
function addAttributeToResult(name, attr, data, type) {
switch (attr.type) {
case 'string':
case 'number':
case 'integer':
case 'boolean':
case 'array':
case 'object':
data[type].properties[name] = { type: attr.type };
break;
case 'json':
data[type].properties[name] = { type: 'object' };
break;
default:
break;
}
}
function attachAdditionalInfoToAttribute(name, model, data, type) {
try {
let additional = model._swagger.attributes.append[type][name];
let current = data[type].properties[name];
current = Object.assign(current, additional);
} catch (err) {}
}
function addAttributes(model, type, data) {
for (let y in modelsDefaultAttributes) {
if (modelsDefaultAttributes.hasOwnProperty(y)) {
let attr = modelsDefaultAttributes[y];
let isDisabled = false;
try {
isDisabled = Boolean(model._swagger.attributes.ignore[type][y]);
} catch (err) {
isDisabled = false;
}
if (model.attributes[y] !== false && !isDisabled) {
addAttributeToResult(y, attr, data, type);
attachAdditionalInfoToAttribute(y, model, data, type);
}
}
}
for (let y in model.attributes) {
if (model.attributes.hasOwnProperty(y)) {
let attr = model.attributes[y];
let isDisabled = false;
try {
isDisabled = Boolean(model._swagger.attributes.ignore[type][y]);
} catch (err) {
isDisabled = false;
}
if (!isDisabled) {
addAttributeToResult(y, attr, data, type);
attachAdditionalInfoToAttribute(y, model, data, type);
}
}
}
}
function addCustomAttributes(model, type, data) {
let attributes;
try {
attributes = model._swagger.attributes.additions[type];
} catch (err) {
// Nothing
return;
}
for (let y in attributes) {
if (attributes.hasOwnProperty(y)) {
let attr = attributes[y];
data[type].properties[y] = attr;
}
}
}
var modelsDefaultAttributes = require(__dirname + '/config/models').models
.attributes;
for (let x in models) {
if (models.hasOwnProperty(x)) {
let data = {
request: {
properties: {},
},
response: {
properties: {},
},
};
let model = models[x];
addAttributes(model, 'request', data);
addAttributes(model, 'response', data);
addCustomAttributes(model, 'request', data);
addCustomAttributes(model, 'response', data);
let filename = __dirname + '/assets/swagger/models/' + x + '.json';
let json = JSON.stringify(data);
fs.writeFileSync(filename, json);
}
}