forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
484 lines (435 loc) · 17.5 KB
/
common.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
'use strict';
const jptr = require('reftools/lib/jptr.js').jptr;
const sampler = require('openapi-sampler');
const safejson = require('safe-json-stringify');
const recurse = require('reftools/lib/recurse.js').recurse;
const visit = require('reftools/lib/visit.js').visit;
const clone = require('reftools/lib/clone.js').clone;
const circularClone = require('reftools/lib/clone.js').circularClone;
const walkSchema = require('oas-schema-walker').walkSchema;
const wsGetState = require('oas-schema-walker').getDefaultState;
const httpsnippetGenerator = require('./httpsnippetGenerator');
/* originally from https://github.com/for-GET/know-your-http-well/blob/master/json/status-codes.json */
/* "Unlicensed", public domain */
const statusCodes = require('./statusCodes.json');
const contentTypes = {
xml: ['^(application|text|image){1}\\/(.*\\+){0,1}xml(;){0,1}(\\s){0,}(charset=.*){0,}$'],
json: ['^(application|text){1}\\/(.*\\+){0,1}json(;){0,1}(\\s){0,}(charset=.*){0,}$'],
yaml: ['application/x-yaml', 'text/x-yaml'],
form: ['multipart/form-data', 'application/x-www-form-urlencoded', 'application/octet-stream']
};
function nop(obj) {
return obj;
}
function doContentType(ctTypes, ctClass) {
for (let type of ctTypes) {
for (let target of contentTypes[ctClass]||[]) {
if (type.match(target)) return true;
}
}
return false;
}
function languageCheck(language, language_tabs, mutate) {
var lcLang = language.toLowerCase();
if (lcLang === 'c#') lcLang = 'csharp';
if (lcLang === 'c++') lcLang = 'cpp';
for (var l in language_tabs) {
var target = language_tabs[l];
if (typeof target === 'object') {
if (Object.keys(target)[0] === lcLang) {
return lcLang;
}
}
else {
if (target === lcLang) return lcLang;
}
}
if (mutate) {
var newLang = {};
newLang[lcLang] = language;
language_tabs.push(newLang);
return lcLang;
}
return false;
}
function getCodeSamples(data) {
let s = '';
const op = data.operation||data.message;
if (op && op["x-code-samples"]) {
for (var c in op["x-code-samples"]) {
var sample = op["x-code-samples"][c];
var lang = languageCheck(sample.lang, data.header.language_tabs, true);
s += generateCodeSnippet(lang, sample.source);
}
}
else {
const samplesGenerator = data.options.httpsnippet
? httpsnippetGenerator.generate
: fileTemplateGenerator;
const codeSamples = data.header.language_tabs
.map(tab => {
const lang = typeof tab === 'object'
? Object.keys(tab)[0]
: tab;
const lowerCaseLanguage = languageCheck(lang, data.header.language_tabs, false);
const target = getLanguageTarget(lowerCaseLanguage);
const client = getLanguageClient(lang, data.options.language_clients);
const sample = (target && samplesGenerator(target, client, data)) || '';
return (sample && generateCodeSnippet(lowerCaseLanguage, sample)) || '';
});
s += codeSamples.join('');
}
return s;
}
function getLanguageTarget(lang) {
// _Check if language custom target is used
// i.e., javascript--nodejs -> nodejs
return (lang && lang.split('--')[1]) || lang;
}
function getLanguageClient(lang, clients) {
if (!(lang && clients && clients.length)) return '';
const client = clients.find(function(e,i,a){
return Object.keys(e)[0] === lang;
});
if (client) return Object.values(client)[0];
return '';
}
function fileTemplateGenerator(target, client, data) {
const templateName = getCodeSampleTemplateName(target);
const templateFunc = data.templates[templateName];
return (templateFunc && templateFunc(data)) || '';
}
function getCodeSampleTemplateName(target) {
return `code_${target}`;
}
function generateCodeSnippet(lang, code) {
const snippetSeparator = '```';
return `${snippetSeparator}${lang}\n${code}\n${snippetSeparator}\n\n`;
}
function gfmLink(text) {
text = text.trim().toLowerCase();
text = text.split("'").join('');
text = text.split('"').join('');
text = text.split('.').join('');
text = text.split('`').join('');
text = text.split(':').join('');
text = text.split('/').join('');
text = text.split('<').join('');
text = text.split('>').join('');
text = text.split('<').join('');
text = text.split('>').join('');
text = text.split(' ').join('-');
return text;
}
function inferType(schema) {
function has(properties) {
for (let property of properties) {
if (typeof schema[property] !== 'undefined') return true;
}
return false;
}
if (schema.type) return schema.type;
let possibleTypes = [];
if (has(['properties','additionalProperties','patternProperties','minProperties','maxProperties','required','dependencies'])) {
possibleTypes.push('object');
}
if (has(['items','additionalItems','maxItems','minItems','uniqueItems'])) {
possibleTypes.push('array');
}
if (has(['exclusiveMaximum','exclusiveMinimum','maximum','minimum','multipleOf'])) {
possibleTypes.push('number');
}
if (has(['maxLength','minLength','pattern'])) {
possibleTypes.push('number');
}
if (schema.enum) {
for (let value of schema.enum) {
possibleTypes.push(typeof value); // doesn't matter about dupes
}
}
if (possibleTypes.length === 1) return possibleTypes[0];
return 'any';
}
function strim(obj,maxDepth) {
if (maxDepth <= 0) return obj;
recurse(obj,{identityDetection:true},function(obj,key,state){
if (state.depth >= maxDepth) {
if (Array.isArray(state.parent[state.pkey])) {
state.parent[state.pkey] = [];
}
else if (typeof state.parent[state.pkey] === 'object') {
state.parent[state.pkey] = {};
}
}
});
return obj;
}
function schemaToArray(schema,offset,options,data) {
let iDepth = 0;
let oDepth = 0;
let blockDepth = 0;
let skipDepth = -1;
let container = [];
let block = { title: '', rows: [] };
if (schema) {
if (schema.title) block.title = schema.title;
if (!block.title && schema.description)
block.title = schema.description;
block.description = schema.description;
if (schema.externalDocs)
block.externalDocs = schema.externalDocs;
}
container.push(block);
let wsState = wsGetState();
wsState.combine = true;
wsState.allowRefSiblings = true;
walkSchema(schema,{},wsState,function(schema,parent,state){
let isBlock = false;
if (state.property && (state.property.startsWith('allOf') || state.property.startsWith('anyOf') || state.property.startsWith('oneOf') || (state.property === 'not'))) {
isBlock = true;
let components = (state.property+'/0').split('/');
if (components[1] !== '0') {
if (components[0] === 'allOf') components[0] = 'and';
if (components[0] === 'anyOf') components[0] = 'or';
if (components[0] === 'oneOf') components[0] = 'xor';
}
block = { title: components[0], rows: [] };
let dschema = schema;
let prefix = '';
if (schema.$ref) {
dschema = jptr(data.api,schema.$ref);
prefix = schema.$ref.replace('#/components/schemas/','')+'.';
}
if (dschema.discriminator) {
block.title += ' - discriminator: '+prefix+dschema.discriminator.propertyName;
}
container.push(block);
blockDepth = state.depth;
}
else {
if (blockDepth && state.depth < blockDepth) {
block = { title: data.translations.continued, rows: [] };
container.push(block);
blockDepth = 0;
}
}
let entry = {};
entry.schema = schema;
entry.in = 'body';
if (state.property && state.property.indexOf('/')) {
if (isBlock) entry.name = '*'+data.translations.anonymous+'*'
else entry.name = state.property.split('/')[1];
}
else if (!state.top) console.warn(state.property);
if (!entry.name && schema.title) entry.name = schema.title;
if (schema.type === 'array' && schema.items && schema.items["x-widdershins-oldRef"] && !entry.name) {
state.top = false; // force it in
}
else if (schema.type === 'array' && schema.items && schema.items.$ref && !entry.name) {
state.top = false; // force it in, for un-dereferenced schemas
}
else if (!entry.name && state.top && schema.type && schema.type !== 'object' && schema.type !== 'array') {
state.top = false;
}
if (!state.top && !entry.name && state.property === 'additionalProperties') {
entry.name = '**additionalProperties**';
}
if (!state.top && !entry.name && state.property === 'additionalItems') {
entry.name = '**additionalItems**';
}
if (!state.top && !entry.name && state.property && state.property.startsWith('patternProperties')) {
entry.name = '*'+entry.name+'*';
}
if (!state.top && !entry.name && !parent.items) {
entry.name = '*'+data.translations.anonymous+'*';
}
// we should be done futzing with entry.name now
if (entry.name) {
if (state.depth > iDepth) {
oDepth++;
}
if (state.depth < iDepth) {
oDepth--;
if (oDepth<0) oDepth=0;
}
iDepth = state.depth;
//console.warn('state %s, idepth %s, odepth now %s, offset %s',state.depth,iDepth,oDepth,offset);
}
entry.depth = Math.max(oDepth+offset,0);
//entry.depth = Math.max(oDepth-1,0)/2;
//if (entry.depth<1) entry.depth = 0;
entry.description = schema.description;
entry.type = schema.type;
entry.format = schema.format;
entry.safeType = entry.type;
if (schema["x-widdershins-oldRef"]) {
entry.$ref = schema["x-widdershins-oldRef"].replace('#/components/schemas/','');
entry.safeType = '['+entry.$ref+'](#schema'+entry.$ref.toLowerCase()+')';
if (data.options.shallowSchemas) skipDepth = entry.depth;
if (!entry.description) {
let target = jptr(data.api,schema["x-widdershins-oldRef"]);
if (target.description) entry.description = target.description;
}
}
if (schema.$ref) { // repeat for un-dereferenced schemas
entry.$ref = schema.$ref.replace('#/components/schemas/','');
entry.type = '$ref';
entry.safeType = '['+entry.$ref+'](#schema'+entry.$ref.toLowerCase()+')';
if (data.options.shallowSchemas) skipDepth = entry.depth;
if (!entry.description) {
let target = jptr(data.api,schema.$ref);
if (target.description) entry.description = target.description;
}
}
if (entry.format) entry.safeType = entry.safeType+'('+entry.format+')';
if ((entry.type === 'array') && schema.items) {
let itemsType = schema.items.type||'any';
if (schema.items["x-widdershins-oldRef"]) {
let $ref = schema.items["x-widdershins-oldRef"].replace('#/components/schemas/','');
itemsType = '['+$ref+'](#schema'+$ref.toLowerCase()+')';
if (!entry.description) {
let target = jptr(data.api,schema.items["x-widdershins-oldRef"]);
if (target.description) entry.description = '['+target.description+']';
}
}
if (schema.items.$ref) { // repeat for un-dereferenced schemas
let $ref = schema.items.$ref.replace('#/components/schemas/','');
itemsType = '['+$ref+'](#schema'+$ref.toLowerCase()+')';
if (!entry.description) {
let target = jptr(data.api,schema.items.$ref);
if (target.description) entry.description = '['+target.description+']';
}
}
if (schema.items.anyOf) itemsType = 'anyOf';
if (schema.items.allOf) itemsType = 'allOf';
if (schema.items.oneOf) itemsType = 'oneOf';
if (schema.items.not) itemsType = 'not';
entry.safeType = '['+itemsType+']';
}
if (options.trim && typeof entry.description === 'string') {
entry.description = entry.description.trim();
}
if (options.join && typeof entry.description === 'string') {
entry.description = entry.description.split('\r').join('').split('\n').join(' ');
}
if (options.truncate && typeof entry.description === 'string') {
entry.description = entry.description.split('\r').join('').split('\n')[0];
}
if (entry.description === 'undefined') { // yes, the string
entry.description = '';
}
if (schema.nullable === true) {
entry.safeType += '\\|null';
}
if (schema.readOnly) entry.restrictions = data.translations.readOnly;
if (schema.writeOnly) entry.restrictions = data.translations.writeOnly;
entry.required = (parent.required && Array.isArray(parent.required) && parent.required.indexOf(entry.name)>=0);
if (typeof entry.required === 'undefined') entry.required = false;
if (typeof entry.type === 'undefined') {
entry.type = inferType(schema);
entry.safeType = entry.type;
}
if (typeof entry.name === 'string' && entry.name.startsWith('x-widdershins-')) {
entry.name = ''; // reset
}
if ((skipDepth >= 0) && (entry.depth >= skipDepth)) entry.name = ''; // reset
if (entry.depth < skipDepth) skipDepth = -1;
entry.displayName = (data.translations.indent.repeat(entry.depth)+' '+entry.name).trim();
if ((!state.top || entry.type !== 'object') && (entry.name)) {
block.rows.push(entry);
}
});
return container;
}
function clean(obj) {
if (typeof obj === 'undefined') return {};
visit(obj,{},{filter:function(obj,key,state){
if (!key.startsWith('x-widdershins')) return obj[key];
}});
return obj;
}
function getSampleInner(orig,options,samplerOptions,api){
if (!options.samplerErrors) options.samplerErrors = new Map();
let obj = circularClone(orig);
let defs = api; //Object.assign({},api,orig);
if (options.sample && obj) {
try {
var sample = sampler.sample(obj,samplerOptions,defs); // was api
if (sample && typeof sample.$ref !== 'undefined') {
//console.warn(util.inspect(orig));
obj = JSON.parse(safejson(orig));
sample = sampler.sample(obj,samplerOptions,defs);
}
if (typeof sample !== 'undefined') {
if (sample !== null && Object.keys(sample).length) return sample
else {
return sampler.sample({ type: 'object', properties: { anonymous: obj}},samplerOptions,defs).anonymous;
}
}
}
catch (ex) {
if (options.samplerErrors.has(ex.message)) {
process.stderr.write('.');
}
else {
console.error('# sampler ' + ex.message);
options.samplerErrors.set(ex.message,true);
}
if (options.verbose) {
console.error(ex);
}
obj = JSON.parse(safejson(orig));
try {
sample = sampler.sample(obj,samplerOptions,defs);
if (typeof sample !== 'undefined') return sample;
}
catch (ex) {
if (options.samplerErrors.has(ex.message)) {
process.stderr.write('.');
}
else {
console.warn('# sampler 2nd error ' + ex.message);
options.samplerErrors.set(ex.message,true);
}
}
}
}
return obj;
}
function getSample(orig,options,samplerOptions,api){
if (orig && orig.example) return orig.example;
let result = getSampleInner(orig,options,samplerOptions,api);
result = clean(result);
result = strim(result,options.maxDepth);
return result;
}
function removeDupeBlankLines(content) {
return content.replace(/[\r\n]{3,}/g, '\n\n');
}
function toPrimitive(v) {
if (typeof v === 'object') { // including arrays
return JSON.stringify(v);
}
return v;
}
function slugify(text) {
return text.toString().toLowerCase().trim()
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[\s\W-]+/g, '-') // Replace spaces, non-word characters and dashes with a single dash (-)
}
module.exports = {
statusCodes : statusCodes,
doContentType : doContentType,
languageCheck : languageCheck,
getCodeSamples : getCodeSamples,
inferType : inferType,
clone : clone,
clean : clean,
strim : strim,
slugify : slugify,
getSample : getSample,
gfmLink : gfmLink,
schemaToArray : schemaToArray,
removeDupeBlankLines: removeDupeBlankLines,
toPrimitive: toPrimitive
};