Improved prompt descriptions. (#167)

I was really naive with combining urls and paths. This should fix it.
This commit is contained in:
David Díaz 2020-11-22 01:03:59 -06:00 committed by GitHub
parent 3cb6851ead
commit f82a529ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 12 deletions

View File

@ -4,6 +4,7 @@ const clc = require('cli-color')
const prompts = require('prompts') const prompts = require('prompts')
const { Line, Spinner } = require('clui') const { Line, Spinner } = require('clui')
const { flags } = require('@oclif/command') const { flags } = require('@oclif/command')
const { expandPath } = require('../../../src/shared')
class RequestCommand extends Base { class RequestCommand extends Base {
prettyOutput(res, verbose = false) { prettyOutput(res, verbose = false) {
@ -69,7 +70,7 @@ class RequestCommand extends Base {
({ VERB, ALIAS, ENDPOINT, PATH }) => ({ ({ VERB, ALIAS, ENDPOINT, PATH }) => ({
title: `${VERB} ${PATH} - ${ALIAS}`, title: `${VERB} ${PATH} - ${ALIAS}`,
value: ALIAS, value: ALIAS,
description: `${ENDPOINT}${PATH}` description: expandPath(ENDPOINT, PATH)
}) })
) )

View File

@ -1,19 +1,37 @@
endpoint: https://pokeapi.co/api/v2 endpoint: https://pokeapi.co/api/v2/
# Try replacing this pokemon using params: # Try replacing this pokemon using params:
# beau request get-pokemon -P "pokemon=dragapult" # beau request get-pokemon -P "pokemon=dragapult"
environment: environment:
_: _:
pokemon: ditto pokemon: ditto
cookiejar: true
GET /pokemon/$env._.pokemon: get-pokemon GET /pokemon/$env._.pokemon: get-pokemon
GET $get-pokemon.body.location_area_encounters: get-encounters GET $get-pokemon.body.location_area_encounters: get-encounters
POST https://httpbin.org/anything: POST https://httpbin.org/anything:
- alias: post-first-area - alias: post-first-area
payload: payload:
area: $get-encounters.body.0.location_area.name area: $get-encounters.body.0.location_area.name
- alias: post-pokemon-type - alias: post-pokemon-type
payload: payload:
type: $get-pokemon.body.types.0.type.name 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

View File

@ -5,7 +5,8 @@ const {
UpperCaseKeys, UpperCaseKeys,
removeOptionalKeys, removeOptionalKeys,
toKebabCase, toKebabCase,
replaceInObject replaceInObject,
expandPath
} = require('../shared') } = require('../shared')
describe('Shared Utilities', () => { describe('Shared Utilities', () => {
@ -72,4 +73,26 @@ describe('Shared Utilities', () => {
expect(replaceInObject(a, (obj) => 'a')).toEqual({ b: 'a', c: 'a' }) 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

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