This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
forked from raml2html/raml2obj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·227 lines (198 loc) · 6.77 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
'use strict';
const raml = require('raml-1-parser');
const tools = require('datatype-expansion');
const fs = require('fs');
const makeExamplesAndTypesConsistent = require('./consistency-helpers');
const helpers = require('./arrays-objects-helpers');
function _makeUniqueId(string) {
const stringWithSpacesReplaced = string.replace(/\W/g, '_');
const stringWithLeadingUnderscoreRemoved = stringWithSpacesReplaced.replace(
new RegExp('^_+'),
''
);
return stringWithLeadingUnderscoreRemoved.toLowerCase();
}
// Add unique id's and parent URL's plus parent URI parameters to resources
function _addRaml2htmlProperties(ramlObj, parentUrl, allUriParameters) {
// Add unique id's to top level documentation chapters
if (ramlObj.documentation) {
ramlObj.documentation.forEach(docSection => {
docSection.uniqueId = _makeUniqueId(docSection.title);
});
}
if (!ramlObj.resources) {
return ramlObj;
}
ramlObj.resources.forEach(resource => {
resource.parentUrl = parentUrl || '';
resource.uniqueId = _makeUniqueId(
resource.parentUrl + resource.relativeUri
);
resource.allUriParameters = [];
if (allUriParameters) {
resource.allUriParameters.push.apply(
resource.allUriParameters,
allUriParameters
);
}
if (resource.uriParameters) {
resource.uriParameters.forEach(uriParameter => {
resource.allUriParameters.push(uriParameter);
});
}
// Copy the RESOURCE uri parameters to the METHOD, because that's where they will be rendered.
if (resource.methods) {
resource.methods.forEach(method => {
method.allUriParameters = resource.allUriParameters;
});
}
_addRaml2htmlProperties(
resource,
resource.parentUrl + resource.relativeUri,
resource.allUriParameters
);
});
return ramlObj;
}
// This uses the datatype-expansion library to expand all the root type to their canonical expanded form
function _expandRootTypes(types) {
if (!types) {
return types;
}
Object.keys(types).forEach(key => {
try {
const original = types[key];
const expanded = tools.expandedForm(original, types, {
trackOriginalType: true,
});
const canonical = tools.canonicalForm(expanded, { hoistUnions: false });
// Save a reference to the type as defined in the RAML, so we can differentiate between declared
// and inherited facets, particularly annotations.
canonical.rawType = original;
types[key] = canonical;
} catch (err) {
// Dump the error to stderr and continue with the non-canonical form
console.error(
'Warning: Unable to canonicalize type "' + key + '": ' + err.message
);
}
});
return types;
}
function _enhanceRamlObj(ramlObj, options) {
// Override default options
options = Object.assign(
{
collectionFormat: 'objects',
},
options
);
// Some of the structures (like `types`) are an array that hold key/value pairs, which is very annoying to work with.
// Let's make them into a simple object, this makes it easy to use them for direct lookups.
//
// EXAMPLE of these structures:
// [
// { foo: { ... } },
// { bar: { ... } },
// ]
//
// EXAMPLE of what we want (default option "objects")
// { foo: { ... }, bar: { ... } }
//
// EXAMPLE of what we want (option "arrays")
// [ { key: "foo", ... }, { key: "bar", ... } ]
// the "arrays" option will be evalulated at the very end to so the conversion and cleanup code
// does not have to handle different data structures.
ramlObj = helpers.arraysToObjects(ramlObj);
// We want to expand inherited root types, so that later on when we copy type properties into an object,
// we get the full graph.
const types = makeExamplesAndTypesConsistent(_expandRootTypes(ramlObj.types));
// Delete the types from the ramlObj so it's not processed again later on.
delete ramlObj.types;
// Recursively go over the entire object and make all examples and types consistent.
ramlObj = makeExamplesAndTypesConsistent(ramlObj, types);
// Other structures (like `responses`) are an object that hold other wrapped objects.
// Flatten this to simple (non-wrapped) objects in an array instead,
// this makes it easy to loop over them in raml2html / raml2md.
//
// EXAMPLE of these structures:
// {
// foo: {
// name: "foo!"
// },
// bar: {
// name: "bar"
// }
// }
//
// EXAMPLE of what we want:
// [ { name: "foo!", key: "foo" }, { name: "bar", key: "bar" } ]
ramlObj = helpers.recursiveObjectToArray(ramlObj);
// Now add all the properties and things that we need for raml2html, stuff like the uniqueId, parentUrl,
// and allUriParameters.
ramlObj = _addRaml2htmlProperties(ramlObj);
if (types) {
ramlObj.types = types;
}
// convert to optional variations in the output structure:
if (options.collectionFormat === 'arrays') {
// repeat recursive to also clean up the types:
ramlObj = helpers.recursiveObjectToArray(ramlObj);
// modify the top-level collections to be arrays
ramlObj = helpers.objectsToArrays(ramlObj);
}
return ramlObj;
}
function _sourceToRamlObj(source, options = {}) {
// "options" was originally a validation flag
if (typeof options === 'boolean') {
options = { validate: options };
}
if (typeof source === 'string') {
if (fs.existsSync(source) || source.indexOf('http') === 0) {
// Parse as file or url
return raml
.loadApi(source, options.extensionsAndOverlays || [], {
rejectOnErrors: !!options.validate,
httpResolver: options.httpResolver,
fsResolver: options.fsResolver,
})
.then(result => {
if (result._node._universe._typedVersion === '0.8') {
throw new Error('_sourceToRamlObj: only RAML 1.0 is supported!');
}
if (result.expand) {
return result.expand(true).toJSON({ serializeMetadata: false });
}
return new Promise((resolve, reject) => {
reject(
new Error(
'_sourceToRamlObj: source could not be parsed. Is it a root RAML file?'
)
);
});
});
}
return new Promise((resolve, reject) => {
reject(new Error('_sourceToRamlObj: source does not exist.'));
});
} else if (typeof source === 'object') {
// Parse RAML object directly
return new Promise(resolve => {
resolve(source);
});
}
return new Promise((resolve, reject) => {
reject(
new Error(
'_sourceToRamlObj: You must supply either file, url or object as source.'
)
);
});
}
module.exports.parse = function(source, options) {
return _sourceToRamlObj(source, options).then(ramlObj =>
_enhanceRamlObj(ramlObj, options)
);
};