@@ -2,168 +2,165 @@ import converter from 'swagger2openapi';
2
2
import $RefParser from 'json-schema-ref-parser' ;
3
3
4
4
export default function ProcessSpec ( specUrl ) {
5
- let p ;
6
- let options = { patch :true , warnOnly :true }
7
- if ( typeof specUrl === "string" ) {
8
- p = converter . convertUrl ( specUrl , options ) ;
9
- }
10
- else {
11
- p = converter . convertObj ( specUrl , options ) ;
12
- }
13
- return p
14
- . then ( function ( api3Spec ) {
15
- console . info ( "%c Convertion to OpenAPI 3.0 - Success !!! " , "color:cornflowerblue" ) ;
16
- return $RefParser . dereference ( api3Spec . openapi ) ;
17
- } )
18
- . then ( function ( deReffedSpec ) {
19
- console . info ( "%c OpenAPI 3.0 Dereferencing - Success !!! " , "color:cornflowerblue" ) ;
20
- let methods = [ 'get' , 'put' , 'post' , 'delete' , 'patch' , 'options' , 'head' ] ;
21
- let tags = [ ] ;
22
- let totalPathCount = 0 ;
23
- // For each path find the tag and push it into the corrosponding tag
24
- for ( let path in deReffedSpec . paths ) {
25
- let commonParams = deReffedSpec . paths [ path ] . parameters ;
26
- let commonPathProp = {
27
- "summary" : deReffedSpec . paths [ path ] . summary ,
28
- "description" : deReffedSpec . paths [ path ] . description ,
29
- "servers" : deReffedSpec . paths [ path ] . servers ?deReffedSpec . paths [ path ] . servers :[ ] ,
30
- "parameters" : deReffedSpec . paths [ path ] . parameters ?deReffedSpec . paths [ path ] . parameters :[ ]
31
- } ;
32
- methods . forEach ( function ( methodName ) {
33
- let tagObj ;
34
- let tagText ;
35
- let tagDescr ;
36
-
37
- if ( deReffedSpec . paths [ path ] [ methodName ] ) {
38
- let fullPath = deReffedSpec . paths [ path ] [ methodName ] ;
39
- // If path.methods are tagged, else generate it from path
40
- if ( fullPath . tags ) {
41
- tagText = fullPath . tags [ 0 ] ;
42
- if ( deReffedSpec . tags ) {
43
- tagDescr = deReffedSpec . tags . find ( function ( v ) {
44
- return ( v . name === tagText )
45
- } ) ;
46
- }
47
- }
48
- else {
49
- let firstWordEndIndex = path . indexOf ( "/" , 1 ) ;
50
- if ( firstWordEndIndex === - 1 ) {
51
- firstWordEndIndex = ( path . length - 1 ) ;
52
- }
53
- else {
54
- firstWordEndIndex = firstWordEndIndex - 1 ;
55
- }
56
- tagText = path . substr ( 1 , firstWordEndIndex ) ;
57
- }
58
- tagObj = tags . find ( v => v . name == tagText ) ;
5
+ let p ;
6
+ let options = { patch :true , warnOnly :true }
7
+ if ( typeof specUrl === "string" ) {
8
+ p = converter . convertUrl ( specUrl , options ) ;
9
+ }
10
+ else {
11
+ p = converter . convertObj ( specUrl , options ) ;
12
+ }
13
+ return p
14
+ . then ( function ( api3Spec ) {
15
+ console . info ( "%c Convertion to OpenAPI 3.0 - Success !!! " , "color:cornflowerblue" ) ;
16
+ return $RefParser . dereference ( api3Spec . openapi ) ;
17
+ } )
18
+ . then ( function ( deReffedSpec ) {
19
+ console . info ( "%c OpenAPI 3.0 Dereferencing - Success !!! " , "color:cornflowerblue" ) ;
20
+ let methods = [ 'get' , 'put' , 'post' , 'delete' , 'patch' , 'options' , 'head' ] ;
21
+ let tags = [ ] ;
22
+ let totalPathCount = 0 ;
23
+ // For each path find the tag and push it into the corrosponding tag
24
+ for ( let path in deReffedSpec . paths ) {
25
+ let commonParams = deReffedSpec . paths [ path ] . parameters ;
26
+ let commonPathProp = {
27
+ "summary" : deReffedSpec . paths [ path ] . summary ,
28
+ "description" : deReffedSpec . paths [ path ] . description ,
29
+ "servers" : deReffedSpec . paths [ path ] . servers ?deReffedSpec . paths [ path ] . servers :[ ] ,
30
+ "parameters" : deReffedSpec . paths [ path ] . parameters ?deReffedSpec . paths [ path ] . parameters :[ ]
31
+ } ;
32
+ methods . forEach ( function ( methodName ) {
33
+ let tagObj ;
34
+ let tagText ;
35
+ let tagDescr ;
36
+
37
+ if ( deReffedSpec . paths [ path ] [ methodName ] ) {
38
+ let fullPath = deReffedSpec . paths [ path ] [ methodName ] ;
39
+ // If path.methods are tagged, else generate it from path
40
+ if ( fullPath . tags ) {
41
+ tagText = fullPath . tags [ 0 ] ;
42
+ if ( deReffedSpec . tags ) {
43
+ tagDescr = deReffedSpec . tags . find ( function ( v ) {
44
+ return ( v . name === tagText )
45
+ } ) ;
46
+ }
47
+ }
48
+ else {
49
+ let firstWordEndIndex = path . indexOf ( "/" , 1 ) ;
50
+ if ( firstWordEndIndex === - 1 ) {
51
+ firstWordEndIndex = ( path . length - 1 ) ;
52
+ }
53
+ else {
54
+ firstWordEndIndex = firstWordEndIndex - 1 ;
55
+ }
56
+ tagText = path . substr ( 1 , firstWordEndIndex ) ;
57
+ }
58
+ tagObj = tags . find ( v => v . name == tagText ) ;
59
59
60
- if ( ! tagObj ) {
61
- tagObj = {
62
- show : true ,
63
- "name" : tagText ,
64
- "description" : tagDescr ?tagDescr . description :"" ,
65
- "paths" : [ ]
66
- }
67
- tags . push ( tagObj ) ;
68
- }
60
+ if ( ! tagObj ) {
61
+ tagObj = {
62
+ show : true ,
63
+ "name" : tagText ,
64
+ "description" : tagDescr ?tagDescr . description :"" ,
65
+ "paths" : [ ]
66
+ }
67
+ tags . push ( tagObj ) ;
68
+ }
69
69
70
- //Generate Path summary and Description if it is missing for a method
71
- let summary = fullPath . summary ?fullPath . summary :"" ;
72
- let description = fullPath . description ?fullPath . description :"" ;
73
- if ( ! summary && description ) {
74
- if ( description . length > 100 ) {
75
- let charIndex = - 1 ;
76
- charIndex = description . indexOf ( "\n" ) ;
77
- if ( charIndex === - 1 || charIndex > 100 ) {
78
- charIndex = description . indexOf ( ". " ) ;
79
- }
80
- if ( charIndex === - 1 || charIndex > 100 ) {
81
- charIndex = description . indexOf ( "." ) ;
82
- }
83
- if ( charIndex === - 1 || charIndex > 100 ) {
84
- summary = description ;
85
- }
86
- else {
87
- summary = description . substr ( 0 , charIndex ) ;
88
- }
89
- }
90
- else {
91
- summary = description ;
92
- }
93
- }
70
+ //Generate Path summary and Description if it is missing for a method
71
+ let summary = fullPath . summary ?fullPath . summary :"" ;
72
+ let description = fullPath . description ?fullPath . description :"" ;
73
+ if ( ! summary && description ) {
74
+ if ( description . length > 100 ) {
75
+ let charIndex = - 1 ;
76
+ charIndex = description . indexOf ( "\n" ) ;
77
+ if ( charIndex === - 1 || charIndex > 100 ) {
78
+ charIndex = description . indexOf ( ". " ) ;
79
+ }
80
+ if ( charIndex === - 1 || charIndex > 100 ) {
81
+ charIndex = description . indexOf ( "." ) ;
82
+ }
83
+ if ( charIndex === - 1 || charIndex > 100 ) {
84
+ summary = description ;
85
+ }
86
+ else {
87
+ summary = description . substr ( 0 , charIndex ) ;
88
+ }
89
+ }
90
+ else {
91
+ summary = description ;
92
+ }
93
+ }
94
94
95
- // Merge Common Parameters with This methods parameters
96
- let finalParameters = [ ] ;
97
- if ( commonParams ) {
98
- if ( fullPath . parameters ) {
99
- finalParameters = commonParams . filter ( commonParam => {
100
- if ( ! fullPath . parameters . some ( param => ( commonParam . name === param . name && commonParam . in === param . in ) ) ) {
101
- return commonParam ;
102
- }
103
- } ) . concat ( fullPath . parameters )
104
- }
105
- else {
106
- finalParameters = commonParams . slice ( 0 ) ;
107
- }
108
- }
109
- else {
110
- finalParameters = fullPath . parameters ? fullPath . parameters . slice ( 0 ) :[ ] ;
111
- }
95
+ // Merge Common Parameters with This methods parameters
96
+ let finalParameters = [ ] ;
97
+ if ( commonParams ) {
98
+ if ( fullPath . parameters ) {
99
+ finalParameters = commonParams . filter ( commonParam => {
100
+ if ( ! fullPath . parameters . some ( param => ( commonParam . name === param . name && commonParam . in === param . in ) ) ) {
101
+ return commonParam ;
102
+ }
103
+ } ) . concat ( fullPath . parameters )
104
+ }
105
+ else {
106
+ finalParameters = commonParams . slice ( 0 ) ;
107
+ }
108
+ }
109
+ else {
110
+ finalParameters = fullPath . parameters ? fullPath . parameters . slice ( 0 ) :[ ] ;
111
+ }
112
112
113
- //Update Responses
114
- tagObj . paths . push ( {
115
- "show" : true ,
116
- "expanded" : false ,
117
- "expandedAtLeastOnce" :false ,
118
- "summary" : summary ,
119
- "method" : methodName ,
120
- "description" : fullPath . description ,
121
- "path" : path ,
122
- "operationId" : fullPath . operationId ,
123
- "requestBody" : fullPath . requestBody ,
124
- "parameters" : finalParameters ,
125
- "servers" : fullPath . servers ? commonPathProp . servers . concat ( fullPath . servers ) :commonPathProp . servers ,
126
- "responses" : fullPath . responses ,
127
- "deprecated" : fullPath . deprecated ,
128
- "security" : fullPath . security ,
129
- "commonSummary" : commonPathProp . summary ,
130
- "commonDescription" : commonPathProp . description ,
131
- } ) ;
132
- totalPathCount ++ ;
133
- }
134
- } ) ; // End of Methods
113
+ //Update Responses
114
+ tagObj . paths . push ( {
115
+ "show" : true ,
116
+ "expanded" : false ,
117
+ "expandedAtLeastOnce" :false ,
118
+ "summary" : summary ,
119
+ "method" : methodName ,
120
+ "description" : fullPath . description ,
121
+ "path" : path ,
122
+ "operationId" : fullPath . operationId ,
123
+ "requestBody" : fullPath . requestBody ,
124
+ "parameters" : finalParameters ,
125
+ "servers" : fullPath . servers ? commonPathProp . servers . concat ( fullPath . servers ) :commonPathProp . servers ,
126
+ "responses" : fullPath . responses ,
127
+ "deprecated" : fullPath . deprecated ,
128
+ "security" : fullPath . security ,
129
+ "commonSummary" : commonPathProp . summary ,
130
+ "commonDescription" : commonPathProp . description ,
131
+ } ) ;
132
+ totalPathCount ++ ;
133
+ }
134
+ } ) ; // End of Methods
135
135
136
- }
136
+ }
137
137
138
- let securitySchemes = { } ;
139
- let servers = [ ] ;
138
+ let securitySchemes = { } ;
139
+ let servers = [ ] ;
140
140
141
- securitySchemes = ( deReffedSpec . components ? deReffedSpec . components . securitySchemes :{ } ) ;
142
- if ( deReffedSpec . servers ) {
143
- deReffedSpec . servers . map ( function ( v ) {
144
- if ( v . url && v . url . substr ( 0 , 1 ) === "/" ) {
145
- let paths = specUrl . split ( "/" ) ;
146
- v . url = paths [ 0 ] + "//" + paths [ 2 ] + v . url ;
147
- }
148
- } )
149
- }
150
- servers = deReffedSpec . servers ;
151
- let parsedSpec = {
152
- "info" : deReffedSpec . info ,
153
- "tags" : tags ,
154
- "externalDocs" : deReffedSpec . externalDocs ,
155
- "securitySchemes" : securitySchemes ,
156
- "servers" : servers , // In swagger 2, its generated from schemes, host and basePath properties
157
- "basePath" : deReffedSpec . basePath , // Only available in swagger V2
158
- "totalPathCount" : totalPathCount
159
- }
160
- return Promise . resolve ( parsedSpec ) ;
161
- } )
162
- . catch ( function ( err ) {
163
- alert ( err ) ;
164
- console . error ( err ) ;
165
- } ) ;
166
-
167
- }
168
-
169
- export { convertSpec }
141
+ securitySchemes = ( deReffedSpec . components ? deReffedSpec . components . securitySchemes :{ } ) ;
142
+ if ( deReffedSpec . servers ) {
143
+ deReffedSpec . servers . map ( function ( v ) {
144
+ if ( v . url && v . url . substr ( 0 , 1 ) === "/" ) {
145
+ let paths = specUrl . split ( "/" ) ;
146
+ v . url = paths [ 0 ] + "//" + paths [ 2 ] + v . url ;
147
+ }
148
+ } )
149
+ }
150
+ servers = deReffedSpec . servers ;
151
+ let parsedSpec = {
152
+ "info" : deReffedSpec . info ,
153
+ "tags" : tags ,
154
+ "externalDocs" : deReffedSpec . externalDocs ,
155
+ "securitySchemes" : securitySchemes ,
156
+ "servers" : servers , // In swagger 2, its generated from schemes, host and basePath properties
157
+ "basePath" : deReffedSpec . basePath , // Only available in swagger V2
158
+ "totalPathCount" : totalPathCount
159
+ }
160
+ return Promise . resolve ( parsedSpec ) ;
161
+ } )
162
+ . catch ( function ( err ) {
163
+ alert ( err ) ;
164
+ console . error ( err ) ;
165
+ } ) ;
166
+ }
0 commit comments