Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/819 add api option to get request response printed to log #820

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ main class
* [Mcdev](#Mcdev)
* [.setSkipInteraction([skipInteraction])](#Mcdev.setSkipInteraction) ⇒ <code>void</code>
* [.setLoggingLevel(argv)](#Mcdev.setLoggingLevel) ⇒ <code>void</code>
* [.setOptions(argv)](#Mcdev.setOptions) ⇒ <code>void</code>
* [.createDeltaPkg(argv)](#Mcdev.createDeltaPkg) ⇒ <code>Promise.&lt;Array.&lt;TYPE.DeltaPkgItem&gt;&gt;</code>
* [.selectTypes()](#Mcdev.selectTypes) ⇒ <code>Promise</code>
* [.explainTypes()](#Mcdev.explainTypes) ⇒ <code>void</code>
Expand Down Expand Up @@ -518,6 +519,17 @@ configures what is displayed in the console
| [argv.verbose] | <code>boolean</code> | chatty user CLI output |
| [argv.debug] | <code>boolean</code> | enables developer output & features |

<a name="Mcdev.setOptions"></a>

### Mcdev.setOptions(argv) ⇒ <code>void</code>
configures what is displayed in the console

**Kind**: static method of [<code>Mcdev</code>](#Mcdev)

| Param | Type | Description |
| --- | --- | --- |
| argv | <code>object</code> | list of command line parameters given by user |

<a name="Mcdev.createDeltaPkg"></a>

### Mcdev.createDeltaPkg(argv) ⇒ <code>Promise.&lt;Array.&lt;TYPE.DeltaPkgItem&gt;&gt;</code>
Expand Down
61 changes: 24 additions & 37 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.retrieve(argv.BU, csvToArray(argv.TYPE), csvToArray(argv.KEY));
},
})
Expand Down Expand Up @@ -69,8 +68,8 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);

Mcdev.deploy(argv.BU, csvToArray(argv.TYPE), csvToArray(argv.KEY), argv.fromRetrieve);
},
})
Expand All @@ -84,17 +83,15 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.initProject(argv.credentialsName);
},
})
.command({
command: 'join',
desc: `clones an existing project from git`,
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.joinProject();
},
})
Expand All @@ -109,8 +106,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.findBUs(argv.credentialsName);
},
})
Expand All @@ -124,8 +120,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.badKeys(argv.BU);
},
})
Expand All @@ -147,8 +142,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.document(argv.BU, argv.TYPE);
},
})
Expand All @@ -173,8 +167,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.deleteByKey(argv.BU, argv.TYPE, argv.EXTERNALKEY);
},
})
Expand Down Expand Up @@ -203,8 +196,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.retrieveAsTemplate(argv.BU, argv.TYPE, csvToArray(argv.NAME), argv.MARKET);
},
})
Expand Down Expand Up @@ -233,8 +225,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.buildTemplate(argv.BU, argv.TYPE, csvToArray(argv.KEY), argv.MARKET);
},
})
Expand Down Expand Up @@ -262,8 +253,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.buildDefinition(argv.BU, argv.TYPE, argv.NAME, argv.MARKET);
},
})
Expand All @@ -287,8 +277,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.buildDefinitionBulk(argv.LISTNAME, argv.TYPE, argv.NAME);
},
})
Expand All @@ -297,8 +286,7 @@ yargs
aliases: ['st'],
desc: 'lets you choose what metadata types to retrieve',
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.selectTypes();
},
})
Expand All @@ -307,8 +295,7 @@ yargs
aliases: ['et'],
desc: 'explains metadata types that can be retrieved',
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.explainTypes();
},
})
Expand All @@ -333,8 +320,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.createDeltaPkg(argv);
},
})
Expand All @@ -359,8 +345,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.getFilesToCommit(argv.BU, argv.TYPE, csvToArray(argv.KEY));
},
})
Expand All @@ -384,8 +369,7 @@ yargs
});
},
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.refresh(argv.BU, argv.TYPE, csvToArray(argv.KEY));
},
})
Expand All @@ -394,8 +378,7 @@ yargs
aliases: ['up'],
desc: 'Add NPM dependencies and IDE configuration files to your project',
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.setOptions(argv);
Mcdev.upgrade(argv.skipInteraction);
},
})
Expand All @@ -415,12 +398,16 @@ yargs
alias: ['yes', 'y'],
description: 'Interactive questions where possible and go with defaults instead',
})
.option('api', {
description: 'Print API calls to log ',
choices: ['log', 'cli'],
})
.demandCommand(1, 'Please enter a valid command')
.strict()
.recommendCommands()
.wrap(yargs.terminalWidth())
.epilog(
'Copyright 2022. Accenture. Get support at https://github.com/Accenture/sfmc-devtools/issues'
'Copyright 2023. Accenture. Get support at https://github.com/Accenture/sfmc-devtools/issues'
)
.help().argv;

