Compare commits

..

No commits in common. "master" and "v0.11.0" have entirely different histories.

20 changed files with 1003 additions and 707 deletions

View File

@ -18,10 +18,6 @@ jobs:
wget https://codeclimate.com/downloads/test-reporter/test-reporter-0.6.3-linux-amd64 -O cc-test-reporter
chmod +x ./cc-test-reporter
- name: Validate Schema
run: |
npx ajv-cli compile -s schema.json
npx ajv-cli validate -s schema.json -d examples/beau.yml
- name: Run CLI Tests
run: npm test -- ./bin
- name: Run Lib Tests

42
LICENSE
View File

@ -1,41 +1,7 @@
“Commons Clause” License Condition v1.0
Copyright 2018 David Sergio Díaz
The Software is provided to you by the Licensor under the License, as defined
below, subject to the following condition.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Without limiting other conditions in the License, the grant of rights under the
License will not include, and the License does not grant to you, the right to
Sell the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
For purposes of the foregoing, “Sell” means practicing any or all of the rights
granted to you under the License to provide to third parties, for a fee or other
consideration (including without limitation fees for hosting or consulting/
support services related to the Software), a product or service whose value
derives, entirely or substantially, from the functionality of the Software. Any
license notice or attribution required by the License must also include this
Commons Clause License Condition notice.
Software: Beau
License: MIT
Licensor: David Díaz
---
Copyright 2020 David Díaz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -18,7 +18,7 @@ class Base extends Command {
}
let config
yaml.loadAll(fs.readFileSync(configFile, 'utf-8'), (doc) => {
yaml.safeLoadAll(fs.readFileSync(configFile, 'utf-8'), (doc) => {
const valid = validate(doc)
if (!valid) {
@ -41,7 +41,6 @@ class Base extends Command {
return config
}
loadConfig(configFile, params = []) {
const config = this.openConfigFile(configFile)
const env = dotenv.config().parsed || {}

View File

@ -1,6 +1,5 @@
const clc = require('cli-color')
const { Line } = require('clui')
const { expandPath } = require('../../../src/shared')
const Base = require('../base')
class ListCommand extends Base {
@ -32,7 +31,9 @@ class ListCommand extends Base {
.padding(2)
.column(VERB, 20, [clc.yellow])
.column(ALIAS, 30, [clc.yellow])
.column(expandPath(ENDPOINT, PATH))
.column(
ENDPOINT.replace(/\/$/, '') + '/' + PATH.replace(/^\//, '')
)
.output()
)

View File

@ -1,10 +1,9 @@
const Base = require('../base')
const cj = require('color-json')
const clc = require('cli-color')
const prompts = require('prompts')
const inquirer = require('inquirer')
const { Line, Spinner } = require('clui')
const { flags } = require('@oclif/command')
const { expandPath } = require('../../../src/shared')
class RequestCommand extends Base {
prettyOutput(res, verbose = false) {
@ -68,16 +67,16 @@ class RequestCommand extends Base {
if (interactive) {
const requests = Beau.requests.list.map(
({ VERB, ALIAS, ENDPOINT, PATH }) => ({
title: `${VERB} ${PATH} - ${ALIAS}`,
name: `${VERB} ${PATH} - ${ALIAS}`,
value: ALIAS,
description: expandPath(ENDPOINT, PATH)
description: 'Hello World'
})
)
const { name } = await prompts({
const { name } = await inquirer.prompt({
name: 'name',
message: 'Pick a Request to execute',
type: 'select',
message: 'Pick as Request to execute',
type: 'list',
choices: requests
})

View File

@ -1,37 +0,0 @@
endpoint: https://pokeapi.co/api/v2/
# Try replacing this pokemon using params:
# beau request get-pokemon -P "pokemon=dragapult"
environment:
_:
pokemon: ditto
cookiejar: true
GET /pokemon/$env._.pokemon: get-pokemon
GET $get-pokemon.body.location_area_encounters: get-encounters
POST https://httpbin.org/anything:
- alias: post-first-area
payload:
area: $get-encounters.body.0.location_area.name
- alias: post-pokemon-type
payload:
type: $get-pokemon.body.types.0.type.name
- alias: form-submission
form:
name: Dragapult
- alias: file-upload
formdata:
name: Beau
logo: $[createReadStream('../media/beau.png')]
GET https://httpbin.org/status/418: teapot
GET https://httpbin.org/cookies/set:
alias: set-cookies
params:
hello: World

15
examples/github.yml Normal file
View File

@ -0,0 +1,15 @@
VERSION: 1
ENDPOINT: https://api.github.com
auth: &auth
HEADERS:
Authorization: token asfdasf123423sd1fgnh7d83n478
User-Agent: Beau
GET /user:
ALIAS: $user
<<: *auth
GET /user/repos:
ALIAS: $repos
<<: *auth

22
examples/hosts.yml Normal file
View File

@ -0,0 +1,22 @@
version: 1
endpoint: http://httpbin.org
environment:
the:
post: 2
defaults:
headers:
hello: 'Hello2'
POST /anything:
alias: anything
payload:
title: $jpa:get-post.body.title
---
host: jpa
endpoint: http://jsonplaceholder.typicode.com
GET /posts/$env.the.post: get-post
GET /users/$jpa:get-post.body.userId: hello

27
examples/httpbin.yml Normal file
View File

@ -0,0 +1,27 @@
version: 1
endpoint: https://httpbin.org/
cookiejar: true
environment:
params:
name: David
GET /anything:
- alias: anything
payload:
name: $env.params.name
- alias: anything2
GET /cookies/set:
alias: set-cookies
params:
hello: World
GET /status/418:
alias: teapot
POST /post:
alias: post
formdata:
id: $[uuid()]
file: $[createReadStream('./github.yml')]

View File

@ -0,0 +1,29 @@
version: 1
endpoint: 'http://jsonplaceholder.typicode.com'
environment:
the:
post: 1
defaults:
headers:
hello: $posts.body.0.userId
GET /posts/$env.the.post: get-post
GET /posts/:
alias: posts
headers:
hello: false
POST /posts/:
alias: new-post
documentation:
title: New Post
GET /users/$posts.body.0.userId:
alias: post-user
documentation:
description: Fetches the user for a give post.
params:
hello: 'world'

11
examples/plugins.yml Normal file
View File

@ -0,0 +1,11 @@
endpoint: http://localhost:10080
plugins:
- jwt:
data:
userId: 12
name: Sergio
secret: 'asdfasdf+asdfasdf/asdfasdfasdfasdf=='
GET /test:
alias: test

37
examples/slack.yml Normal file
View File

@ -0,0 +1,37 @@
VERSION: '1'
ENDPOINT: https://slack.com/api
auth: &auth
token: xoxp-139455775026-139455775090-147751461120-f224ed6ffee029869a0f138d0859e7d6
GET /users.getPresence:
ALIAS: presence
PARAMS:
<<: *auth
GET /channels.list:
ALIAS: channel-list
PARAMS:
<<: *auth
exclude_archived: true
GET /channels.info:
ALIAS: channel-info
PARAMS:
<<: *auth
channel: $channel-list.response.body.channels.0.id
POST /chat.postMessage:
ALIAS: new-message
PARAMS:
<<: *auth
channel: $channel-info.response.body.channel.id
text: 'Hey Seich!'
parse: full
link_names: true
username: Beau
GET /users.list:
ALIAS: user-list
PARAMS:
<<: *auth

1359
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
{
"name": "beau",
"version": "0.11.3",
"version": "0.11.0",
"description": "Testing APIs made easy.",
"main": "./src/beau.js",
"author": "David Díaz <seich@martianwabbit.com>",
"license": "MIT + Commons Clause",
"author": "Sergio Diaz <seich@martianwabbit.com>",
"license": "MIT",
"scripts": {
"test": "jest -i",
"test:coverage": "jest --coverage ./src",
@ -17,20 +17,20 @@
"dependencies": {
"@oclif/command": "1.8.0",
"@oclif/config": "1.17.0",
"@oclif/plugin-help": "3.2.1",
"@oclif/plugin-help": "3.2.0",
"@oclif/plugin-warn-if-update-available": "1.7.0",
"ajv": "7.0.3",
"ajv": "7.0.0-beta.6",
"beau-std": "0.9.4",
"better-ajv-errors": "0.7.0",
"better-ajv-errors": "0.6.7",
"cli-color": "2.0.0",
"clui": "0.3.6",
"color-json": "2.0.1",
"deepmerge": "4.2.2",
"dotenv": "8.2.0",
"globby": "11.0.2",
"globby": "11.0.1",
"inquirer": "7.3.3",
"is-plain-object": "5.0.0",
"js-yaml": "4.0.0",
"prompts": "2.4.0",
"js-yaml": "3.14.0",
"request": "2.88.2",
"request-promise-native": "1.0.9",
"requireg": "0.2.2"
@ -40,7 +40,7 @@
"jest": "26.6.3",
"jest-watch-typeahead": "0.6.1",
"strip-ansi": "6.0.0",
"np": "7.2.0"
"np": "7.0.0"
},
"oclif": {
"commands": "./bin/cli/commands",

View File

@ -62,7 +62,6 @@
"properties": {
"version": {
"type": "number",
"description": "The beau version this document was created for.",
"enum": [1]
},
"endpoint": {
@ -70,19 +69,12 @@
"description": "The root endpoint for this host."
},
"cookiejar": {
"type": "boolean",
"description": "Enable cookie support for requests?"
},
"host": {
"type": "string",
"description": "The name of the current host. It allows referencing requests between different hosts."
"type": "boolean"
},
"defaults": {
"description": "Default values to be added to all requests.",
"$ref": "#/definitions/requestObject"
},
"plugins": {
"description": "Plugins to be enabled for this document.",
"type": "array",
"items": {
"anyOf": [
@ -91,18 +83,7 @@
]
}
},
"environment": {
"type": "object",
"description": "Global document variables for easy access.",
"additionalProperties": true,
"properties": {
"_": {
"type": "object",
"description": "Environment variables brought in by cli params or dotenv.",
"additionalProperties": true
}
}
}
"environment": {}
},
"patternProperties": {
"(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH)\\s.*": {

View File

@ -11,7 +11,7 @@ describe(`Beau's config Loader.`, () => {
it('should load the config', () => {
moduleVersion.mockReturnValue(1)
const doc = yaml.load(`
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://example.com'
@ -27,7 +27,7 @@ describe(`Beau's config Loader.`, () => {
it(`should load the request list using the configuration`, () => {
moduleVersion.mockReturnValue(1)
const doc = yaml.load(`
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://example.com'

View File

@ -6,7 +6,7 @@ requireg.resolving = false
describe('Config', () => {
it('should load valid config keys', () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
version: 1
endpoint: http://martianwabbit.com
shouldntBeAdded: true
@ -19,7 +19,7 @@ describe('Config', () => {
})
it('should load requests', () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
endpoint: http://example.com
GET /profile: get-profile
@ -36,7 +36,7 @@ describe('Config', () => {
})
it('should set up defaults for all requests', () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://example.com'
@ -60,7 +60,7 @@ describe('Config', () => {
})
it('should load multiple hosts', () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
version: 1
endpoint: http://example.org
@ -105,7 +105,7 @@ describe('Config', () => {
})
it('should namespace all aliases within an host', () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
hosts:
- host: test1
endpoint: http://example.com
@ -122,7 +122,7 @@ describe('Config', () => {
})
it(`should throw if host doesn't have a host key`, () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
hosts:
- endpoint: http://example.com
GET /posts: posts
@ -136,7 +136,7 @@ describe('Config', () => {
})
it(`should merge host settings with global settings`, () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
defaults:
headers:
hello: 1
@ -159,7 +159,7 @@ describe('Config', () => {
})
it(`should allow different settings for the same request`, () => {
const doc = yaml.load(`
const doc = yaml.safeLoad(`
host: https://example.com
GET /1:
- alias: req1

View File

@ -5,8 +5,7 @@ const {
UpperCaseKeys,
removeOptionalKeys,
toKebabCase,
replaceInObject,
expandPath
replaceInObject
} = require('../shared')
describe('Shared Utilities', () => {
@ -73,26 +72,4 @@ describe('Shared Utilities', () => {
expect(replaceInObject(a, (obj) => 'a')).toEqual({ b: 'a', c: 'a' })
})
})
describe('expandPath', () => {
test.each([
['https://alchem.ee', 'api/v1/hello'],
['https://alchem.ee/', '/api/v1/hello'],
['https://alchem.ee', '/api/v1/hello'],
['https://alchem.ee/', 'api/v1/hello']
])(
'should add a base url to the path is the path is not a url: %s, %s',
(url, path) => {
expect(expandPath(url, path)).toEqual(
'https://alchem.ee/api/v1/hello'
)
}
)
it('should return the path if its a fully fledged url on its own', () => {
expect(
expandPath('https://alchem.ee', 'https://martianwabbit.com')
).toEqual('https://martianwabbit.com')
})
})
})

View File

@ -64,8 +64,10 @@ class Request {
}
async exec(cache = new RequestCache()) {
const isPathFullUrl = isUrl(this.PATH)
let settings = cache.parse({
baseUrl: '',
baseUrl: isPathFullUrl ? '' : this.ENDPOINT,
uri: this.PATH,
method: this.VERB,
jar: this.COOKIEJAR,
@ -81,9 +83,6 @@ class Request {
resolveWithFullResponse: true
})
const isPathFullUrl = isUrl(settings.uri)
settings.baseUrl = isPathFullUrl ? '' : this.ENDPOINT
settings = removeOptionalKeys(settings, [
'headers',
'qs',

View File

@ -77,14 +77,6 @@ const isUrl = function (str) {
}
}
const expandPath = (url, path) => {
if (isUrl(path)) {
return path
}
return url.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '')
}
module.exports = {
requestRegex,
replacementRegex,
@ -94,6 +86,5 @@ module.exports = {
toKebabCase,
replaceInObject,
moduleVersion,
isUrl,
expandPath
isUrl
}