mirror of https://github.com/Seich/Beau.git
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:
parent
56d536a509
commit
248f4a9223
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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')]
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue