Added params as a flag to request. (#20)

This allows the use to inject arbitrary values to Beau's environment.
This commit is contained in:
Sergio Díaz 2018-05-06 18:26:19 -06:00 committed by GitHub
parent 56d536a509
commit 248f4a9223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 23 deletions

View File

@ -15,11 +15,14 @@ class Base extends Command {
return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
}
loadConfig(configFile) {
loadConfig(configFile, params = []) {
const config = this.openConfigFile(configFile);
const env = dotenv.config().parsed || {};
params = dotenv.parse(params.reduce((a, p) => a + '\n' + p, ''));
return new Beau(config, env);
const envParams = { params: Object.assign(env, params) };
return new Beau(config, envParams);
}
}

View File

@ -33,33 +33,38 @@ class RequestCommand extends Base {
}
async run() {
const { flags, args } = this.parse(RequestCommand);
const Beau = this.loadConfig(flags.config);
this.spinner = new Spinner(clc.yellow(`Requesting: ${args.alias}`), [
'⣾',
'⣽',
'⣻',
'⢿',
'⡿',
'⣟',
'⣯',
'⣷'
]);
const {
flags: {
param: params,
config,
'no-format': noFormat = false,
verbose = false
},
args
} = this.parse(RequestCommand);
const Beau = this.loadConfig(config, params);
const spinnerSprite = ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'];
this.spinner = new Spinner(
clc.yellow(`Requesting: ${args.alias}`),
spinnerSprite
);
try {
if (!flags['no-format']) {
if (!noFormat) {
this.spinner.start();
}
let res = await Beau.requests.execByAlias(args.alias);
if (flags['no-format']) {
if (noFormat) {
this.log(res.response.status);
this.log(res.request.endpoint);
this.log(JSON.stringify(res.response.headers));
this.log(JSON.stringify(res.response.body));
} else {
this.prettyOutput(res, flags.verbose);
this.prettyOutput(res, verbose);
}
} catch (err) {
new Line().output();
@ -70,7 +75,15 @@ class RequestCommand extends Base {
}
RequestCommand.description = `Executes a request by name.`;
RequestCommand.flags = { ...Base.flags };
RequestCommand.flags = {
...Base.flags,
param: flags.string({
char: 'P',
multiple: true,
default: [],
description: `Allows you to inject values into the request's environment.`
})
};
RequestCommand.args = [
{

View File

@ -2,13 +2,14 @@ version: 1
endpoint: https://httpbin.org/
cookiejar: true
environment:
params:
name: David
GET /anything:
alias: anything
form:
name: David
params:
hello: World
payload:
name: $env.params.name
GET /cookies/set:
alias: set-cookies
@ -22,4 +23,5 @@ POST /post:
alias: post
formdata:
file: $[createReadStream('./github.yml')]