-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (76 loc) · 3.9 KB
/
Copy pathindex.js
File metadata and controls
105 lines (76 loc) · 3.9 KB
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
#!/usr/bin/env node
'use strict';
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const { getName ,getNameExample, getProperty } = require('./src/utils/schemaUtils');
const { verifyProperties , errorsList} = require('./src/utils/verifyProperties')
const createComponent = require("./src/generator/createComponent")();
const argv = require('yargs')(process.argv.slice(2))
.usage('Usage: $0 -f [file]')
.option('f', {
alias: 'file',
describe: 'Path to openapi file',
type: 'string',
demandOption: true
})
.option('o', {
alias: 'overwrite',
describe: 'Overwrite the existing file if it exists',
type: 'boolean',
default: false
})
.example('\x1b[32m $0 -f /path/to/openapi.yaml \x1b[0m')
.example('\x1b[32m $0 -f /path/to/openapi.yml \x1b[0m')
.example('\x1b[32m $0 -f /path/to/openapi.yaml -o \x1b[0m')
.help()
.alias('h', 'help')
.argv;
global.definition = require('./src/parser/definition.js')();
function generateExample(property, schemaPath, nameExample, example , nameSchema) {
const exampleKey = `${property}.content.application/json.examples.${nameExample}`;
const routeKey = createComponent(definition, example, exampleKey);
const validate = verifyProperties(example, schemaPath , nameSchema , exampleKey);
if (!validate) {
return;
}
_.unset(definition, `${property}.content.application/json.example`);
_.set(definition, exampleKey, { $ref: routeKey });
require('./src/utils/sucess')(`Example generated in ${exampleKey.replace('paths./', '#/').replace(/\./g, '/')}`);
}
Object.keys(definition.paths).forEach(path => {
Object.keys(definition.paths[path]).forEach(method => {
const methodObj = definition.paths[path][method];
Object.keys(methodObj.responses ?? []).forEach(key => {
const response = methodObj.responses[key];
const property = getProperty(response, path, method, key);
const schemaPath = `${property}.content.application/json.schema`;
const nameExample = getNameExample(schemaPath);
const schema = _.get(definition, schemaPath);
const schemaDetails = require('./src/parser/refs.js')(schema, definition);
if (schema) {
if (schema.example) {
generateExample(property, schemaDetails, nameExample, schema.example , getName(schemaPath));
}
if (_.get(response, 'content.application/json.example')) {
generateExample(property, schemaDetails, nameExample, _.get(response, 'content.application/json.example') , getName(schemaPath));
}
if (response?.['$ref'] && !_.get(definition, `${property}.content.application/json.examples`)) {
if (_.get(response, 'content.application/json.example')) {
_.unset(definition, `${property}.content.application/json.example`);
}
require('./src/generator/examples.js')(schema, definition, `${property}.content.application/json.examples.${nameExample}` , schemaDetails , getName(schemaPath))
}
}
if (response?.['content']?.['application/json']?.schema && !response?.['content']?.['application/json']?.examples) {
require('./src/generator/examples.js')(schema, definition, `${property}.content.application/json.examples.${nameExample}` , schemaDetails , getName(schemaPath))
}
});
});
});
if (errorsList.length !== 0) {
require('./src/utils/warning')(`The file could not be generated due to errors encountered in the examples.`);
console.log(errorsList);
} else {
require('./src/generator/file.js')();
}