Skip to content

Commit e3e6cb8

Browse files
authored
Merge pull request #33 from JaredCE/add-verbose-logging
Add verbose logging
2 parents 344c198 + 2be7a4e commit e3e6cb8

File tree

9 files changed

+87
-10
lines changed

9 files changed

+87
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ arazzo-runner -a arazzo.json -i input.json
3434
# With URL
3535
arazzo-runner -a https://example.com/arazzo.json -i ./input.json
3636

37+
# With Verbose Logging
38+
arazzo-runner -a https://example.com/arazzo.json -i ./input.json --verbose
39+
3740
# Show help
3841
arazzo-runner --help
3942

cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const options = {
1717
type: "string",
1818
short: "i",
1919
},
20+
verbose: {
21+
type: "boolean",
22+
},
2023
help: {
2124
type: "boolean",
2225
short: "h",
@@ -40,6 +43,7 @@ OPTIONS:
4043
-i, --input <file> Path to input JSON file (required)
4144
-h, --help Show this help message
4245
-v, --version Show version number
46+
-vv, --verbose Verbose Logging
4347
4448
EXAMPLES:
4549
# Run with local files
@@ -139,7 +143,7 @@ async function main() {
139143
console.log("");
140144

141145
try {
142-
const runner = new Runner();
146+
const runner = new Runner({ verbose: values?.verbose || false });
143147
await runner.runArazzo(arazzoPath, inputPath);
144148

145149
console.log("");

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arazzo-runner",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "A runner to run through Arazzo Document workflows",
55
"main": "index.js",
66
"scripts": {

src/Arazzo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,20 @@ class Arazzo extends Document {
343343

344344
let response;
345345
if (this.operation?.mutualTLS) {
346+
this.logger.verbose("Using mutualTLS call");
346347
response = await this.mutualTLS();
347348
} else {
349+
this.logger.verbose("Using fetch call");
350+
351+
this.logger.verbose(`url: ${url}`);
352+
this.logger.verbose(`method: ${options.method}`);
353+
this.logger.verbose("headers:");
354+
for (const [key, value] of options.headers.entries()) {
355+
this.logger.verbose(`${key}: ${value}`);
356+
}
357+
358+
this.logger.verbose(`body: ${options.body}`);
359+
348360
response = await fetch(url, options);
349361
}
350362

@@ -367,6 +379,7 @@ class Arazzo extends Document {
367379
let clientKeyPath;
368380
try {
369381
clientKeyPath = path.resolve(this.inputs.key);
382+
this.logger.verbose(`clientKey Path: ${clientKeyPath}`);
370383
} catch (err) {
371384
this.logger.error(`could not resolve clientKey`);
372385
throw err;
@@ -375,6 +388,7 @@ class Arazzo extends Document {
375388
let clientCertPath;
376389
try {
377390
clientCertPath = path.resolve(this.inputs.cert);
391+
this.logger.verbose(`clientCert Path: ${clientCertPath}`);
378392
} catch (err) {
379393
this.logger.error(`could not resolve clientCert`);
380394
throw err;
@@ -388,11 +402,17 @@ class Arazzo extends Document {
388402

389403
const opUrl = new URL(url);
390404

405+
this.logger.verbose(`url: ${opUrl.pathname + opUrl.search}`);
406+
this.logger.verbose(`method: ${this.operation.method}`);
407+
this.logger.verbose("headers:");
391408
const headersObj = {};
392409
for (const [key, value] of this.operation.headers.entries()) {
410+
this.logger.verbose(`${key}: ${value}`);
393411
Object.assign(headersObj, { [key]: value });
394412
}
395413

414+
this.logger.verbose(`body: ${this.operation.data}`);
415+
396416
return new Promise((resolve, reject) => {
397417
const options = {
398418
key: fs.readFileSync(clientKeyPath),

src/DocFactory.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ const OpenAPI = require("./OpenAPI");
66
class DocumentFactory {
77
constructor() {}
88

9+
/**
10+
* Tests whether a string is a URL or not
11+
* @private
12+
* @param {string} str
13+
* @returns
14+
*/
15+
isUrl(str) {
16+
try {
17+
new URL(str);
18+
return true;
19+
} catch {
20+
return false;
21+
}
22+
}
23+
924
/**
1025
* Document Factory to create Arazzo or OpenAPI documents
1126
* @function buildDocument
@@ -23,7 +38,9 @@ class DocumentFactory {
2338
document = new Arazzo(path, name, options, this);
2439
}
2540

26-
await document.loadDocument();
41+
if (this.isUrl(path)) {
42+
await document.loadDocument();
43+
} else document.setMainArazzo();
2744

2845
return document;
2946
}

src/Logger.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
class Logger {
4-
constructor() {
4+
constructor(verboseLogging) {
5+
this.verboseLogging = verboseLogging || false;
56
this.logOutput = {
67
debug: (message) => console.debug(message),
78
error: (message) => console.error(`❌ ${message}`),
@@ -50,7 +51,7 @@ class Logger {
5051
}
5152

5253
verbose(str) {
53-
this.log(str, this.logTypes.VERBOSE);
54+
if (this.verboseLogging) this.log(str, this.logTypes.VERBOSE);
5455
}
5556

5657
warning(str) {

src/Runner.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const Input = require("./Input");
66
const Logger = require("./Logger");
77

88
class Runner {
9-
constructor(logger) {
10-
this.logger = logger || new Logger();
9+
constructor(options, logger) {
10+
const verboseLogging = options?.verbose || false;
11+
this.logger = logger || new Logger(verboseLogging);
1112
}
1213

1314
/**

test/unit/Logger.spec.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const expect = require("chai").expect;
4+
const sinon = require("sinon");
45

56
const Logger = require("../../src/Logger");
67

@@ -46,10 +47,40 @@ describe(`Logger`, function () {
4647
});
4748

4849
describe(`verbose`, function () {
49-
it(`should output the expected string`, function () {
50+
it(`should output the expected string when verboseLogging is true`, function () {
51+
const logger = new Logger(true);
52+
53+
const spy = sinon.spy(logger, "log");
54+
55+
logger.verbose("The expected string");
56+
57+
expect(spy.callCount).to.be.equal(1);
58+
59+
spy.restore();
60+
});
61+
62+
it(`should output the expected string when verboseLogging is false`, function () {
63+
const logger = new Logger(false);
64+
65+
const spy = sinon.spy(logger, "log");
66+
67+
logger.verbose("The expected string");
68+
69+
expect(spy.callCount).to.be.equal(0);
70+
71+
spy.restore();
72+
});
73+
74+
it(`should output the expected string when verboseLogging is undefined`, function () {
5075
const logger = new Logger();
5176

77+
const spy = sinon.spy(logger, "log");
78+
5279
logger.verbose("The expected string");
80+
81+
expect(spy.callCount).to.be.equal(0);
82+
83+
spy.restore();
5384
});
5485
});
5586

0 commit comments

Comments
 (0)