This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
233 lines (221 loc) · 7.17 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
228
229
230
231
232
233
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const util = require('util');
const fetch = require('node-fetch');
const co = require('co');
const yaml = require('yaml');
yaml.defaultOptions.prettyErrors = true;
const jsy = require('js-yaml');
const swagger2openapi = require('swagger2openapi');
const openapi3 = require('oas-validator');
const asyncApiSchema1 = require('asyncapi/schemas/1.2.0.json');
const asyncApiSchema2 = require('asyncapi/schemas/2.0.0.json');
const ss = require('@exodus/schemasafe').validator;
const recurse = require('reftools/lib/recurse').recurse;
//require("raml-1-parser/plugins/resourceUriValidationPlugin");
//require("raml-1-parser/plugins/crudAnnotationsValidator");
//require("raml-1-parser/plugins/uninheritedTypesDetector");
//require("raml-1-parser/plugins/xmlAnnotationsValidator");
//https://schema.getpostman.com/
function check_api(api,options,callback) {
options.mode = 'text';
options.format = 'none';
if (typeof api === 'object') {
options.mode = 'json';
}
else if (typeof api === 'string') {
api = api.split('\r\n').join('\n').split('\r').join('').split('---').join('###');
}
if (options.mode == 'text') {
try {
api = JSON.parse(api);
options.mode = 'json';
}
catch (ex) {
console.error(ex.message);
}
}
if (options.mode == 'text') {
try {
if (api && api.startsWith('#%RAML')) options.format = 'raml';
try {
api = yaml.parse(api||'');
options.mode = 'yaml';
}
catch (ex) {
console.warn(ex.message,ex.linePos);
console.warn('Falling back to js-yaml');
api = jsy.safeLoad(api||'');
options.mode = 'yaml';
}
}
catch (ex) {
console.error(ex.message);
}
}
///
if ((options.mode == 'text') && (typeof api === 'string') && (api.startsWith('#') || (api.startsWith('FORMAT: '))) && (options.format !== 'raml')) {
options.format = 'api_blueprint';
}
else {
if (api && api.swaggerVersion && (typeof api.swaggerVersion === 'string') && api.swaggerVersion.startsWith('1.')) {
options.format = 'swagger_1';
}
else if (api && api.swagrVersion) {
options.format = 'swagger_1';
options.patchSwagr = true;
}
else if (api && api.swagger && api.swagger === '2.0') {
options.format = 'swagger_2';
}
else if (api && api.swagger) {
console.warn('Unknown swagger version: '+api.swagger);
}
else if (api && api.openapi && (typeof api.openapi === 'string') && api.openapi.startsWith('3.0')) {
options.format = 'openapi_3';
}
else if (api && api.openapi) {
console.warn('Unknown openapi version: '+api.openapi);
}
else if (api && api.asyncapi && (typeof api.asyncapi === 'string') && api.asyncapi.startsWith('1.')) {
options.format = 'asyncapi_1';
}
else if (api && api.asyncapi && (typeof api.asyncapi === 'string') && api.asyncapi.startsWith('2.')) {
options.format = 'asyncapi_2';
}
else if (api && api.asyncapi) {
console.warn('Unknown asyncapi version: '+api.asyncapi);
}
else if (api && api.version) {
console.warn('Kin!');
options.format = 'asyncapi_1';
api.asyncapi = api.version;
delete api.version;
recurse(api,{},function(obj,key,state){
if (key === 'asyncapi_servers_variables') {
obj.variables = obj.asyncapi_servers_variables;
delete obj.asyncapi_servers_variables;
}
});
}
}
if (options.format === 'openapi_3') {
var options = {laxRefs:true,resolve:true,source:options.source,convert:options.convert,verbose:1};
try {
openapi3.validate(api, options, function(err, opts) {
if (err) {
return callback(err, err.options||opts||options);
}
options.converted = options.openapi||api;
options.message = 'Valid openapi 3.0.x';
options.context = [api.info.version + ' ' +
(api.servers && api.servers.length ? api.servers[0].url : 'Relative')];
callback(null, options);
});
}
catch (ex) {
var context = options.context.pop();
options.context = [context];
callback(ex, options);
}
}
else if (options.format === 'asyncapi_1') {
const validate = ss(asyncApiSchema1);
validate(api);
var errors = validate.errors;
if (errors) {
callback(errors, options);
}
else {
options.message = 'Valid AsyncAPI 1.x';
callback(null, options);
}
}
else if (options.format === 'asyncapi_2') {
const validate = ss(asyncApiSchema2);
validate(api);
var errors = validate.errors;
if (errors) {
callback(errors, options);
}
else {
options.message = 'Valid AsyncAPI 2.x';
callback(null, options);
}
}
else if (options.format === 'api_blueprint') {
var drafter = require('drafter.js')
var res = drafter.parse(api, {generateSourceMap: true}, function (err, res) {
if (err) {
callback(err, options);
}
else {
if (options.convert) {
var apib2swagger = require('apib2swagger');
apib2swagger.convert(api, function (err, res) {
options.converted = res.swagger;
callback(err, options);
});
}
else {
options.message = 'Valid API Blueprint';
callback(null, options);
}
}
});
}
else if (options.format === 'swagger_2') {
var orig = api;
const options = { resolve: true, patch: true, warnOnly: true };
swagger2openapi.convertObj(api,options,function(err,options){
api = options.openapi;
openapi3.validate(api,options,function(err,options){
if (api && (!api.info || !api.info.title)) {
err = {error:'No title'};
}
if (err || !options.valid) {
if (!options) options = {};
options.message = 'Invalid swagger 2.0';
}
else {
options.message = 'Valid swagger 2.0';
if (options.patches) options.message += ' (with '+options.patches+' patches)';
options.context = [api.info.title+' '+api.info.version+
' host:'+(api.host ? api.host : 'relative')];
if (api.info["x-logo"] && api.info["x-logo"].url) {
options.context[0] += '\nHas logo: '+api.info["x-logo"].url;
}
}
options.api = api||orig;
callback(err, options);
});
});
}
else if (options.format === 'swagger_1') {
const err = {};
err.message = 'Swagger 1.x is no longer supported';
callback(err,options);
}
else if (options.format == 'raml') {
var raml = require('raml-1-parser');
var node = raml.loadApiSync(options.source);
var res = node.toJSON({"rootNodeDetails":true});
if (res.errors && res.errors.length) callback(res.errors,options)
else {
options.api = res;
options.message = api.title + ' ' +api.version;
callback(null, options);
}
//console.log(JSON.stringify(node.toJSON({"rootNodeDetails":true}),null,2));
}
else {
const err = {};
err.message = 'No format/type detected';
callback(err,options);
}
}
module.exports = {
check_api : check_api
};