From 628f3840066920564b6a03105bf91dda2fa15374 Mon Sep 17 00:00:00 2001 From: cqian4 Date: Thu, 13 Feb 2020 09:05:27 -0600 Subject: [PATCH 1/2] Produce json schema files --- src/index.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 6248e84..ede3a7d 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,10 @@ 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') @@ -30,6 +34,11 @@ function apib2postman(apib, options) { 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 => { @@ -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); @@ -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')); @@ -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, @@ -159,7 +170,7 @@ function parseAttributes(attributes, uriTemplate) { key: name, value: `{{${pathName}${name}}}` }); - + result.envVariable.push(pathName + name); } }); @@ -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) }; } @@ -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; } @@ -301,7 +313,7 @@ exports.convert = function (data, options, callback) { } return callback(err); } - + try { const newResult = apib2postman(result, options); return callback(null, newResult); From 0abaa32e9a70d36b069ec2f83b87ecd572f328dd Mon Sep 17 00:00:00 2001 From: cqian4 Date: Thu, 13 Feb 2020 10:46:54 -0600 Subject: [PATCH 2/2] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4418dc8..ac89283 100644 --- a/package.json +++ b/package.json @@ -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",