@@ -23,13 +23,22 @@ function apib2postman(apib, options) {
23
23
} ;
24
24
25
25
addEnvVariables ( environment . values , [ 'base_url' , 'username' , 'password' , 'include_sad_tests' ] ) ;
26
+ const schemaDir = 'schema' ;
27
+ if ( ! fs . existsSync ( schemaDir ) ) {
28
+ fs . mkdirSync ( schemaDir ) ;
29
+ }
26
30
27
31
apib . content [ 0 ] . content
28
32
. filter ( content => content . element === 'category' )
29
33
. forEach ( category => {
30
34
const title = category . meta . title ;
31
35
const groups = [ ] ;
32
36
37
+ const schemaGroupDir = schemaDir + '/' + title ;
38
+ if ( ! fs . existsSync ( schemaGroupDir ) ) {
39
+ fs . mkdirSync ( schemaGroupDir ) ;
40
+ }
41
+
33
42
category . content
34
43
. filter ( content => content . element === 'resource' )
35
44
. forEach ( resource => {
@@ -44,10 +53,12 @@ function apib2postman(apib, options) {
44
53
variables : attributes . variable
45
54
} ;
46
55
56
+ const schemaFilePath = schemaGroupDir + '/' + resource . meta . title + '.json' ;
47
57
const actions = parseActions (
48
58
baseAction ,
49
59
resource . content . filter ( x => x . element === 'transition' ) ,
50
- environment
60
+ environment ,
61
+ schemaFilePath
51
62
) ;
52
63
53
64
addEnvVariables ( environment . values , attributes . envVariable ) ;
@@ -86,7 +97,7 @@ function parsePath(uriTemplate) {
86
97
return decodeURIComponent ( uriTemplate . expand ( params ) ) . split ( '/' ) . slice ( 1 ) ;
87
98
}
88
99
89
- function parseActions ( baseAction , actions , environment ) {
100
+ function parseActions ( baseAction , actions , environment , schemaFilePath ) {
90
101
return actions . map ( action => {
91
102
const transaction = _ . find ( action . content , x => x . element === 'httpTransaction' ) ;
92
103
const request = parseRequest ( _ . find ( transaction . content , x => x . element === 'httpRequest' ) ) ;
@@ -108,7 +119,7 @@ function parseActions(baseAction, actions, environment) {
108
119
addEnvVariables ( environment . values , attributes . envVariable ) ;
109
120
}
110
121
111
- const response = parseResponse ( _ . find ( transaction . content , x => x . element === 'httpResponse' ) ) ;
122
+ const response = parseResponse ( _ . find ( transaction . content , x => x . element === 'httpResponse' ) , schemaFilePath ) ;
112
123
113
124
return _ . merge ( { } , newAction , {
114
125
name : action . meta . title ,
@@ -159,7 +170,7 @@ function parseAttributes(attributes, uriTemplate) {
159
170
key : name ,
160
171
value : `{{${ pathName } ${ name } }}`
161
172
} ) ;
162
-
173
+
163
174
result . envVariable . push ( pathName + name ) ;
164
175
}
165
176
} ) ;
@@ -188,12 +199,12 @@ function parseRequestHeaders(headers) {
188
199
return parseHeaders ( headers . content . filter ( x => x . content . key . content !== 'Authorization' ) ) ;
189
200
}
190
201
191
- function parseResponse ( response ) {
202
+ function parseResponse ( response , schemaFilePath ) {
192
203
return {
193
204
statusCode : response . attributes . statusCode ,
194
205
headers : response . attributes . headers ? parseHeaders ( response . attributes . headers . content ) : { } ,
195
206
body : parseContent ( response . content , 'messageBody' ) . content ,
196
- jsonSchema : parseJsonSchema ( response . content ) ,
207
+ jsonSchema : parseJsonSchema ( response . content , schemaFilePath ) ,
197
208
tests : parseBodyTests ( response . content )
198
209
} ;
199
210
}
@@ -231,10 +242,11 @@ function parseBodyTests(content) {
231
242
return tests [ 1 ] . split ( / \r \n ? | \n / g) ;
232
243
}
233
244
234
- function parseJsonSchema ( content ) {
245
+ function parseJsonSchema ( content , schemaFilePath ) {
235
246
try {
236
- const schema = JSON . parse ( parseContent ( content , 'messageBodySchema' ) . content ) ;
237
-
247
+ const schemaJson = parseContent ( content , 'messageBodySchema' ) . content ;
248
+ const schema = JSON . parse ( schemaJson ) ;
249
+ fs . writeFileSync ( schemaFilePath , schemaJson ) ;
238
250
if ( schema ) {
239
251
return schema ;
240
252
}
@@ -301,7 +313,7 @@ exports.convert = function (data, options, callback) {
301
313
}
302
314
return callback ( err ) ;
303
315
}
304
-
316
+
305
317
try {
306
318
const newResult = apib2postman ( result , options ) ;
307
319
return callback ( null , newResult ) ;
0 commit comments