Skip to content

Commit

Permalink
Merge pull request #5 from jqian33/ProduceJsonSchemaFiles
Browse files Browse the repository at this point in the history
Produce json schema files
  • Loading branch information
ConnorChristie authored Feb 13, 2020
2 parents 8b90551 + 0abaa32 commit 8ad64d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@apib/2postman",
"description": "Convert API Blueprints to Postman Collections",
"author": "Johnson Controls, Plc.",
"version": "1.1.3",
"version": "1.2.0",
"license": "BSD",
"dependencies": {
"apib-include-directive": "^0.1.0",
Expand Down
32 changes: 22 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ function apib2postman(apib, options) {
};

addEnvVariables(environment.values, ['base_url', 'username', 'password', 'include_sad_tests']);
const schemaDir = 'schema';
if (!fs.existsSync(schemaDir)){
fs.mkdirSync(schemaDir);
}

apib.content[0].content
.filter(content => content.element === 'category')
.forEach(category => {
const title = category.meta.title;
const groups = [];

const schemaGroupDir = schemaDir + '/' + title;
if (!fs.existsSync(schemaGroupDir)){
fs.mkdirSync(schemaGroupDir);
}

category.content
.filter(content => content.element === 'resource')
.forEach(resource => {
Expand All @@ -44,10 +53,12 @@ function apib2postman(apib, options) {
variables: attributes.variable
};

const schemaFilePath = schemaGroupDir + '/' + resource.meta.title + '.json';
const actions = parseActions(
baseAction,
resource.content.filter(x => x.element === 'transition'),
environment
environment,
schemaFilePath
);

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

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

const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'));
const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'), schemaFilePath);

return _.merge({}, newAction, {
name: action.meta.title,
Expand Down Expand Up @@ -159,7 +170,7 @@ function parseAttributes(attributes, uriTemplate) {
key: name,
value: `{{${pathName}${name}}}`
});

result.envVariable.push(pathName + name);
}
});
Expand Down Expand Up @@ -188,12 +199,12 @@ function parseRequestHeaders(headers) {
return parseHeaders(headers.content.filter(x => x.content.key.content !== 'Authorization'));
}

function parseResponse(response) {
function parseResponse(response, schemaFilePath) {
return {
statusCode: response.attributes.statusCode,
headers: response.attributes.headers ? parseHeaders(response.attributes.headers.content) : {},
body: parseContent(response.content, 'messageBody').content,
jsonSchema: parseJsonSchema(response.content),
jsonSchema: parseJsonSchema(response.content, schemaFilePath),
tests: parseBodyTests(response.content)
};
}
Expand Down Expand Up @@ -231,10 +242,11 @@ function parseBodyTests(content) {
return tests[1].split(/\r\n?|\n/g);
}

function parseJsonSchema(content) {
function parseJsonSchema(content, schemaFilePath) {
try {
const schema = JSON.parse(parseContent(content, 'messageBodySchema').content);

const schemaJson = parseContent(content, 'messageBodySchema').content;
const schema = JSON.parse(schemaJson);
fs.writeFileSync(schemaFilePath, schemaJson);
if (schema) {
return schema;
}
Expand Down Expand Up @@ -301,7 +313,7 @@ exports.convert = function (data, options, callback) {
}
return callback(err);
}

try {
const newResult = apib2postman(result, options);
return callback(null, newResult);
Expand Down

0 comments on commit 8ad64d0

Please sign in to comment.