Expand Down
33 changes: 33 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ class Mcdev {
static setLoggingLevel(argv) {
Util.setLoggingLevel(argv);
}
/**
* configures what is displayed in the console
*
* @param {object} argv list of command line parameters given by user
* @returns {void}
*/
static setOptions(argv) {
const knownOptions = [
'api',
'commitHistory',
'filter',
'fromRetrieve',
'refresh',
'skipInteraction',
];
for (const option of knownOptions) {
if (argv[option] !== undefined) {
Util.OPTIONS[option] = argv[option];
}
}
// set logging level
const loggingOptions = ['silent', 'verbose', 'debug'];
for (const option of loggingOptions) {
if (argv[option] !== undefined) {
this.setLoggingLevel(argv);
break;
}
}
// set skip interaction
if (argv.skipInteraction !== undefined) {
this.setSkipInteraction(argv.skipInteraction);
}
}
/**
* handler for 'mcdev createDeltaPkg
*
Expand Down
32 changes: 25 additions & 7 deletions lib/util/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,31 @@ function setupSDK(sessionKey, authObject) {
);
Util.logger.errorStack(ex);
},
// logRequest: (req) =>
// console.log(`${Util.color.fgMagenta}request${Util.color.reset}`, req), // uncomment to log requests
// logResponse: (res) =>
// console.log(
// `${Util.color.bgMagenta}response${Util.color.reset}:`,
// typeof res.data == 'string' ? res.data : JSON.stringify(res.data, null, 2)
// ), // uncomment to log responses
logRequest: (req) => {
const msg = JSON.parse(JSON.stringify(req));
if (msg.url === '/Service.asmx') {
msg.data = msg.data.replace(
/<fueloauth(.*)<\/fueloauth>/gim,
'<fueloauth>*** TOKEN REMOVED ***</fueloauth>'
);
} else if (msg.headers?.Authorization) {
msg.headers.Authorization = 'Bearer *** TOKEN REMOVED ***';

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "Bearer *** TOKEN REMOVED ***" is used as [authorization header](1).
}
if (Util.OPTIONS.api === 'cli') {
console.log(`${Util.color.fgMagenta}API REQUEST >>${Util.color.reset}`, msg); // eslint-disable-line no-console
} else if (Util.OPTIONS.api === 'log') {
Util.logger.debug('API REQUEST >> ' + JSON.stringify(msg, null, 2));
}
},
logResponse: (res) => {
const msg =
typeof res.data == 'string' ? res.data : JSON.stringify(res.data, null, 2);
if (Util.OPTIONS.api === 'cli') {
console.log(`${Util.color.fgMagenta}API RESPONSE <<${Util.color.reset}`, msg); // eslint-disable-line no-console
} else if (Util.OPTIONS.api === 'log') {
Util.logger.debug('API RESPONSE << ' + msg);
}
},
},
requestAttempts: 4,
retryOnConnectionError: true,
Expand Down
6 changes: 5 additions & 1 deletion lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Util = {
/** @type {TYPE.skipInteraction} */
skipInteraction: false,
packageJsonMcdev: packageJsonMcdev,

OPTIONS: {},
/**
* helper that allows filtering an object by its keys
*
Expand Down Expand Up @@ -469,23 +469,27 @@ const Util = {
if (argv.silent) {
// only errors printed to CLI
Util.logger.level = 'error';
Util.OPTIONS.loggerLevel = 'error';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this? or can we just set the line above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first one we need, the second one we technically don't. I figured it's good practice to have it all in one place though for reference by other classes

Util.loggerTransports.console.level = 'error';
Util.logger.debug('CLI logger set to: silent');
} else if (argv.verbose) {
// chatty user cli logs
Util.logger.level = 'verbose';
Util.OPTIONS.loggerLevel = 'verbose';
Util.loggerTransports.console.level = 'verbose';
Util.logger.debug('CLI logger set to: verbose');
} else {
// default user cli logs
// TODO to be switched to "warn" when cli-process is integrated
Util.logger.level = 'info';
Util.OPTIONS.loggerLevel = 'info';
Util.loggerTransports.console.level = 'info';
Util.logger.debug('CLI logger set to: info / default');
}
if (argv.debug) {
// enables developer output & features. no change to actual logs
Util.logger.level = 'debug';
Util.OPTIONS.loggerLevel = 'debug';
Util.loggerTransports.console.level = 'debug';
Util.logger.debug('CLI logger set to: debug');
}
Expand Down