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'));
|
return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
|
||||||
}
|
}
|
||||||
|
|
||||||
loadConfig(configFile) {
|
loadConfig(configFile, params = []) {
|
||||||
const config = this.openConfigFile(configFile);
|
const config = this.openConfigFile(configFile);
|
||||||
const env = dotenv.config().parsed || {};
|
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() {
|
async run() {
|
||||||
const { flags, args } = this.parse(RequestCommand);
|
const {
|
||||||
const Beau = this.loadConfig(flags.config);
|
flags: {
|
||||||
this.spinner = new Spinner(clc.yellow(`Requesting: ${args.alias}`), [
|
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 {
|
try {
|
||||||
if (!flags['no-format']) {
|
if (!noFormat) {
|
||||||
this.spinner.start();
|
this.spinner.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = await Beau.requests.execByAlias(args.alias);
|
let res = await Beau.requests.execByAlias(args.alias);
|
||||||
|
|
||||||
if (flags['no-format']) {
|
if (noFormat) {
|
||||||
this.log(res.response.status);
|
this.log(res.response.status);
|
||||||
this.log(res.request.endpoint);
|
this.log(res.request.endpoint);
|
||||||
this.log(JSON.stringify(res.response.headers));
|
this.log(JSON.stringify(res.response.headers));
|
||||||
this.log(JSON.stringify(res.response.body));
|
this.log(JSON.stringify(res.response.body));
|
||||||
} else {
|
} else {
|
||||||
this.prettyOutput(res, flags.verbose);
|
this.prettyOutput(res, verbose);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
new Line().output();
|
new Line().output();
|
||||||
|
|
@ -70,7 +75,15 @@ class RequestCommand extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestCommand.description = `Executes a request by name.`;
|
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 = [
|
RequestCommand.args = [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,14 @@ version: 1
|
||||||
endpoint: https://httpbin.org/
|
endpoint: https://httpbin.org/
|
||||||
|
|
||||||
cookiejar: true
|
cookiejar: true
|
||||||
|
environment:
|
||||||
|
params:
|
||||||
|
name: David
|
||||||
|
|
||||||
GET /anything:
|
GET /anything:
|
||||||
alias: anything
|
alias: anything
|
||||||
form:
|
payload:
|
||||||
name: David
|
name: $env.params.name
|
||||||
params:
|
|
||||||
hello: World
|
|
||||||
|
|
||||||
GET /cookies/set:
|
GET /cookies/set:
|
||||||
alias: set-cookies
|
alias: set-cookies
|
||||||
|
|
@ -23,3 +24,4 @@ POST /post:
|
||||||
formdata:
|
formdata:
|
||||||
file: $[createReadStream('./github.yml')]
|
file: $[createReadStream('./github.yml')]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue