Skip to content

Commit

Permalink
Create contract output files (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jqian33 authored Feb 20, 2020
1 parent 0988010 commit 3292f6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
10 changes: 5 additions & 5 deletions bin/apib2postman.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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 <file> Output result to file.");
console.log(" -s --schema <directory> Directory containing json schema output files.");
console.log(" -c --contract <directory> Directory for contract output files.");
console.log(" -e --environment <file> The output file for the Postman environment.");
console.log(" -t --testTemplate <template.hbs> The postman test template to use for each action.");
process.exit();
Expand All @@ -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) => {
Expand Down
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.2.2",
"version": "1.2.3",
"license": "BSD",
"dependencies": {
"apib-include-directive": "^0.1.0",
Expand Down
31 changes: 20 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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'));
Expand All @@ -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,
Expand Down Expand Up @@ -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)
};
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3292f6d

Please sign in to comment.