From 3292f6d9f4893f1fb3d3ebc0c8f7e9e36affc2c0 Mon Sep 17 00:00:00 2001 From: Jieyang Qian Date: Thu, 20 Feb 2020 09:32:07 -0600 Subject: [PATCH] Create contract output files (#8) --- bin/apib2postman.js | 10 +++++----- package.json | 2 +- src/index.js | 31 ++++++++++++++++++++----------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/bin/apib2postman.js b/bin/apib2postman.js index 4f32408..d6d6048 100644 --- a/bin/apib2postman.js +++ b/bin/apib2postman.js @@ -9,13 +9,13 @@ const apib2postman = require('../src/index.js'); var options = nopt({ 'output': String, - 'schema': String, + 'contract': String, 'environment': String, 'testTemplate': String, 'help': Boolean }, { 'o': ['--output'], - 's': ['--schema'], + 'c': ['--contract'], 'e': ['--environment'], 't': ['--testTemplate'], 'h': ['--help'] @@ -33,7 +33,7 @@ if (options.help || options.argv.remain.length === 0) { console.log("Options:") console.log(" -h --help Print this help and exit."); console.log(" -o --output Output result to file."); - console.log(" -s --schema Directory containing json schema output files."); + console.log(" -c --contract Directory for contract output files."); console.log(" -e --environment The output file for the Postman environment."); console.log(" -t --testTemplate The postman test template to use for each action."); process.exit(); @@ -44,8 +44,8 @@ const collectionFile = options.output || 'API.postman_collection.json'; const environmentFile = options.environment || 'API.postman_environment.json'; const includePath = input ? path.dirname(input) : process.cwd(); var apibData = ''; -if (!options.schema) { - options.schema = 'schema'; +if (!options.contract) { + options.contract = 'contract'; } fs.createReadStream(input).on('data', (chunk) => { diff --git a/package.json b/package.json index 4e77902..886e80a 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.2.2", + "version": "1.2.3", "license": "BSD", "dependencies": { "apib-include-directive": "^0.1.0", diff --git a/src/index.js b/src/index.js index e67327b..7891cdc 100644 --- a/src/index.js +++ b/src/index.js @@ -31,9 +31,9 @@ function apib2postman(apib, options) { const title = category.meta.title; const groups = []; - const schemaGroupDir = options.schema + '/' + title; - if (!fs.existsSync(schemaGroupDir)){ - shell.mkdir('-p', schemaGroupDir); + const contractGroupDir = options.contract + '/' + title; + if (!fs.existsSync(contractGroupDir)){ + shell.mkdir('-p', contractGroupDir); } category.content @@ -50,12 +50,12 @@ function apib2postman(apib, options) { variables: attributes.variable }; - const schemaFilePath = schemaGroupDir + '/' + resource.meta.title + '.json'; + const contractFilePath = contractGroupDir + '/' + resource.meta.title + '.json'; const actions = parseActions( baseAction, resource.content.filter(x => x.element === 'transition'), environment, - schemaFilePath + contractFilePath ); addEnvVariables(environment.values, attributes.envVariable); @@ -94,7 +94,7 @@ function parsePath(uriTemplate) { return decodeURIComponent(uriTemplate.expand(params)).split('/').slice(1); } -function parseActions(baseAction, actions, environment, schemaFilePath) { +function parseActions(baseAction, actions, environment, contractFilePath) { return actions.map(action => { const transaction = _.find(action.content, x => x.element === 'httpTransaction'); const request = parseRequest(_.find(transaction.content, x => x.element === 'httpRequest')); @@ -116,7 +116,17 @@ function parseActions(baseAction, actions, environment, schemaFilePath) { addEnvVariables(environment.values, attributes.envVariable); } - const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'), schemaFilePath); + const resource = newAction.path.join('/'); + const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse')); + const contract = { + resource: resource, + queryParameters: newAction.query, + request: request, + statusCode: response.statusCode, + responseHeaders: response.headers, + jsonSchema: response.jsonSchema + } + fs.writeFileSync(contractFilePath, JSON.stringify(contract, null, 2)); return _.merge({}, newAction, { name: action.meta.title, @@ -196,12 +206,12 @@ function parseRequestHeaders(headers) { return parseHeaders(headers.content.filter(x => x.content.key.content !== 'Authorization')); } -function parseResponse(response, schemaFilePath) { +function parseResponse(response) { return { statusCode: response.attributes.statusCode, headers: response.attributes.headers ? parseHeaders(response.attributes.headers.content) : {}, body: parseContent(response.content, 'messageBody').content, - jsonSchema: parseJsonSchema(response.content, schemaFilePath), + jsonSchema: parseJsonSchema(response.content), tests: parseBodyTests(response.content) }; } @@ -239,11 +249,10 @@ function parseBodyTests(content) { return tests[1].split(/\r\n?|\n/g); } -function parseJsonSchema(content, schemaFilePath) { +function parseJsonSchema(content) { try { const schemaJson = parseContent(content, 'messageBodySchema').content; const schema = JSON.parse(schemaJson); - fs.writeFileSync(schemaFilePath, schemaJson); if (schema) { return schema; }