11const YAML = require ( 'yamljs' ) ;
22
3- /////////////
4- const Ajv = require ( 'ajv' ) ;
5- const openapiSchema = require ( 'openapi-schema-validation' ) ;
6-
7- const ajv = new Ajv ( ) ;
8- const validateOpenAPI = ajv . compile ( openapiSchema ) ;
9-
10-
11- const errors = validateOpenAPI . errors ;
12- if ( errors ) {
13- const errorMessages = errors . map ( ( error ) => `Validation Error: ${ error . dataPath } ${ error . message } ` ) ;
14- throw new Error ( `Invalid OpenAPI document.\n${ errorMessages . join ( '\n' ) } ` ) ;
15- }
16- /////////////
17-
183// TODO The security keys need to be implmented into the OpenApi request
194
205const openapiParserFunc = ( input ) => {
216
22- // Input validation
23- if ( typeof input !== 'string' ) {
24- throw new TypeError ( 'Input must be a string.' ) ;
25- }
26-
277 if ( input === undefined || input === null ) {
288 throw new ReferenceError ( 'OpenAPI Document not found.' ) ;
299 }
3010 // Parse the input into JSON or YAML
3111 let doc ;
3212 try {
33- //try json parse
3413 doc = JSON . parse ( input ) ;
35- } catch ( jsonError ) {
36- // try to parse as yaml
37- try {
38- doc = YAML . parse ( input )
39- } catch ( yamlError ) {
40- throw new Error ( 'Invalid JSON, or YAML format: ' + yamlError . message )
41- }
42- }
43-
44- // Schema validation
45- const isValidOpenAPI = validateOpenAPI ( doc ) ;
46- if ( ! isValidOpenAPI ) {
47- throw new Error ( 'Invalid OpenAPI document. Schema validation failed.' ) ;
14+ } catch ( SyntaxError ) {
15+ doc = YAML . parse ( input ) ;
4816 }
4917
50-
51- const { info = { } , servers = [ ] , tags = [ ] , paths = { } , components = { } } = doc ;
18+ const { info, servers, tags, paths, components } = doc ;
5219
5320 info . openapi = doc . openapi ;
5421
55- const serverUrls = servers . map ( ( server ) => server . url ) ;
56-
57- const openapiReqArray = [ ] ;
22+ let serverUrls
23+ if ( servers ) {
24+ serverUrls = [ ...servers . map ( ( server ) => server . url ) ] ;
25+ } else {
26+ serverUrls = [ ]
27+ }
5828 let id = 0 ;
5929
30+ const openapiReqArray = [ ] ;
6031 Object . entries ( paths ) . forEach ( ( [ endpoint , pathObj ] ) => {
6132 Object . entries ( pathObj ) . forEach ( ( [ method , operationObj ] ) => {
6233 id += 1 ;
63-
6434 const {
65- summary = '' ,
66- description = '' ,
67- operationId = '' ,
68- tags = [ ] ,
69- parameters = [ ] ,
70- security = [ ] ,
71- responses = { } ,
72- externalDocs = { } ,
73- version = '' ,
35+ summary,
36+ description,
37+ operationId,
38+ tags,
39+ parameters, // security
7440 } = operationObj ;
7541
76- const securitySchemes = components . securitySchemes || { } ;
77- const responseExamples = { } ; // Extract response examples from responses object if available
78-
79- // const request = {
80- // id,
81- // // enabled: true,
82- // reqTags: tags,
83- // summary,
84- // description,
85- // operationId,
86- // method: method.toUpperCase(),
87- // reqServers: [],
88- // endpoint,
89- // parameters,
90- // body: new Map(),
91- // headers: {},
92- // cookies: {},
93- // // params: {},
94- // // queries: {},
95- // urls: [],
96- // };
9742 const request = {
9843 id,
44+ // enabled: true,
9945 reqTags : tags ,
10046 summary,
10147 description,
@@ -107,10 +53,8 @@ const openapiParserFunc = (input) => {
10753 body : new Map ( ) ,
10854 headers : { } ,
10955 cookies : { } ,
110- securitySchemes,
111- responseExamples,
112- externalDocs,
113- version,
56+ // params: {},
57+ // queries: {},
11458 urls : [ ] ,
11559 } ;
11660 openapiReqArray . push ( request ) ;
0 commit comments