From f82a529ebecc38e963974ae3e10ae62aa49a9093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz?= Date: Sun, 22 Nov 2020 01:03:59 -0600 Subject: [PATCH] Improved prompt descriptions. (#167) I was really naive with combining urls and paths. This should fix it. --- bin/cli/commands/request.js | 3 ++- examples/beau.yml | 36 +++++++++++++++++++++++++++--------- src/__tests__/shared.spec.js | 25 ++++++++++++++++++++++++- src/shared.js | 11 ++++++++++- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/bin/cli/commands/request.js b/bin/cli/commands/request.js index a4a247e..1c7e3c4 100644 --- a/bin/cli/commands/request.js +++ b/bin/cli/commands/request.js @@ -4,6 +4,7 @@ const clc = require('cli-color') const prompts = require('prompts') const { Line, Spinner } = require('clui') const { flags } = require('@oclif/command') +const { expandPath } = require('../../../src/shared') class RequestCommand extends Base { prettyOutput(res, verbose = false) { @@ -69,7 +70,7 @@ class RequestCommand extends Base { ({ VERB, ALIAS, ENDPOINT, PATH }) => ({ title: `${VERB} ${PATH} - ${ALIAS}`, value: ALIAS, - description: `${ENDPOINT}${PATH}` + description: expandPath(ENDPOINT, PATH) }) ) diff --git a/examples/beau.yml b/examples/beau.yml index 3bf859f..e8bb229 100644 --- a/examples/beau.yml +++ b/examples/beau.yml @@ -1,19 +1,37 @@ -endpoint: https://pokeapi.co/api/v2 +endpoint: https://pokeapi.co/api/v2/ # Try replacing this pokemon using params: # beau request get-pokemon -P "pokemon=dragapult" environment: - _: - pokemon: ditto + _: + 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-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: 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 diff --git a/src/__tests__/shared.spec.js b/src/__tests__/shared.spec.js index 5acfa61..1d28900 100644 --- a/src/__tests__/shared.spec.js +++ b/src/__tests__/shared.spec.js @@ -5,7 +5,8 @@ const { UpperCaseKeys, removeOptionalKeys, toKebabCase, - replaceInObject + replaceInObject, + expandPath } = require('../shared') describe('Shared Utilities', () => { @@ -72,4 +73,26 @@ 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') + }) + }) }) diff --git a/src/shared.js b/src/shared.js index d292f09..e8e96e3 100644 --- a/src/shared.js +++ b/src/shared.js @@ -77,6 +77,14 @@ const isUrl = function (str) { } } +const expandPath = (url, path) => { + if (isUrl(path)) { + return path + } + + return url.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '') +} + module.exports = { requestRegex, replacementRegex, @@ -86,5 +94,6 @@ module.exports = { toKebabCase, replaceInObject, moduleVersion, - isUrl + isUrl, + expandPath }