Skip to content

Commit 8ad64d0

Browse files
Merge pull request #5 from jqian33/ProduceJsonSchemaFiles
Produce json schema files
2 parents 8b90551 + 0abaa32 commit 8ad64d0

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@apib/2postman",
33
"description": "Convert API Blueprints to Postman Collections",
44
"author": "Johnson Controls, Plc.",
5-
"version": "1.1.3",
5+
"version": "1.2.0",
66
"license": "BSD",
77
"dependencies": {
88
"apib-include-directive": "^0.1.0",

Diff for: src/index.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ function apib2postman(apib, options) {
2323
};
2424

2525
addEnvVariables(environment.values, ['base_url', 'username', 'password', 'include_sad_tests']);
26+
const schemaDir = 'schema';
27+
if (!fs.existsSync(schemaDir)){
28+
fs.mkdirSync(schemaDir);
29+
}
2630

2731
apib.content[0].content
2832
.filter(content => content.element === 'category')
2933
.forEach(category => {
3034
const title = category.meta.title;
3135
const groups = [];
3236

37+
const schemaGroupDir = schemaDir + '/' + title;
38+
if (!fs.existsSync(schemaGroupDir)){
39+
fs.mkdirSync(schemaGroupDir);
40+
}
41+
3342
category.content
3443
.filter(content => content.element === 'resource')
3544
.forEach(resource => {
@@ -44,10 +53,12 @@ function apib2postman(apib, options) {
4453
variables: attributes.variable
4554
};
4655

56+
const schemaFilePath = schemaGroupDir + '/' + resource.meta.title + '.json';
4757
const actions = parseActions(
4858
baseAction,
4959
resource.content.filter(x => x.element === 'transition'),
50-
environment
60+
environment,
61+
schemaFilePath
5162
);
5263

5364
addEnvVariables(environment.values, attributes.envVariable);
@@ -86,7 +97,7 @@ function parsePath(uriTemplate) {
8697
return decodeURIComponent(uriTemplate.expand(params)).split('/').slice(1);
8798
}
8899

89-
function parseActions(baseAction, actions, environment) {
100+
function parseActions(baseAction, actions, environment, schemaFilePath) {
90101
return actions.map(action => {
91102
const transaction = _.find(action.content, x => x.element === 'httpTransaction');
92103
const request = parseRequest(_.find(transaction.content, x => x.element === 'httpRequest'));
@@ -108,7 +119,7 @@ function parseActions(baseAction, actions, environment) {
108119
addEnvVariables(environment.values, attributes.envVariable);
109120
}
110121

111-
const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'));
122+
const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'), schemaFilePath);
112123

113124
return _.merge({}, newAction, {
114125
name: action.meta.title,
@@ -159,7 +170,7 @@ function parseAttributes(attributes, uriTemplate) {
159170
key: name,
160171
value: `{{${pathName}${name}}}`
161172
});
162-
173+
163174
result.envVariable.push(pathName + name);
164175
}
165176
});
@@ -188,12 +199,12 @@ function parseRequestHeaders(headers) {
188199
return parseHeaders(headers.content.filter(x => x.content.key.content !== 'Authorization'));
189200
}
190201

191-
function parseResponse(response) {
202+
function parseResponse(response, schemaFilePath) {
192203
return {
193204
statusCode: response.attributes.statusCode,
194205
headers: response.attributes.headers ? parseHeaders(response.attributes.headers.content) : {},
195206
body: parseContent(response.content, 'messageBody').content,
196-
jsonSchema: parseJsonSchema(response.content),
207+
jsonSchema: parseJsonSchema(response.content, schemaFilePath),
197208
tests: parseBodyTests(response.content)
198209
};
199210
}
@@ -231,10 +242,11 @@ function parseBodyTests(content) {
231242
return tests[1].split(/\r\n?|\n/g);
232243
}
233244

234-
function parseJsonSchema(content) {
245+
function parseJsonSchema(content, schemaFilePath) {
235246
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);
238250
if (schema) {
239251
return schema;
240252
}
@@ -301,7 +313,7 @@ exports.convert = function (data, options, callback) {
301313
}
302314
return callback(err);
303315
}
304-
316+
305317
try {
306318
const newResult = apib2postman(result, options);
307319
return callback(null, newResult);

0 commit comments

Comments
 (0)