forked from apiaddicts/openapi2postman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (107 loc) · 6.3 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
/** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/
'use strict'
const _ = require('lodash');
const argv = require('yargs').argv
const fs = require('fs');
//PARSER-------------------------------- */
let configurationFile
try {
configurationFile = JSON.parse(fs.readFileSync(argv.configuration, "utf8"))
} catch(err) {
require('./src/utils/error.js')('Configuration file not exist or not is correct: ' + argv.configuration);
}
global.definition = require('./src/parser/definition.js')()
const version = require('./src/parser/version.js')()
global.environmentVariables = {}
global.configurationFile = configurationFile
require('./src/parser/'+version+'/refs.js')()
const schemaHostBasePath = require('./src/parser/'+version+'/schemaHostBasePath.js')()
const endpointsParsed = require('./src/parser/endpoints.js')()
const authorizationTokens = []
_.forEach(endpointsParsed, function (endpointParsed, i) {
endpointsParsed[i].status = require('./src/parser/status.js')(endpointParsed.verb, endpointParsed.path)
if (endpointParsed.verb === 'POST' || endpointParsed.verb === 'PUT' || endpointParsed.verb === 'PATCH') {
endpointsParsed[i].body = require('./src/parser/'+version+'/body.js')(endpointParsed.verb, endpointParsed.path)
endpointsParsed[i].consumes = require('./src/parser/'+version+'/consumes.js')(endpointParsed.verb, endpointParsed.path)
}
endpointsParsed[i].pathParameters = require('./src/parser/'+version+'/pathParameters.js')(endpointParsed.verb, endpointParsed.path)
endpointsParsed[i].bodyResponse = require('./src/parser/'+version+'/body.js')(endpointParsed.verb, endpointParsed.path, true)
endpointsParsed[i].authorization = require('./src/parser/authorization.js')(endpointParsed.verb, endpointParsed.path, authorizationTokens)
endpointsParsed[i].queryParams = require('./src/parser/'+version+'/queryParams.js')(endpointParsed.verb, endpointParsed.path)
endpointsParsed[i].summary = require('./src/parser/summary.js')(endpointParsed.verb, endpointParsed.path)
});
//GENERATOR-------------------------------- */
const endpointsPostman = [];
const endpoints = require('./src/generator/endpoints.js')(endpointsParsed);
_.forEach(endpoints, function (endpoint, i) {
endpoint = require('./src/generator/testStatus.js')(endpoint);
endpoint = require('./src/generator/testBody.js')(endpoint);
endpoint = require('./src/generator/contentType.js')(endpoint);
endpoint = require('./src/generator/authorization.js')(endpoint, endpoint.aux.status)
global.currentId = endpoint.request.method + endpoint.request.url.path[0]
global.currentId = global.currentId.replace(/{{/g,'{').replace(/}}/g,'}').split('?')[0]
if (endpoint.aux.status === 404 && endpoint.aux.pathParameter) {
endpoint.request.url.raw = _.replace(endpoint.request.url.raw, '{{' + endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
endpoint.request.url.path[0] = _.replace(endpoint.request.url.path[0], '{{' +endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
endpoint = require('./src/generator/body.js')(endpoint)
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint)
endpointsPostman.push(endpoint)
} else if (endpoint.aux.status === 400) {
global.queryParamsRequiredAdded = []
let endpointPostman
do{
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpoint,true)
if (endpointPostman){
endpointPostman = require('./src/generator/body.js')(endpointPostman)
endpointPostman.name += '.without.' + _.last(global.queryParamsRequiredAdded) ;
endpointPostman.aux.suffix = '.without.' +_.last(global.queryParamsRequiredAdded) ;
endpointsPostman.push(endpointPostman);
}
} while(endpointPostman)
addBadRequestEndpoints(endpointsPostman, endpoint, 'requiredParams', '', true, false);
addBadRequestEndpoints(endpointsPostman, endpoint, 'wrongParams', '.wrong', false, true);
} else if ((endpoint.aux.status >= 200 && endpoint.aux.status < 300) || ((endpoint.aux.status === 401 || endpoint.aux.status === 403) && endpoint.aux.authorization)) {
endpoint = require('./src/generator/body.js')(endpoint);
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint);
endpointsPostman.push(endpoint);
}
})
//EXPORT-------------------------------- */
let apiName = argv.api_name || configurationFile.api_name
configurationFile = configurationFile.environments
_.forEach(configurationFile, function (element) {
const endpointsStage = _.cloneDeep(endpointsPostman)
let exclude = {}
if ( element.read_only ){
exclude.write = true
}
if ( element.custom_authorizations_file ) {
require('./src/parser/authorizationRequests.js')(endpointsStage,element.custom_authorizations_file)
} else {
exclude.auth = true
}
let endpointsPostmanWithFolders = require('./src/generator/folders.js')(endpointsStage, exclude)
let environmentVariables = require('./src/generator/environmentVariablesNames.js')(endpointsPostmanWithFolders)
if (element.validate_schema === false){
require('./src/generator/validateSchema.js')(endpointsPostmanWithFolders)
}
if ( apiName ) {
element.postman_collection_name = _.replace(element.postman_collection_name, '%api_name%', apiName)
element.postman_environment_name = _.replace(element.postman_environment_name, '%api_name%', apiName)
}
require('./src/generator/collection.js')(element.target_folder, element.postman_collection_name, endpointsPostmanWithFolders)
require('./src/generator/environment.js')(element.target_folder, element.postman_environment_name, element.host, element.port, schemaHostBasePath,environmentVariables)
})
function addBadRequestEndpoints(endpointsPostman, endpointBase, memoryAlreadyAdded, suffix, withoutRequired, withWrongParam) {
global[memoryAlreadyAdded] = [];
do {
var initialCount = global[memoryAlreadyAdded].length;
let endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointBase);
endpointPostman = require('./src/generator/body.js')(endpointPostman, withoutRequired, withWrongParam);
if (global[memoryAlreadyAdded].length > initialCount) {
endpointPostman.name += '-' + _.last(global[memoryAlreadyAdded]) + suffix;
endpointPostman.aux.suffix = _.last(global[memoryAlreadyAdded]) + suffix;
endpointsPostman.push(endpointPostman);
}
} while (global[memoryAlreadyAdded].length > initialCount)
}