Skip to content

Commit 3292f6d

Browse files
authored
Create contract output files (#8)
1 parent 0988010 commit 3292f6d

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Diff for: bin/apib2postman.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const apib2postman = require('../src/index.js');
99

1010
var options = nopt({
1111
'output': String,
12-
'schema': String,
12+
'contract': String,
1313
'environment': String,
1414
'testTemplate': String,
1515
'help': Boolean
1616
}, {
1717
'o': ['--output'],
18-
's': ['--schema'],
18+
'c': ['--contract'],
1919
'e': ['--environment'],
2020
't': ['--testTemplate'],
2121
'h': ['--help']
@@ -33,7 +33,7 @@ if (options.help || options.argv.remain.length === 0) {
3333
console.log("Options:")
3434
console.log(" -h --help Print this help and exit.");
3535
console.log(" -o --output <file> Output result to file.");
36-
console.log(" -s --schema <directory> Directory containing json schema output files.");
36+
console.log(" -c --contract <directory> Directory for contract output files.");
3737
console.log(" -e --environment <file> The output file for the Postman environment.");
3838
console.log(" -t --testTemplate <template.hbs> The postman test template to use for each action.");
3939
process.exit();
@@ -44,8 +44,8 @@ const collectionFile = options.output || 'API.postman_collection.json';
4444
const environmentFile = options.environment || 'API.postman_environment.json';
4545
const includePath = input ? path.dirname(input) : process.cwd();
4646
var apibData = '';
47-
if (!options.schema) {
48-
options.schema = 'schema';
47+
if (!options.contract) {
48+
options.contract = 'contract';
4949
}
5050

5151
fs.createReadStream(input).on('data', (chunk) => {

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.2.2",
5+
"version": "1.2.3",
66
"license": "BSD",
77
"dependencies": {
88
"apib-include-directive": "^0.1.0",

Diff for: src/index.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ function apib2postman(apib, options) {
3131
const title = category.meta.title;
3232
const groups = [];
3333

34-
const schemaGroupDir = options.schema + '/' + title;
35-
if (!fs.existsSync(schemaGroupDir)){
36-
shell.mkdir('-p', schemaGroupDir);
34+
const contractGroupDir = options.contract + '/' + title;
35+
if (!fs.existsSync(contractGroupDir)){
36+
shell.mkdir('-p', contractGroupDir);
3737
}
3838

3939
category.content
@@ -50,12 +50,12 @@ function apib2postman(apib, options) {
5050
variables: attributes.variable
5151
};
5252

53-
const schemaFilePath = schemaGroupDir + '/' + resource.meta.title + '.json';
53+
const contractFilePath = contractGroupDir + '/' + resource.meta.title + '.json';
5454
const actions = parseActions(
5555
baseAction,
5656
resource.content.filter(x => x.element === 'transition'),
5757
environment,
58-
schemaFilePath
58+
contractFilePath
5959
);
6060

6161
addEnvVariables(environment.values, attributes.envVariable);
@@ -94,7 +94,7 @@ function parsePath(uriTemplate) {
9494
return decodeURIComponent(uriTemplate.expand(params)).split('/').slice(1);
9595
}
9696

97-
function parseActions(baseAction, actions, environment, schemaFilePath) {
97+
function parseActions(baseAction, actions, environment, contractFilePath) {
9898
return actions.map(action => {
9999
const transaction = _.find(action.content, x => x.element === 'httpTransaction');
100100
const request = parseRequest(_.find(transaction.content, x => x.element === 'httpRequest'));
@@ -116,7 +116,17 @@ function parseActions(baseAction, actions, environment, schemaFilePath) {
116116
addEnvVariables(environment.values, attributes.envVariable);
117117
}
118118

119-
const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'), schemaFilePath);
119+
const resource = newAction.path.join('/');
120+
const response = parseResponse(_.find(transaction.content, x => x.element === 'httpResponse'));
121+
const contract = {
122+
resource: resource,
123+
queryParameters: newAction.query,
124+
request: request,
125+
statusCode: response.statusCode,
126+
responseHeaders: response.headers,
127+
jsonSchema: response.jsonSchema
128+
}
129+
fs.writeFileSync(contractFilePath, JSON.stringify(contract, null, 2));
120130

121131
return _.merge({}, newAction, {
122132
name: action.meta.title,
@@ -196,12 +206,12 @@ function parseRequestHeaders(headers) {
196206
return parseHeaders(headers.content.filter(x => x.content.key.content !== 'Authorization'));
197207
}
198208

199-
function parseResponse(response, schemaFilePath) {
209+
function parseResponse(response) {
200210
return {
201211
statusCode: response.attributes.statusCode,
202212
headers: response.attributes.headers ? parseHeaders(response.attributes.headers.content) : {},
203213
body: parseContent(response.content, 'messageBody').content,
204-
jsonSchema: parseJsonSchema(response.content, schemaFilePath),
214+
jsonSchema: parseJsonSchema(response.content),
205215
tests: parseBodyTests(response.content)
206216
};
207217
}
@@ -239,11 +249,10 @@ function parseBodyTests(content) {
239249
return tests[1].split(/\r\n?|\n/g);
240250
}
241251

242-
function parseJsonSchema(content, schemaFilePath) {
252+
function parseJsonSchema(content) {
243253
try {
244254
const schemaJson = parseContent(content, 'messageBodySchema').content;
245255
const schema = JSON.parse(schemaJson);
246-
fs.writeFileSync(schemaFilePath, schemaJson);
247256
if (schema) {
248257
return schema;
249258
}

0 commit comments

Comments
 (0)